增加一些玩家进出房间的广播消息

This commit is contained in:
zhl 2021-04-30 14:21:25 +08:00
parent 911b2d107b
commit f785915b7a
6 changed files with 78 additions and 1 deletions

24
src/global.d.ts vendored
View File

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

View File

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

27
src/rooms/MSender.ts Normal file
View File

@ -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
},
})

View File

@ -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() {

View File

@ -10,5 +10,6 @@ export class OnJoinCommand extends Command<PuzzleGameState, {
async execute({ client, accountId} = this.payload) {
let player = new Player(client.id, accountId)
this.state.players.set(client.sessionId, player)
this.room.bUserJoin(`${ client.sessionId }`, { except: client })
}
}

View File

@ -14,6 +14,8 @@ export class Player extends Schema {
rank: number
team: number
@type('number')
online:number
constructor(id: string, accountId: string) {
super()