return JSON.stringify({
id: this.id,
name: this.name,
- team: this.team.name
+ team: this.team.name,
+ score: this.score
});
}
}
/* 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). */
/* 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 => {
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 = {
}
broadcast_move(move) {
- this.broadcast_event("move", move);
+ this.broadcast_event("move", JSON.stringify(move));
}
}