Page 1 of 1

Show current player's board at the top

Posted: 10 August 2020, 15:45
by Kellbot
I'm working on a game where each player has their own board. Does anyone have an example handy of how to set up the view so that each player sees their own board at the top? Games like Potion Explosion and 7 Wonders do this.

I'm not sure if there's a way to do this in view.php, or if I should just use javascript to bring the appropriate board into place after the game loads. Doing it on the PHP side seems preferable. Right now my approach is to pull out the current player id from the player list array, set up their board, then loop through the remaining players after the current player is set up.

I've put together a crude illustration of the layout I'm going for, where the top div contains the player area consisting of their personal board (blue rectangle) and shared board (purple rectangle, same for everyone), and the bottom div has smaller versions of each of the opponents' player boards.

Image

Re: Show current player's board at the top

Posted: 10 August 2020, 19:59
by hersh
I follow the same approach. In view.php I get the current player id and loop through rest. Not sure there is a better approach.

Re: Show current player's board at the top

Posted: 10 August 2020, 21:06
by tchobello
Did you have a look at Hearts tutorial ?

Re: Show current player's board at the top

Posted: 10 August 2020, 23:43
by Tisaac
I usually do all this stuff in js, leaving the view.php almost empty.

Code: Select all

  var currentPlayerNo = gamedatas.bplayers.reduce((carry, player) => (player.id == this.player_id)? player.no : carry, 0);
  gamedatas.bplayers.forEach( player => {
    let isCurrent = player.id == this.player_id;
    player.no = (player.no + nPlayers - currentPlayerNo) % nPlayers;
    dojo.place(this.format_block('jstpl_player', player), 'board');

    if(isCurrent){
      ...
    }
  });
Notice here I am re-indexing the "no" field to display the board in a circle with the current player on top (as on 7Wonders), but if the player's order is not relevant in your game, you can just remove this re-indexing.
(I am also using a custom bplayers field instead of players since this one is sent as an object to the js side... (so basically $bplayers = array_values($this->players))

Re: Show current player's board at the top

Posted: 11 August 2020, 05:10
by Marcuda
I went with php as well. My function is actually in game.php but I realize now it doesn't need to be. One thing I ran into is I had to account for spectators.

Code: Select all

// game.php
function getPlayersInOrder() {
    $result = array();

    $players = self::loadPlayersBasicInfos();
    $next_player = self::getNextPlayerTable();
    $player_id = self::getCurrentPlayerId();

    // Check for spectator
    if (!key_exists($player_id, $players)) {
        $player_id = $next_player[0];
    }

    // Build array starting with current player
    for ($i=0; $i<count($players); $i++) {
        $result[] = $player_id;
        $player_id = $next_player[$player_id];
    }

    return $result;
}

// view.php
$players = $this->game->getPlayersInOrder();
foreach ($players as $player_id) { ... }