From 0149bfd84c21bd3c53e6e0e4d4eee96c50b3f232 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 5 Jun 2020 17:07:10 -0700 Subject: [PATCH] Assign new players to the first team with no players But only if the game has already started, (since with an unstarted game, all players are intentionally unassigned so that any player can go first). If there is no team that has no players, the new player will be unassigned, (and will instead be a spectator until they join a team). This logic is designed so that a game in which the precise number of people were invited to play, (most notabl, a two-player game), these players will be automatically assigned to teams as soon as one player makes a move, (regardless of whether the players joined the game before or after that move). --- game.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/game.js b/game.js index b43a75c..0946568 100644 --- a/game.js +++ b/game.js @@ -169,6 +169,19 @@ class Game { this.players_by_session[session.id] = player; this.next_player_id++; + /* After adding the player to the list, and if we are already past + * the first move, assign this player to the first team that + * doesn't already have a player aissgned (if any). */ + if (! this.first_move) { + const have_players = Array(this.teams.length).fill(false); + this.players.forEach(p => { + if (p.team.id !== undefined) + have_players[p.team.id] = true; + }); + const first_empty = have_players.findIndex(i => i === false); + this.assign_player_to_team_perhaps(player, this.teams[first_empty]); + } + return player; } -- 2.43.0