Completed
Pull Request — master (#68)
by De Cramer
03:16
created

BaseRecords   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 210
ccs 46
cts 48
cp 0.9583
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getRecordsHandler() 0 4 1
A setStatus() 0 14 2
A onStartMapStart() 0 11 1
A onPlayerConnect() 0 4 1
A onEndMapEnd() 0 4 1
A onStartMatchStart() 0 4 1
A onStartMatchEnd() 0 4 1
A onStartMapEnd() 0 4 1
A onEndMapStart() 0 4 1
A onPlayerDisconnect() 0 4 1
A onPlayerInfoChanged() 0 4 1
A onPlayerAlliesChanged() 0 4 1
A dispatchEvent() 0 7 1
A getNbLaps() 0 4 1
1
<?php
2
3
namespace eXpansion\Bundle\LocalRecords\Plugins;
4
5
use eXpansion\Bundle\LocalRecords\Services\RecordHandler;
6
use eXpansion\Bundle\LocalRecords\Services\RecordHandlerFactory;
7
use eXpansion\Framework\Core\DataProviders\Listener\PlayerDataListenerInterface;
8
use eXpansion\Framework\Core\Model\UserGroups\Group;
9
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
10
use eXpansion\Framework\Core\Services\Application\DispatcherInterface;
11
use eXpansion\Framework\Core\Storage\Data\Player;
12
use eXpansion\Framework\Core\Storage\MapStorage;
13
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\BaseDataListenerInterface as MpBaseDataListenerInterface;
14
use eXpansion\Framework\GameTrackmania\DataProviders\Listener\BaseDataListenerInterface as TmBaseDataListenerInterface;
15
use Maniaplanet\DedicatedServer\Structures\Map;
16
17
/**
18
 * Class RaceRecords
19
 *
20
 * ADD status aware interface and load on activation.
21
 *
22
 * @package eXpansion\Bundle\LocalRecords\Plugins;
23
 * @author  oliver de Cramer <[email protected]>
24
 */
25
class BaseRecords implements MpBaseDataListenerInterface, PlayerDataListenerInterface, StatusAwarePluginInterface
26
{
27
    /** @var  RecordHandler */
28
    protected $recordsHandler;
29
30
    /** @var Group */
31
    protected $allPlayersGroup;
32
33
    /** @var MapStorage */
34
    protected $mapStorage;
35
36
    /** @var string */
37
    protected $eventPrefix;
38
39
    /** @var DispatcherInterface */
40
    protected $dispatcher;
41
42
    /**
43
     * BaseRecords constructor.
44
     *
45
     * @param RecordHandlerFactory $recordsHandlerFactory
46
     * @param Group                $allPlayersGroup
47
     * @param MapStorage           $mapStorage
48
     * @param                      $eventPrefix
49
     */
50 8
    public function __construct(
51
        RecordHandlerFactory $recordsHandlerFactory,
52
        Group $allPlayersGroup,
53
        MapStorage $mapStorage,
54
        DispatcherInterface $dispatcher,
55
        $eventPrefix
56
    ) {
57 8
        $this->recordsHandler = $recordsHandlerFactory->create();
58 8
        $this->allPlayersGroup = $allPlayersGroup;
59 8
        $this->mapStorage = $mapStorage;
60 8
        $this->eventPrefix = $eventPrefix;
61 8
        $this->dispatcher = $dispatcher;
62 8
    }
63
64
    /**
65
     * Get the current record handler.
66
     *
67
     * @return RecordHandler|mixed
68
     */
69
    public function getRecordsHandler()
70
    {
71
        return $this->recordsHandler;
72
    }
73
74
    /**
75
     * Set the status of the plugin
76
     *
77
     * @param boolean $status
78
     *
79
     * @return null
80
     */
81 2
    public function setStatus($status)
82
    {
83 2
        if ($status) {
84 1
            $map = $this->mapStorage->getCurrentMap();
85
86
            // Load firs X records for this map.
87 1
            $this->recordsHandler->loadForMap($map->uId, $this->getNbLaps());
88
89
            // Load time information for remaining players.
90 1
            $this->recordsHandler->loadForPlayers($map->uId, $this->getNbLaps(), $this->allPlayersGroup->getLogins());
91
92 1
            $this->dispatchEvent(['event' => 'loaded', 'records' => $this->recordsHandler->getRecords()]);
93
        }
94 2
    }
95
96
    /**
97
     * Callback sent when the "StartMap" section start.
98
     *
99
     * @param int     $count     Each time this section is played, this number is incremented by one
100
     * @param int     $time      Server time when the callback was sent
101
     * @param boolean $restarted true if the map was restarted, false otherwise
102
     * @param Map     $map       Map started with.
103
     *
104
     * @return mixed
105
     */
106 1
    public function onStartMapStart($count, $time, $restarted, Map $map)
107
    {
108
        // Load firs X records for this map.
109 1
        $this->recordsHandler->loadForMap($map->uId, $this->getNbLaps());
110
111
        // Load time information for remaining players.
112 1
        $this->recordsHandler->loadForPlayers($map->uId, $this->getNbLaps(), $this->allPlayersGroup->getLogins());
113
114
        // Let others know that records information is now available.
115 1
        $this->dispatchEvent(['event' => 'loaded', 'records' => $this->recordsHandler->getRecords()]);
116 1
    }
117
118
    /**
119
     * @param Player $player
120
     */
121 1
    public function onPlayerConnect(Player $player)
122
    {
123 1
        $this->recordsHandler->loadForPlayers($this->mapStorage->getCurrentMap()->uId, [1], [$player->getLogin()]);
124 1
    }
125
126
    /**
127
     * Callback sent when the "EndMap" section end.
128
     *
129
     * @param int     $count     Each time this section is played, this number is incremented by one
130
     * @param int     $time      Server time when the callback was sent
131
     * @param boolean $restarted true if the map was restarted, false otherwise
132
     * @param Map     $map       Map started with.
133
     *
134
     * @return mixed
135
     */
136 1
    public function onEndMapEnd($count, $time, $restarted, Map $map)
137
    {
138
        // Nothing to do
139 1
    }
140
141
    /**
142
     * Callback sent when the "StartMatch" section start.
143
     *
144
     * @param int $count Each time this section is played, this number is incremented by one
145
     * @param int $time  Server time when the callback was sent
146
     *
147
     * @return mixed
148
     */
149 1
    public function onStartMatchStart($count, $time)
150
    {
151
        // Nothing to do.
152 1
    }
153
154
    /**
155
     * Callback sent when the "StartMatch" section end.
156
     *
157
     * @param int $count Each time this section is played, this number is incremented by one
158
     * @param int $time  Server time when the callback was sent
159
     *
160
     * @return mixed
161
     */
162 1
    public function onStartMatchEnd($count, $time)
163
    {
164 1
        $this->recordsHandler->save();
165 1
    }
166
167
    /**
168
     * Callback sent when the "StartMap" section end.
169
     *
170
     * @param int     $count     Each time this section is played, this number is incremented by one
171
     * @param int     $time      Server time when the callback was sent
172
     * @param boolean $restarted true if the map was restarted, false otherwise
173
     * @param Map     $map       Map started with.
174
     *
175
     * @return mixed
176
     */
177 1
    public function onStartMapEnd($count, $time, $restarted, Map $map)
178
    {
179
        // Nothing to do.
180 1
    }
181
182
    /**
183
     * Callback sent when the "EndMap" section start.
184
     *
185
     * @param int     $count     Each time this section is played, this number is incremented by one
186
     * @param int     $time      Server time when the callback was sent
187
     * @param boolean $restarted true if the map was restarted, false otherwise
188
     * @param Map     $map       Map started with.
189
     *
190
     * @return mixed
191
     */
192 1
    public function onEndMapStart($count, $time, $restarted, Map $map)
193
    {
194
        // Nothing to do.
195 1
    }
196
197 1
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
198
    {
199
        // Nothing to do.
200 1
    }
201
202 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
203
    {
204
        // Nothing to do.
205 1
    }
206
207 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
208
    {
209
        // Nothing to do.
210 1
    }
211
212
    /**
213
     * Dispatch a record event.
214
     *
215
     * @param $eventData
216
     */
217 4
    protected function dispatchEvent($eventData)
218
    {
219 4
        $event = $this->eventPrefix . '.' . $eventData['event'];
220 4
        unset($eventData['event']);
221
222 4
        $this->dispatcher->dispatch($event, [$eventData]);
223 4
    }
224
225
    /**
226
     * Get number of laps
227
     *
228
     * @return int
229
     */
230 2
    protected function getNbLaps()
231
    {
232 2
        return 1;
233
    }
234
}