-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
96 lines (84 loc) · 2.13 KB
/
server.js
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
// Modules
const express = require('express')
const chess = require('./dumb-chess')
const morgan = require('morgan')
// Get legal moves in algebraic notation and sorted
function getLegalMoves() {
return chess.getLegalMoves()
.map(
function(m) {
return chess.moveToStr(m)
})
.sort(
function(a, b) {
return a > b ? 1 : -1
})
}
// Restart the game by unplaying all history moves
function restart() {
while(chess.history.length > 0) {
chess.unplay(chess.history[chess.history.length - 1])
}
}
// Unplay the last move
function unplay() {
// Check if there is one
if (chess.history.length === 0) return
// Unplay the last move
chess.unplay(chess.history[chess.history.length - 1])
}
// Create the common response
function resp(res) {
res.json({
colorToPlay: chess.colorToPlay() == chess.BLACK ? 'Black' : 'White',
fen: chess.posToFen(),
legalMoves: getLegalMoves().map(function(m) {return '/chess/play/'+ m;}),
actions: chess.history.length > 0 ? ['/chess/restart', '/chess/unplay'] : undefined,
history: chess.history.map(function(m) {return chess.moveToStr(m);})
})
}
// Express app
const app = express()
// Log
//app.use(morgan('tiny'))
// Routes
app.get('/chess', function(req, res) {
resp(res)
})
app.get('/chess/play/:move', function(req, res) {
if (chess.history.length > 0) {
if (chess.moveToStr(chess.history[chess.history.length - 1]) === req.params.move) {
console.log("Move = Last Move ", req.params.move, "(Jersey calls me twice sometimes but the rest api is not stateless)")
res.end()
return
}
}
const moves = chess.getLegalMoves()
for (var m = 0; m < moves.length; m++) {
if (chess.moveToStr(moves[m]) === req.params.move) {
break
}
}
if (m === moves.length) {
res.status(403).end()
} else {
chess.play(moves[m])
resp(res)
}
})
app.get('/chess/restart', function(req, res) {
restart()
resp(res)
})
app.get('/chess/unplay', function(req, res) {
unplay()
resp(res)
})
app.post('/chess', function(req, res) {
req.body.history.forEach(m => chess.play(m))
resp(res)
})
// Start the server
const port = 3000
app.listen(port)
console.log('Server running at port ' + port)