X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=game.js;h=86a0e70b96f311dc2133898e679c1afa619406a7;hb=fa3bdc8a5207fc93f4d720771cb6155913f06262;hp=0946568e9bbd8adc5c99d82832261fa1ac254d5d;hpb=0149bfd84c21bd3c53e6e0e4d4eee96c50b3f232;p=lmno-server diff --git a/game.js b/game.js index 0946568..86a0e70 100644 --- a/game.js +++ b/game.js @@ -36,7 +36,8 @@ class Player { return JSON.stringify({ id: this.id, name: this.name, - team: this.team.name + team: this.team.name, + score: this.score }); } } @@ -158,7 +159,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). */ @@ -171,7 +185,7 @@ class Game { /* 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). */ + * doesn't already have a player assigned (if any). */ if (! this.first_move) { const have_players = Array(this.teams.length).fill(false); this.players.forEach(p => { @@ -211,6 +225,10 @@ class Game { this.broadcast_string(`event: ${type}\ndata: ${data}\n`); } + broadcast_event_object(type, obj) { + this.broadcast_event(type, JSON.stringify(obj)); + } + handle_events(request, response) { /* These headers will keep the connection open so we can stream events. */ const headers = { @@ -293,7 +311,7 @@ class Game { } broadcast_move(move) { - this.broadcast_event("move", move); + this.broadcast_event("move", JSON.stringify(move)); } }