1 const express = require("express");
2 const Game = require("./game.js");
16 * Shuffles array in place.
17 * @param {Array} a items An array containing the items.
24 for (i = a.length - 1; i > 0; i--) {
25 j = Math.floor(Math.random() * (i + 1));
32 class Empires extends Game {
35 this._spectators = [];
36 this.next_spectator_id = 1;
38 this.next_player_id = 1;
39 this.characters_to_reveal = null;
40 this.phase = GamePhase.JOIN;
42 /* Send a comment to every connected client every 15 seconds. */
43 setInterval(() => {this.broadcast_string(":");}, 15000);
46 add_spectator(name, session_id) {
47 /* Don't add another spectator that matches an existing session. */
48 const existing = this._spectators.findIndex(
49 spectator => spectator.session_id === session_id);
53 const new_spectator = {id: this.next_spectator_id,
55 session_id: session_id
57 this._spectators.push(new_spectator);
58 this.next_spectator_id++;
59 this.broadcast_event("spectator-join", JSON.stringify(new_spectator));
61 return new_spectator.id;
64 remove_spectator(id) {
65 const index = this._spectators.findIndex(spectator => spectator.id === id);
66 this._spectators.splice(index, 1);
68 this.broadcast_event("spectator-leave", `{"id": ${id}}`);
71 add_player(name, character) {
72 const new_player = {id: this.next_player_id,
77 this._players.push(new_player);
78 this.next_player_id++;
79 /* The syntax here is using an anonymous function to create a new
80 object from new_player with just the subset of fields that we
82 const player_data = JSON.stringify((({id, name}) => ({id, name}))(new_player));
83 this.broadcast_event("player-join", player_data);
89 const index = this._players.findIndex(player => player.id === id);
90 this._players.splice(index, 1);
92 this.broadcast_event("player-leave", `{"id": ${id}}`);
97 this.characters_to_reveal = null;
98 this.next_player_id = 1;
100 this.change_phase(GamePhase.JOIN);
102 this.broadcast_event("spectators", "{}");
103 this.broadcast_event("players", "{}");
107 /* Don't try to reveal anything if we aren't in the reveal phase. */
108 if (this.phase != GamePhase.REVEAL) {
109 clearInterval(this.reveal_interval);
113 if (this.reveal_index >= this.characters_to_reveal.length) {
114 clearInterval(this.reveal_interval);
115 this.broadcast_event("character-reveal", '{"character":""}');
118 const character = this.characters_to_reveal[this.reveal_index];
120 const character_data = JSON.stringify({"character":character});
121 this.broadcast_event("character-reveal", character_data);
125 this.change_phase(GamePhase.REVEAL);
127 if (this.characters_to_reveal === null) {
128 this.characters_to_reveal = [];
129 this.characters_to_reveal = this._players.reduce((characters, player) => {
130 characters.push(player.character);
133 shuffle(this.characters_to_reveal);
136 this.reveal_index = 0;
138 this.reveal_interval = setInterval(this.reveal_next.bind(this), 3000);
142 this.change_phase(GamePhase.CAPTURE);
145 capture(captor_id, captee_id) {
146 /* TODO: Fix to fail on already-captured players (or to move the
147 * captured player from an old captor to a new—need to clarify in
148 * the API specification which we want here. */
149 let captor = this._players.find(player => player.id === captor_id);
150 captor.captures.push(captee_id);
152 this.broadcast_event("capture", `{"captor": ${captor_id}, "captee": ${captee_id}}`);
155 liberate(captee_id) {
156 let captor = this._players.find(player => player.captures.includes(captee_id));
157 captor.captures.splice(captor.captures.indexOf(captee_id), 1);
161 for (const player of this._players) {
162 player.captures = [];
167 return this._players.map(player => player.character);
171 return this._players.map(player => ({id: player.id, captures: player.captures}));
175 /* We return only "id" and "name" here (specifically not session_id!). */
176 return this._spectators.map(spectator => ({
183 return this._players.map(player => ({id: player.id, name: player.name }));
186 game_phase_event_data(old_phase, new_phase) {
189 old_phase_name = GamePhase.properties[old_phase].name;
191 old_phase_name = "none";
192 const new_phase_name = GamePhase.properties[new_phase].name;
194 return `{"old_phase":"${old_phase_name}","new_phase":"${new_phase_name}"}`;
197 /* Inform clients about a phase change. */
198 broadcast_phase_change() {
199 const event_data = this.game_phase_event_data(this.old_phase, this.phase);
200 this.broadcast_event("game-phase", event_data);
203 /* Change game phase and broadcast the change to all clients. */
204 change_phase(phase) {
205 /* Do nothing if there is no actual change happening. */
206 if (phase === this.phase)
209 this.old_phase = this.phase;
212 this.broadcast_phase_change();
215 handle_events(request, response) {
217 super.handle_events(request, response);
219 /* Now that a client has connected, first we need to stream all of
220 * the existing spectators and players (if any). */
221 if (this._spectators.length > 0) {
222 const spectators_json = JSON.stringify(this.spectators);
223 const spectators_data = `event: spectators\ndata: ${spectators_json}\n\n`;
224 response.write(spectators_data);
227 if (this._players.length > 0) {
228 const players_json = JSON.stringify(this.players);
229 const players_data = `event: players\ndata: ${players_json}\n\n`;
230 response.write(players_data);
233 /* And we need to inform the client of the current game phase.
235 * In fact, we need to cycle through each phase transition from the
236 * beginning so the client can see each.
238 var old_phase = null;
239 for (var phase = GamePhase.JOIN; phase <= this.phase; phase++) {
240 var event_data = this.game_phase_event_data(old_phase, phase);
241 response.write("event: game-phase\n" + "data: " + event_data + "\n\n");
248 Empires.router = express.Router();
249 const router = Empires.router;
251 router.post('/spectator', (request, response) => {
252 const game = request.game;
253 var name = request.session.nickname;
255 /* If the request includes a name, that overrides the session nickname. */
256 if (request.body.name)
257 name = request.body.name;
259 const id = game.add_spectator(name, request.session.id);
260 response.send(JSON.stringify(id));
263 router.delete('/spectator/:id', (request, response) => {
264 const game = request.game;
265 game.remove_spectator(parseInt(request.params.id));
269 router.post('/register', (request, response) => {
270 const game = request.game;
271 var name = request.session.nickname;;
273 /* If the request includes a name, that overrides the session nickname. */
274 if (request.body.name)
275 name = request.body.name;
277 const player = game.add_player(name, request.body.character);
278 response.send(JSON.stringify(player.id));
281 router.post('/deregister/:id', (request, response) => {
282 const game = request.game;
283 game.remove_player(parseInt(request.params.id));
287 router.post('/reveal', (request, response) => {
288 const game = request.game;
293 router.post('/start', (request, response) => {
294 const game = request.game;
299 router.post('/reset', (request, response) => {
300 const game = request.game;
305 router.post('/capture/:captor/:captee', (request, response) => {
306 const game = request.game;
307 game.capture(parseInt(request.params.captor), parseInt(request.params.captee));
311 router.post('/liberate/:id', (request, response) => {
312 const game = request.game;
313 game.liberate(parseInt(request.params.id));
317 router.post('/restart', (request, response) => {
318 const game = request.game;
319 game.restart(parseInt(request.params.id));
323 router.get('/characters', (request, response) => {
324 const game = request.game;
325 response.send(game.characters);
328 router.get('/empires', (request, response) => {
329 const game = request.game;
330 response.send(game.empires);
333 router.get('/spectators', (request, response) => {
334 const game = request.game;
335 response.send(game.spectators);
338 router.get('/players', (request, response) => {
339 const game = request.game;
340 response.send(game.players);
345 identifier: "empires"
348 exports.Game = Empires;