]> git.cworth.org Git - empires-server/blob - tictactoe.js
Add new game.js with a new parent class Game
[empires-server] / tictactoe.js
1 const express = require("express");
2 const Game = require("./game.js");
3
4 const engine_name = "tictactoe";
5
6 const router = express.Router();
7
8 class TicTacToe extends Game {
9   constructor() {
10     super(engine_name);
11     this.moves = [];
12     this.board = Array(9).fill(null);
13     this.clients = [];
14     this.next_client_id = 1;
15   }
16
17   /* Returns Boolean indicating whether move was legal and added. */
18   add_move(square) {
19     /* Cannot move to an occupied square. */
20     if (this.board[square])
21       return false;
22
23     this.board[square] = 'X';
24     this.moves.push(square);
25
26     return true;
27   }
28
29   add_client(response) {
30     const id = this.next_client_id;
31     this.clients.push({id: id,
32                        response: response});
33     this.next_client_id++;
34
35     return id;
36   }
37
38   remove_client(id) {
39     this.clients = this.clients.filter(client => client.id !== id);
40   }
41
42   /* Send a string to all clients */
43   broadcast_string(str) {
44     this.clients.forEach(client => client.response.write(str + '\n'));
45   }
46
47   /* Send an event to all clients.
48    *
49    * An event has both a declared type and a separate data block.
50    * It also ends with two newlines (to mark the end of the event).
51    */
52   broadcast_event(type, data) {
53     this.broadcast_string(`event: ${type}\ndata: ${data}\n`);
54   }
55
56   broadcast_move(square) {
57     this.broadcast_event("move", square);
58   }
59 }
60
61 router.get('/', (request, response) => {
62   const game = request.game;
63
64   if (! request.session.nickname)
65     response.render('choose-nickname.html', { game_name: "Tic Tac Toe" });
66   else
67     response.render('tictactoe-game.html');
68 });
69
70 router.post('/move', (request, response) => {
71   const game = request.game;
72   const square = request.body.square;
73
74   const legal = game.add_move(square);
75
76   /* Inform this client whether the move was legal. */
77   response.send(JSON.stringify(legal));
78
79   /* And only if legal, inform all clients. */
80   if (! legal)
81     return;
82
83   game.broadcast_move(square);
84 });
85
86 router.get('/events', (request, response) => {
87   const game = request.game;
88
89   /* These headers will keep the connection open so we can stream events. */
90   const headers = {
91     "Content-type": "text/event-stream",
92     "Connection": "keep-alive",
93     "Cache-Control": "no-cache"
94   };
95   response.writeHead(200, headers);
96
97   /* Add this new client to our list of clients. */
98   const id = game.add_client(response);
99
100   /* And queue up cleanup to be triggered on client close. */
101   request.on('close', () => {
102     game.remove_client(id);
103   });
104 });
105
106 exports.router = router;
107 exports.name = engine_name;
108 exports.Game = TicTacToe;