Completed
Pull Request — master (#167)
by
unknown
03:24
created

MapRatingsService::onStartMapStart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 4
crap 6
1
<?php
2
3
namespace eXpansion\Bundle\LocalMapRatings\Services;
4
5
use eXpansion\Bundle\LocalMapRatings\Model\Map\MapratingTableMap;
6
use eXpansion\Bundle\LocalMapRatings\Model\Maprating;
7
use eXpansion\Bundle\LocalMapRatings\Model\MapratingQueryBuilder;
8
use eXpansion\Bundle\LocalRecords\Plugins\ChatNotification;
9
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
10
use eXpansion\Framework\Core\Services\Application\Dispatcher;
11
use eXpansion\Framework\Core\Storage\Data\Player;
12
use eXpansion\Framework\Core\Storage\MapStorage;
13
use eXpansion\Framework\Core\Storage\PlayerStorage;
14
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyChat;
15
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap;
16
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMatch;
17
use eXpansion\Framework\PlayersBundle\Model\PlayerQueryBuilder;
18
use Maniaplanet\DedicatedServer\Structures\Map;
19
use Propel\Runtime\Propel;
20
21
22
class MapRatingsService implements ListenerInterfaceExpApplication, ListenerInterfaceMpScriptMatch,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
Coding Style introduced by
Only one interface may be specified per line in a multi-line implements declaration
Loading history...
23
    ListenerInterfaceMpScriptMap, ListenerInterfaceMpLegacyChat
0 ignored issues
show
Coding Style introduced by
Only one interface may be specified per line in a multi-line implements declaration
Loading history...
24
{
25
26
    /** @var Maprating[] */
27
    protected $changedRatings;
28
29
    /** @var Maprating[] */
30
    protected $ratingsPerPlayer = [];
31
    /**
32
     * @var MapStorage
33
     */
34
    private $mapStorage;
35
    /**
36
     * @var PlayerStorage
37
     */
38
    private $playerStorage;
39
40
    /**
41
     * @var MapratingQueryBuilder
42
     */
43
    private $mapratingQueryBuilder;
44
    /**
45
     * @var PlayerQueryBuilder
46
     */
47
    private $playerQueryBuilder;
48
    /**
49
     * @var ChatNotification
50
     */
51
    private $chatNotification;
52
    /**
53
     * @var Dispatcher
54
     */
55
    private $dispatcher;
56
57
    /**
58
     * MapRatingService constructor.
59
     * @param MapStorage            $mapStorage
60
     * @param PlayerStorage         $playerStorage
61
     * @param MapratingQueryBuilder $mapratingQueryBuilder
62
     * @param PlayerQueryBuilder    $playerQueryBuilder
63
     * @param ChatNotification      $chatNotification
64
     * @param Dispatcher            $dispatcher
65
     */
66
    public function __construct(
67
        MapStorage $mapStorage,
68
        PlayerStorage $playerStorage,
69
        MapratingQueryBuilder $mapratingQueryBuilder,
70
        PlayerQueryBuilder $playerQueryBuilder,
71
        ChatNotification $chatNotification,
72
        Dispatcher $dispatcher
73
74
    ) {
75
        $this->mapStorage = $mapStorage;
76
        $this->playerStorage = $playerStorage;
77
        $this->mapratingQueryBuilder = $mapratingQueryBuilder;
78
        $this->playerQueryBuilder = $playerQueryBuilder;
79
        $this->chatNotification = $chatNotification;
80
        $this->dispatcher = $dispatcher;
81
    }
82
83
    /**
84
     * @param Map $map
85
     * @throws \Propel\Runtime\Exception\PropelException
86
     *
87
     * @return void
88
     */
89
    public function load(Map $map)
90
    {
91
        $this->changedRatings = [];
92
        $this->ratingsPerPlayer = [];
93
        MapratingTableMap::clearInstancePool();
94
95
        /** @var Maprating[] $ratings */
96
        $ratings = $this->mapratingQueryBuilder->getRatingsForMap($map);
97
98
        foreach ($ratings as $rating) {
99
            $this->ratingsPerPlayer[$rating->getLogin()] = $rating;
100
        }
101
102
        $this->dispatcher->dispatch("expansion.mapratings.loaded", [$this->ratingsPerPlayer]);
103
    }
104
105
106
    public function save()
107
    {
108
109
        $con = Propel::getWriteConnection(MapratingTableMap::DATABASE_NAME);
110
        $con->beginTransaction();
111
112
        foreach ($this->changedRatings as $rating) {
113
            $rating->save();
114
        }
115
        $con->commit();
116
        $this->changedRatings = [];
117
118
        MapratingTableMap::clearInstancePool();
119
    }
120
121
    /**
122
     * @param string $login
123
     * @param int    $score
124
     */
125
    public function changeRating($login, $score)
126
    {
127
        $rating = $this->getRating($login);
128
        $rating->setScore($score);
129
130
        $this->changedRatings[$login] = $rating;
131
        $this->ratingsPerPlayer[$login] = $rating;
132
133
        $this->dispatcher->dispatch("expansion.mapratings.changed", [$this->ratingsPerPlayer]);
134
    }
135
136
137
    private function getRating($login)
138
    {
139
        if (array_key_exists($login, $this->changedRatings)) {
140
            $rating = $this->changedRatings[$login];
141
        } else {
142
            if (array_key_exists($login, $this->ratingsPerPlayer)) {
143
                $rating = $this->ratingsPerPlayer[$login];
144
            } else {
145
                $rating = new Maprating();
146
                $rating->setLogin($login);
147
                $rating->setMapuid($this->mapStorage->getCurrentMap()->uId);
148
            }
149
        }
150
151
        return $rating;
152
    }
153
154
    /**
155
     * Called when a player chats.
156
     *
157
     * @param Player $player
158
     * @param        $text
159
     *
160
     * @return void
161
     */
162
    public function onPlayerChat(Player $player, $text)
163
    {
164
        if ($player->getPlayerId() == 0) {
165
            return;
166
        }
167
168
        if ($player->getPlayerId() != 0 && substr($text, 0, 1) != "/") {
169
            if ($text === "++") {
170
                $this->changeRating($player->getLogin(), 1);
171
172
                return;
173
            }
174
175
            if ($text === "--") {
176
                $this->changeRating($player->getLogin(), -1);
177
178
                return;
179
            }
180
        }
181
    }
182
183
    /**
184
     * called at eXpansion init
185
     *
186
     * @return void
187
     */
188
    public function onApplicationInit()
189
    {
190
191
    }
192
193
    /**
194
     * called when init is done and callbacks are enabled
195
     *
196
     * @return void
197
     */
198
    public function onApplicationReady()
199
    {
200
        $this->load($this->mapStorage->getCurrentMap());
201
    }
202
203
    /**
204
     * called when requesting application stop
205
     *
206
     * @return void
207
     */
208
    public function onApplicationStop()
209
    {
210
211
    }
212
213
    /**
214
     * @return Maprating[]
215
     */
216
    public function getRatingsPerPlayer(): array
217
    {
218
        return $this->ratingsPerPlayer;
219
    }
220
221
    /**
222
     * Callback sent when the "StartMatch" section start.
223
     *
224
     * @param int $count Each time this section is played, this number is incremented by one
225
     * @param int $time  Server time when the callback was sent
226
     *
227
     * @return void
228
     */
229
    public function onStartMatchStart($count, $time)
230
    {
231
232
    }
233
234
    /**
235
     * Callback sent when the "StartMatch" section end.
236
     *
237
     * @param int $count Each time this section is played, this number is incremented by one
238
     * @param int $time  Server time when the callback was sent
239
     *
240
     * @return void
241
     */
242
    public function onStartMatchEnd($count, $time)
243
    {
244
        $this->load($this->mapStorage->getCurrentMap());
245
    }
246
247
    /**
248
     * Callback sent when the "EndMatch" section start.
249
     *
250
     * @param int $count Each time this section is played, this number is incremented by one
251
     * @param int $time  Server time when the callback was sent
252
     *
253
     * @return void
254
     */
255
    public function onEndMatchStart($count, $time)
256
    {
257
258
    }
259
260
    /**
261
     * Callback sent when the "EndMatch" section end.
262
     *
263
     * @param int $count Each time this section is played, this number is incremented by one
264
     * @param int $time  Server time when the callback was sent
265
     *
266
     * @return void
267
     */
268
    public function onEndMatchEnd($count, $time)
269
    {
270
        $this->save();
271
    }
272
273
    /**
274
     * Callback sent when the "StartTurn" section start.
275
     *
276
     * @param int $count Each time this section is played, this number is incremented by one
277
     * @param int $time  Server time when the callback was sent
278
     *
279
     * @return void
280
     */
281
    public function onStartTurnStart($count, $time)
282
    {
283
        // TODO: Implement onStartTurnStart() method.
284
    }
285
286
    /**
287
     * Callback sent when the "StartTurn" section end.
288
     *
289
     * @param int $count Each time this section is played, this number is incremented by one
290
     * @param int $time  Server time when the callback was sent
291
     *
292
     * @return void
293
     */
294
    public function onStartTurnEnd($count, $time)
295
    {
296
        // TODO: Implement onStartTurnEnd() method.
297
    }
298
299
    /**
300
     * Callback sent when the "EndMatch" section start.
301
     *
302
     * @param int $count Each time this section is played, this number is incremented by one
303
     * @param int $time  Server time when the callback was sent
304
     *
305
     * @return void
306
     */
307
    public function onEndTurnStart($count, $time)
308
    {
309
        // TODO: Implement onEndTurnStart() method.
310
    }
311
312
    /**
313
     * Callback sent when the "EndMatch" section end.
314
     *
315
     * @param int $count Each time this section is played, this number is incremented by one
316
     * @param int $time  Server time when the callback was sent
317
     *
318
     * @return void
319
     */
320
    public function onEndTurnEnd($count, $time)
321
    {
322
        // TODO: Implement onEndTurnEnd() method.
323
    }
324
325
    /**
326
     * Callback sent when the "StartRound" section start.
327
     *
328
     * @param int $count Each time this section is played, this number is incremented by one
329
     * @param int $time  Server time when the callback was sent
330
     *
331
     * @return void
332
     */
333
    public function onStartRoundStart($count, $time)
334
    {
335
        // TODO: Implement onStartRoundStart() method.
336
    }
337
338
    /**
339
     * Callback sent when the "StartRound" section end.
340
     *
341
     * @param int $count Each time this section is played, this number is incremented by one
342
     * @param int $time  Server time when the callback was sent
343
     *
344
     * @return void
345
     */
346
    public function onStartRoundEnd($count, $time)
347
    {
348
        // TODO: Implement onStartRoundEnd() method.
349
    }
350
351
    /**
352
     * Callback sent when the "EndMatch" section start.
353
     *
354
     * @param int $count Each time this section is played, this number is incremented by one
355
     * @param int $time  Server time when the callback was sent
356
     *
357
     * @return void
358
     */
359
    public function onEndRoundStart($count, $time)
360
    {
361
        // TODO: Implement onEndRoundStart() method.
362
    }
363
364
    /**
365
     * Callback sent when the "EndMatch" section end.
366
     *
367
     * @param int $count Each time this section is played, this number is incremented by one
368
     * @param int $time  Server time when the callback was sent
369
     *
370
     * @return void
371
     */
372
    public function onEndRoundEnd($count, $time)
373
    {
374
        // TODO: Implement onEndRoundEnd() method.
375
    }
376
377
378
    /**
379
     * Callback sent when the "StartMap" section start.
380
     *
381
     * @param int     $count     Each time this section is played, this number is incremented by one
382
     * @param int     $time      Server time when the callback was sent
383
     * @param boolean $restarted true if the map was restarted, false otherwise
384
     * @param Map     $map       Map started with.
385
     *
386
     * @return void
387
     */
388
    public function onStartMapStart($count, $time, $restarted, Map $map)
389
    {
390
        if ($restarted) {
391
            $this->save();
392
        }
393
    }
394
395
    /**
396
     * Callback sent when the "StartMap" section end.
397
     *
398
     * @param int     $count     Each time this section is played, this number is incremented by one
399
     * @param int     $time      Server time when the callback was sent
400
     * @param boolean $restarted true if the map was restarted, false otherwise
401
     * @param Map     $map       Map started with.
402
     *
403
     * @return void
404
     */
405
    public function onStartMapEnd($count, $time, $restarted, Map $map)
406
    {
407
        // TODO: Implement onStartMapEnd() method.
408
    }
409
410
    /**
411
     * Callback sent when the "EndMap" section start.
412
     *
413
     * @param int     $count     Each time this section is played, this number is incremented by one
414
     * @param int     $time      Server time when the callback was sent
415
     * @param boolean $restarted true if the map was restarted, false otherwise
416
     * @param Map     $map       Map started with.
417
     *
418
     * @return void
419
     */
420
    public function onEndMapStart($count, $time, $restarted, Map $map)
421
    {
422
        $this->save();
423
    }
424
425
    /**
426
     * Callback sent when the "EndMap" section end.
427
     *
428
     * @param int     $count     Each time this section is played, this number is incremented by one
429
     * @param int     $time      Server time when the callback was sent
430
     * @param boolean $restarted true if the map was restarted, false otherwise
431
     * @param Map     $map       Map started with.
432
     *
433
     * @return void
434
     */
435
    public function onEndMapEnd($count, $time, $restarted, Map $map)
436
    {
437
        // TODO: Implement onEndMapEnd() method.
438
    }
439
}
440