From 58c81f7688c30dc4c3ec787f0c95bc6283ec289c Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 9 Dec 2020 12:03:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E7=8E=B0,=20=E5=A2=9E=E5=8A=A0=E5=BE=80?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global.d.ts | 22 ++++++++++++ src/rooms/GeneralRoom.ts | 45 ++++++++++++++++++++++++- src/rooms/commands/DiscardCommand.ts | 27 +++++++++++---- src/rooms/commands/DrawCommand.ts | 17 +++++++--- src/rooms/commands/EatConfirmCommand.ts | 26 +++++++++----- src/rooms/commands/NextSubCommand.ts | 13 ++++--- src/rooms/commands/SelectPetCommand.ts | 25 +++++++++----- 7 files changed, 141 insertions(+), 34 deletions(-) diff --git a/src/global.d.ts b/src/global.d.ts index 2697a9c..e5a6b18 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -182,6 +182,28 @@ declare module "colyseus" { */ updatePet(datas: PetInfo[]): void; + /** + * 给room.mainClock设定任务 + * mainClock任何时候只有一个可执行的任务 + * @param millisecond + * @param handler + * @param name + */ + beginSchedule(millisecond: number, handler: Function, name: string): void; + + /** + * 取消当前room.mainClock的任务 + * mainClock任何时候只有一个可执行的任务 + * 返回当前剩余的毫秒数 + */ + stopSchedule(): number; + /** + * 给room.mainClock增加n秒 + * @param millisecond + * @param reason + */ + addScheduleTime(millisecond: number, reason?: string): void; + } } diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index ce7100d..cec5f0d 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -10,7 +10,7 @@ import {SelectHeroCommand} from "./commands/SelectHeroCommand"; import {EatCardCommand} from "./commands/EatCardCommand"; import {GiveUpCommand} from "./commands/GiveUpCommand"; import {BattleHandler} from "./logic/Handler/BattleHandler"; -import {msgLog} from "../common/Debug"; +import {debugRoom, error, msgLog} from "../common/Debug"; import {Delayed} from "@gamestdio/timer/lib/Delayed"; import {IncomingMessage} from "http"; import {PlayerStateConst} from "../constants/PlayerStateConst"; @@ -109,4 +109,47 @@ export class GeneralRoom extends Room { return this.clients.find(client => client.sessionId == sessionId ); } + /** + * 给room.mainClock设定任务 + * mainClock任何时候只有一个可执行的任务 + * @param millisecond + * @param handler + * @param name + */ + beginSchedule(millisecond: number, handler: Function, name: string): void { + debugRoom(`begin schedule: `, name, millisecond / 1000); + if (this.mainClock?.active) { + error(`当前已存在进行中的mainClock: ${this.mainClock['args']}`); + } + this.mainClock = this.clock.setTimeout(handler, millisecond , name); + } + + /** + * 取消当前room.mainClock的任务 + * mainClock任何时候只有一个可执行的任务 + */ + stopSchedule(): number { + debugRoom(`manual stop schedule: ${this.mainClock['args']}`); + if (!this.mainClock.active) { + return -1; + } else { + let time = this.mainClock.elapsedTime; + this.mainClock.clear(); + return time; + } + } + + /** + * 给room的mainClock增加n秒 + * @param millisecond + * @param reason + */ + addScheduleTime(millisecond: number, reason?: string): void { + debugRoom(`add schedule for ${this.mainClock['args']}, time: ${millisecond/1000}`); + if (this.mainClock?.active) { + this.mainClock.time += millisecond ; + debugRoom(`schedule remain: ${(this.mainClock.time - this.mainClock.elapsedTime)/1000}`); + } + } + } diff --git a/src/rooms/commands/DiscardCommand.ts b/src/rooms/commands/DiscardCommand.ts index 5713cf0..a63152d 100644 --- a/src/rooms/commands/DiscardCommand.ts +++ b/src/rooms/commands/DiscardCommand.ts @@ -44,13 +44,20 @@ export class DiscardCommand extends Command= 0) { let maxTime = singleton(GameEnv).maxDiscardTime * 1000; - let count = this.room.mainClock.elapsedTime - maxTime; - let newCount = player.extraTime - count; + let count = elapsedTime - maxTime; + let newCount = player.extraTime - Math.min(count, 0); player.extraTime = Math.max(newCount, 0); - this.room.mainClock.clear(); } + // if (this.room.mainClock?.active) { + // let maxTime = singleton(GameEnv).maxDiscardTime * 1000; + // let count = this.room.mainClock.elapsedTime - maxTime; + // let newCount = player.extraTime - count; + // player.extraTime = Math.max(newCount, 0); + // this.room.mainClock.clear(); + // } for (let id of cards) { this.state.cards.set(id+'', player.cards.get(id + '')); player.cards.delete(id + ''); @@ -69,12 +76,18 @@ export class DiscardCommand extends Command { 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(); + let timeOverDraw = function () { if (sessionId == self.state.currentTurn) { let client = self.room.getClient(sessionId); let card = player.cards.values().next().value; @@ -24,8 +23,18 @@ export class DrawCommand extends Command { player.extraTime = 0; self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1}); } - }, maxTime + player.extraTime) - // await this.delay((maxTime + player.extraTime) * 1000); + } + this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, `draw_card_${sessionId}`); + // 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); + // player.extraTime = 0; + // self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1}); + // } + // }, maxTime + player.extraTime) } } diff --git a/src/rooms/commands/EatConfirmCommand.ts b/src/rooms/commands/EatConfirmCommand.ts index 633614c..39ae588 100644 --- a/src/rooms/commands/EatConfirmCommand.ts +++ b/src/rooms/commands/EatConfirmCommand.ts @@ -31,9 +31,10 @@ export class EatConfirmCommand extends Command= playerCount) { // 所有人都放弃了, 则取消定时, 直接进入下一轮 - if (this.room.mainClock?.active) { - this.room.mainClock.clear(); - } + this.room.stopSchedule(); + // if (this.room.mainClock?.active) { + // this.room.mainClock.clear(); + // } return [new TurnEndCommand()]; } @@ -65,9 +66,10 @@ export class EatConfirmCommand extends Command { let time = singleton(GameEnv).maxEatTime * 1000; let self = this; // 启动吃牌定时, 到时后自动进入下一轮 - this.room.mainClock = this.clock.setTimeout(function () { + let timeOverEat = function () { debugRoom('吃牌时间到, 进入下一轮') - self.room.mainClock.clear(); - // self.room.dispatcher.dispatch(new TurnEndCommand()); self.room.dispatcher.dispatch(new EatConfirmCommand(), {timeUp: true}); - }, time); + } + this.room.beginSchedule(time, timeOverEat, `eat_round_${this.state.currentTurn}`); + // this.room.mainClock = this.clock.setTimeout(function () { + // debugRoom('吃牌时间到, 进入下一轮') + // self.room.mainClock.clear(); + // // self.room.dispatcher.dispatch(new TurnEndCommand()); + // self.room.dispatcher.dispatch(new EatConfirmCommand(), {timeUp: true}); + // }, time); } } diff --git a/src/rooms/commands/SelectPetCommand.ts b/src/rooms/commands/SelectPetCommand.ts index ea08108..1e439f4 100644 --- a/src/rooms/commands/SelectPetCommand.ts +++ b/src/rooms/commands/SelectPetCommand.ts @@ -30,9 +30,9 @@ export class SelectPetCommand extends Command= 0) { + let count = elapsedTime - singleton(GameEnv).playerActTime * 1000; + let newCount = player.extraTime - Math.min(count, 0); player.extraTime = Math.max(newCount, 0); - this.room.mainClock.clear(); } + // if (this.room.mainClock?.active) { + // let count = this.room.mainClock.elapsedTime - singleton(GameEnv).playerActTime * 1000; + // let newCount = player.extraTime - count; + // player.extraTime = Math.max(newCount, 0); + // this.room.mainClock.clear(); + // } let dstplayer = this.state.players.get(playerId); let dstpet; @@ -83,6 +89,7 @@ export class SelectPetCommand extends Command