增加一些玩家进出房间的广播消息
This commit is contained in:
parent
911b2d107b
commit
f785915b7a
24
src/global.d.ts
vendored
24
src/global.d.ts
vendored
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ app.use(bodyParser.urlencoded({
|
|||||||
extended: true,
|
extended: true,
|
||||||
}));
|
}));
|
||||||
initData()
|
initData()
|
||||||
|
require('./rooms/MSender')
|
||||||
global.isProd = isProd
|
global.isProd = isProd
|
||||||
const server = http.createServer(app)
|
const server = http.createServer(app)
|
||||||
let port: number
|
let port: number
|
||||||
|
27
src/rooms/MSender.ts
Normal file
27
src/rooms/MSender.ts
Normal 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
|
||||||
|
},
|
||||||
|
})
|
@ -7,6 +7,7 @@ import { Player } from './schema/Player'
|
|||||||
import { BeginGameCommand } from './commands/BeginGameCommand'
|
import { BeginGameCommand } from './commands/BeginGameCommand'
|
||||||
import { PuzzleGameState } from './schema/PuzzleGameState'
|
import { PuzzleGameState } from './schema/PuzzleGameState'
|
||||||
import { EndGameCommand } from './commands/EndGameCommand'
|
import { EndGameCommand } from './commands/EndGameCommand'
|
||||||
|
import { GameStateConst } from '../constants/GameStateConst'
|
||||||
|
|
||||||
|
|
||||||
export class PuzzleMathRoom extends Room {
|
export class PuzzleMathRoom extends Room {
|
||||||
@ -18,6 +19,7 @@ export class PuzzleMathRoom extends Room {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
onCreate(options: any) {
|
onCreate(options: any) {
|
||||||
|
this.autoDispose = false
|
||||||
let cs = new PuzzleGameState()
|
let cs = new PuzzleGameState()
|
||||||
this.setState(cs)
|
this.setState(cs)
|
||||||
|
|
||||||
@ -32,7 +34,27 @@ export class PuzzleMathRoom extends Room {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async onLeave(client: Client, consented: boolean) {
|
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() {
|
onDispose() {
|
||||||
|
@ -10,5 +10,6 @@ export class OnJoinCommand extends Command<PuzzleGameState, {
|
|||||||
async execute({ client, accountId} = this.payload) {
|
async execute({ client, accountId} = this.payload) {
|
||||||
let player = new Player(client.id, accountId)
|
let player = new Player(client.id, accountId)
|
||||||
this.state.players.set(client.sessionId, player)
|
this.state.players.set(client.sessionId, player)
|
||||||
|
this.room.bUserJoin(`${ client.sessionId }`, { except: client })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,8 @@ export class Player extends Schema {
|
|||||||
rank: number
|
rank: number
|
||||||
|
|
||||||
team: number
|
team: number
|
||||||
|
@type('number')
|
||||||
|
online:number
|
||||||
|
|
||||||
constructor(id: string, accountId: string) {
|
constructor(id: string, accountId: string) {
|
||||||
super()
|
super()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user