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