X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=game.js;h=f9e18cb760725eb9d05a993115bf286f5f0b2e85;hb=3740e6f5ca87eac244bc2969b19a37939e9efc28;hp=5386cd212d902b7fb5e2266b26a4c8690ef7b3d2;hpb=64a0af1235ca767c27e7cbd1f5416c359f212278;p=empires-server diff --git a/game.js b/game.js index 5386cd2..f9e18cb 100644 --- a/game.js +++ b/game.js @@ -162,10 +162,19 @@ class Game { } add_player(session, connection) { + let nickname = session.nickname; + /* First see if we already have a player object for this session. */ - const existing = this.players_by_session[session.id]; + let existing = this.players_by_session[session.id]; + + /* Otherwise, see if we can match an inactive player by name. */ + if (! existing) { + existing = this.players.find(player => + player.name == nickname && + ! player.active); + } + if (existing) { - existing.add_connection(connection); if (! existing.active) { /* If we're re-activating a previously idled player, then we * need to alert everyone that this player is now back. @@ -174,12 +183,12 @@ class Game { this.active_players++; this.broadcast_event("player-enter", existing.info_json()); } + existing.add_connection(connection); return existing; } /* No existing player. Add a new one. */ const id = this.next_player_id; - let nickname = session.nickname; if (nickname === "") nickname = "Guest"; const nickname_orig = nickname;