]> git.cworth.org Git - lmno-server/blobdiff - tictactoe.js
Add new game.js with a new parent class Game
[lmno-server] / tictactoe.js
index 82c5f7e839d770cb4b4bb33f6f040ae9f079c6aa..5b8729e48568ff114bc3acc9884495ff61081062 100644 (file)
@@ -1,21 +1,13 @@
 const express = require("express");
-const cors = require("cors");
-const body_parser = require("body-parser");
-const path = require("path");
-const nunjucks = require("nunjucks");
-
-const app = express();
-app.use(cors());
-app.use(body_parser.urlencoded({ extended: false }));
-app.use(body_parser.json());
-
-nunjucks.configure("templates", {
-  autoescape: true,
-  express: app
-});
+const Game = require("./game.js");
+
+const engine_name = "tictactoe";
+
+const router = express.Router();
 
-class TicTacToe {
+class TicTacToe extends Game {
   constructor() {
+    super(engine_name);
     this.moves = [];
     this.board = Array(9).fill(null);
     this.clients = [];
@@ -66,7 +58,7 @@ class TicTacToe {
   }
 }
 
-app.get('/', (request, response) => {
+router.get('/', (request, response) => {
   const game = request.game;
 
   if (! request.session.nickname)
@@ -75,7 +67,7 @@ app.get('/', (request, response) => {
     response.render('tictactoe-game.html');
 });
 
-app.post('/move', (request, response) => {
+router.post('/move', (request, response) => {
   const game = request.game;
   const square = request.body.square;
 
@@ -91,7 +83,7 @@ app.post('/move', (request, response) => {
   game.broadcast_move(square);
 });
 
-app.get('/events', (request, response) => {
+router.get('/events', (request, response) => {
   const game = request.game;
 
   /* These headers will keep the connection open so we can stream events. */
@@ -111,6 +103,6 @@ app.get('/events', (request, response) => {
   });
 });
 
-exports.app = app;
-exports.name = "tictactoe";
+exports.router = router;
+exports.name = engine_name;
 exports.Game = TicTacToe;