]> git.cworth.org Git - empires-server/blobdiff - game.js
Assign new players to the first team with no players
[empires-server] / 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;
   }