Page 1 of 1
Issue with playerorder in spectator mode
Posted: 26 February 2025, 13:21
by DarkNeerb
Hi all,
I am having an issue with gamedatas.playerorder in my JS' setup function.
In a regular game, playerorder is an array having player_id's for all players in the game in table order. I am using this to sit players in the table in correct order and build the game UI area accordingly.
But in spectator mode, the playerorder array just have one player id regardless of number of players in table. This causes the players to be in incoherent order for an spectator instead of consistenly move next turn to the player on the left of current player.
I already have a workaround for this but wanted to confirm this is an issue in BGA and not in my game. The workaround: Build a custom "tableorder" array in PHP getAllDatasForPlayer()
Has anybody suffered this issue?
Thx, Regards
Re: Issue with playerorder in spectator mode
Posted: 26 February 2025, 14:40
by ufm
From BGA Hearts (thanks to thoun):
Game.php wrote:
protected function getAllDatas() {
$result = [];
$current_player_id = $this->getCurrentPlayerId(); // !! We must only return informations visible by this player !!
// Get information about players
// Note: you can retrieve some extra field you add for "player" table in "dbmodel.sql" if you need it.
$sql = "SELECT player_id id, player_score score, player_no playerNo FROM player";
$result['players'] = $this->getCollectionFromDb($sql);
...
}
hearts.js wrote:
getOrderedPlayers(gamedatas) {
const players = Object.values(gamedatas.players).sort((a, b) => a.playerNo - b.playerNo);
const playerIndex = players.findIndex(player => Number(player.id) === Number(this.player_id));
const orderedPlayers = playerIndex > 0 ? [...players.slice(playerIndex), ...players.slice(0, playerIndex)] : players;
return orderedPlayers;
},
Then you can order this.gamedatas.players with ease by this.getOrderedPlayers(this.gamedatas)
Re: Issue with playerorder in spectator mode
Posted: 26 February 2025, 15:24
by DarkNeerb
Thanks ufm for your response,
From BGA Hearts (thanks to thoun):
Yes, the code you propose looks like what I already have in place and I was refering to when I said:
I already have a workaround for this but wanted to confirm this is an issue in BGA and not in my game. The workaround: Build a custom "tableorder" array in PHP getAllDatasForPlayer()
I was not looking for a fix (I already have one) but for confirmation from somebody else that in spectator mode the playerorder array looks corrupted (Issue happens in all my projects).
It would also be nice to have someone explain the reason for this behaviour if it is expected.
Thx
Re: Issue with playerorder in spectator mode
Posted: 26 February 2025, 16:01
by ufm
DarkNeerb wrote: ↑26 February 2025, 15:24
Thanks ufm for your response,
From BGA Hearts (thanks to thoun):
Yes, the code you propose looks like what I already have in place and I was refering to when I said:
I already have a workaround for this but wanted to confirm this is an issue in BGA and not in my game. The workaround: Build a custom "tableorder" array in PHP getAllDatasForPlayer()
I was not looking for a fix (I already have one) but for confirmation from somebody else that in spectator mode the playerorder array looks corrupted (Issue happens in all my projects).
It would also be nice to have someone explain the reason for this behaviour if it is expected.
Thx
The reason is simple. For spectators, gamedatas.playerorder is empty and it is supposed to work like that.
Re: Issue with playerorder in spectator mode
Posted: 02 July 2025, 11:15
by thoun
ufm wrote: ↑26 February 2025, 16:01
The reason is simple. For spectators, gamedatas.playerorder is empty and it is supposed to work like that.
I couldn't find a reason to explain why it would be supposed to work like that, so I fixed it. The playerorder should now return the id of all payers, using playing order.