增加客户端暂停和恢复消息的处理

This commit is contained in:
zhl 2021-03-24 12:35:05 +08:00
parent 21695fc5c0
commit e1a40e4a73
4 changed files with 64 additions and 0 deletions

10
src/global.d.ts vendored
View File

@ -266,6 +266,16 @@ declare module 'colyseus' {
*/
scheduleActive(name: string): boolean;
/**
*
*/
pauseAllSchedule(): string[];
/**
*
*/
resumeAllSchedule(): string[];
/**
*
*/

View File

@ -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

View File

@ -0,0 +1,11 @@
import { Command } from '@colyseus/command'
import { CardGameState } from '../schema/CardGameState'
import { Client } from 'colyseus'
export class PauseCommand extends Command<CardGameState, { client: Client }> {
execute({ client } = this.payload) {
let result = this.room.pauseAllSchedule()
client.send('pause_s2c', {})
}
}

View File

@ -0,0 +1,11 @@
import { Command } from '@colyseus/command'
import { CardGameState } from '../schema/CardGameState'
import { Client } from 'colyseus'
export class ResumeCommand extends Command<CardGameState, {client: Client}> {
execute({client} = this.payload) {
this.room.resumeAllSchedule()
client.send('resume_s2c', {})
}
}