Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
y0unghe opened this issue Nov 3, 2023 · 0 comments

Comments

@y0unghe
Copy link

y0unghe commented Nov 3, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant