diff --git a/src/server/kcp/Session.ts b/src/server/kcp/Session.ts index 5e1378c..cb8b4f2 100644 --- a/src/server/kcp/Session.ts +++ b/src/server/kcp/Session.ts @@ -12,10 +12,12 @@ import { BlackLimitLevel, PlayerKickOutScNotify, PlayerKickOutScNotify_KickType, import Avatar from '../../db/Avatar'; import SRServer from './SRServer'; import { HandshakeType } from './Handshake'; +import ProtoFactory, { MessageType } from '../../util/ProtoFactory'; function r(...args: string[]) { return fs.readFileSync(resolve(__dirname, ...args)); } +type UnWrapMessageType = T extends MessageType ? U : T; export default class Session { public key: Buffer = r('./initial.key'); @@ -78,20 +80,32 @@ export default class Session { public async sync() { const avatars = await Avatar.fromUID(this.player.db._id); - this.send("PlayerSyncScNotify", { + this.sendT(PlayerSyncScNotify, PlayerSyncScNotify.fromPartial({ avatarSync: { avatarList: avatars.map(x => x.data), }, basicInfo: this.player.db.basicInfo - } as PlayerSyncScNotify); + })); this.player.save(); } + public async sendT, >(type: Class, data: UnWrapMessageType) { + const encodedBuffer = type.encode(data).finish(); + const typeName = ProtoFactory.getName(type); + this.c.verbL(data); + this.c.verbH(encodedBuffer); + if (Logger.VERBOSE_LEVEL >= VerboseLevel.WARNS) this.c.log(typeName); + + //todo: might want to regen the ts-proto types with env = node + this.kcpobj.send(Buffer.from(encodedBuffer)); + } + + public kick(hard: boolean = true) { SRServer.getInstance().sessions.delete(this.id); this.kicked = true; - if (hard) this.send("PlayerKickOutScNotify", { + if (hard) this.sendT(PlayerKickOutScNotify, { kickType: PlayerKickOutScNotify_KickType.KICK_BLACK, blackInfo: { limitLevel: BlackLimitLevel.BLACK_LIMIT_LEVEL_ALL, @@ -99,7 +113,7 @@ export default class Session { endTime: Math.round(Date.now() / 1000), banType: 2 } - } as PlayerKickOutScNotify); + }); SRServer.getInstance().handshake(HandshakeType.DISCONNECT, this.ctx); } diff --git a/src/util/ProtoFactory.ts b/src/util/ProtoFactory.ts index ebc3b82..2fbb9b0 100644 --- a/src/util/ProtoFactory.ts +++ b/src/util/ProtoFactory.ts @@ -5,7 +5,7 @@ import { CmdID, PacketName } from "../server/kcp/Packet" import Logger, { VerboseLevel } from "./Logger"; const c = new Logger("ProtoFactory"); -class MessageType { +export class MessageType { "encode": (arg0: T) => protobufjs.Writer; "fromPartial": (arg0: object) => T; // "decode": (input: protobufjs.Reader | Uint8Array, length?: number)=> T; @@ -15,18 +15,14 @@ class MessageType { //fromjson etc... } -type UnWrapMessageType = T extends MessageType ? U : T; const messageTypeMap = new Map>(); const messageTypeMapReversed = new Map, PacketName>(); -function send, >(type: Class, data: UnWrapMessageType) { - console.log(type.encode(data).finish()) -} -function isMessageType(pet: MessageType | any): pet is MessageType { - return (>pet).encode !== undefined; +function isMessageType(type: MessageType | any): type is MessageType { + return (>type).encode !== undefined; } @@ -64,26 +60,6 @@ export default class ProtoFactory { // return; //if you want a partial type - send(types.PlayerLoginScRsp, { - basicInfo: { - exp: 0, - level: 1, - hcoin: 0, - mcoin: 0, - nickname: "test", - scoin: 0, - stamina: 100, - worldLevel: 1, - }, - isNewPlayer: true, - stamina: 100, - curTimezone: 1, - serverTimestampMs: Math.round(new Date().getTime() / 1000), - bsBinVersion: "1.0.0", - retcode: 0, - isRelay: false, - loginRandom: 0, - }); } }