Skip to content

Trying to reset the child elements' state of ArraySchema, client-side sync callbacks are not triggered #132

Open
@y0unghe

Description

@y0unghe

When a round of the game ends, we need to reset the player's state, so we created a ResetStateCommand to reset the player's state before a new round of the game starts.

Below is the player onChange listener to set players when the player's state changes. But when the below server executes the reset command, player.onChange won't pick up the changes. But when I looked into the colyseus monitor, the player's state did change.

room.state.players.onAdd((player) => {
    player.onChange(() => {
      setPlayers(new Map(players.set(player.index, player)));
    });
  });

Below are the server codes to execute the player's state change.

export class RoomState extends Schema {
    @type({ map: PlayerState })
    players = new MapSchema<PlayerState>();
}
export class ResetStateCommand extends Command<PokerRoom, {}> {
    async execute() {
        await new Promise(resolve => {
            this.state.users.forEach((user) => {
                if (user.sit) {
                    user.isInRound = user.isInHand = user.chips > 0;
                    user.isAllIn = false;
                    user.isBettor = false;
                    user.lastAction = null;
                    user.playerHands = new ArraySchema();
                    user.hasOption = false;
                    user.isFold = false;
                    user.currentBet = 0;
                    user.isWinner = false;
                }
            });
            resolve(true);
        })
    }
}

Did I do something wrong? What's the best practice to prevent something like this to happen? What's the best practice to make the client-side sync callbacks trigger when the server executes the state changes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions