This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
173 lines (137 loc) · 3.68 KB
/
index.js
File metadata and controls
173 lines (137 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
*
* Connect Four as as Service (CFaaS)
*
*/
const express = require('express');
const bodyParser = require('body-parser');
const Long = require('big-integer');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const Game = require('./lib/models/game.js');
const gameView = require('./lib/views/game.js');
const Player = require('./lib/models/player.js');
const playerView = require('./lib/views/player.js');
const boardUtil = require('./lib/boardUtil.js');
// middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// data storage
const DB = {};
// helpers
const allGames = function() {
var games = [];
var now = new Date().getTime();
for (var game in DB) {
games.push(DB[game]);
}
games = games.sort(function(a, b) {
return a.created > b.created ? -1 : 1;
});
return games.filter(function(game) {
return game.isActive() ||
(now - game.created) < (60*1000) * 2
}).map(function(game) {
return gameView(game);
});
};
const notFound = function(res) {
res.status(404).send({ error: 'not found' });
};
const playRound = function(game, col) {
if (!game.makePlay(col)) {
game.finish(game.otherPlayer());
io.emit('games', allGames());
return false;
}
if (boardUtil.isWon(game.currentPlayer.board)) {
game.finish(game.currentPlayer);
io.emit('games', allGames());
return false;
}
if (boardUtil.isFull(game.board)) {
game.finish();
io.emit('games', allGames());
return false;
}
return true;
}
// routes
app.get('/games', function(req, res) {
res.send(allGames());
});
app.get('/games/:id', function(req, res) {
var game = DB[req.params.id];
if (!game) {
return notFound(res);
}
return res.send(gameView(game));
});
app.post('/games', function(req, res) {
var game = new Game();
DB[game.id] = game;
io.emit('games', allGames());
res.status(201).send(gameView(game));
});
app.post('/games/:id/players', function(req, res) {
var game = DB[req.params.id];
var player = new Player(req.body.name);
if (!game) {
return notFound(res);
}
if (game.isFull()) {
return res.status(400).send({ error: 'game full' });
}
game.players.push(player);
game.responses.push(res);
if (req.body.match && game.players.length === 1) {
var ai = new Player('bot', 'ai');
game.players.push(ai);
}
if (game.isFull()) {
game.start();
game.responses.shift().send(gameView(game));
game.startClock();
}
io.emit('games', allGames());
req.on('close', function() {
game.finish(game.currentPlayer);
});
});
app.post('/games/:id/plays', function(req, res) {
var game = DB[req.params.id];
var col = req.body.col;
var token = req.header('X-Token');
if (!game) {
return notFound(res);
}
if (token !== game.currentPlayer.token) {
return res.status(400).send({ error: 'out of turn' });
}
if (!game.isActive()) {
return res.status(400).send({ error: 'game not active' });
}
if (typeof col === 'undefined') {
game.finish(game.otherPlayer());
}
game.responses.push(res);
if (!playRound(game, col)) { return; }
game.startClock();
game.currentPlayer = game.otherPlayer();
if (!game.currentPlayer.isHuman()) {
var moves = boardUtil.availableMoves(game.board);
var move = moves[Math.floor(Math.random() * moves.length)];
playRound(game, move[0]);
game.startClock();
game.currentPlayer = game.otherPlayer();
}
game.responses.shift().send(gameView(game));
io.emit('games', allGames());
req.on('close', function() {
game.finish(game.currentPlayer);
});
});
http.listen(3000, function() {
console.log('listening on 3000');
});