Commit eb4a85ab authored by Chunchi Che's avatar Chunchi Che Committed by WANG HE

add playerInfo adapt

parent 9d367625
import { ygopro } from "./idl/ocgcore";
const PACKET_MIN_LEN = 3;
const littleEndian: boolean = true;
const CtosPlayerInfo = 16;
class ygoProPacket {
packetLen: number;
proto: number;
exData: Uint8Array;
constructor(packetLen: number, proto: number, exData: Uint8Array) {
this.packetLen = packetLen;
this.proto = proto;
this.exData = exData;
}
serialize(): Uint8Array {
const packetLen = this.packetLen || 0;
const proto = this.proto || 0;
const exData = this.exData || new Uint8Array();
const array = new Uint8Array(packetLen + 2);
const dataView = new DataView(array);
dataView.setUint16(0, packetLen, littleEndian);
dataView.setUint8(2, proto);
array.slice(3, packetLen + 2).set(exData);
return array;
}
}
export class ygoArrayBuilder extends ygoProPacket {
constructor(array: Uint8Array) {
try {
if (array.length < PACKET_MIN_LEN) {
throw new Error("Packet length too short, length = " + array.length);
} else {
const dataView = new DataView(array);
const packetLen = dataView.getInt16(0, littleEndian);
const proto = dataView.getInt8(2);
const exData = array.slice(3, packetLen + 2);
super(packetLen, proto, exData);
}
} catch (e) {
console.log("[e][ygoProPacket][constructor]" + e);
}
}
}
export class playerInfoPacket extends ygoProPacket {
constructor(pb: ygopro.YgoCtosMsg) {
const encoder = new TextEncoder();
const player = pb.ctos_player_info.name;
const exData = encoder.encode(player);
super(exData.length + 1, CtosPlayerInfo, exData);
}
}
import { ygopro } from "./idl/ocgcore";
import socketMiddleWare, { socketCmd } from "../../middleware/socket";
import { IDeck } from "../Card";
import { playerInfoPacket } from "./ocgAdapter";
export function sendUpdateDeck(deck: IDeck) {
const updateDeck = new ygopro.YgoCtosMsg({
......@@ -36,8 +37,9 @@ export function sendPlayerInfo(ws: WebSocket, player: string) {
name: player,
}),
});
const packet = new playerInfoPacket(playerInfo);
ws.send(playerInfo.serialize());
ws.send(packet.serialize());
}
export function sendJoinGame(ws: WebSocket, version: number, passWd: string) {
......
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