-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.php
49 lines (39 loc) · 1.42 KB
/
run.php
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
<?php
// Your bot response should be json object
header('Content-Type: application/json');
// Available actions and directions
const ACTIONS = ['stay', 'move', 'eat', 'take', 'put'];
const DIRECTIONS = ['up', 'down', 'right', 'left'];
// Sim will make http post request to your bot
// with json payload that contains information
// about map and ants
$payload = file_get_contents('php://input');
// Hive object from request payload
$hive = json_decode($payload, true);
// As example, we give order to 'move' in random direction.
// Your bot will have to be more complex and change the strategy
// based on map information (payload.canvas).
// Payload example https://github.com/anthive/php/blob/master/payload.json
// More information https://anthive.io/rules/
$antStrategy = function ($ant) {
return [
'antId' => $ant['id'],
'act' => 'move',
'dir' => DIRECTIONS[rand(0, 3)],
];
};
// Loop through ants and give orders
$orders = array_map($antStrategy, $hive['ants']);
// Response json should look something like this:
// {'orders': [
// {'antId':1,'act':'move','dir':'down'},
// {'antId':17,'act':'load','dir':'up'}
// ]}
// Finish your json response
echo json_encode([
'orders' => $orders,
]);
// This code available at https://github.com/anthive/php
// to test it localy, submit post request with payload.json using postman or curl
// curl -X 'POST' -d @payload.json http://localhost/
// Have fun!