|
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\Services\Application\Dispatcher; |
|
24
|
|
|
use Maniaplanet\DedicatedServer\Structures\GameInfos; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Description of Connection |
|
28
|
|
|
* |
|
29
|
|
|
* @author Reaby |
|
30
|
|
|
*/ |
|
31
|
|
|
class Connection |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
private $address = "http://karma.mania-exchange.com/api2/"; |
|
35
|
|
|
|
|
36
|
|
|
private $connected = false; |
|
37
|
|
|
|
|
38
|
|
|
private $sessionKey = null; |
|
39
|
|
|
|
|
40
|
|
|
private $sessionSeed = null; |
|
41
|
|
|
|
|
42
|
|
|
private $apiKey = ""; |
|
43
|
|
|
|
|
44
|
|
|
/** @var MXRating */ |
|
45
|
|
|
private $ratings = null; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var Dispatcher |
|
48
|
|
|
*/ |
|
49
|
|
|
private $dispatcher; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Connection constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Dispatcher $dispatcher |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(Dispatcher $dispatcher) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->dispatcher = $dispatcher; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* connect to MX karma |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $serverLogin |
|
66
|
|
|
* @param string $apiKey |
|
67
|
|
|
*/ |
|
68
|
|
|
public function connect($serverLogin, $apiKey) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->apiKey = $apiKey; |
|
71
|
|
|
|
|
72
|
|
|
$params = array( |
|
73
|
|
|
"serverLogin" => $serverLogin, |
|
74
|
|
|
"applicationIdentifier" => "eXpansion 2.0.0.0", |
|
75
|
|
|
"testMode" => "true", |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$this->httpGet( |
|
79
|
|
|
$this->buildUrl("startSession", $params), |
|
80
|
|
|
array($this, "xConnect") |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function xConnect($answer, $httpCode) |
|
85
|
|
|
{ |
|
86
|
|
|
|
|
87
|
|
|
if ($httpCode != 200) { |
|
88
|
|
|
return; |
|
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->dataAccess->httpGet( |
|
|
|
|
|
|
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
|
|
|
$first = true; |
|
321
|
|
|
$buffer = ""; |
|
322
|
|
|
foreach ($params as $key => $value) { |
|
323
|
|
|
$prefix = "&"; |
|
324
|
|
|
if ($first) { |
|
325
|
|
|
$first = false; |
|
326
|
|
|
$prefix = "?"; |
|
327
|
|
|
} |
|
328
|
|
|
$buffer .= $prefix.$key."=".rawurlencode($value); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
return $url.$buffer; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @return bool |
|
336
|
|
|
*/ |
|
337
|
|
|
public function isConnected() |
|
338
|
|
|
{ |
|
339
|
|
|
return $this->connected; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @param $url |
|
344
|
|
|
* @param callable $callback |
|
345
|
|
|
*/ |
|
346
|
|
|
public function httpGet($url, callable $callback) |
|
347
|
|
|
{ |
|
348
|
|
|
$ch = curl_init($url); |
|
349
|
|
|
curl_setopt($ch, CURLOPT_HEADER, "application/json"); |
|
350
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
351
|
|
|
$output = curl_exec($ch); |
|
352
|
|
|
$httpCode = curl_getinfo($ch)["http_code"]; |
|
353
|
|
|
curl_close($ch); |
|
354
|
|
|
call_user_func($callback, $output, $httpCode); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
} |
|
358
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.