1 const express = require("express");
2 const cors = require("cors");
3 const body_parser = require("body-parser");
11 this.next_player_id = 1;
13 this.next_client_id = 1;
15 /* Send a comment to every connected client every 15 seconds. */
16 setInterval(() => {this.broadcast_string(":");}, 15000);
19 add_player(name, character) {
20 const new_player = {id: this.next_player_id,
25 this._players.push(new_player);
26 this.next_player_id++;
27 /* The syntax here is using an anonymous function to create a new
28 object from new_player with just the subset of fields that we
30 const player_data = JSON.stringify((({id, name}) => ({id, name}))(new_player));
31 this.broadcast_event("player-join", player_data);
35 const index = this._players.findIndex(player => player.id === id);
36 this._players.splice(index, 1);
38 this.broadcast_event("player-leave", `{"id": ${id}}`);
41 remove_all_players() {
43 this.next_player_id = 1;
46 capture(captor_id, captee_id) {
47 /* TODO: Fix to fail on already-captured players (or to move the
48 * captured player from an old captor to a new—need to clarify in
49 * the API specification which we want here. */
50 let captor = this._players.find(player => player.id === captor_id);
51 captor.captures.push(captee_id);
53 this.broadcast_event("capture", `{"captor": ${captor_id}, "captee": ${captee_id}}`);
57 let captor = this._players.find(player => player.captures.includes(captee_id));
58 captor.captures.splice(captor.captures.indexOf(captee_id), 1);
62 for (const player of this._players) {
68 return this._players.map(player => player.character);
72 return this._players.map(player => ({id: player.id, captures: player.captures}));
76 return this._players.map(player => ({id: player.id, name: player.name }));
79 add_client(response) {
80 const id = this.next_client_id;
81 this.clients.push({id: id,
83 this.next_client_id++;
89 this.clients = this.clients.filter(client => client.id !== id);
92 /* Send a string to all clients */
93 broadcast_string(str) {
94 this.clients.forEach(client => client.response.write(str + '\n'));
97 /* Send an event to all clients.
99 * An event has both a declared type and a separate data block.
100 * It also ends with two newlines (to mark the end of the event).
102 broadcast_event(type, data) {
103 this.broadcast_string(`event: ${type}\ndata: ${data}\n`);
107 const game = new Game();
109 app.use(body_parser.urlencoded({ extended: false }));
110 app.use(body_parser.json());
112 function handle_events(request, response) {
113 /* These headers will keep the connection open so we can stream events. */
115 "Content-type": "text/event-stream",
116 "Connection": "keep-alive",
117 "Cache-Control": "no-cache"
119 response.writeHead(200, headers);
121 /* Now that a client has connected, first we need to stream all of
122 * the existing players (if any). */
123 if (game._players.length > 0) {
124 const players_json = JSON.stringify(game.players);
125 const players_data = `event: players\ndata: ${players_json}\n\n`;
126 response.write(players_data);
129 /* Add this new client to our list of clients. */
130 const id = game.add_client(response);
132 /* And queue up cleanup to be triggered on client close. */
133 request.on('close', () => {
134 game.remove_client(id);
138 app.get('/', (request, response) => {
139 response.send('Hello World!');
142 app.post('/register', (request, response) => {
143 game.add_player(request.body.name, request.body.character);
147 app.post('/deregister/:id', (request, response) => {
148 game.remove_player(parseInt(request.params.id));
152 app.post('/reset', (request, response) => {
153 game.remove_all_players();
157 app.post('/capture/:captor/:captee', (request, response) => {
158 game.capture(parseInt(request.params.captor), parseInt(request.params.captee));
162 app.post('/liberate/:id', (request, response) => {
163 game.liberate(parseInt(request.params.id));
167 app.post('/restart', (request, response) => {
168 game.restart(parseInt(request.params.id));
172 app.get('/characters', (request, response) => {
173 response.send(game.characters);
176 app.get('/empires', (request, response) => {
177 response.send(game.empires);
180 app.get('/players', (request, response) => {
181 response.send(game.players);
184 app.get('/events', handle_events);
186 app.listen(3000, function () {
187 console.log('Empires server listening on localhost:3000');