Create New Room according to specified condition(solved)

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

Hi Zack!
Currently there's no concepts like "Player" or "Spectators" in Colyseus, but "Clients".
So if (this.state.players.size === 2), your custom player lock should be true and if that happens,
spectators are still allowed to join in the room onAuth, but not Players.
Use maxClients to control max number of players + spectators.
This is my personal opinion.

Hi COCO,
Thanks for your reply, after 2 players are joined the room it will be available in pool so when a 3rd player try to join the room this room will be available to join. My requirement is when 2 players are joined then room should not be available in the pool and when 3rd player try to join the room a new room should be created. I have tried to lock the room after 2 players are joined so it will be removed from the pool which is fine but when i am trying to join this room as spectator from my lobbyroom using matchmaker api i got error message "The room is locked". Can you please help how can i handle this scenario. If you share some code it will be better for me to understand.

Thanks.

hi @zack1991 , my recommendation: since you can have the same players connected in more than one room at the time, then you can make 2 rooms:
1 - for the two players that will handle the entire game-play
2 - for everyone (spectators and players), then you can make your players broadcast to the second room their "movements" and all the information you want to make "public", that way you can have all spectators getting those updates.

Does this make sense for your flow?
Unfortunatelly I don't have any code examples for that specific case, though I have examples of users connected to more than one room, but that shouldn't be the issue for you.
Also you can check the colyseus showcase and look into other projects, or in the colyseus examples.

Best,
Damian