Basic Generic Send as sendT

This commit is contained in:
TheLostTree 2022-08-02 22:51:42 -07:00
parent 44eb6177f0
commit a5500cf32f
2 changed files with 21 additions and 31 deletions

View File

@ -12,10 +12,12 @@ import { BlackLimitLevel, PlayerKickOutScNotify, PlayerKickOutScNotify_KickType,
import Avatar from '../../db/Avatar'; import Avatar from '../../db/Avatar';
import SRServer from './SRServer'; import SRServer from './SRServer';
import { HandshakeType } from './Handshake'; import { HandshakeType } from './Handshake';
import ProtoFactory, { MessageType } from '../../util/ProtoFactory';
function r(...args: string[]) { function r(...args: string[]) {
return fs.readFileSync(resolve(__dirname, ...args)); return fs.readFileSync(resolve(__dirname, ...args));
} }
type UnWrapMessageType<T> = T extends MessageType<infer U> ? U : T;
export default class Session { export default class Session {
public key: Buffer = r('./initial.key'); public key: Buffer = r('./initial.key');
@ -78,20 +80,32 @@ export default class Session {
public async sync() { public async sync() {
const avatars = await Avatar.fromUID(this.player.db._id); const avatars = await Avatar.fromUID(this.player.db._id);
this.send("PlayerSyncScNotify", { this.sendT(PlayerSyncScNotify, PlayerSyncScNotify.fromPartial({
avatarSync: { avatarSync: {
avatarList: avatars.map(x => x.data), avatarList: avatars.map(x => x.data),
}, },
basicInfo: this.player.db.basicInfo basicInfo: this.player.db.basicInfo
} as PlayerSyncScNotify); }));
this.player.save(); this.player.save();
} }
public async sendT<Class extends MessageType<any>, >(type: Class, data: UnWrapMessageType<Class>) {
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) { public kick(hard: boolean = true) {
SRServer.getInstance().sessions.delete(this.id); SRServer.getInstance().sessions.delete(this.id);
this.kicked = true; this.kicked = true;
if (hard) this.send("PlayerKickOutScNotify", { if (hard) this.sendT(PlayerKickOutScNotify, {
kickType: PlayerKickOutScNotify_KickType.KICK_BLACK, kickType: PlayerKickOutScNotify_KickType.KICK_BLACK,
blackInfo: { blackInfo: {
limitLevel: BlackLimitLevel.BLACK_LIMIT_LEVEL_ALL, limitLevel: BlackLimitLevel.BLACK_LIMIT_LEVEL_ALL,
@ -99,7 +113,7 @@ export default class Session {
endTime: Math.round(Date.now() / 1000), endTime: Math.round(Date.now() / 1000),
banType: 2 banType: 2
} }
} as PlayerKickOutScNotify); });
SRServer.getInstance().handshake(HandshakeType.DISCONNECT, this.ctx); SRServer.getInstance().handshake(HandshakeType.DISCONNECT, this.ctx);
} }

View File

@ -5,7 +5,7 @@ import { CmdID, PacketName } from "../server/kcp/Packet"
import Logger, { VerboseLevel } from "./Logger"; import Logger, { VerboseLevel } from "./Logger";
const c = new Logger("ProtoFactory"); const c = new Logger("ProtoFactory");
class MessageType<T> { export class MessageType<T> {
"encode": (arg0: T) => protobufjs.Writer; "encode": (arg0: T) => protobufjs.Writer;
"fromPartial": (arg0: object) => T; "fromPartial": (arg0: object) => T;
// "decode": (input: protobufjs.Reader | Uint8Array, length?: number)=> T; // "decode": (input: protobufjs.Reader | Uint8Array, length?: number)=> T;
@ -15,18 +15,14 @@ class MessageType<T> {
//fromjson etc... //fromjson etc...
} }
type UnWrapMessageType<T> = T extends MessageType<infer U> ? U : T;
const messageTypeMap = new Map<PacketName, MessageType<any>>(); const messageTypeMap = new Map<PacketName, MessageType<any>>();
const messageTypeMapReversed = new Map<MessageType<any>, PacketName>(); const messageTypeMapReversed = new Map<MessageType<any>, PacketName>();
function send<Class extends MessageType<any>, >(type: Class, data: UnWrapMessageType<Class>) {
console.log(type.encode(data).finish())
}
function isMessageType<T>(pet: MessageType<T> | any): pet is MessageType<T> { function isMessageType<T>(type: MessageType<T> | any): type is MessageType<T> {
return (<MessageType<T>>pet).encode !== undefined; return (<MessageType<T>>type).encode !== undefined;
} }
@ -64,26 +60,6 @@ export default class ProtoFactory {
// return; // return;
//if you want a partial type //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,
});
} }
} }