From 832e5266b0385deaa5cece30d6074a76059d5009 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 11 Dec 2020 17:27:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9state=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AE=B5listerners=E7=9A=84null=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/index.ts | 2 +- src/robot/RobotClient.ts | 8 +++++++- src/rooms/commands/GameResultCommand.ts | 2 +- src/rooms/commands/NextTurnCommand.ts | 4 ++-- src/rooms/schema/CardGameState.ts | 4 ++-- 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 6bcbbe7..9124825 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "debug": "node --require ts-node/register --inspect src/index.ts", "dev": "DEBUG=colyseus:*,jc:* node --require ts-node/register --inspect src/index.ts", "dev:less": "DEBUG=colyseus:command,jc:* node --require ts-node/register --inspect=0.0.0.0:9229 src/index.ts", + "dev:jc": "DEBUG=jc:* node --require ts-node/register --inspect=0.0.0.0:9229 src/index.ts", "loadtest": "colyseus-loadtest loadtest/example.ts --room my_room --numClients 3", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/src/index.ts b/src/index.ts index fef3163..1972080 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ const server = http.createServer(app); const gameServer = new Server({ server, // driver: new MongooseDriver('mongodb://127.0.0.1/card-development'), - driver: new MongooseDriver('mongodb://192.168.100.24/card-development-z'), + driver: new MongooseDriver('mongodb://192.168.100.24/card-development-x'), }); // register your room handlers diff --git a/src/robot/RobotClient.ts b/src/robot/RobotClient.ts index e9df88a..c464d18 100644 --- a/src/robot/RobotClient.ts +++ b/src/robot/RobotClient.ts @@ -6,6 +6,9 @@ import {CardGameState} from "../rooms/schema/CardGameState"; import Clock from "@gamestdio/timer"; import {GameStateConst} from "../constants/GameStateConst"; import {Card} from "../rooms/schema/Card"; +import {BaseConst} from "../constants/BaseConst"; +import arrUtil from "../utils/array.util"; +import {HeroCfg} from "../cfg/parsers/HeroCfg"; export class RobotClient implements Client { id: string; @@ -122,8 +125,11 @@ export class RobotClient implements Client { } private selectHero() { this.cards = this.svrstate.players.get(this.sessionId).cards; + let heroMap: Map = global.$cfg.get(BaseConst.HERO); + let heroArr = [...heroMap.values()]; + let hero = arrUtil.randomGet(heroArr, 1); this.reply('select_hero_c2s', { - heroId: 30011 + heroId: hero[0].id }); } private changeCard(cardIds: number[]) { diff --git a/src/rooms/commands/GameResultCommand.ts b/src/rooms/commands/GameResultCommand.ts index fa97753..72056d3 100644 --- a/src/rooms/commands/GameResultCommand.ts +++ b/src/rooms/commands/GameResultCommand.ts @@ -49,7 +49,7 @@ export class GameResultCommand extends Command { */ resetAllState() { this.state.restartCount = 0; - this.state.currentTurn = undefined; + this.state.updateGameTurn( undefined); this.state.subTurn = undefined; this.state.round = 0; this.state.cardQueue.length = 0; diff --git a/src/rooms/commands/NextTurnCommand.ts b/src/rooms/commands/NextTurnCommand.ts index e52960c..a404d24 100644 --- a/src/rooms/commands/NextTurnCommand.ts +++ b/src/rooms/commands/NextTurnCommand.ts @@ -32,9 +32,9 @@ export class NextTurnCommand extends Command { } } } - this.state.currentTurn = (this.state.currentTurn) + this.state.updateGameTurn((this.state.currentTurn) ? sessionIds[(sessionIds.indexOf(this.state.currentTurn) + 1) % sessionIds.length] - : sessionIds[0]; + : sessionIds[0]); let player = this.state.players.get(this.state.currentTurn); if (!player) { error('未找到玩家'); diff --git a/src/rooms/schema/CardGameState.ts b/src/rooms/schema/CardGameState.ts index 6a29140..a683bc5 100644 --- a/src/rooms/schema/CardGameState.ts +++ b/src/rooms/schema/CardGameState.ts @@ -60,12 +60,12 @@ export class CardGameState extends Schema { updateGameState(val: number) { let preVal = this.gameState; this.gameState = val; - this.$listeners.gameState.invoke(val, preVal); + this.$listeners?.gameState?.invoke(val, preVal); } updateGameTurn(val: string) { let preVal = this.currentTurn; this.currentTurn = val; - this.$listeners.currentTurn.invoke(val, preVal); + this.$listeners?.currentTurn?.invoke(val, preVal); } }