diff --git a/src/global.d.ts b/src/global.d.ts index c20e83b..18a8912 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -266,6 +266,16 @@ declare module 'colyseus' { */ scheduleActive(name: string): boolean; + /** + * 暂停所有激活的定时器 + */ + pauseAllSchedule(): string[]; + + /** + * 恢复所有暂停了的定时器 + */ + resumeAllSchedule(): string[]; + /** * 取消某个计时器 */ diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index d366a25..a93cd89 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -25,6 +25,8 @@ import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand' import { RoomOptions } from '../cfg/RoomOptions' import { PlayerStateConst } from '../constants/PlayerStateConst' import { PlayLeftCommand } from './commands/PlayLeftCommand' +import { PauseCommand } from './commands/PauseCommand' +import { ResumeCommand } from './commands/ResumeCommand' export class GeneralRoom extends Room { dispatcher = new Dispatcher(this) @@ -162,6 +164,15 @@ export class GeneralRoom extends Room { msgLog('player_left_c2s from ', client.sessionId) this.dispatcher.dispatch(new PlayLeftCommand(), { client }) }) + this.onMessage('pause_c2s', (client) => { + msgLog('pause_c2s from ', client.sessionId) + this.dispatcher.dispatch(new PauseCommand(), { client }) + }) + + this.onMessage('resume_c2s', (client) => { + msgLog('resume_c2s from ', client.sessionId) + this.dispatcher.dispatch(new ResumeCommand(), { client }) + }) this.onMessage('*', (client, type, message) => { // @@ -307,6 +318,27 @@ export class GeneralRoom extends Room { scheduleActive(name: string): boolean { return this.gameClock.has(name) && this.gameClock.get(name).active } + pauseAllSchedule() { + let result: string[] = [] + for (let [name, clock] of this.gameClock) { + if (clock.active) { + result.push(name) + clock.pause() + } + } + return result + } + resumeAllSchedule(): string[] { + let result: string[] = [] + for (let [name, clock] of this.gameClock) { + if (clock.active && clock.paused) { + result.push(name) + clock.resume() + } + } + return result + } + /** * 暂停某个计时器, 返回这个机器器的剩余时间 * @param {string} name diff --git a/src/rooms/commands/PauseCommand.ts b/src/rooms/commands/PauseCommand.ts new file mode 100644 index 0000000..d0656a3 --- /dev/null +++ b/src/rooms/commands/PauseCommand.ts @@ -0,0 +1,11 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { Client } from 'colyseus' + +export class PauseCommand extends Command { + + execute({ client } = this.payload) { + let result = this.room.pauseAllSchedule() + client.send('pause_s2c', {}) + } +} diff --git a/src/rooms/commands/ResumeCommand.ts b/src/rooms/commands/ResumeCommand.ts new file mode 100644 index 0000000..0c9d398 --- /dev/null +++ b/src/rooms/commands/ResumeCommand.ts @@ -0,0 +1,11 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { Client } from 'colyseus' + +export class ResumeCommand extends Command { + + execute({client} = this.payload) { + this.room.resumeAllSchedule() + client.send('resume_s2c', {}) + } +}