forked from jhaagsma/ee_npc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommunication.php
209 lines (186 loc) · 5.18 KB
/
communication.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php namespace EENPC;
include_once('colors.php');
$colors = new Colors();
/*
This file holds the communications with the EE server, so that we can keep
only the real bot logic in the ee_npc file...
*/
///DATA HANDLING AND OUTPUT
/**
* Main Communication
* @param string $function which string to call
* @param array $parameterArray parameters to send
* @return object a JSON object converted to class
*/
function ee($function, $parameterArray = array())
{
global $baseURL, $username, $aiKey, $serv, $cnum, $APICalls;
$init = $parameterArray;
$parameterArray['ai_key'] = $aiKey;
$parameterArray['username'] = $username;
$parameterArray['server'] = $serv;
if ($cnum) {
$parameterArray['cnum'] = $cnum;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseURL);
curl_setopt($ch, CURLOPT_POST, 1);
$send = "api_function=".$function."&api_payload=".json_encode($parameterArray);
//out($send);
curl_setopt($ch, CURLOPT_POSTFIELDS, $send);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$serverOutput = curl_exec($ch);
curl_close($ch);
$APICalls++;
$return = handle_output($serverOutput, $function);
if ($return === false) {
out_data($init);
}
//out($function);
return $return;
}
/**
* Handle the server output
* @param JSON $serverOutput JSON return
* @param string $function function to call
* @return object json object -> class
*/
function handle_output($serverOutput, $function)
{
$response = json_decode($serverOutput);
$message = key($response);
$response = isset($response->$message) ? $response->$message : null;
//$parts = explode(':', $serverOutput, 2);
//This will simply kill the script if EE returns with an error
//This is to avoid foulups, and to simplify the code checking above
if ($message == 'COUNTRY_IS_DEAD') {
out("Country is Dead!");
return false;
} elseif ($message == 'OWNED') {
out("Trying to sell more than owned!");
return false;
} elseif (expected_result($function) && $message != expected_result($function)) {
out("\n\nUnexpected Result for '$function': ".$message.':'.$response."\n\n");
return false;
} elseif (!expected_result($function)) {
out($message);
out_data($response);
return false;
}
return $response;
}
/**
* just verifies that these things exist
* @param string $input Whatever the game returned
* @return string proper result
*/
function expected_result($input)
{
global $lastFunction;
$lastFunction = $input;
//This is simply a list of expected return values for each function
//This allows us to quickly verify if an error occurred
switch ($input) {
case 'server':
return 'SERVER_INFO';
case 'create':
return 'CNUM';
case 'advisor':
return 'ADVISOR';
case 'main':
return 'MAIN';
case 'build':
return 'BUILD';
case 'explore':
return 'EXPLORE';
case 'cash':
return 'CASH';
case 'pm_info':
return 'PM_INFO';
case 'pm':
return 'PM';
case 'tech':
return 'TECH';
case 'market':
return 'MARKET';
case 'onmarket':
return 'ONMARKET';
case 'buy':
return 'BUY';
case 'sell':
return 'SELL';
case 'govt':
return 'GOVT';
case 'rules':
return 'RULES';
case 'indy':
return 'INDY';
}
}
/**
* Ouput strings nicely
* @param string $str The string to format
* @param boolean $newline If we shoudl make a new line
* @return void echoes, not returns
*/
function out($str, $newline = true)
{
//This just formats output strings nicely
if (is_object($str)) {
return out_data($str);
}
echo ($newline ? "\n" : null)."[".date("H:i:s")."] $str";
}
/**
* I need a debugging function
* @param string $str Output
* @param boolean $newline Whether to print new lines
* @return void Prints text
*/
function debug($str, $newline = true)
{
global $debug;
if ($debug == true) {
out($str, $newline);
}
}
/**
* Output and format data
* @param array,object $data Data to ouput
* @return void
*/
function out_data($data)
{
$debug = debug_backtrace();
//out(var_export($debug, true));
//This function is to output and format some data nicely
out("DATA: ({$debug[0]['file']}:{$debug[0]['line']})\n".str_replace(",\n", "\n", var_export($data, true)));
}
/**
* Does count() in some case where it doesn't work right
* @param object $data probably a $result object
* @return int count of things in $data
*/
function actual_count($data)
{
//do not ask me why, but count() doesn't work on $result->turns
$i = 0;
foreach ($data as $stuff) {
$i++;
}
return $i;
}
/**
* Exit
* @param string $str Final output String
* @return exit
*/
function done($str = null)
{
if ($str) {
out($str);
}
out("Exiting\n\n");
exit;
}