aboutsummaryrefslogtreecommitdiff
path: root/src/ur/server.js
diff options
context:
space:
mode:
authorMark Powers <markppowers0@gmail.com>2020-04-07 14:17:24 -0500
committerMark Powers <markppowers0@gmail.com>2020-04-07 14:17:24 -0500
commit1bffb064e2414ced5b1924a4f9fbd822a09c718e (patch)
treef0e4a0a8d9a6f4f58619329bf408fe9bf7d1dd4c /src/ur/server.js
Initial commit
Diffstat (limited to 'src/ur/server.js')
-rw-r--r--src/ur/server.js175
1 files changed, 175 insertions, 0 deletions
diff --git a/src/ur/server.js b/src/ur/server.js
new file mode 100644
index 0000000..81437ee
--- /dev/null
+++ b/src/ur/server.js
@@ -0,0 +1,175 @@
+const path = require('path');
+const fs = require('fs');
+
+function setUpRoutes(server, models, jwtFunctions, database) {
+
+ server.get('/ur', (req, res) => res.sendFile(__dirname + "/index.html"))
+ server.get('/ur/main.js', (req, res) => res.sendFile(__dirname + "/static/main.js"))
+ server.get('/ur/styles.css', (req, res) => res.sendFile(__dirname + "/static/styles.css"))
+ // server.use('/static', express.static(path.join(__dirname, '/static')))
+
+ var games = {}
+ var gameList = []
+ var id = 0
+ server.get('/ur/status', (req, res) => {
+ let cookie = req.cookies.session;
+ if (!cookie) {
+ cookie = uuidv4();
+ res.cookie('session', cookie, { expires: new Date(Date.now() + (1000 * 60 * 60)) });
+ }
+ currentGames = []
+ gameList.forEach( game => {
+ var diff = new Date() - game.time;
+ // Keep games that are recently updated
+ if(diff < 1000 * 10) {// 10 seconds with no update
+ currentGames.push(game)
+ } else { // otherwise don't save it and get rid of game
+ game.players.forEach(c => {
+ delete games[c]
+ });
+ }
+ })
+ gameList = currentGames
+ if (!games[cookie]) {
+ var game = gameList.find((el) => el.waiting)
+ if (game) { // Start game
+ game.players.push(cookie)
+ game.waiting = false
+ game.board = [[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
+ [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
+ [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]]
+ game.turn = Math.floor(Math.random() * 2)
+ games[cookie] = game
+ } else { // Create new game, and wait
+ game = {
+ waiting: true,
+ players: [cookie],
+ id: id++,
+ time: new Date(),
+ }
+ gameList.push(game)
+ games[cookie] = game
+ }
+ } else {
+ games[cookie].time = new Date()
+ }
+ res.status(200).send(games[cookie]);
+ })
+
+ // returns undefined if game in progress, or index of winner
+ function gameOver(game) {
+ // TODO
+ return false;
+
+ for (var player = 0; player < 2; player++) {
+ // 4 in a row in a column
+ for (var col = 0; col < 7; col++) {
+ for (var row = 0; row < 2; row++) {
+ if (game.board[row][col] == player){
+ return player
+ }
+ }
+ }
+ // 4 in a row in a row
+ for (var col = 0; col < 3; col++) {
+ for (var row = 0; row < 6; row++) {
+ if (game.board[col][row] == player &&
+ game.board[col + 1][row] == player &&
+ game.board[col + 2][row] == player &&
+ game.board[col + 3][row] == player) {
+ return player
+ }
+ }
+ }
+ // Check up right diagonal
+ for (var row = 0; row < 2; row++) {
+ for (var col = 0; col < 3; col++) {
+ if (game.board[col][row] == player &&
+ game.board[col + 1][row + 1] == player &&
+ game.board[col + 2][row + 2] == player &&
+ game.board[col + 3][row + 3] == player) {
+ return player
+ }
+ }
+ }
+ // Check down right diagonal
+ for (var row = 3; row < 6; row++) {
+ for (var col = 0; col < 3; col++) {
+ if (game.board[col][row] == player &&
+ game.board[col + 1][row - 1] == player &&
+ game.board[col + 2][row - 2] == player &&
+ game.board[col + 3][row - 3] == player) {
+ return player
+ }
+ }
+ }
+ }
+ return undefined
+ }
+
+ function sendGame(res, game, cookie){
+ var winner = gameOver(game)
+ if (winner != undefined) {
+ res.status(200).send({ game: game, turn: -1, winner: game.players[winner] == cookie });
+ } else {
+ res.status(200).send({ game: game, turn: game.players[game.turn] == cookie });
+ }
+ }
+ server.get('/ur/game/:col/:row', (req, res, next) => {
+ let cookie = req.cookies.session;
+ if (!cookie || !games[cookie]) {
+ next()
+ return
+ }
+ var game = games[cookie]
+ const { col } = req.params;
+ if (game.players[game.turn] == cookie && gameOver(game) == undefined) {
+ // Add player's token to column
+
+ // do nothing
+ if(game.board == undefined){
+
+ } else {
+ // move piece at col, row
+ game.board[col, row]
+ }
+
+ for (var row = 0; row < 6; row++) {
+ if (game.board[col][row] == undefined) {
+ game.board[col][row] = (game.players.indexOf(cookie))
+ game.turn = (game.turn + 1) % 2
+ break
+ }
+ }
+ }
+ sendGame(res, game, cookie)
+ })
+ server.get('/ur/game', (req, res, next) => {
+ let cookie = req.cookies.session;
+ if (!cookie || !games[cookie]) {
+ res.status(200).send({restart: true});
+ return
+ }
+ var game = games[cookie]
+ if(gameOver(game) != undefined){
+ delete games[cookie]
+ }
+ sendGame(res, game, cookie)
+ })
+
+ server.get('/ur/new', (req, res, next) => {
+ let cookie = req.cookies.session;
+ if (!cookie || !games[cookie]) {
+ next()
+ return
+ }
+ games[cookie].players.forEach( el =>{
+ delete games[el]
+ })
+ })
+
+}
+
+module.exports = {
+ setUpRoutes
+};