]> git.cworth.org Git - lmno-server/blobdiff - game.js
game/tictactoe: Expand player to include a team property
[lmno-server] / game.js
diff --git a/game.js b/game.js
index baf1e557df502cc28f775f3540fa5f14d55102c8..de77148aa3ed58888930d4b872da8e0c92a28644 100644 (file)
--- a/game.js
+++ b/game.js
@@ -6,6 +6,7 @@ class Player {
     this.session_id = session_id;
     this.name = name;
     this.connections = [connection];
+    this.team = "";
   }
 
   add_connection(connection) {
@@ -33,6 +34,7 @@ class Player {
     return JSON.stringify({
       id: this.id,
       name: this.name,
+      team: this.team
     });
   }
 }
@@ -43,6 +45,7 @@ class Game {
     this.id = id;
     this.players = [];
     this.next_player_id = 1;
+    this.teams = [];
 
     /* Send a comment to every connected client every 15 seconds. */
     setInterval(() => {this.broadcast_string(":");}, 15000);
@@ -151,6 +154,9 @@ class Game {
     });
     response.write(`event: game-info\ndata: ${game_info_json}\n\n`);
 
+    /* And the player-info event. */
+    response.write(`event: player-info\ndata: ${player.info_json()}\n\n`);
+
     /* Finally, if this game class has a "state" property, stream that
      * current state to the client. */
     if (this.state) {
@@ -159,6 +165,41 @@ class Game {
     }
   }
 
+  handle_player(request, response) {
+    const player = this.find_player(request.session);
+    const name = request.body.name;
+    const team = request.body.team;
+    var updated = false;
+    if (! player) {
+      response.sendStatus(404);
+      return;
+    }
+
+    if (name && (player.name !== name)) {
+      player.name = name;
+
+      /* In addition to setting the name within this game's player
+       * object, also set the name in the session. */
+      request.session.nickname = name;
+      request.session.save();
+
+      updated = true;
+    }
+
+    if (team !== null && (player.team !== team) &&
+        (team === "" || this.teams.includes(team)))
+    {
+      player.team = team;
+
+      updated = true;
+    }
+
+    if (updated)
+      this.broadcast_event("player-update", player.info_json());
+
+    response.send("");
+  }
+
   broadcast_move(move) {
     this.broadcast_event("move", move);
   }