]> git.cworth.org Git - empires-server/commitdiff
Assign new players to the first team with no players
authorCarl Worth <cworth@cworth.org>
Sat, 6 Jun 2020 00:07:10 +0000 (17:07 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 6 Jun 2020 00:07:10 +0000 (17:07 -0700)
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

diff --git a/game.js b/game.js
index b43a75c42a36fd69212b75a29aa9f140ea153d1f..0946568e9bbd8adc5c99d82832261fa1ac254d5d 100644 (file)
--- 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;
   }