增加客户端暂停和恢复消息的处理
This commit is contained in:
parent
21695fc5c0
commit
e1a40e4a73
10
src/global.d.ts
vendored
10
src/global.d.ts
vendored
@ -266,6 +266,16 @@ declare module 'colyseus' {
|
|||||||
*/
|
*/
|
||||||
scheduleActive(name: string): boolean;
|
scheduleActive(name: string): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 暂停所有激活的定时器
|
||||||
|
*/
|
||||||
|
pauseAllSchedule(): string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 恢复所有暂停了的定时器
|
||||||
|
*/
|
||||||
|
resumeAllSchedule(): string[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取消某个计时器
|
* 取消某个计时器
|
||||||
*/
|
*/
|
||||||
|
@ -25,6 +25,8 @@ import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand'
|
|||||||
import { RoomOptions } from '../cfg/RoomOptions'
|
import { RoomOptions } from '../cfg/RoomOptions'
|
||||||
import { PlayerStateConst } from '../constants/PlayerStateConst'
|
import { PlayerStateConst } from '../constants/PlayerStateConst'
|
||||||
import { PlayLeftCommand } from './commands/PlayLeftCommand'
|
import { PlayLeftCommand } from './commands/PlayLeftCommand'
|
||||||
|
import { PauseCommand } from './commands/PauseCommand'
|
||||||
|
import { ResumeCommand } from './commands/ResumeCommand'
|
||||||
|
|
||||||
export class GeneralRoom extends Room {
|
export class GeneralRoom extends Room {
|
||||||
dispatcher = new Dispatcher(this)
|
dispatcher = new Dispatcher(this)
|
||||||
@ -162,6 +164,15 @@ export class GeneralRoom extends Room {
|
|||||||
msgLog('player_left_c2s from ', client.sessionId)
|
msgLog('player_left_c2s from ', client.sessionId)
|
||||||
this.dispatcher.dispatch(new PlayLeftCommand(), { client })
|
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) => {
|
this.onMessage('*', (client, type, message) => {
|
||||||
//
|
//
|
||||||
@ -307,6 +318,27 @@ export class GeneralRoom extends Room {
|
|||||||
scheduleActive(name: string): boolean {
|
scheduleActive(name: string): boolean {
|
||||||
return this.gameClock.has(name) && this.gameClock.get(name).active
|
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
|
* @param {string} name
|
||||||
|
11
src/rooms/commands/PauseCommand.ts
Normal file
11
src/rooms/commands/PauseCommand.ts
Normal 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', {})
|
||||||
|
}
|
||||||
|
}
|
11
src/rooms/commands/ResumeCommand.ts
Normal file
11
src/rooms/commands/ResumeCommand.ts
Normal 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', {})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user