机器人移除时, 移除事件监听
This commit is contained in:
parent
6166dc8e16
commit
3a8e67b284
@ -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,33 +48,10 @@ export class RobotClient implements Client {
|
|||||||
log(`new robot with session: ${sessionId}`);
|
log(`new robot with session: ${sessionId}`);
|
||||||
}
|
}
|
||||||
addListeners() {
|
addListeners() {
|
||||||
let self = this;
|
this.listenerState = this.svrstate.listen('gameState', this.gameSateUpdate.bind(this));
|
||||||
this.svrstate.listen('gameState', function (currentValue, previousValue) {
|
this.listenerTurn = this.svrstate.listen('currentTurn', this.gameTurnUpdate.bind(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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(code?: number, data?: string): void {
|
close(code?: number, data?: string): void {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -85,12 +64,42 @@ export class RobotClient implements Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
leave(code?: number, data?: string): void {
|
leave(code?: number, data?: string): void {
|
||||||
|
this.listenerState && this.listenerState();
|
||||||
|
this.listenerTurn && this.listenerTurn();
|
||||||
this.ref.emit('close');
|
this.ref.emit('close');
|
||||||
}
|
}
|
||||||
|
|
||||||
raw(data: ArrayLike<number>, options?: ISendOptions): void {
|
raw(data: ArrayLike<number>, 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) {
|
public send(messageOrType: any, messageOrOptions?: any | ISendOptions, options?: ISendOptions) {
|
||||||
log(`receive server msg: ${messageOrType}, ${messageOrOptions}`);
|
log(`receive server msg: ${messageOrType}, ${messageOrOptions}`);
|
||||||
let self = this;
|
let self = this;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user