Completed
Pull Request — master (#145)
by
unknown
02:36
created

MapInfoService::onApplicationStop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace eXpansion\Bundle\Maps\Services;
4
5
use eXpansion\Bundle\Maps\Model\Map;
6
use eXpansion\Bundle\Maps\Model\Map\MapTableMap;
7
use eXpansion\Bundle\Maps\Model\MapQuery;
8
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
9
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
10
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyMap;
11
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyMaplist;
12
use eXpansion\Framework\Core\Services\Console;
13
use eXpansion\Framework\Core\Storage\MapStorage;
14
use Maniaplanet\DedicatedServer\Structures\Map as DedicatedMap;
15
use Propel\Runtime\ActiveQuery\Criteria;
16
use Propel\Runtime\Map\TableMap;
17
use Propel\Runtime\Propel;
18
19
class MapInfoService implements ListenerInterfaceExpApplication, ListenerInterfaceMpLegacyMap, ListenerInterfaceMpLegacyMaplist
20
{
21
22
    /**
23
     * @var AdminGroups
24
     */
25
    private $adminGroups;
26
    /**
27
     * @var MapStorage
28
     */
29
    private $mapStorage;
30
    /**
31
     * @var Console
32
     */
33
    private $console;
34
35
    /**
36
     * JukeboxService constructor.
37
     * @param Console $console
38
     * @param MapStorage $mapStorage
39
     * @param AdminGroups $adminGroups
40
     */
41
    public function __construct(Console $console, MapStorage $mapStorage, AdminGroups $adminGroups)
42
    {
43
44
        $this->mapStorage = $mapStorage;
45
        $this->adminGroups = $adminGroups;
46
        $this->console = $console;
47
    }
48
49
    /**
50
     * called at eXpansion init
51
     *
52
     * @return void
53
     */
54
    public function onApplicationInit()
55
    {
56
57
    }
58
59
    /**
60
     * called when init is done and callbacks are enabled
61
     *
62
     * @return void
63
     */
64
    public function onApplicationReady()
65
    {
66
        $this->syncMaps();
67
    }
68
69
    /**
70
     * called when requesting application stop
71
     *
72
     * @return void
73
     */
74
    public function onApplicationStop()
75
    {
76
        // TODO: Implement onApplicationStop() method.
77
    }
78
79
    /**
80
     * @param DedicatedMap $map
81
     *
82
     * @return mixed
83
     */
84
    public function onBeginMap(DedicatedMap $map)
85
    {
86
        // TODO: Implement onBeginMap() method.
87
    }
88
89
    /**
90
     * @param DedicatedMap $map
91
     *
92
     * @return void
93
     */
94
    public function onEndMap(DedicatedMap $map)
95
    {
96
        // TODO: Implement onEndMap() method.
97
    }
98
99
    /**
100
     * @param DedicatedMap[] $oldMaps
101
     * @param string $currentMapUid
102
     * @param string $nextMapUid
103
     * @param bool $isListModified
104
     * @return void
105
     */
106
    public function onMapListModified($oldMaps, $currentMapUid, $nextMapUid, $isListModified)
107
    {
108
        if ($isListModified) {
109
            $this->syncMaps();
110
        }
111
    }
112
113
    public function onExpansionMapChange($currentMap, $previousMap)
114
    {
115
        // TODO: Implement onExpansionMapChange() method.
116
    }
117
118
    public function onExpansionNextMapChange($nextMap, $previousNextMap)
119
    {
120
        // TODO: Implement onExpansionNextMapChange() method.
121
    }
122
123
    protected function syncMaps()
124
    {
125
        $this->console->writeln("Starting Database DedicatedMap Sync...");
126
127
        $mapuids = [];
128
        $allMaps = [];
129
        foreach ($this->mapStorage->getMaps() as $map) {
130
            $mapuids[] = $map->uId;
131
            $allMaps[$map->uId] = $this->convertMap($map);
132
        }
133
134
        $x = 0;
135
        $maps = array_chunk($mapuids, 200);
136
        $mapsInDb = [];
137
138
        foreach ($maps as $uids) {
139
            $this->console->writeln("Processing chunk 1 of ".count($maps)."...");
140
            $mapQuery = new MapQuery();
141
            $mapQuery->filterByMapuid($uids, Criteria::IN);
142
            /** @var \eXpansion\Bundle\Maps\Model\Map[] $data */
143
            $data = $mapQuery->find()->getData();
144
145
            foreach ($data as $map) {
146
                $mapsInDb[] = $map->getMapuid();
147
            }
148
            $x++;
149
        }
150
151
        $con = Propel::getWriteConnection(MapTableMap::DATABASE_NAME);
152
        $con->beginTransaction();
153
154
        $diff = array_diff($mapuids, $mapsInDb);
155
156
        foreach ($diff as $uid) {
157
            $dbmap = new Map();
158
            $dbmap->fromArray($allMaps[$uid], TableMap::TYPE_FIELDNAME);
159
            $dbmap->save();
160
161
        }
162
        $con->commit();
163
164
        $this->console->writeln("Sync done.");
165
166
    }
167
168
    /**
169
     * @param DedicatedMap $map
170
     * @return array
171
     */
172
    private function convertMap($map)
173
    {
174
        $outMap = (array)$map;
175
        $outMap["mapUid"] = $map->uId;
176
177
        return $outMap;
178
179
    }
180
181
182
}
183