1 /* A single player can have multiple connections, (think, multiple
2 * browser windows with a common session cookie). */
4 constructor(id, session_id, name, connection) {
6 this.session_id = session_id;
8 this.connections = [connection];
12 add_connection(connection) {
13 /* Don't add a duplicate connection if this player already has it. */
14 for (let c of this.connections) {
19 this.connections.push(connection);
22 /* Returns the number of remaining connections after this one is removed. */
23 remove_connection(connection) {
24 this.connections.filter(c => c !== connection);
25 return this.connections.length;
28 /* Send a string to all connections for this player. */
30 this.connections.forEach(connection => connection.write(data));
34 return JSON.stringify({
42 /* Base class providing common code for game engine implementations. */
47 this.next_player_id = 1;
50 /* Send a comment to every connected client every 15 seconds. */
51 setInterval(() => {this.broadcast_string(":");}, 15000);
54 /* Suport for game meta-data.
56 * What we want here is an effectively static field that is
57 * accessible through either the class name (SomeGame.meta) or an
58 * instance (some_game.meta). To pull this off we do keep two copies
59 * of the data. But the game classes can just set SomeGame.meta once
60 * and then reference it either way.
62 static set meta(data) {
63 /* This allows class access (SomeGame.meta) via the get method below. */
66 /* While this allows access via an instance (some_game.meta). */
67 this.prototype.meta = data;
74 add_player(session, connection) {
75 /* First see if we already have a player object for this session. */
76 const existing_index = this.players.findIndex(
77 player => player.session_id === session.id);
78 if (existing_index >= 0) {
79 const player = this.players[existing_index];
80 player.add_connection(connection);
84 /* No existing player. Add a new one. */
85 const id = this.next_player_id;
86 const player = new Player(id, session.id, session.nickname, connection);
88 /* Broadcast before adding player to list (to avoid announcing the
89 * new player to itself). */
90 const player_data = JSON.stringify({ id: player.id, name: player.name });
91 this.broadcast_event("player-enter", player_data);
93 this.players.push(player);
94 this.next_player_id++;
99 find_player(session) {
100 const existing_index = this.players.findIndex(
101 player => player.session_id === session.id);
102 if (existing_index >= 0)
103 return this.players[existing_index];
108 /* Drop a connection object from a player, and if it's the last one,
109 * then drop that player from the game's list of players. */
110 remove_player_connection(player, connection) {
111 const remaining = player.remove_connection(connection);
112 if (remaining === 0) {
113 const player_data = JSON.stringify({ id: player.id });
114 this.players.filter(p => p !== player);
115 this.broadcast_event("player-exit", player_data);
119 /* Send a string to all players */
120 broadcast_string(str) {
121 this.players.forEach(player => player.send(str + '\n'));
124 /* Send an event to all players.
126 * An event has both a declared type and a separate data block.
127 * It also ends with two newlines (to mark the end of the event).
129 broadcast_event(type, data) {
130 this.broadcast_string(`event: ${type}\ndata: ${data}\n`);
133 handle_events(request, response) {
134 /* These headers will keep the connection open so we can stream events. */
136 "Content-type": "text/event-stream",
137 "Connection": "keep-alive",
138 "Cache-Control": "no-cache"
140 response.writeHead(200, headers);
142 /* Add this new player. */
143 const player = this.add_player(request.session, response);
145 /* And queue up cleanup to be triggered on client close. */
146 request.on('close', () => {
147 this.remove_player_connection(player, response);
150 /* Give the client the game-info event. */
151 const game_info_json = JSON.stringify({
153 url: `${request.protocol}://${request.hostname}/${this.id}`
155 response.write(`event: game-info\ndata: ${game_info_json}\n\n`);
157 /* And the player-info event. */
158 response.write(`event: player-info\ndata: ${player.info_json()}\n\n`);
160 /* Finally, if this game class has a "state" property, stream that
161 * current state to the client. */
163 const state_json = JSON.stringify(this.state);
164 response.write(`event: game-state\ndata: ${state_json}\n\n`);
168 handle_player(request, response) {
169 const player = this.find_player(request.session);
170 const name = request.body.name;
171 const team = request.body.team;
174 response.sendStatus(404);
178 if (name && (player.name !== name)) {
181 /* In addition to setting the name within this game's player
182 * object, also set the name in the session. */
183 request.session.nickname = name;
184 request.session.save();
189 if (team !== null && (player.team !== team) &&
190 (team === "" || this.teams.includes(team)))
198 this.broadcast_event("player-update", player.info_json());
203 broadcast_move(move) {
204 this.broadcast_event("move", move);
209 module.exports = Game;