+ add_player(session, connection) {
+ /* First see if we already have a player object for this session. */
+ const existing_index = this.players.findIndex(
+ player => player.session_id === session.id);
+ if (existing_index >= 0) {
+ const player = this.players[existing_index];
+ player.add_connection(connection);
+ return player;
+ }
+
+ /* No existing player. Add a new one. */
+ const id = this.next_player_id;
+ const player = new Player(id, session.id, session.nickname, connection);
+
+ /* Broadcast before adding player to list (to avoid announcing the
+ * new player to itself). */
+ const player_data = JSON.stringify({ id: player.id, name: player.name });
+ this.broadcast_event("player-enter", player_data);
+
+ this.players.push(player);
+ this.next_player_id++;
+
+ return player;
+ }
+
+ find_player(session) {
+ const existing_index = this.players.findIndex(
+ player => player.session_id === session.id);
+ if (existing_index >= 0)
+ return this.players[existing_index];