Completed
Pull Request — master (#168)
by De Cramer
03:02
created

MapListDataProvider::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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