]> git.cworth.org Git - empires-server/commitdiff
empires: Rename GameState to GamePhase
authorCarl Worth <cworth@cworth.org>
Mon, 1 Jun 2020 05:45:10 +0000 (22:45 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 1 Jun 2020 05:45:10 +0000 (22:45 -0700)
In order to free up "state" as a more generic term for capturing the
totality of the state needed for a game.

empires.js
templates/empires-game.html

index 5053b61581ec53ec6d308dabf4319c9dfdf644ec..8d57543817e659a515922134915c8bd65d877677 100644 (file)
@@ -3,7 +3,7 @@ const Game = require("./game.js");
 
 const router = express.Router();
 
-const GameState = {
+const GamePhase = {
   JOIN:    1,
   REVEAL:  2,
   CAPTURE: 3,
@@ -39,7 +39,7 @@ class Empires extends Game {
     this._players = [];
     this.next_player_id = 1;
     this.characters_to_reveal = null;
-    this.state = GameState.JOIN;
+    this.phase = GamePhase.JOIN;
 
     /* Send a comment to every connected client every 15 seconds. */
     setInterval(() => {this.broadcast_string(":");}, 15000);
@@ -99,15 +99,15 @@ class Empires extends Game {
     this.characters_to_reveal = null;
     this.next_player_id = 1;
 
-    this.change_state(GameState.JOIN);
+    this.change_phase(GamePhase.JOIN);
 
     this.broadcast_event("spectators", "{}");
     this.broadcast_event("players", "{}");
   }
 
   reveal_next() {
-    /* Don't try to reveal anything if we aren't in the reveal state. */
-    if (this.state != GameState.REVEAL) {
+    /* Don't try to reveal anything if we aren't in the reveal phase. */
+    if (this.phase != GamePhase.REVEAL) {
       clearInterval(this.reveal_interval);
       return;
     }
@@ -124,7 +124,7 @@ class Empires extends Game {
   }
 
   reveal() {
-    this.change_state(GameState.REVEAL);
+    this.change_phase(GamePhase.REVEAL);
 
     if (this.characters_to_reveal === null) {
       this.characters_to_reveal = [];
@@ -141,7 +141,7 @@ class Empires extends Game {
   }
 
   start() {
-    this.change_state(GameState.CAPTURE);
+    this.change_phase(GamePhase.CAPTURE);
   }
 
   capture(captor_id, captee_id) {
@@ -185,33 +185,33 @@ class Empires extends Game {
     return this._players.map(player => ({id: player.id, name: player.name }));
   }
 
-  game_state_event_data(old_state, new_state) {
-    var old_state_name;
-    if (old_state)
-      old_state_name = GameState.properties[old_state].name;
+  game_phase_event_data(old_phase, new_phase) {
+    var old_phase_name;
+    if (old_phase)
+      old_phase_name = GamePhase.properties[old_phase].name;
     else
-      old_state_name = "none";
-    const new_state_name = GameState.properties[new_state].name;
+      old_phase_name = "none";
+    const new_phase_name = GamePhase.properties[new_phase].name;
 
-    return `{"old_state":"${old_state_name}","new_state":"${new_state_name}"}`;
+    return `{"old_phase":"${old_phase_name}","new_phase":"${new_phase_name}"}`;
   }
 
-  /* Inform clients about a state change. */
-  broadcast_state_change() {
-    const event_data = this.game_state_event_data(this.old_state, this.state);
-    this.broadcast_event("game-state", event_data);
+  /* Inform clients about a phase change. */
+  broadcast_phase_change() {
+    const event_data = this.game_phase_event_data(this.old_phase, this.phase);
+    this.broadcast_event("game-phase", event_data);
   }
 
-  /* Change game state and broadcast the change to all clients. */
-  change_state(state) {
+  /* Change game phase and broadcast the change to all clients. */
+  change_phase(phase) {
     /* Do nothing if there is no actual change happening. */
-    if (state === this.state)
+    if (phase === this.phase)
       return;
 
-    this.old_state = this.state;
-    this.state = state;
+    this.old_phase = this.phase;
+    this.phase = phase;
 
-    this.broadcast_state_change();
+    this.broadcast_phase_change();
   }
 
   handle_events(request, response) {
@@ -232,16 +232,16 @@ class Empires extends Game {
       response.write(players_data);
     }
 
-    /* And we need to inform the client of the current game state.
+    /* And we need to inform the client of the current game phase.
      *
-     * In fact, we need to cycle through each state transition from the
+     * In fact, we need to cycle through each phase transition from the
      * beginning so the client can see each.
      */
-    var old_state = null;
-    for (var state = GameState.JOIN; state <= this.state; state++) {
-      var event_data = this.game_state_event_data(old_state, state);
-      response.write("event: game-state\n" + "data: " + event_data + "\n\n");
-      old_state = state;
+    var old_phase = null;
+    for (var phase = GamePhase.JOIN; phase <= this.phase; phase++) {
+      var event_data = this.game_phase_event_data(old_phase, phase);
+      response.write("event: game-phase\n" + "data: " + event_data + "\n\n");
+      old_phase = phase;
     }
   }
 
index 9c860ca60d9d303b8baaf8addb5b8d17ec915212..3eb63385894abe2ac5495eed16410da41f285605 100644 (file)
 
 <h1>The Game of Empires</h1>
 
-<div id="loading" class="show-state-none">
+<div id="loading" class="show-phase-none">
   <p>
     Contacting server. Please wait...
   </p>
 </div>
 
-<div id="pre-game" class="show-state-join">
+<div id="pre-game" class="show-phase-join">
 
   <h2>Choose your character</h2>
 
 
 </div>
 
-<div class="show-state-reveal">
+<div class="show-phase-reveal">
   <h1>Watch and memorize each character!</h1>
 
   <span id="character-reveal"></span>
 </div>
 
-<div class="hide-state-reveal" id="spectators-div">
+<div class="hide-phase-reveal" id="spectators-div">
   <h1>Spectators</h1>
 
   <ul id="spectators">
   </ul>
 </div>
 
-<div class="hide-state-reveal" id="players-div">
+<div class="hide-phase-reveal" id="players-div">
   <h1>Players</h1>
 
   <ul id="players">