diff --git a/src/global.d.ts b/src/global.d.ts index cfbf4ae..66d3451 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -18,13 +18,16 @@ import {BattleHandler} from "./rooms/logic/Handler/BattleHandler"; import {SkillInfoData, SkillInfoMsg} from "./message/SkillInfo"; import {PartResultMsg} from "./message/PartResult"; import {RemovePetMsg} from "./message/RemovePetMsg"; +import {Dispatcher} from "@colyseus/command"; +import {Delayed} from "@gamestdio/timer/lib/Delayed"; /** * GeneralRoom 扩展方法 */ declare module "colyseus" { interface Room { battleMan: BattleHandler; - + dispatcher: Dispatcher; + mainClock: Delayed; /** * 根据sessionId获取client * @param sessionId diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 3f18b9d..f9b6d98 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -12,6 +12,7 @@ import {EatCardCommand} from "./commands/EatCardCommand"; import {GiveUpCommand} from "./commands/GiveUpCommand"; import {BattleHandler} from "./logic/Handler/BattleHandler"; import {debugRoom} from "../common/Debug"; +import {Delayed} from "@gamestdio/timer/lib/Delayed"; export class GeneralRoom extends Room { @@ -19,6 +20,8 @@ export class GeneralRoom extends Room { maxClients = 4; clientMap = new Map(); battleMan = new BattleHandler(); + // 用于游戏过程中各种计时器, 使用该计时器的前提是, 只针对当前操作玩家 + mainClock: Delayed; onCreate (options: any) { let cs = new CardGameState(); diff --git a/src/rooms/commands/DiscardCommand.ts b/src/rooms/commands/DiscardCommand.ts index b3b771c..68c21f5 100644 --- a/src/rooms/commands/DiscardCommand.ts +++ b/src/rooms/commands/DiscardCommand.ts @@ -4,6 +4,8 @@ import gameUtil from "../../utils/game.util"; import {Client} from "colyseus"; import {NextSubCommand} from "./NextSubCommand"; import {GameStateConst} from "../../constants/GameStateConst"; +import {singleton} from "../../common/Singleton"; +import {GameEnv} from "../../cfg/GameEnv"; /** * 出牌 @@ -39,6 +41,14 @@ export class DiscardCommand extends Command { async execute() { let sessionId = this.state.currentTurn; this.room.addCard(sessionId, singleton(GameEnv).roundDrawNum, 0); - let maxTime = singleton(GameEnv).maxDiscardTime; - await this.delay(maxTime * 1000); - if (sessionId == this.state.currentTurn) { - let client = this.room.getClient(sessionId); - error('出牌时间到, 自动出牌'); - let player = this.state.players.get(sessionId); - let card = player.cards.values().next().value; - return [new DiscardCommand().setPayload({client, cards: [card.id], dtype: 1})] - } + let maxTime = singleton(GameEnv).maxDiscardTime * 1000; + let player = this.state.players.get(sessionId); + let self = this; + this.room.mainClock = this.clock.setTimeout(function () { + self.room.mainClock.clear(); + if (sessionId == self.state.currentTurn) { + let client = self.room.getClient(sessionId); + let card = player.cards.values().next().value; + debugRoom('出牌时间到, 自动出牌: ', card.id); + self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1}); + } + }, maxTime + player.extraTime) + // await this.delay((maxTime + player.extraTime) * 1000); + } } diff --git a/src/rooms/commands/GiveUpCommand.ts b/src/rooms/commands/GiveUpCommand.ts index 1f3c7a1..8af2190 100644 --- a/src/rooms/commands/GiveUpCommand.ts +++ b/src/rooms/commands/GiveUpCommand.ts @@ -12,6 +12,9 @@ export class GiveUpCommand extends Command { this.state.giveUpCount += 1; this.room.broadcast('give_up_eat_s2c', {player: client.sessionId}); if (this.state.giveUpCount >= this.room.maxClients - 1) { + if (this.room.mainClock) { + this.room.mainClock.clear(); + } return [new TurnEndCommand()]; } } diff --git a/src/rooms/commands/NextSubCommand.ts b/src/rooms/commands/NextSubCommand.ts index 928546c..a86f334 100644 --- a/src/rooms/commands/NextSubCommand.ts +++ b/src/rooms/commands/NextSubCommand.ts @@ -1,15 +1,25 @@ import { Command } from "@colyseus/command"; import { CardGameState } from "../schema/CardGameState"; import {GameStateConst} from "../../constants/GameStateConst"; +import {singleton} from "../../common/Singleton"; +import {GameEnv} from "../../cfg/GameEnv"; +import {NextTurnCommand} from "./NextTurnCommand"; +import {TurnEndCommand} from "./TurnEndCommand"; /** * 下一个吃牌轮 */ export class NextSubCommand extends Command { - execute() { + async execute() { this.state.gameState = GameStateConst.STATE_BEGIN_EAT; this.state.giveUpCount = 0; + let time = singleton(GameEnv).maxEatTime * 1000; + let self = this; + this.room.mainClock = this.clock.setTimeout(function (){ + self.room.mainClock.clear(); + self.room.dispatcher.dispatch(new TurnEndCommand()); + }, time); // const sessionIds = [...this.state.players.keys()]; // let nextSubTurn = this.state.subTurn ? // sessionIds[(sessionIds.indexOf(this.state.subTurn) + 1) % sessionIds.length] diff --git a/src/rooms/commands/NextTurnCommand.ts b/src/rooms/commands/NextTurnCommand.ts index fdd2f14..1ea9bd3 100644 --- a/src/rooms/commands/NextTurnCommand.ts +++ b/src/rooms/commands/NextTurnCommand.ts @@ -23,6 +23,14 @@ export class NextTurnCommand extends Command { if (this.state.currentTurn && sessionIds.indexOf(this.state.currentTurn) == (sessionIds.length - 1)) { this.state.round += 1; + // 所有玩家根据配置, 增加5点灵活值 + if (this.state.round > 0) { + let moreRoundTime = singleton(GameEnv).roundExtTime * 1000; + let maxTime = 20 * 1000; + for (let [key, p] of this.state.players) { + p.extraTime = Math.min(p.extraTime + moreRoundTime * 1000, maxTime); + } + } } this.state.currentTurn = (this.state.currentTurn) ? sessionIds[(sessionIds.indexOf(this.state.currentTurn) + 1) % sessionIds.length] diff --git a/src/rooms/commands/SelectHeroCommand.ts b/src/rooms/commands/SelectHeroCommand.ts index cad745b..1849e0a 100644 --- a/src/rooms/commands/SelectHeroCommand.ts +++ b/src/rooms/commands/SelectHeroCommand.ts @@ -6,6 +6,8 @@ import {BeginGameCommand} from "./BeginGameCommand"; import {BattleHandler} from "../logic/Handler/BattleHandler"; import {BaseConst} from "../../constants/BaseConst"; import {error} from "../../common/Debug"; +import {singleton} from "../../common/Singleton"; +import {GameEnv} from "../../cfg/GameEnv"; /** * 选择英雄 @@ -27,6 +29,7 @@ export class SelectHeroCommand extends Command