/
server.js
101 lines (79 loc) · 2.75 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
97
98
99
100
const bodyParser = require('body-parser')
const express = require('express')
const smorts = require('./smorts.js')
const _ = require('lodash')
const flooder = require('./lib/floodFill.js')
const app = express()
app.set('port', (process.env.PORT || 8080))
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.json({status: 1})
})
// Handle POST request to '/start'
app.post('/start', (request, response) => {
// Response data
const data = {
color: '#FEFEFE',
secondary_color: '#f4f4f4',
headType: 'beluga',
tailType: 'pixel'
}
return response.json(data)
})
// Handle POST request to '/move'
app.post('/move', (request, response) => {
const body = request.body
// console.log(`TURN: ${body.turn} ====================`)
const turn = body.turn
const board = body.board
const food = board.food
const health = body.you.health
const myBody = body.you.body
const head = myBody[0]
const allSnakes = _.flatten(_.map(board.snakes, (x) => x.body))
const snakeObj = _.reduce(allSnakes, (acc, x) => {
acc[`${x.x},${x.y}`] = true
return acc
}, {})
const snekParts = smorts.posDiff(allSnakes, myBody)
let moveOptions = smorts.getMoveOptions(allSnakes, head, board.width, board.height)
// Floodfill to avoid dead ends
let newMoveOptions = _.filter(moveOptions, (x) => flooder.floodFrom(board.width, board.height, snakeObj, myBody.length + 5, x) >= myBody.length + 5)
if (newMoveOptions.length > 0) {
moveOptions = newMoveOptions
} else {
moveOptions = [_.sortBy(moveOptions, (x) => flooder.floodFrom(board.width, board.height, snakeObj, myBody.length, x) * -1)[0]]
}
// TODO: Be scared of enemy heads
// TODO: Avoid board edges
// TODO: Currently elon won't eat when scared... https://play.battlesnake.com/g/c525981e-0749-4a07-a359-49bb6b3574d7/
const headToSnek = smorts.smallestDistance(head, snekParts)
const hungry = turn < 10 ? 200 : smorts.getHungry(food)
// const hungry = 70
let my_move = 'up';
if(headToSnek <= 3) { //scared state
my_move = smorts.collisionAvoider(moveOptions, snekParts, myBody, head)
} else if (health <= hungry && food.length > 0) { //hungry state
my_move = smorts.foodSeeker(moveOptions, food, head)
} else { //wimpy state
my_move = smorts.tailChaser(moveOptions, myBody, head)
}
if(moveOptions.length == 0) {
my_move = smorts.whatDir(head, _.last(myBody))
}
// Response data
const data = {
move: my_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) => {
return response.json({});
})
app.listen(app.get('port'), () => {
console.log('Server listening on port %s', app.get('port'))
})