Completed
Pull Request — master (#180)
by De Cramer
03:01
created

MapListDataProvider   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 112
ccs 38
cts 44
cp 0.8636
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
B updateMapList() 0 26 6
B onMapListModified() 0 34 6
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
            } catch (IndexOutOfBoundException $e) {
67
                // 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
                return;
72
            }
73
74 2
            if (!empty($maps)) {
75 2
                foreach ($maps as $map) {
76 2
                    $map = $this->connection->getMapInfo($map->fileName);
77 2
                    $this->mapStorage->addMap($map);
78
                }
79
            }
80
81 2
            $start += self::BATCH_SIZE;
82
83 2
        } while (count($maps) == self::BATCH_SIZE);
84 2
    }
85
86
    /**
87
     * Called when map list is modified.
88
     *
89
     * @param $curMapIndex
90
     * @param $nextMapIndex
91
     * @param $isListModified
92
     *
93
     */
94 1
    public function onMapListModified($curMapIndex, $nextMapIndex, $isListModified)
95
    {
96 1
        if ($isListModified) {
97 1
            $oldMaps = $this->mapStorage->getMaps();
98
99 1
            $this->mapStorage->resetMapData();
100 1
            $this->updateMapList();
101
102
            // We will dispatch even only when list is modified. If not we dispatch specific events.
103 1
            $this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex, $isListModified]);
104
        }
105
106 1
        $currentMap = $this->mapStorage->getMapByIndex($curMapIndex);
107
        // current map can be false if map by index is not found..
108 1
        if ($currentMap) {
109 1
            if ($this->mapStorage->getCurrentMap()->uId != $currentMap->uId) {
110 1
                $previousMap = $this->mapStorage->getCurrentMap();
111 1
                $this->mapStorage->setCurrentMap($currentMap);
112
113 1
                $this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]);
114
            }
115
        }
116
117 1
        $nextMap = $this->mapStorage->getMapByIndex($nextMapIndex);
118
        // next map can be false if map by index is not found..
119 1
        if ($nextMap) {
120 1
            if ($this->mapStorage->getNextMap()->uId != $nextMap->uId) {
121 1
                $previousNextMap = $this->mapStorage->getNextMap();
122 1
                $this->mapStorage->setNextMap($nextMap);
123
124 1
                $this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]);
125
            }
126
        }
127 1
    }
128
}
129