Commit ce2e85cf authored by chechunchi's avatar chechunchi

rerange code

parent 477ee9b3
Pipeline #19498 passed with stages
in 2 minutes and 55 seconds
......@@ -13,13 +13,12 @@ import MsgNewPhaseAdapter from "./newPhase";
import MsgHintAdapter from "./hint";
import MsgSelectIdleCmdAdapter from "./selectIdleCmd";
import MsgSelectPlaceAdapter from "./selectPlace";
import MsgMoveAdapter from "./move";
import MsgSelectCardAdapter from "./selectCard";
import MsgSelectChainAdapter from "./selectChain";
import MsgSelectEffectYnAdapter from "./selectEffectYn";
import MsgSelectPositionAdapter from "./selectPosition";
import MsgSelectOptionAdapter from "./selectOption";
import { penetrate } from "./penetrate";
import PENETRATE from "./penetrate";
/*
* STOC GameMsg
......@@ -44,7 +43,7 @@ export default class GameMsgAdapter implements StocAdapter {
const gameData = exData.slice(1);
let gameMsg: any = new ygopro.StocGameMessage({}).toObject();
if (!penetrate(func, gameMsg, gameData)) {
if (!PENETRATE.penetrate(func, gameMsg, gameData)) {
switch (func) {
case GAME_MSG.MSG_START: {
gameMsg.start = MsgStartAdapter(gameData);
......@@ -81,12 +80,6 @@ export default class GameMsgAdapter implements StocAdapter {
break;
}
// case GAME_MSG.MSG_MOVE: {
// // gameMsg.move = MsgMoveAdapter(gameData);
// gameMsg["move"] = MsgMoveAdapter(gameData);
//
// break;
// }
case GAME_MSG.MSG_SELECT_CARD: {
gameMsg.select_card = MsgSelectCardAdapter(gameData);
......
......@@ -7,13 +7,13 @@ import { ygopro } from "../../../idl/ocgcore";
type Constructor<T = any> = new (...args: any[]) => T;
const PenetrateConfig = _objToMap(PenetrateData);
const readFieldHandlerMap: Map<string, readFieldHandler> = new Map([
const ReadFieldHandlerMap: Map<string, readFieldHandler> = new Map([
["uint8", ((reader) => reader.readUint8()) as readFieldHandler],
["uint16", (reader) => reader.readUint16()],
["uint32", (reader) => reader.readUint32()],
["CardLocation", (reader) => reader.readCardLocation()],
]);
const msgConstructorMap: Map<string, Constructor> = new Map([
const MsgConstructorMap: Map<string, Constructor> = new Map([
["move", ygopro.StocGameMessage.MsgMove],
]);
......@@ -29,27 +29,51 @@ interface readFieldHandler {
(reader: BufferReader): any;
}
export function penetrate(
msgKey: number,
gameMsg: any,
gameData: Uint8Array
): boolean {
const config = PenetrateConfig.get(msgKey.toString());
const reader = new BufferReader(gameData, true);
if (config) {
const protoType = config.protoType;
const fields = config.fields;
let object: any = {};
for (let field of fields) {
object[field.fieldName] = readField(reader, field.fieldType);
class PenetrateManager {
config: Map<string, penetrateType>;
readFieldHandlerMap: Map<string, readFieldHandler> = ReadFieldHandlerMap;
msgConstructorMap: Map<string, Constructor> = MsgConstructorMap;
constructor(config: any) {
this.config = _objToMap(config);
}
private readField(reader: BufferReader, fieldType: string): any {
const handler = this.readFieldHandlerMap.get(fieldType);
if (handler) {
return handler(reader);
}
return undefined;
}
gameMsg[protoType] = constructMsg(protoType, object);
private constructMsg(protoType: string, object: any): any {
const constructor = this.msgConstructorMap.get(protoType);
if (constructor) {
return new constructor(object);
}
return undefined;
}
return config ? true : false;
penetrate(msgKey: number, gameMsg: any, gameData: Uint8Array): boolean {
const config = this.config.get(msgKey.toString());
const reader = new BufferReader(gameData, true);
if (config) {
const protoType = config.protoType;
const fields = config.fields;
let object: any = {};
for (let field of fields) {
object[field.fieldName] = this.readField(reader, field.fieldType);
}
gameMsg[protoType] = this.constructMsg(protoType, object);
}
return config ? true : false;
}
}
function _objToMap(obj: any): Map<string, penetrateType> {
......@@ -62,20 +86,4 @@ function _objToMap(obj: any): Map<string, penetrateType> {
return map;
}
function readField(reader: BufferReader, fieldType: string): any {
const handler = readFieldHandlerMap.get(fieldType);
if (handler) {
return handler(reader);
}
return undefined;
}
function constructMsg(protoType: string, object: any): any | undefined {
const constructor = msgConstructorMap.get(protoType);
if (constructor) {
return new constructor(object);
}
return undefined;
}
export default new PenetrateManager(PenetrateConfig);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment