-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.js
70 lines (59 loc) · 1.81 KB
/
run.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
const http = require('http');
const PORT = 7070;
const actions = ['stay', 'move', 'eat', 'take', 'put'];
const directions = ['up', 'down', 'right', 'left'];
/**
* This is just an example.
* For example, we give random orders to ants.
* Your bot will have to be more complex and change the strategy
* based on the information on the map (payload.canvas).
* Payload example https://github.com/anthive/js/blob/master/payload.json
* Return should look something like this.
* { orders: [
* { antId: 1, act: 'move', dir: 'down' },
* { antId: 17, act: 'move', dir: 'up' },
* ]}
* More information https://anthive.io/rules/
*/
const strategy = (payload) => {
const orders = [];
for (const ant of payload.ants) {
const randomNumber = Math.floor(Math.random() * 3);
const randomDirections = directions[randomNumber];
const order = {
antId: ant.id,
act: 'move',
dir: randomDirections,
};
orders.push(order);
}
return { orders };
};
/**
* Creating a bot server.
* Sim will make http post request to your bot each tick.
*/
const server = http.createServer((req, res) => {
if (req.method !== 'POST') {
res.end('only POST allowed');
return;
}
let body = '';
// Reading request body with information about map and ants
req.on('data', (chunk) => {
body += chunk.toString();
});
// Apply the strategy and send the result
req.on('end', () => {
const payload = JSON.parse(body);
const response = JSON.stringify(strategy(payload));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(response);
});
});
server.listen(PORT);
/**
* This code available at https://github.com/anthive/js
* to test it localy, submit post request with payload.json using postman or curl
* curl -X 'POST' -d @payload.json http://localhost:7070
*/