diff --git a/src/rooms/commands/PartResultCommand.ts b/src/rooms/commands/PartResultCommand.ts index d5959d7..0edfec3 100644 --- a/src/rooms/commands/PartResultCommand.ts +++ b/src/rooms/commands/PartResultCommand.ts @@ -119,18 +119,8 @@ export class PartResultCommand extends Command { result.push(obj2) } - let deadCount0 = 0; - let deadCount1 = 0; - for (let [, player] of this.state.players) { - if (player.team == 0 && player.state == PlayerStateConst.PLAYER_DEAD ) { - deadCount0 ++; - } else if (player.team == 1 && player.state == PlayerStateConst.PLAYER_DEAD ){ - deadCount1 ++; - } - } - const roundNum = new GameEnv().duelRoundNum; - const totalRound = roundNum * new GameEnv().maxDuelNum; - if ((deadCount0 == 2 || deadCount1 == 2) || (this.state.round >= totalRound - 1)) { // 游戏结束 + let gameResult = gameUtil.checkGameEnd(this.state); + if (gameResult) { return [new Wait().setPayload(time*1000) ,new GameResultCommand()]; } else { // 下发消息, 进入正常的下一轮 this.room.bPartResult(new PartResultMsg(result)); diff --git a/src/rooms/commands/TurnEndCommand.ts b/src/rooms/commands/TurnEndCommand.ts index 5f62705..40d5802 100644 --- a/src/rooms/commands/TurnEndCommand.ts +++ b/src/rooms/commands/TurnEndCommand.ts @@ -6,6 +6,7 @@ import {NextTurnCommand} from "./NextTurnCommand"; import {PlayerStateConst} from "../../constants/PlayerStateConst"; import {Wait} from "./Wait"; import {GameResultCommand} from "./GameResultCommand"; +import gameUtil from "../../utils/game.util"; /** * 一轮结束 @@ -30,21 +31,10 @@ export class TurnEndCommand extends Command { // } // 判断是否胜利 - let deadCount0 = 0; - let deadCount1 = 0; - for (let [, player] of this.state.players) { - if (player.team == 0 && player.state == PlayerStateConst.PLAYER_DEAD ) { - deadCount0 ++; - } else if (player.team == 1 && player.state == PlayerStateConst.PLAYER_DEAD ){ - deadCount1 ++; - } - } - const roundNum = new GameEnv().duelRoundNum; - const totalRound = roundNum * new GameEnv().maxDuelNum; - if ((deadCount0 == 2 || deadCount1 == 2) || (this.state.round >= totalRound - 1)) { // 游戏结束 + let result = gameUtil.checkGameEnd(this.state); + if (result) { return [new GameResultCommand()]; } - return [new NextTurnCommand()] } diff --git a/src/utils/game.util.ts b/src/utils/game.util.ts index 9f59bb3..63705ab 100644 --- a/src/utils/game.util.ts +++ b/src/utils/game.util.ts @@ -6,6 +6,10 @@ import {SystemCardCfg} from "../cfg/parsers/SystemCardCfg"; import {EffectCardCfg} from "../cfg/parsers/EffectCardCfg"; import {error} from "../common/Debug"; import {Room} from "colyseus"; +import {PlayerStateConst} from "../constants/PlayerStateConst"; +import {GameEnv} from "../cfg/GameEnv"; +import {GameResultCommand} from "../rooms/commands/GameResultCommand"; +import {CardGameState} from "../rooms/schema/CardGameState"; let gameUtil = { /** @@ -292,6 +296,20 @@ let gameUtil = { return result; }, + checkGameEnd(state: CardGameState) { + let deadCount0 = 0; + let deadCount1 = 0; + for (let [, player] of state.players) { + if (player.team == 0 && player.state == PlayerStateConst.PLAYER_DEAD ) { + deadCount0 ++; + } else if (player.team == 1 && player.state == PlayerStateConst.PLAYER_DEAD ){ + deadCount1 ++; + } + } + const roundNum = new GameEnv().duelRoundNum; + const totalRound = roundNum * new GameEnv().maxDuelNum; + return (deadCount0 == 2 || deadCount1 == 2) || (state.round >= totalRound - 1); + } } export default gameUtil;