]> git.cworth.org Git - empires-server/blobdiff - game.js
Scribe: Small tweaks to the page layout
[empires-server] / game.js
diff --git a/game.js b/game.js
index b43a75c42a36fd69212b75a29aa9f140ea153d1f..eb78f31ecbe531a38801cbbb9b4a466eb968d8cc 100644 (file)
--- a/game.js
+++ b/game.js
@@ -158,7 +158,20 @@ class Game {
 
     /* No existing player. Add a new one. */
     const id = this.next_player_id;
-    const player = new Player(id, session.id, session.nickname, connection);
+    let nickname = session.nickname;
+    if (nickname === "")
+      nickname = "Guest";
+    const nickname_orig = nickname;
+
+    /* Ensure we don't have a name collision with a previous player. */
+    let unique_suffix = 1;
+    while (this.players.find(player => player.name === nickname))
+    {
+      nickname = `${nickname_orig}${unique_suffix.toString().padStart(2, '0')}`;
+      unique_suffix++;
+    }
+
+    const player = new Player(id, session.id, nickname, connection);
 
     /* Broadcast before adding player to list (to avoid announcing the
      * new player to itself). */
@@ -169,6 +182,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;
   }
 
@@ -280,7 +306,7 @@ class Game {
   }
 
   broadcast_move(move) {
-    this.broadcast_event("move", move);
+    this.broadcast_event("move", JSON.stringify(move));
   }
 
 }