机器人移除时, 移除事件监听

This commit is contained in:
zhl 2020-12-14 18:31:03 +08:00
parent 6166dc8e16
commit 3a8e67b284

View File

@ -28,6 +28,8 @@ export class RobotClient implements Client {
myTurn: boolean = false; myTurn: boolean = false;
cards: Map<String, Card>; cards: Map<String, Card>;
onMessageHandlers: {[id: string]: (client: Client, message: any) => void} = {}; 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}) { constructor(sessionId: string, state: CardGameState, clock: Clock, onMessageHandlers: {[id: string]: (client: Client, message: any) => void}) {
this.sessionId = sessionId; this.sessionId = sessionId;
@ -46,8 +48,32 @@ export class RobotClient implements Client {
log(`new robot with session: ${sessionId}`); log(`new robot with session: ${sessionId}`);
} }
addListeners() { addListeners() {
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 {
}
enqueueRaw(data: ArrayLike<number>, options?: ISendOptions): void {
// log('enqueueRaw:' + data.length);
}
error(code: number, message?: string): void {
}
leave(code?: number, data?: string): void {
this.listenerState && this.listenerState();
this.listenerTurn && this.listenerTurn();
this.ref.emit('close');
}
raw(data: ArrayLike<number>, options?: ISendOptions): void {
}
private gameSateUpdate(currentValue: number, previousValue: number) {
let self = this; let self = this;
this.svrstate.listen('gameState', function (currentValue, previousValue) {
log(`server game state change: ${currentValue}, pre value: ${previousValue}`); log(`server game state change: ${currentValue}, pre value: ${previousValue}`);
switch (currentValue) { switch (currentValue) {
case GameStateConst.CHANGE_HERO: case GameStateConst.CHANGE_HERO:
@ -64,31 +90,14 @@ export class RobotClient implements Client {
case GameStateConst.STATE_ROUND_RESULT: case GameStateConst.STATE_ROUND_RESULT:
break; break;
} }
}); }
this.svrstate.listen('currentTurn', function (currentValue, previousValue) { private gameTurnUpdate(currentValue: string, previousValue: string) {
let self = this;
log(`server turn change: ${currentValue}, pre value: ${previousValue}`); log(`server turn change: ${currentValue}, pre value: ${previousValue}`);
self.myTurn = currentValue === self.sessionId; self.myTurn = currentValue === self.sessionId;
if (self.myTurn) { if (self.myTurn) {
self.discard(); self.discard();
} }
})
}
close(code?: number, data?: string): void {
}
enqueueRaw(data: ArrayLike<number>, options?: ISendOptions): void {
// log('enqueueRaw:' + data.length);
}
error(code: number, message?: string): void {
}
leave(code?: number, data?: string): void {
this.ref.emit('close');
}
raw(data: ArrayLike<number>, options?: ISendOptions): void {
} }
public send(messageOrType: any, messageOrOptions?: any | ISendOptions, options?: ISendOptions) { public send(messageOrType: any, messageOrOptions?: any | ISendOptions, options?: ISendOptions) {