Completed
Pull Request — master (#148)
by De Cramer
02:37
created

MapListDataProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace eXpansion\Framework\GameManiaplanet\DataProviders;
4
5
use eXpansion\Framework\Core\DataProviders\AbstractDataProvider;
6
use eXpansion\Framework\Core\Storage\MapStorage;
7
use Maniaplanet\DedicatedServer\Connection;
8
use Maniaplanet\DedicatedServer\Xmlrpc\IndexOutOfBoundException;
9
10
/**
11
 * Class MapDataProvider provides information to plugins about what is going on with the maps on the server.
12
 *
13
 * @package eXpansion\Framework\Core\DataProviders
14
 */
15
class MapListDataProvider extends AbstractDataProvider
16
{
17
    /** Size of batch to get maps from the storage. */
18
    const BATCH_SIZE = 500;
19
20
    /**
21
     * @var MapStorage
22
     */
23
    protected $mapStorage;
24
25
    /**
26
     * @var Connection
27
     */
28
    protected $connection;
29
30
    /**
31
     * PlayerDataProvider constructor.
32
     *
33
     * @param MapStorage $mapStorage
34
     * @param Connection $connection
35
     */
36 2
    public function __construct(MapStorage $mapStorage, Connection $connection)
37
    {
38 2
        $this->mapStorage = $mapStorage;
39 2
        $this->connection = $connection;
40
41 2
        $this->updateMapList();
42
43 2
        $currentMap = $this->connection->getCurrentMapInfo();
44 2
        if ($currentMap) {
45 2
            $this->mapStorage->setCurrentMap($currentMap);
46 2
            $this->mapStorage->setNextMap($this->connection->getNextMapInfo());
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 1
            } 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
            if (!empty($maps)) {
66 2
                foreach ($maps as $map) {
67 2
                    $this->mapStorage->addMap($map);
68
                }
69
            }
70
71 2
            $start += self::BATCH_SIZE;
72
73 2
        } while (count($maps) == self::BATCH_SIZE);
74 1
    }
75
76
    /**
77
     * Called when map list is modified.
78
     *
79
     * @param $curMapIndex
80
     * @param $nextMapIndex
81
     * @param $isListModified
82
     *
83
     */
84 1
    public function onMapListModified($curMapIndex, $nextMapIndex, $isListModified)
85
    {
86 1
        if ($isListModified) {
87 1
            $oldMaps = $this->mapStorage->getMaps();
88
89 1
            $this->mapStorage->resetMapData();
90 1
            $this->updateMapList();
91
92
            // We will dispatch even only when list is modified. If not we dispatch specific events.
93 1
            $this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex, $isListModified]);
94
        }
95
96 1
        $currentMap = $this->mapStorage->getMapByIndex($curMapIndex);
97
        // current map can be false if map by index is not found..
98 1
        if ($currentMap) {
99 1
            if ($this->mapStorage->getCurrentMap()->uId != $currentMap->uId) {
100 1
                $previousMap = $this->mapStorage->getCurrentMap();
101 1
                $this->mapStorage->setCurrentMap($currentMap);
102
103 1
                $this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]);
104
            }
105
        }
106
107 1
        $nextMap = $this->mapStorage->getMapByIndex($nextMapIndex);
108
        // next map can be false if map by index is not found..
109 1
        if ($nextMap) {
110 1
            if ($this->mapStorage->getNextMap()->uId != $nextMap->uId) {
111 1
                $previousNextMap = $this->mapStorage->getNextMap();
112 1
                $this->mapStorage->setNextMap($nextMap);
113
114 1
                $this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]);
115
            }
116
        }
117 1
    }
118
}
119