From 3a8e67b284714402a6ad66843e8606e09b991247 Mon Sep 17 00:00:00 2001 From: zhl Date: Mon, 14 Dec 2020 18:31:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E6=97=B6,=20=E7=A7=BB=E9=99=A4=E4=BA=8B=E4=BB=B6=E7=9B=91?= =?UTF-8?q?=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/robot/RobotClient.ts | 61 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/robot/RobotClient.ts b/src/robot/RobotClient.ts index f7e362d..310a3a9 100644 --- a/src/robot/RobotClient.ts +++ b/src/robot/RobotClient.ts @@ -28,6 +28,8 @@ export class RobotClient implements Client { myTurn: boolean = false; cards: Map; onMessageHandlers: {[id: string]: (client: Client, message: any) => void} = {}; + listenerState: any; + listenerTurn: any; constructor(sessionId: string, state: CardGameState, clock: Clock, onMessageHandlers: {[id: string]: (client: Client, message: any) => void}) { this.sessionId = sessionId; @@ -46,33 +48,10 @@ export class RobotClient implements Client { log(`new robot with session: ${sessionId}`); } addListeners() { - let self = this; - this.svrstate.listen('gameState', function (currentValue, previousValue) { - log(`server game state change: ${currentValue}, pre value: ${previousValue}`); - switch (currentValue) { - case GameStateConst.CHANGE_HERO: - self.selectHero(); - break; - case GameStateConst.STATE_CHANGE_CARD: - self.changeCard(); - break; - case GameStateConst.STATE_BEGIN_EAT: - if (!self.myTurn) { - self.eatOrGiveUp(); - } - break; - case GameStateConst.STATE_ROUND_RESULT: - break; - } - }); - this.svrstate.listen('currentTurn', function (currentValue, previousValue) { - log(`server turn change: ${currentValue}, pre value: ${previousValue}`); - self.myTurn = currentValue === self.sessionId; - if (self.myTurn) { - self.discard(); - } - }) + this.listenerState = this.svrstate.listen('gameState', this.gameSateUpdate.bind(this)); + this.listenerTurn = this.svrstate.listen('currentTurn', this.gameTurnUpdate.bind(this)); } + close(code?: number, data?: string): void { } @@ -85,12 +64,42 @@ export class RobotClient implements Client { } leave(code?: number, data?: string): void { + this.listenerState && this.listenerState(); + this.listenerTurn && this.listenerTurn(); this.ref.emit('close'); } raw(data: ArrayLike, options?: ISendOptions): void { } + private gameSateUpdate(currentValue: number, previousValue: number) { + let self = this; + log(`server game state change: ${currentValue}, pre value: ${previousValue}`); + switch (currentValue) { + case GameStateConst.CHANGE_HERO: + self.selectHero(); + break; + case GameStateConst.STATE_CHANGE_CARD: + self.changeCard(); + break; + case GameStateConst.STATE_BEGIN_EAT: + if (!self.myTurn) { + self.eatOrGiveUp(); + } + break; + case GameStateConst.STATE_ROUND_RESULT: + break; + } + } + private gameTurnUpdate(currentValue: string, previousValue: string) { + let self = this; + log(`server turn change: ${currentValue}, pre value: ${previousValue}`); + self.myTurn = currentValue === self.sessionId; + if (self.myTurn) { + self.discard(); + } + } + public send(messageOrType: any, messageOrOptions?: any | ISendOptions, options?: ISendOptions) { log(`receive server msg: ${messageOrType}, ${messageOrOptions}`); let self = this;