forked from BattlesnakeOfficial/starter-snake-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (56 loc) · 1.59 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
70
71
72
73
const bodyParser = require('body-parser')
const express = require('express')
const logger = require('morgan')
const app = express()
const {
fallbackHandler,
notFoundHandler,
genericErrorHandler,
poweredByHandler
} = require('./handlers.js')
const { strategy } = require('./logic.js');
// For deployment to Heroku, the port needs to be set using ENV, so
// we check for the port number in process.env
app.set('port', (process.env.PORT || 9001))
app.enable('verbose errors')
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(poweredByHandler)
// --- SNAKE LOGIC GOES BELOW THIS LINE ---
// Handle POST request to '/start'
app.post('/start', (request, response) => {
// NOTE: Do something here to start the game
// Response data
const data = {
color: '#AD3058',
headType: 'evil',
tailType: 'hook',
}
return response.json(data)
})
// Handle POST request to '/move'
app.post('/move', (request, response) => {
// NOTE: Do something here to generate your move
const { body } = request;
let move = strategy(body);
// Response data
const data = {
move,
};
return response.json(data);
})
app.post('/end', (request, response) => {
// NOTE: Any cleanup when a game is complete.
return response.json({})
})
app.post('/ping', (request, response) => {
// Used for checking if this snake is still alive.
return response.json({});
})
// --- SNAKE LOGIC GOES ABOVE THIS LINE ---
app.use('*', fallbackHandler)
app.use(notFoundHandler)
app.use(genericErrorHandler)
app.listen(app.get('port'), () => {
console.log('Server listening on port %s', app.get('port'))
})