|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Core\DataProviders; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Core\Storage\MapStorage; |
|
6
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
7
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\IndexOutOfBoundException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class MapDataProvider provides information to plugins about what is going on with the maps on the server. |
|
11
|
|
|
* |
|
12
|
|
|
* @package eXpansion\Core\DataProviders |
|
13
|
|
|
*/ |
|
14
|
|
|
class MapDataProvider extends AbstractDataProvider |
|
15
|
|
|
{ |
|
16
|
|
|
/** Size of batch to get maps from the storage. */ |
|
17
|
|
|
const BATCH_SIZE = 500; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var MapStorage |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $mapStorage; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Connection |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $connection; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* PlayerDataProvider constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param MapStorage $mapStorage |
|
33
|
|
|
* @param Connection $connection |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function __construct(MapStorage $mapStorage, Connection $connection) |
|
36
|
|
|
{ |
|
37
|
3 |
|
$this->mapStorage = $mapStorage; |
|
38
|
3 |
|
$this->connection = $connection; |
|
39
|
3 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Called when eXpansion is started. |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function onRun() |
|
45
|
|
|
{ |
|
46
|
2 |
|
$this->updateMapList(); |
|
47
|
|
|
|
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Update the list of maps in the storage. |
|
52
|
|
|
*/ |
|
53
|
2 |
|
protected function updateMapList() |
|
54
|
|
|
{ |
|
55
|
2 |
|
$start = 0; |
|
56
|
|
|
|
|
57
|
|
|
do { |
|
58
|
|
|
try { |
|
59
|
2 |
|
$maps = $this->connection->getMapList(self::BATCH_SIZE, $start); |
|
60
|
2 |
|
} catch (IndexOutOfBoundException $e) { |
|
61
|
|
|
// This is normal error when we we are trying to find all maps and we are out of bounds. |
|
62
|
1 |
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
foreach ($maps as $map) { |
|
66
|
2 |
|
$this->mapStorage->addMap($map); |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
$start += self::BATCH_SIZE; |
|
70
|
|
|
|
|
71
|
2 |
|
} while(count($maps) == self::BATCH_SIZE); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Called when map list is modified. |
|
76
|
|
|
* |
|
77
|
|
|
* @param $curMapIndex |
|
78
|
|
|
* @param $nextMapIndex |
|
79
|
|
|
* @param $isListModified |
|
80
|
|
|
* |
|
81
|
|
|
*/ |
|
82
|
1 |
|
function onMapListModified($curMapIndex, $nextMapIndex, $isListModified) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
1 |
|
if ($isListModified) { |
|
85
|
1 |
|
$oldMaps = $this->mapStorage->getMaps(); |
|
86
|
|
|
|
|
87
|
1 |
|
$this->mapStorage->resetMapData(); |
|
88
|
1 |
|
$this->updateMapList(); |
|
89
|
|
|
|
|
90
|
|
|
// We will dispatch even only when list is modified. If not we dispatch specific events. |
|
91
|
1 |
|
$this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex]); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$currentMap = $this->mapStorage->getMap($curMapIndex); |
|
95
|
1 |
|
if ($this->mapStorage->getCurrentMap()->uId != $curMapIndex) { |
|
96
|
1 |
|
$previousMap = $this->mapStorage->getCurrentMap(); |
|
97
|
1 |
|
$this->mapStorage->setCurrentMap($currentMap); |
|
98
|
|
|
|
|
99
|
1 |
|
$this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]); |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
$nextMap = $this->mapStorage->getMap($nextMapIndex); |
|
103
|
1 |
|
if ($this->mapStorage->getNextMap()->uId != $nextMapIndex) { |
|
104
|
1 |
|
$previousNextMap = $this->mapStorage->getNextMap(); |
|
105
|
1 |
|
$this->mapStorage->setNextMap($nextMap); |
|
106
|
|
|
|
|
107
|
1 |
|
$this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]); |
|
108
|
1 |
|
} |
|
109
|
1 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.