Commit 610fcd05 authored by Chunchi Che's avatar Chunchi Che Committed by WANG HE

update

parent 3dd74577
import { ygopro } from "../idl/ocgcore";
import { ygoProPacket } from "./packet";
import { CTOS_JOIN_GAME } from "./protoDecl";
import { ygopro } from "../../idl/ocgcore";
import { ygoProPacket } from "../packet";
import { CTOS_JOIN_GAME } from "../protoDecl";
import { strEncodeUTF16 } from "../util";
const littleEndian: boolean = true;
export default class CtosJoinGamePacket extends ygoProPacket {
constructor(pb: ygopro.YgoCtosMsg) {
const encoder = new TextEncoder();
const joinGame = pb.ctos_join_game;
const version = joinGame.version;
const gameId = joinGame.gameid;
const passWd = encoder.encode(joinGame.passwd);
const passWd = strEncodeUTF16(joinGame.passwd);
const exDataLen = 2 + 4 + passWd.length;
const exData = new Uint8Array(exDataLen);
......
import { ygopro } from "../idl/ocgcore";
import { ygoProPacket } from "./packet";
import { CTOS_PLAYER_INFO } from "./protoDecl";
import { ygopro } from "../../idl/ocgcore";
import { ygoProPacket } from "../packet";
import { CTOS_PLAYER_INFO } from "../protoDecl";
import { strEncodeUTF16 } from "../util";
export default class CtosPlayerInfoPacket extends ygoProPacket {
constructor(pb: ygopro.YgoCtosMsg) {
......
......@@ -15,16 +15,12 @@ export class ygoProPacket {
}
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 array = new Uint8Array(this.packetLen + 2);
const dataView = new DataView(array.buffer);
dataView.setUint16(0, packetLen, littleEndian);
dataView.setUint8(2, proto);
array.slice(3, packetLen + 2).set(exData);
dataView.setUint16(0, this.packetLen, littleEndian);
dataView.setUint8(2, this.proto);
array.slice(3, this.packetLen + 2).set(this.exData);
return array;
}
......
......@@ -2,3 +2,4 @@ export const CTOS_PLAYER_INFO = 16;
export const CTOS_JOIN_GAME = 18;
export const STOC_JOIN_GAME = 18;
export const STOC_CHAT = 25;
import { ygopro } from "../../idl/ocgcore";
import { ygoProPacket, ygoProtobuf } from "../packet";
import { utf8ArrayToStr } from "../util";
export default class StocChatPB implements ygoProtobuf {
packet: ygoProPacket;
constructor(packet: ygoProPacket) {
this.packet = packet;
}
adapt(): ygopro.YgoStocMsg {
const player = new DataView(this.packet.exData.buffer).getInt16(0, true);
const msg = utf8ArrayToStr(this.packet.exData.slice(2));
return new ygopro.YgoStocMsg({
stoc_chat: new ygopro.StocChat({
player,
msg,
}),
});
}
}
import { ygopro } from "../idl/ocgcore";
import { ygoProPacket, ygoProtobuf } from "./packet";
import { ygopro } from "../../idl/ocgcore";
import { ygoProPacket, ygoProtobuf } from "../packet";
export default class StocJoinGamePB implements ygoProtobuf {
packet: ygoProPacket;
......
const UTF16_BUFFER_MAX_LEN = 20;
const FILLING_TOKEN: number = 0xcccc;
export function strEncodeUTF16(str: string) {
let buf = new ArrayBuffer(UTF16_BUFFER_MAX_LEN * 2);
let bufView = new Uint16Array(buf);
bufView.fill(FILLING_TOKEN, 0, UTF16_BUFFER_MAX_LEN);
for (
let i = 0, strLen = str.length;
i < strLen && i < UTF16_BUFFER_MAX_LEN;
i++
) {
bufView[i] = str.charCodeAt(i);
}
return new Uint8Array(buf);
}
export function utf8ArrayToStr(array: Uint8Array) {
let out, i, len, c;
let char2, char3;
out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(
((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
);
break;
}
}
return out;
}
import { ygopro } from "./idl/ocgcore";
import socketMiddleWare, { socketCmd } from "../../middleware/socket";
import { IDeck } from "../Card";
import playerInfoPacket from "./ocgAdapter/ctosPlayerInfo";
import joinGamePacket from "./ocgAdapter/ctosJoinGame";
import playerInfoPacket from "./ocgAdapter/ctos/ctosPlayerInfo";
import joinGamePacket from "./ocgAdapter/ctos/ctosJoinGame";
export function sendUpdateDeck(deck: IDeck) {
const updateDeck = new ygopro.YgoCtosMsg({
......
......@@ -6,11 +6,13 @@ import handleJoinGame from "./room/joinGame";
import handleChat from "./room/chat";
import handleHsWatchChange from "./room/hsWatchChange";
import { ygoArrayBuilder } from "../api/ocgcore/ocgAdapter/packet";
import StocJoinGame from "../api/ocgcore/ocgAdapter/stocJoinGame";
import { STOC_JOIN_GAME } from "../api/ocgcore/ocgAdapter/protoDecl";
import StocJoinGame from "../api/ocgcore/ocgAdapter/stoc/stocJoinGame";
import { STOC_CHAT, STOC_JOIN_GAME } from "../api/ocgcore/ocgAdapter/protoDecl";
import StocChat from "../api/ocgcore/ocgAdapter/stoc/stocChat";
export default function handleSocketMessage(e: MessageEvent) {
const packet = new ygoArrayBuilder(e.data);
console.log(packet);
let pb = new ygopro.YgoStocMsg({});
switch (packet.proto) {
......@@ -19,6 +21,11 @@ export default function handleSocketMessage(e: MessageEvent) {
break;
}
case STOC_CHAT: {
pb = new StocChat(packet).adapt();
break;
}
default: {
break;
}
......
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