]> git.cworth.org Git - empires-server/blobdiff - game.js
game: Store players in both an array _and_ a session-indexed object
[empires-server] / game.js
diff --git a/game.js b/game.js
index b2193363b92dd1d1e7ac2227ce43456111200eee..72c3c7f16fd1c8fd0b24b96935ad588228cdfbf0 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
     });
   }
 }
@@ -42,7 +44,12 @@ class Game {
   constructor(id) {
     this.id = id;
     this.players = [];
+    this.players_by_session = {};
     this.next_player_id = 1;
+    this.teams = [];
+    this.state = {
+      team_to_play: ""
+    };
 
     /* Send a comment to every connected client every 15 seconds. */
     setInterval(() => {this.broadcast_string(":");}, 15000);
@@ -68,14 +75,34 @@ class Game {
     return this._meta;
   }
 
+  /* Just performs some checks for whether a move is definitely not
+   * legal (such as not the player's turn). A child class is expected
+   * to override this (and call super.add_move early!) to implement
+   * the actual logic for a move. */
+  add_move(player, move) {
+    /* Cannot move if you are not on a team. */
+    if (player.team === "")
+    {
+      return { legal: false,
+               message: "You must be on a team to take a turn" };
+    }
+
+    /* Cannot move if it's not this player's team's turn. */
+    if (player.team !== this.state.team_to_play)
+    {
+      return { legal: false,
+               message: "It's not your turn to move" };
+    }
+
+    return { legal: true };
+  }
+
   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;
+    const existing = this.players_by_session[session.id];
+    if (existing) {
+      existing.add_connection(connection);
+      return existing;
     }
 
     /* No existing player. Add a new one. */
@@ -88,20 +115,12 @@ class Game {
     this.broadcast_event("player-enter", player_data);
 
     this.players.push(player);
+    this.players_by_session[session.id] = 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];
-
-    return null;
-  }
-
   /* Drop a connection object from a player, and if it's the last one,
    * then drop that player from the game's list of players. */
   remove_player_connection(player, connection) {
@@ -109,6 +128,7 @@ class Game {
     if (remaining === 0) {
       const player_data = JSON.stringify({ id: player.id });
       this.players.filter(p => p !== player);
+      delete this.players_by_session[player.session_id];
       this.broadcast_event("player-exit", player_data);
     }
   }
@@ -162,6 +182,41 @@ class Game {
     }
   }
 
+  handle_player(request, response) {
+    const player = this.players_by_session[request.session.id];
+    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);
   }