]> git.cworth.org Git - empires-server/blobdiff - empires.js
Add dependencies for bcrypt
[empires-server] / empires.js
index b0fc79ea2fb664c52246549f6c720a81b2f93391..593598ad6153c5c524fd2dfeaf7274f07cc36b63 100644 (file)
@@ -1,6 +1,7 @@
 const express = require("express");
 const cors = require("cors");
 const body_parser = require("body-parser");
+const path = require("path");
 
 const app = express();
 app.use(cors());
@@ -79,6 +80,12 @@ class Game {
   }
 
   reveal_next() {
+    /* Don't try to reveal anything if we aren't in the reveal state. */
+    if (this.state != GameState.REVEAL) {
+      clearInterval(this.reveal_interval);
+      return;
+    }
+
     if (this.reveal_index >= this.characters_to_reveal.length) {
       clearInterval(this.reveal_interval);
       this.broadcast_event("character-reveal", '{"character":""}');
@@ -201,12 +208,11 @@ class Game {
   }
 }
 
-const game = new Game();
-
 app.use(body_parser.urlencoded({ extended: false }));
 app.use(body_parser.json());
 
 function handle_events(request, response) {
+  const game = request.game;
   /* These headers will keep the connection open so we can stream events. */
   const headers = {
     "Content-type": "text/event-stream",
@@ -245,63 +251,73 @@ function handle_events(request, response) {
 }
 
 app.get('/', (request, response) => {
-  response.send('Hello World!');
+  response.sendFile(path.join(__dirname, './game.html'));
 });
 
 app.post('/register', (request, response) => {
+  const game = request.game;
   game.add_player(request.body.name, request.body.character);
   response.send();
 });
 
 app.post('/deregister/:id', (request, response) => {
+  const game = request.game;
   game.remove_player(parseInt(request.params.id));
   response.send();
 });
 
 app.post('/reveal', (request, response) => {
+  const game = request.game;
   game.reveal();
   response.send();
 });
 
 app.post('/start', (request, response) => {
+  const game = request.game;
   game.start();
   response.send();
 });
 
 app.post('/reset', (request, response) => {
+  const game = request.game;
   game.reset();
   response.send();
 });
 
 app.post('/capture/:captor/:captee', (request, response) => {
+  const game = request.game;
   game.capture(parseInt(request.params.captor), parseInt(request.params.captee));
   response.send();
 });
 
 app.post('/liberate/:id', (request, response) => {
+  const game = request.game;
   game.liberate(parseInt(request.params.id));
   response.send();
 });
 
 app.post('/restart', (request, response) => {
+  const game = request.game;
   game.restart(parseInt(request.params.id));
     response.send();
 });
 
 app.get('/characters', (request, response) => {
+  const game = request.game;
   response.send(game.characters);
 });
 
 app.get('/empires', (request, response) => {
+  const game = request.game;
   response.send(game.empires);
 });
 
 app.get('/players', (request, response) => {
+  const game = request.game;
   response.send(game.players);
 });
 
 app.get('/events', handle_events);
 
-app.listen(3000, function () {
-  console.log('Empires server listening on localhost:3000');
-});
+exports.app = app;
+exports.Game = Game;