-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (56 loc) · 1.57 KB
/
index.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
const bodyParser = require('body-parser')
const express = require('express')
const http = require('http')
const path = require('path')
const io = require('socket.io')
const PORT = process.env.PORT || 3000
let NEXT_MOVE = 'down'
const httpServer = express()
const host = http.createServer(httpServer)
// Express web server
httpServer.use(bodyParser.json())
httpServer.get('/', httpIndex)
httpServer.post('/start', httpStart)
httpServer.post('/move', httpMove)
httpServer.post('/end', httpEnd)
httpServer.get('/controller', function(request, response) {
response.sendFile(path.join(__dirname, '/controller.html'))
})
function httpIndex(request, response) {
var battlesnakeInfo = {
apiversion: '1',
author: '',
color: '#3E338F',
head: 'default',
tail: 'default'
}
response.status(200).json(battlesnakeInfo)
}
function httpStart(request, response) {
response.status(200).send('ok')
}
function httpEnd(request, response) {
response.status(200).send('ok')
}
function httpMove(request, response) {
// Give ourselves 1000ms to respond to the game engine
var responseDelay = request.body.game.timeout - 1000
setTimeout(function() {
response.status(200).send({
move: NEXT_MOVE
})
}, responseDelay)
}
// Websocket Server
const wsServer = io(host)
wsServer.on('connection', function(socket) {
console.log('Client connected... ')
socket.on('move', function(msg) {
console.log('BROWSER: ' + msg)
NEXT_MOVE = msg
})
})
// Start server
host.listen(PORT, function() {
console.log(`Battlesnake Server listening at http://127.0.0.1:${PORT}`)
})