-
Notifications
You must be signed in to change notification settings - Fork 8
/
Client.php
executable file
·120 lines (103 loc) · 3.79 KB
/
Client.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
class Client
{
CONST TIMEOUT = 15;
private $key;
private $mode;
private $numberOfGames;
private $numberOfTurns;
private $serverUrl = 'http://vindinium.org';
public function __construct()
{
if ($_SERVER['argc'] < 4) {
echo "Usage: " . $_SERVER['SCRIPT_FILENAME'] . " <key> <[training|arena]> <number-of-games|number-of-turns> [server-url]\n";
echo "Example: " . $_SERVER['SCRIPT_FILENAME'] . " mySecretKey training 20\n";
} else {
$this->key = $_SERVER['argv'][1];
$this->mode = $_SERVER['argv'][2];
if ($this->mode == "training") {
$this->numberOfGames = 1;
$this->numberOfTurns = (int)$_SERVER['argv'][3];
} else {
$this->numberOfGames = (int)$_SERVER['argv'][3];
$this->numberOfTurns = 300; # Ignored in arena mode
}
if ($_SERVER['argc'] == 5) {
$this->serverUrl = $_SERVER['argv'][4];
}
}
}
public function load()
{
require('./Bot.php');
require('./HttpPost.php');
for ($i = 0; $i <= ($this->numberOfGames - 1); $i++) {
$this->start(new RandomBot());
echo "\nGame finished: " . ($i + 1) . "/" . $this->numberOfGames . "\n";
}
}
private function start($botObject)
{
// Starts a game with all the required parameters
if ($this->mode == 'arena') {
echo "Connected and waiting for other players to join...\n";
}
// Get the initial state
$state = $this->getNewGameState();
echo "Playing at: " . $state['viewUrl'] . "\n";
ob_start();
while ($this->isFinished($state) === false) {
// Some nice output ;)
echo '.';
ob_flush();
// Move to some direction
$url = $state['playUrl'];
$direction = $botObject->move($state);
$state = $this->move($url, $direction);
}
ob_end_clean();
}
private function getNewGameState()
{
// Get a JSON from the server containing the current state of the game
if ($this->mode == 'training') {
// Don't pass the 'map' parameter if you want a random map
$params = array('key' => $this->key, 'turns' => $this->numberOfTurns, 'map' => 'm1');
$api_endpoint = '/api/training';
} elseif ($this->mode == 'arena') {
$params = array('key' => $this->key);
$api_endpoint = '/api/arena';
}
// Wait for 10 minutes
$r = HttpPost::post($this->serverUrl . $api_endpoint, $params, 10 * 60);
if (isset($r['headers']['status_code']) && $r['headers']['status_code'] == 200) {
return json_decode($r['content'], true);
} else {
echo "Error when creating the game\n";
echo $r['content'];
}
}
private function move($url, $direction)
{
/*
* Send a move to the server
* Moves can be one of: 'Stay', 'North', 'South', 'East', 'West'
*/
try {
$r = HttpPost::post($url, array('dir' => $direction), self::TIMEOUT);
if (isset($r['headers']['status_code']) && $r['headers']['status_code'] == 200) {
return json_decode($r['content'], true);
} else {
echo "Error HTTP " . $r['headers']['status_code'] . "\n" . $r['content'] . "\n";
return array('game' => array('finished' => true));
}
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
return array('game' => array('finished' => true));
}
}
private function isFinished($state)
{
return $state['game']['finished'];
}
}