Completed
Pull Request — master (#175)
by
unknown
02:55
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.054

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
crap 3.054
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
     * @param Connection $connection
37
     */
38 2
    public function __construct(MapStorage $mapStorage, Connection $connection)
39
    {
40 2
        $this->mapStorage = $mapStorage;
41 2
        $this->connection = $connection;
42
43 2
        $this->updateMapList();
44
45 2
        $currentMap = $this->connection->getCurrentMapInfo();
46 2
        if ($currentMap) {
47 2
            $this->mapStorage->setCurrentMap($currentMap);
48
            try {
49 2
                $this->mapStorage->setNextMap($this->connection->getNextMapInfo());
50
            } catch (NextMapException $ex) {
51
                $this->mapStorage->setNextMap($currentMap);
52
            }
53
        }
54 2
    }
55
56
    /**
57
     * Update the list of maps in the storage.
58
     */
59 2
    protected function updateMapList()
60
    {
61 2
        $start = 0;
62
63
        do {
64
            try {
65 2
                $maps = $this->connection->getMapList(self::BATCH_SIZE, $start);
66 1
            } catch (IndexOutOfBoundException $e) {
67
                // This is normal error when we we are trying to find all maps and we are out of bounds.
68 1
                return;
69
            } catch (NextMapException $ex) {
70
                // this is if no maps defined
71
                return;
72
            }
73
74 2
            if (!empty($maps)) {
75 2
                foreach ($maps as $map) {
76 2
                    $this->mapStorage->addMap($map);
77
                }
78
            }
79
80 2
            $start += self::BATCH_SIZE;
81
82 2
        } while (count($maps) == self::BATCH_SIZE);
83 1
    }
84
85
    /**
86
     * Called when map list is modified.
87
     *
88
     * @param $curMapIndex
89
     * @param $nextMapIndex
90
     * @param $isListModified
91
     *
92
     */
93 1
    public function onMapListModified($curMapIndex, $nextMapIndex, $isListModified)
94
    {
95 1
        if ($isListModified) {
96 1
            $oldMaps = $this->mapStorage->getMaps();
97
98 1
            $this->mapStorage->resetMapData();
99 1
            $this->updateMapList();
100
101
            // We will dispatch even only when list is modified. If not we dispatch specific events.
102 1
            $this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex, $isListModified]);
103
        }
104
105 1
        $currentMap = $this->mapStorage->getMapByIndex($curMapIndex);
106
        // current map can be false if map by index is not found..
107 1
        if ($currentMap) {
108 1
            if ($this->mapStorage->getCurrentMap()->uId != $currentMap->uId) {
109 1
                $previousMap = $this->mapStorage->getCurrentMap();
110 1
                $this->mapStorage->setCurrentMap($currentMap);
111
112 1
                $this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]);
113
            }
114
        }
115
116 1
        $nextMap = $this->mapStorage->getMapByIndex($nextMapIndex);
117
        // next map can be false if map by index is not found..
118 1
        if ($nextMap) {
119 1
            if ($this->mapStorage->getNextMap()->uId != $nextMap->uId) {
120 1
                $previousNextMap = $this->mapStorage->getNextMap();
121 1
                $this->mapStorage->setNextMap($nextMap);
122
123 1
                $this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]);
124
            }
125
        }
126 1
    }
127
}
128