|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2014 Reaby |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace eXpansion\Bundle\MxKarma\Plugins; |
|
21
|
|
|
|
|
22
|
|
|
use eXpansion\Bundle\MxKarma\Entity\MxRating; |
|
23
|
|
|
use eXpansion\Framework\Core\Helpers\Http; |
|
24
|
|
|
use eXpansion\Framework\Core\Services\Application\Dispatcher; |
|
25
|
|
|
use Maniaplanet\DedicatedServer\Structures\GameInfos; |
|
26
|
|
|
use oliverde8\AsynchronousJobs\Job\CallbackCurl; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Description of Connection |
|
30
|
|
|
* |
|
31
|
|
|
* @author Reaby |
|
32
|
|
|
*/ |
|
33
|
|
|
class Connection |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
// private $address = "http://karma.mania-exchange.com/api2/"; |
|
|
|
|
|
|
37
|
|
|
private $address = "http://localhost/index.html?"; |
|
38
|
|
|
|
|
39
|
|
|
private $connected = false; |
|
40
|
|
|
|
|
41
|
|
|
private $sessionKey = null; |
|
42
|
|
|
|
|
43
|
|
|
private $sessionSeed = null; |
|
44
|
|
|
|
|
45
|
|
|
private $apiKey = ""; |
|
46
|
|
|
|
|
47
|
|
|
/** @var MXRating */ |
|
48
|
|
|
private $ratings = null; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var Dispatcher |
|
51
|
|
|
*/ |
|
52
|
|
|
private $dispatcher; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Connection constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Dispatcher $dispatcher |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(Dispatcher $dispatcher, Http $http) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->dispatcher = $dispatcher; |
|
62
|
|
|
$this->http = $http; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* connect to MX karma |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $serverLogin |
|
70
|
|
|
* @param string $apiKey |
|
71
|
|
|
*/ |
|
72
|
|
|
public function connect($serverLogin, $apiKey) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->apiKey = $apiKey; |
|
75
|
|
|
|
|
76
|
|
|
$params = array( |
|
77
|
|
|
"serverLogin" => $serverLogin, |
|
78
|
|
|
"applicationIdentifier" => "eXpansion 2.0.0.0", |
|
79
|
|
|
"testMode" => "true", |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$this->http->get( |
|
83
|
|
|
$this->buildUrl("startSession", $params), |
|
84
|
|
|
array($this, "xConnect") |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function xConnect( $answer) |
|
89
|
|
|
{ |
|
90
|
|
|
|
|
91
|
|
|
$data = $this->getObject($answer); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
if ($data === null) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->sessionKey = $data->sessionKey; |
|
98
|
|
|
$this->sessionSeed = $data->sessionSeed; |
|
99
|
|
|
|
|
100
|
|
|
$outHash = hash("sha512", ($this->apiKey.$this->sessionSeed)); |
|
101
|
|
|
|
|
102
|
|
|
$params = array("sessionKey" => $this->sessionKey, "activationHash" => $outHash); |
|
103
|
|
|
$this->http->call( |
|
104
|
|
|
$this->buildUrl("activateSession", $params), |
|
105
|
|
|
array($this, "xActivate"), |
|
106
|
|
|
array(), |
|
107
|
|
|
"ManiaLive - eXpansionPluginPack", |
|
108
|
|
|
"application/json" |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function xActivate($answer, $httpCode) |
|
113
|
|
|
{ |
|
114
|
|
|
|
|
115
|
|
|
if ($httpCode != 200) { |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$data = $this->getObject($answer, "onActivate"); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
if ($data === null) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($data->activated) { |
|
126
|
|
|
$this->connected = true; |
|
127
|
|
|
// Dispatcher::dispatch(new MXKarmaEvent(MXKarmaEvent::ON_CONNECTED)); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function getRatings($players = array(), $getVotesOnly = false) |
|
132
|
|
|
{ |
|
133
|
|
|
if (!$this->connected) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$params = array("sessionKey" => $this->sessionKey); |
|
138
|
|
|
$postData = array( |
|
139
|
|
|
"gamemode" => $this->getGameMode(), |
|
140
|
|
|
"titleid" => $this->expStorage->titleId, |
|
|
|
|
|
|
141
|
|
|
"mapuid" => $this->storage->currentMap->uId, |
|
|
|
|
|
|
142
|
|
|
"getvotesonly" => $getVotesOnly, |
|
143
|
|
|
"playerlogins" => $players, |
|
144
|
|
|
); |
|
145
|
|
|
$this->dataAccess->httpPost( |
|
|
|
|
|
|
146
|
|
|
$this->buildUrl("getMapRating", $params), |
|
147
|
|
|
json_encode($postData), |
|
148
|
|
|
array($this, "xGetRatings"), |
|
149
|
|
|
array(), |
|
150
|
|
|
"ManiaLive - eXpansionPluginPack", |
|
151
|
|
|
"application/json" |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function saveVotes(\Maniaplanet\DedicatedServer\Structures\Map $map, $time, $votes) |
|
156
|
|
|
{ |
|
157
|
|
|
if (!$this->connected) { |
|
158
|
|
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$params = array("sessionKey" => $this->sessionKey); |
|
162
|
|
|
$postData = array( |
|
163
|
|
|
"gamemode" => $this->getGameMode(), |
|
164
|
|
|
"titleid" => $this->expStorage->titleId, |
|
165
|
|
|
"mapuid" => $map->uId, |
|
166
|
|
|
"mapname" => $map->name, |
|
167
|
|
|
"mapauthor" => $map->author, |
|
168
|
|
|
"isimport" => false, |
|
169
|
|
|
"maptime" => $time, |
|
170
|
|
|
"votes" => $votes, |
|
171
|
|
|
); |
|
172
|
|
|
$this->dataAccess->httpPost( |
|
173
|
|
|
$this->buildUrl("saveVotes", $params), |
|
174
|
|
|
json_encode($postData), |
|
175
|
|
|
array($this, "xSaveVotes"), |
|
176
|
|
|
array(), |
|
177
|
|
|
"ManiaLive - eXpansionPluginPack", |
|
178
|
|
|
"application/json" |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function xSaveVotes($answer, $httpCode) |
|
183
|
|
|
{ |
|
184
|
|
|
|
|
185
|
|
|
if ($httpCode != 200) { |
|
186
|
|
|
return; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$data = $this->getObject($answer); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
if ($data === null) { |
|
192
|
|
|
return; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// Dispatcher::dispatch(new MXKarmaEvent(MXKarmaEvent::ON_VOTE_SAVE, $data->updated)); |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param $answer |
|
200
|
|
|
* @param $httpCode |
|
201
|
|
|
* |
|
202
|
|
|
* @return MxRating|null |
|
203
|
|
|
*/ |
|
204
|
|
|
public function xGetRatings($answer, $httpCode) |
|
205
|
|
|
{ |
|
206
|
|
|
|
|
207
|
|
|
if ($httpCode != 200) { |
|
208
|
|
|
return null; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$data = $this->getObject($answer); |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
if ($data === null) { |
|
214
|
|
|
return null; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$this->ratings = new MXRating(); |
|
|
|
|
|
|
218
|
|
|
$this->ratings->append($data); |
|
219
|
|
|
|
|
220
|
|
|
return $this->ratings; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function getGameMode() |
|
224
|
|
|
{ |
|
225
|
|
|
switch ($this->storage->gameInfos->gameMode) { |
|
|
|
|
|
|
226
|
|
|
case GameInfos::GAMEMODE_SCRIPT: |
|
227
|
|
|
$gamemode = strtolower($this->storage->gameInfos->scriptName); |
|
|
|
|
|
|
228
|
|
|
break; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $gamemode; |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string $data json data |
|
236
|
|
|
* |
|
237
|
|
|
* @return null |
|
238
|
|
|
*/ |
|
239
|
|
|
public function getObject($data) |
|
240
|
|
|
{ |
|
241
|
|
|
$obj = (object)json_decode($data); |
|
242
|
|
|
if ($obj->success === false) { |
|
243
|
|
|
$this->handleErrors($obj); |
|
244
|
|
|
|
|
245
|
|
|
return null; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
return $obj->data; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param object $object |
|
|
|
|
|
|
253
|
|
|
*/ |
|
254
|
|
|
public function handleErrors($obj) |
|
255
|
|
|
{ |
|
256
|
|
|
switch ($obj->data->code) { |
|
257
|
|
|
case 1: |
|
258
|
|
|
echo "internal server error"; |
|
259
|
|
|
break; |
|
260
|
|
|
case 2: |
|
261
|
|
|
echo "Session key is invalid (not activated, experied or got disabled)."; |
|
262
|
|
|
break; |
|
263
|
|
|
case 4: |
|
264
|
|
|
echo "Some parameters are not provided."; |
|
265
|
|
|
break; |
|
266
|
|
|
case 5: |
|
267
|
|
|
echo "API key not found or suspended."; |
|
268
|
|
|
break; |
|
269
|
|
|
case 6: |
|
270
|
|
|
echo "Server not found or suspended."; |
|
271
|
|
|
break; |
|
272
|
|
|
case 7: |
|
273
|
|
|
echo "Cross-server call rejected."; |
|
274
|
|
|
break; |
|
275
|
|
|
case 8: |
|
276
|
|
|
echo "Invalid activation hash provided, session closed."; |
|
277
|
|
|
$this->connected = false; |
|
278
|
|
|
break; |
|
279
|
|
|
case 9: |
|
280
|
|
|
echo "Session already active."; |
|
281
|
|
|
break; |
|
282
|
|
|
case 10: |
|
283
|
|
|
echo "Unsupported Content-Type."; |
|
284
|
|
|
break; |
|
285
|
|
|
case 11: |
|
286
|
|
|
echo "Too many logins requested."; |
|
287
|
|
|
break; |
|
288
|
|
|
case 12: |
|
289
|
|
|
echo "Invalid JSON or invalid structure."; |
|
290
|
|
|
break; |
|
291
|
|
|
case 13: |
|
292
|
|
|
echo "Malformed vote request."; |
|
293
|
|
|
break; |
|
294
|
|
|
case 14: |
|
295
|
|
|
echo "no votes cast - please do not make requests if there are no votes!"; |
|
296
|
|
|
break; |
|
297
|
|
|
case 15: |
|
298
|
|
|
echo "too many import votes - request a limit raise if needed"; |
|
299
|
|
|
break; |
|
300
|
|
|
case 16: |
|
301
|
|
|
echo "Import rejected."; |
|
302
|
|
|
break; |
|
303
|
|
|
default: |
|
304
|
|
|
echo "unknown error"; |
|
305
|
|
|
break; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
// Dispatcher::dispatch(new MXKarmaEvent(MXKarmaEvent::ON_ERROR, $origin, $obj->data->code, $obj->data->message)); |
|
|
|
|
|
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @param string $method |
|
313
|
|
|
* @param array $params |
|
314
|
|
|
* |
|
315
|
|
|
* @return string |
|
316
|
|
|
*/ |
|
317
|
|
|
private function buildUrl($method, $params) |
|
318
|
|
|
{ |
|
319
|
|
|
$url = $this->address.$method; |
|
320
|
|
|
|
|
321
|
|
|
return $url."?".http_build_query($params); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @return bool |
|
326
|
|
|
*/ |
|
327
|
|
|
public function isConnected() |
|
328
|
|
|
{ |
|
329
|
|
|
return $this->connected; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @param $url |
|
334
|
|
|
* @param callable $callback |
|
335
|
|
|
*/ |
|
336
|
|
|
public function httpGet($url, callable $callback) |
|
337
|
|
|
{ |
|
338
|
|
|
$ch = curl_init($url); |
|
339
|
|
|
curl_setopt($ch, CURLOPT_HEADER, "application/json"); |
|
340
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
341
|
|
|
$output = curl_exec($ch); |
|
342
|
|
|
$httpCode = curl_getinfo($ch)["http_code"]; |
|
343
|
|
|
curl_close($ch); |
|
344
|
|
|
call_user_func($callback, $output, $httpCode); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.