From f785915b7a8746e65b15ef63b023c6fe2f088629 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 30 Apr 2021 14:21:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B=E7=8E=A9?= =?UTF-8?q?=E5=AE=B6=E8=BF=9B=E5=87=BA=E6=88=BF=E9=97=B4=E7=9A=84=E5=B9=BF?= =?UTF-8?q?=E6=92=AD=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global.d.ts | 24 ++++++++++++++++++++++++ src/index.ts | 1 + src/rooms/MSender.ts | 27 +++++++++++++++++++++++++++ src/rooms/PuzzleMathRoom.ts | 24 +++++++++++++++++++++++- src/rooms/commands/OnJoinCommand.ts | 1 + src/rooms/schema/Player.ts | 2 ++ 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/rooms/MSender.ts diff --git a/src/global.d.ts b/src/global.d.ts index 159fe23..7b60d3a 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -11,5 +11,29 @@ declare global { } } +declare module 'colyseus' { + interface Room { + // >>>>>>>>> Begin of extend send message <<<<<<<<<<<<< + /** + * 广播玩家加入房间 + * @param data + * @param options + */ + bUserJoin(data?: any, options?: any): void; + + /** + * 广播玩家离开 + * @param data + */ + bUserLeft(data?: any): void; + + /** + * 玩家重连 + * @param data + */ + bUserReconnect(data?: any): void; + } +} + diff --git a/src/index.ts b/src/index.ts index 4f831cf..1685ec0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ app.use(bodyParser.urlencoded({ extended: true, })); initData() +require('./rooms/MSender') global.isProd = isProd const server = http.createServer(app) let port: number diff --git a/src/rooms/MSender.ts b/src/rooms/MSender.ts new file mode 100644 index 0000000..8baed60 --- /dev/null +++ b/src/rooms/MSender.ts @@ -0,0 +1,27 @@ +import { Room } from 'colyseus' + +Object.defineProperties(Room.prototype, { + /** + * 广播玩家加入房间 + * @param data + * @param options + */ + bUserJoin: { + value: function (data?: any, options?: any) { + this.broadcast("player_join", data, options); + }, + writable: true + }, + bUserLeft: { + value: function (data?: any) { + this.broadcast("player_left", data); + }, + writable: true + }, + bUserReconnect: { + value: function (data?: any) { + this.broadcast("player_reconnect", data); + }, + writable: true + }, +}) diff --git a/src/rooms/PuzzleMathRoom.ts b/src/rooms/PuzzleMathRoom.ts index 37c4613..b790295 100644 --- a/src/rooms/PuzzleMathRoom.ts +++ b/src/rooms/PuzzleMathRoom.ts @@ -7,6 +7,7 @@ import { Player } from './schema/Player' import { BeginGameCommand } from './commands/BeginGameCommand' import { PuzzleGameState } from './schema/PuzzleGameState' import { EndGameCommand } from './commands/EndGameCommand' +import { GameStateConst } from '../constants/GameStateConst' export class PuzzleMathRoom extends Room { @@ -18,6 +19,7 @@ export class PuzzleMathRoom extends Room { return true } onCreate(options: any) { + this.autoDispose = false let cs = new PuzzleGameState() this.setState(cs) @@ -32,7 +34,27 @@ export class PuzzleMathRoom extends Room { } async onLeave(client: Client, consented: boolean) { - + if (this.state.gameState === GameStateConst.STATE_GAME_OVER ) { + this.state.players.delete(client.sessionId) + this.bUserLeft(client.sessionId) + } else { + let player = this.state.players.get(client.sessionId) + player.online = 0 + this.bUserLeft(client.sessionId) + try { + if (consented) { + throw new Error('consented leave') + } else { + await this.allowReconnection(client, 20 * 60) + debugRoom(`${ client.sessionId } 重连`) + this.bUserReconnect(client.sessionId) + player.online = 1 + } + } catch (e) { + debugRoom(`player realy level :${ client.sessionId }`) + // this.state.players.delete(client.sessionId); + } + } } onDispose() { diff --git a/src/rooms/commands/OnJoinCommand.ts b/src/rooms/commands/OnJoinCommand.ts index 95f25af..3d8ca46 100644 --- a/src/rooms/commands/OnJoinCommand.ts +++ b/src/rooms/commands/OnJoinCommand.ts @@ -10,5 +10,6 @@ export class OnJoinCommand extends Command