Completed
Pull Request — master (#307)
by
unknown
04:00
created

MapListDataProvider   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 131
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setStatus() 0 15 4
B updateMapList() 0 25 6
C onMapListModified() 0 46 8
1
<?php
2
3
namespace eXpansion\Framework\GameManiaplanet\DataProviders;
4
5
use eXpansion\Framework\Core\DataProviders\AbstractDataProvider;
6
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
7
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
8
use eXpansion\Framework\Core\Storage\MapStorage;
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 implements StatusAwarePluginInterface
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 Factory
29
     */
30
    protected $factory;
31
32
    /**
33
     * MapListDataProvider constructor.
34
     *
35
     * @param MapStorage $mapStorage
36
     * @param Factory    $factory
37
     */
38
    public function __construct(MapStorage $mapStorage, Factory $factory)
39
    {
40
        $this->mapStorage = $mapStorage;
41
        $this->factory = $factory;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function setStatus($status)
48
    {
49
        if ($status) {
50
            $this->updateMapList();
51
            $currentMap = $this->factory->getConnection()->getCurrentMapInfo();
52
            if ($currentMap) {
53
                $this->mapStorage->setCurrentMap($currentMap);
54
                try {
55
                    $this->mapStorage->setNextMap($this->factory->getConnection()->getNextMapInfo());
56
                } catch (NextMapException $ex) {
57
                    $this->mapStorage->setNextMap($currentMap);
58
                }
59
            }
60
        }
61
    }
62
63
64
    /**
65
     * Update the list of maps in the storage.
66
     */
67
    protected function updateMapList()
68
    {
69
        $start = 0;
70
71
        do {
72
            try {
73
                $maps = $this->factory->getConnection()->getMapList(self::BATCH_SIZE, $start);
74
            } catch (IndexOutOfBoundException $e) {
75
                // This is normal error when we we are trying to find all maps and we are out of bounds.
76
                return;
77
            } catch (NextMapException $ex) {
78
                // this is if no maps defined
79
                return;
80
            }
81
82
            if (!empty($maps)) {
83
                foreach ($maps as $map) {
84
                    $this->mapStorage->addMap($map);
85
                }
86
            }
87
88
            $start += self::BATCH_SIZE;
89
90
        } while (count($maps) == self::BATCH_SIZE);
91
    }
92
93
    /**
94
     * Called when map list is modified.
95
     *
96
     * @param $curMapIndex
97
     * @param $nextMapIndex
98
     * @param $isListModified
99
     *
100
     */
101
    public function onMapListModified($curMapIndex, $nextMapIndex, $isListModified)
102
    {
103
        if ($isListModified) {
104
            $oldMaps = $this->mapStorage->getMaps();
105
106
            $this->mapStorage->resetMapData();
107
            $this->updateMapList();
108
109
            // We will dispatch even only when list is modified. If not we dispatch specific events.
110
            $this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex, $isListModified]);
111
        }
112
113
        try {
114
            $currentMap = $this->factory->getConnection()->getCurrentMapInfo();  // sync better
115
        } catch (\Exception $e) {
116
            // fallback to use map storage
117
            $currentMap = $this->mapStorage->getMapByIndex($curMapIndex);
118
        }
119
120
        // current map can be false if map by index is not found..
121
        if ($currentMap) {
122
            if ($this->mapStorage->getCurrentMap()->uId != $currentMap->uId) {
123
                $previousMap = $this->mapStorage->getCurrentMap();
124
                $this->mapStorage->setCurrentMap($currentMap);
125
126
                $this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]);
127
            }
128
        }
129
130
        try {
131
            $nextMap = $this->factory->getConnection()->getNextMapInfo();  // sync better
132
        } catch (\Exception $e) {
133
            // fallback to use map storage
134
            $nextMap = $this->mapStorage->getMapByIndex($nextMapIndex);
135
136
        }
137
        // next map can be false if map by index is not found..
138
        if ($nextMap) {
139
            if ($this->mapStorage->getNextMap()->uId != $nextMap->uId) {
140
                $previousNextMap = $this->mapStorage->getNextMap();
141
                $this->mapStorage->setNextMap($nextMap);
142
143
                $this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]);
144
            }
145
        }
146
    }
147
}
148