I am using colyseus version 0.14.18. My requirement is after 2 players are joined the game then no other players can join that room while spectators can. How can i achieve this. Please help. I am sharing my code below.
class MyRoom extends colyseus.Room {
maxClinets = 50;
onCreate(options) {
console.log("New Room created!", options);
this.dispatcher = new command.Dispatcher(this);
this.setState(new GameState());
// Broadcast chat messages
this.onMessage('chat', (client, message) => {
this.broadcast('chat', {sessionId: client.sessionId, message: `${this.state.players[client.sessionId].name}: ${message}`});
});
this.dispatcher.dispatch(new BoardCommand.SetupBoard(), {});
}
onJoin(client, options) {
this.dispatcher.dispatch(new PlayerCommand.OnJoin(), {sessionId: client.sessionId, playerData: options});
this.broadcast("chat", {sessionId: client.sessionId, message: `${this.state.players[client.sessionId].name} Joined`});
//if 2 players joined then roll 1 dices for each player for turn
if (this.state.players.size === 2) {
}
}
async onLeave(client, consented) {
// Broadcast leaving to room chat
this.broadcast('chat', {sessionId: client.sessionId, message: `${this.state.players[client.sessionId].name} disconnected`}, {except: client});
console.log(`${this.state.players[client.sessionId].name} disconnected`);
this.dispatcher.dispatch(new PlayerCommand.OnLeave(), {
sessionId: client.sessionId,
consented
});
// Player has lost connection and player is not spectator
if (!consented && !this.state.players.get(client.sessionId).isSpectator) {
try {
await this.allowReconnection(client, 60);
// Regains connection
this.dispatcher.dispatch(new PlayerCommand.OnConnect(), {sessionId: client.sessionId});
this.broadcast('chat', {sessionId: client.sessionId, message: `${this.state.players[client.sessionId].name} has returned`});
console.log(`${this.state.players[client.sessionId].name} has returned!`);
} catch (exc) {
// ...or times out
this.dispatcher.dispatch(new PlayerCommand.OnReconnectTimeout(), {sessionId: client.sessionId});
}
}
}
onDispose() {
console.log("Dispose BackgammonRoom");
this.dispatcher.stop();
}
}
module.exports = {MyRoom};