|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\Maps\Plugins; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Bundle\Maps\Model\Map; |
|
6
|
|
|
use eXpansion\Bundle\Maps\Model\MapQuery; |
|
7
|
|
|
use eXpansion\Bundle\Maps\Model\Mxmap; |
|
8
|
|
|
use eXpansion\Bundle\Maps\Model\MxmapQuery; |
|
9
|
|
|
use eXpansion\Bundle\Maps\Services\JukeboxService; |
|
10
|
|
|
use eXpansion\Bundle\Maps\Structure\MxInfo; |
|
11
|
|
|
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups; |
|
12
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication; |
|
13
|
|
|
use eXpansion\Framework\Core\Helpers\ChatNotification; |
|
14
|
|
|
use eXpansion\Framework\Core\Helpers\FileSystem; |
|
15
|
|
|
use eXpansion\Framework\Core\Helpers\Http; |
|
16
|
|
|
use eXpansion\Framework\Core\Helpers\Structures\HttpResult; |
|
17
|
|
|
use eXpansion\Framework\Core\Helpers\TMString; |
|
18
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
19
|
|
|
use eXpansion\Framework\Core\Storage\GameDataStorage; |
|
20
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
21
|
|
|
use Maniaplanet\DedicatedServer\Structures\Map as DedicatedMap; |
|
22
|
|
|
use Propel\Runtime\Map\TableMap; |
|
23
|
|
|
use Psr\Log\LoggerInterface; |
|
24
|
|
|
|
|
25
|
|
|
class ManiaExchange implements ListenerInterfaceExpApplication |
|
26
|
|
|
{ |
|
27
|
|
|
const SITE_TM = "TM"; |
|
28
|
|
|
const SITE_SM = "SM"; |
|
29
|
|
|
|
|
30
|
|
|
/** @var bool */ |
|
31
|
|
|
private $downloadProgressing = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Connection |
|
35
|
|
|
*/ |
|
36
|
|
|
private $connection; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var ChatNotification |
|
39
|
|
|
*/ |
|
40
|
|
|
private $chatNotification; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Http |
|
44
|
|
|
*/ |
|
45
|
|
|
private $http; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var AdminGroups |
|
49
|
|
|
*/ |
|
50
|
|
|
private $adminGroups; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var GameDataStorage |
|
54
|
|
|
*/ |
|
55
|
|
|
private $gameDataStorage; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Console |
|
59
|
|
|
*/ |
|
60
|
|
|
private $console; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var LoggerInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
private $logger; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var JukeboxService |
|
69
|
|
|
*/ |
|
70
|
|
|
private $jukebox; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var FileSystem |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $fileSystem; |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
protected $addQueue = []; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* ManiaExchange constructor. |
|
82
|
|
|
* @param Connection $connection |
|
83
|
|
|
* @param ChatNotification $chatNotification |
|
84
|
|
|
* @param Http $http |
|
85
|
|
|
* @param AdminGroups $adminGroups |
|
86
|
|
|
* @param GameDataStorage $gameDataStorage |
|
87
|
|
|
* @param Console $console |
|
88
|
|
|
* @param LoggerInterface $logger |
|
89
|
|
|
* @param JukeboxService $jukebox |
|
90
|
|
|
* @param FileSystem $fileSystem |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
93
|
|
|
Connection $connection, |
|
94
|
|
|
ChatNotification $chatNotification, |
|
95
|
|
|
Http $http, |
|
96
|
|
|
AdminGroups $adminGroups, |
|
97
|
|
|
GameDataStorage $gameDataStorage, |
|
98
|
|
|
Console $console, |
|
99
|
|
|
LoggerInterface $logger, |
|
|
|
|
|
|
100
|
|
|
JukeboxService $jukebox, |
|
101
|
|
|
FileSystem $fileSystem |
|
102
|
|
|
) { |
|
103
|
|
|
$this->connection = $connection; |
|
104
|
|
|
$this->chatNotification = $chatNotification; |
|
105
|
|
|
$this->http = $http; |
|
106
|
|
|
$this->adminGroups = $adminGroups; |
|
107
|
|
|
$this->gameDataStorage = $gameDataStorage; |
|
108
|
|
|
$this->console = $console; |
|
109
|
|
|
$this->jukebox = $jukebox; |
|
110
|
|
|
$this->fileSystem = $fileSystem; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** @var Mxmap[] $maps */ |
|
114
|
|
|
public function addAllMaps($login, $maps) |
|
115
|
|
|
{ |
|
116
|
|
|
if (!$this->adminGroups->hasPermission($login, "maps.add")) { |
|
117
|
|
|
$this->chatNotification->sendMessage('expansion_mx.chat.nopermission', $login); |
|
118
|
|
|
|
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->addQueue = $maps; |
|
123
|
|
|
$this->connection->chatSendServerMessage("Starting download. Maps in queue: ".count($this->addQueue)); |
|
124
|
|
|
$map = array_shift($this->addQueue); |
|
125
|
|
|
$this->addMap($login, $map['mxid'], $map['mxsite']); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* add map to queue |
|
130
|
|
|
* @param string $login |
|
131
|
|
|
* @param integer $id |
|
132
|
|
|
* @param string $mxsite "TM" or "SM" |
|
133
|
|
|
*/ |
|
134
|
|
|
public function addMapToQueue($login, $id, $mxsite) |
|
135
|
|
|
{ |
|
136
|
|
|
|
|
137
|
|
|
if (!$this->adminGroups->hasPermission($login, "maps.add")) { |
|
138
|
|
|
$this->chatNotification->sendMessage('expansion_mx.chat.nopermission', $login); |
|
139
|
|
|
|
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($this->downloadProgressing || count($this->addQueue) > 1) { |
|
144
|
|
|
|
|
145
|
|
|
$this->addQueue[] = ['mxid' => $id, 'mxsite' => $mxsite]; |
|
146
|
|
|
$this->chatNotification->sendMessage("|info| Adding map to download queue...", $login); |
|
147
|
|
|
|
|
148
|
|
|
return; |
|
149
|
|
|
} else { |
|
150
|
|
|
$this->addMap($login, $id, $mxsite); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param $login |
|
157
|
|
|
* @param $id |
|
158
|
|
|
* @param $mxsite |
|
159
|
|
|
*/ |
|
160
|
|
|
public function addMap($login, $id, $mxsite) |
|
161
|
|
|
{ |
|
162
|
|
|
|
|
163
|
|
|
$options = [ |
|
164
|
|
|
CURLOPT_HTTPHEADER => [ |
|
165
|
|
|
"Content-Type" => "application/json", |
|
166
|
|
|
"X-ManiaPlanet-ServerLogin" => $this->gameDataStorage->getSystemInfo()->serverLogin, |
|
167
|
|
|
], |
|
168
|
|
|
]; |
|
169
|
|
|
|
|
170
|
|
|
if (!$mxsite) { |
|
171
|
|
|
$mxsite = "TM"; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$group = $this->adminGroups->getLoginUserGroups($login); |
|
175
|
|
|
|
|
176
|
|
|
$this->chatNotification->sendMessage( |
|
177
|
|
|
'expansion_mx.chat.start', |
|
178
|
|
|
$group, |
|
179
|
|
|
["%id%" => $id, "%site%" => $mxsite] |
|
180
|
|
|
); |
|
181
|
|
|
$this->downloadProgressing = true; |
|
182
|
|
|
$this->http->get("https://api.mania-exchange.com/".strtolower($mxsite)."/maps?ids=".$id, |
|
183
|
|
|
[$this, 'callbackAddMap1'], |
|
184
|
|
|
['login' => $login, 'site' => $mxsite, 'mxId' => $id], $options); |
|
185
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
public function callbackAddMap1(HttpResult $result) |
|
190
|
|
|
{ |
|
191
|
|
|
$additionalData = $result->getAdditionalData(); |
|
192
|
|
|
$group = $this->adminGroups->getLoginUserGroups($additionalData['login']); |
|
193
|
|
|
|
|
194
|
|
|
$json = json_decode($result->getResponse(), true); |
|
195
|
|
|
|
|
196
|
|
|
if (isset($json['StatusCode'])) { |
|
197
|
|
|
$this->chatNotification->sendMessage( |
|
198
|
|
|
'expansion_mx.chat.apierror', |
|
199
|
|
|
$group, |
|
200
|
|
|
["%status%" => $json['StatusCode'], "%message%" => $json['Message']] |
|
201
|
|
|
); |
|
202
|
|
|
$this->downloadProgressing = false; |
|
203
|
|
|
return; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$mxInfo = new MxInfo($json[0]); |
|
207
|
|
|
$additionalData['mxInfo'] = $mxInfo; |
|
208
|
|
|
|
|
209
|
|
|
if (!$result->hasError()) { |
|
210
|
|
|
$options = [ |
|
211
|
|
|
CURLOPT_FOLLOWLOCATION => false, |
|
212
|
|
|
CURLOPT_HTTPHEADER => [ |
|
213
|
|
|
"Content-Type" => "application/json", |
|
214
|
|
|
"X-ManiaPlanet-ServerLogin" => $this->gameDataStorage->getSystemInfo()->serverLogin, |
|
215
|
|
|
] |
|
216
|
|
|
]; |
|
217
|
|
|
|
|
218
|
|
|
$this->http->get("https://".strtolower($additionalData['site']). |
|
219
|
|
|
".mania-exchange.com/tracks/download/".$additionalData['mxId'], |
|
220
|
|
|
[$this, 'callbackAddMap2'], |
|
221
|
|
|
$additionalData, $options); |
|
222
|
|
|
} else { |
|
223
|
|
|
$this->chatNotification->sendMessage( |
|
224
|
|
|
'expansion_mx.chat.httperror', |
|
225
|
|
|
$group, |
|
226
|
|
|
["%status%" => $result->getHttpCode(), "%message%" => $result->getError()] |
|
227
|
|
|
); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function callbackAddMap2(HttpResult $result) |
|
232
|
|
|
{ |
|
233
|
|
|
$data = $result->getAdditionalData(); |
|
234
|
|
|
$group = $this->adminGroups->getLoginUserGroups($data['login']); |
|
235
|
|
|
|
|
236
|
|
|
if ($result->hasError()) { |
|
237
|
|
|
|
|
238
|
|
|
if ($result->getHttpCode() == 302) { |
|
239
|
|
|
$this->chatNotification->sendMessage( |
|
240
|
|
|
'expansion_mx.chat.decline', |
|
241
|
|
|
$group, |
|
242
|
|
|
[] |
|
243
|
|
|
); |
|
244
|
|
|
$this->downloadProgressing = false; |
|
245
|
|
|
return; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
$this->chatNotification->sendMessage( |
|
249
|
|
|
'expansion_mx.chat.httperror', |
|
250
|
|
|
$group, |
|
251
|
|
|
["%status%" => $result->getHttpCode(), "%message%" => $result->getError()] |
|
252
|
|
|
); |
|
253
|
|
|
|
|
254
|
|
|
$this->downloadProgressing = false; |
|
255
|
|
|
return; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** @var MxInfo $info */ |
|
259
|
|
|
$info = $data['mxInfo']; |
|
260
|
|
|
|
|
261
|
|
|
$authorName = $this->cleanString($info->username); |
|
262
|
|
|
$mapName = $this->cleanString( |
|
263
|
|
|
trim( |
|
264
|
|
|
mb_convert_encoding( |
|
265
|
|
|
substr(TMString::trimStyles($info->gbxMapName), 0, 20), |
|
266
|
|
|
"7bit", |
|
267
|
|
|
"UTF-8" |
|
268
|
|
|
) |
|
269
|
|
|
) |
|
270
|
|
|
); |
|
271
|
|
|
|
|
272
|
|
|
$filename = $data['mxId']."-".$authorName."-".$mapName.".Map.Gbx"; |
|
273
|
|
|
|
|
274
|
|
|
try { |
|
275
|
|
|
$fileSystem = $this->fileSystem->getUserData(); |
|
276
|
|
|
$dir = 'Maps'.DIRECTORY_SEPARATOR.$info->titlePack; |
|
277
|
|
|
$file = $dir.DIRECTORY_SEPARATOR.$filename; |
|
278
|
|
|
|
|
279
|
|
|
if (!$fileSystem->createDir($dir)) { |
|
280
|
|
|
$this->console->writeln('<error>Error while adding map!</error>'); |
|
281
|
|
|
|
|
282
|
|
|
return; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
if (!$fileSystem->has($file)) { |
|
286
|
|
|
$fileSystem->write($file, $result->getResponse()); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
if (!$this->connection->checkMapForCurrentServerParams($info->titlePack.DIRECTORY_SEPARATOR.$filename)) { |
|
291
|
|
|
$this->chatNotification->sendMessage("expansion_mx.chat.fail"); |
|
292
|
|
|
|
|
293
|
|
|
return; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$map = $this->connection->getMapInfo($info->titlePack.DIRECTORY_SEPARATOR.$filename); |
|
297
|
|
|
$this->connection->addMap($map->fileName); |
|
298
|
|
|
|
|
299
|
|
|
$this->jukebox->addMap($map, $data['login']); |
|
300
|
|
|
$this->chatNotification->sendMessage( |
|
301
|
|
|
'expansion_mx.chat.success', |
|
302
|
|
|
null, |
|
303
|
|
|
[ |
|
304
|
|
|
"%mxid%" => $data['mxId'], |
|
305
|
|
|
"%mapauthor%" => $map->author, |
|
306
|
|
|
"%mapname%" => TMString::trimControls($map->name), |
|
307
|
|
|
] |
|
308
|
|
|
); |
|
309
|
|
|
|
|
310
|
|
|
$this->persistMapData($map, $info); |
|
311
|
|
|
$this->downloadProgressing = false; |
|
312
|
|
|
} catch (\Exception $e) { |
|
313
|
|
|
$this->chatNotification->sendMessage( |
|
314
|
|
|
'expansion_mx.chat.dedicatedexception', |
|
315
|
|
|
$group, ["%message%" => $e->getMessage()] |
|
316
|
|
|
); |
|
317
|
|
|
// $this->logger->alert("Error while adding map : ".$e->getMessage(), ['exception' => $e]); |
|
|
|
|
|
|
318
|
|
|
$this->downloadProgressing = false; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
if (count($this->addQueue) > 0) { |
|
322
|
|
|
$map = array_shift($this->addQueue); |
|
323
|
|
|
$this->connection->chatSendServerMessage("Processing queue. Maps in queue: ".count($this->addQueue)); |
|
324
|
|
|
$this->addMap($data['login'], $map['mxid'], $map['mxsite']); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param DedicatedMap $map |
|
330
|
|
|
* @param MxInfo $mxInfo |
|
331
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
|
332
|
|
|
*/ |
|
333
|
|
|
protected function persistMapData($map, $mxInfo) |
|
334
|
|
|
{ |
|
335
|
|
|
|
|
336
|
|
|
$mapquery = new MapQuery(); |
|
337
|
|
|
$dbMap = $mapquery->findOneByMapuid($map->uId); |
|
338
|
|
|
|
|
339
|
|
|
if ($dbMap) { |
|
340
|
|
|
$dbMap->fromArray($this->convertMap($map), TableMap::TYPE_FIELDNAME); |
|
341
|
|
|
} else { |
|
342
|
|
|
$dbMap = new Map(); |
|
343
|
|
|
$dbMap->fromArray($this->convertMap($map), TableMap::TYPE_FIELDNAME); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
$mxquery = new MxmapQuery(); |
|
347
|
|
|
$mxMap = $mxquery->findOneByTrackuid($map->uId); |
|
348
|
|
|
|
|
349
|
|
|
print_r($mxInfo->toArray()); |
|
350
|
|
|
|
|
351
|
|
|
if ($mxMap) { |
|
352
|
|
|
$mxMap->fromArray($mxInfo->toArray(), TableMap::TYPE_FIELDNAME); |
|
353
|
|
|
} else { |
|
354
|
|
|
$mxMap = new Mxmap(); |
|
355
|
|
|
$mxMap->fromArray($mxInfo->toArray(), TableMap::TYPE_FIELDNAME); |
|
356
|
|
|
} |
|
357
|
|
|
$dbMap->addMxmap($mxMap); |
|
358
|
|
|
$dbMap->save(); |
|
359
|
|
|
$mxMap->save(); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* @param DedicatedMap $map |
|
364
|
|
|
* @return array |
|
365
|
|
|
*/ |
|
366
|
|
|
private function convertMap($map) |
|
367
|
|
|
{ |
|
368
|
|
|
$outMap = (array)$map; |
|
369
|
|
|
$outMap["mapUid"] = $map->uId; |
|
370
|
|
|
|
|
371
|
|
|
return $outMap; |
|
372
|
|
|
|
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Remove special characters from map name |
|
377
|
|
|
* |
|
378
|
|
|
* @param $string |
|
379
|
|
|
* @return mixed |
|
380
|
|
|
*/ |
|
381
|
|
|
protected function cleanString($string) |
|
382
|
|
|
{ |
|
383
|
|
|
return str_replace(array("/", "\\", ":", ".", "?", "*", '"', "|", "<", ">", "'"), "", $string); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* called at eXpansion init |
|
389
|
|
|
* |
|
390
|
|
|
* @return void |
|
391
|
|
|
*/ |
|
392
|
|
|
public function onApplicationInit() |
|
393
|
|
|
{ |
|
394
|
|
|
// Nothin here. |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* called when init is done and callbacks are enabled |
|
399
|
|
|
* |
|
400
|
|
|
* @return void |
|
401
|
|
|
*/ |
|
402
|
|
|
public function onApplicationReady() |
|
403
|
|
|
{ |
|
404
|
|
|
// Nothin here. |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* called when requesting application stop |
|
409
|
|
|
* |
|
410
|
|
|
* @return void |
|
411
|
|
|
*/ |
|
412
|
|
|
public function onApplicationStop() |
|
413
|
|
|
{ |
|
414
|
|
|
// Nothin here. |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.