Completed
Pull Request — master (#68)
by De Cramer
05:45
created

BaseRecords   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 7
dl 0
loc 199
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A setStatus() 0 14 2
A onStartMapStart() 0 10 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 6
    public function __construct(
51
        RecordHandlerFactory $recordsHandlerFactory,
52
        Group $allPlayersGroup,
53
        MapStorage $mapStorage,
54
        DispatcherInterface $dispatcher,
55
        $eventPrefix
56
    ) {
57 6
        $this->recordsHandler = $recordsHandlerFactory->create();
58 6
        $this->allPlayersGroup = $allPlayersGroup;
59 6
        $this->mapStorage = $mapStorage;
60 6
        $this->eventPrefix = $eventPrefix;
61 6
        $this->dispatcher = $dispatcher;
62 6
    }
63
64
    /**
65
     * Set the status of the plugin
66
     *
67
     * @param boolean $status
68
     *
69
     * @return null
70
     */
71 2
    public function setStatus($status)
72
    {
73 2
        if ($status) {
74 1
            $map = $this->mapStorage->getCurrentMap();
75
76
            // Load firs X records for this map.
77 1
            $this->recordsHandler->loadForMap($map->uId, $this->getNbLaps());
78
79
            // Load time information for remaining players.
80 1
            $this->recordsHandler->loadForPlayers($map->uId, $this->getNbLaps(), $this->allPlayersGroup->getLogins());
81
82 1
            $this->dispatchEvent(['event' => 'loaded', 'records' => $this->recordsHandler->getRecords()]);
83
        }
84 2
    }
85
86
    /**
87
     * Callback sent when the "StartMap" section start.
88
     *
89
     * @param int     $count     Each time this section is played, this number is incremented by one
90
     * @param int     $time      Server time when the callback was sent
91
     * @param boolean $restarted true if the map was restarted, false otherwise
92
     * @param Map     $map       Map started with.
93
     *
94
     * @return mixed
95
     */
96 1
    public function onStartMapStart($count, $time, $restarted, Map $map)
97
    {
98
        // Load firs X records for this map.
99 1
        $this->recordsHandler->loadForMap($map->uId, $this->getNbLaps());
100
101
        // Load time information for remaining players.
102 1
        $this->recordsHandler->loadForPlayers($map->uId, $this->getNbLaps(), $this->allPlayersGroup->getLogins());
103
104 1
        $this->dispatchEvent(['event' => 'loaded', 'records' => $this->recordsHandler->getRecords()]);
105 1
    }
106
107
    /**
108
     * @param Player $player
109
     */
110 1
    public function onPlayerConnect(Player $player)
111
    {
112 1
        $this->recordsHandler->loadForPlayers($this->mapStorage->getCurrentMap()->uId, [1], [$player->getLogin()]);
113 1
    }
114
115
    /**
116
     * Callback sent when the "EndMap" section end.
117
     *
118
     * @param int     $count     Each time this section is played, this number is incremented by one
119
     * @param int     $time      Server time when the callback was sent
120
     * @param boolean $restarted true if the map was restarted, false otherwise
121
     * @param Map     $map       Map started with.
122
     *
123
     * @return mixed
124
     */
125 1
    public function onEndMapEnd($count, $time, $restarted, Map $map)
126
    {
127
        // Nothing to do
128 1
    }
129
130
    /**
131
     * Callback sent when the "StartMatch" section start.
132
     *
133
     * @param int $count Each time this section is played, this number is incremented by one
134
     * @param int $time  Server time when the callback was sent
135
     *
136
     * @return mixed
137
     */
138 1
    public function onStartMatchStart($count, $time)
139
    {
140
        // Nothing to do.
141 1
    }
142
143
    /**
144
     * Callback sent when the "StartMatch" section end.
145
     *
146
     * @param int $count Each time this section is played, this number is incremented by one
147
     * @param int $time  Server time when the callback was sent
148
     *
149
     * @return mixed
150
     */
151 1
    public function onStartMatchEnd($count, $time)
152
    {
153 1
        $this->recordsHandler->save();
154 1
    }
155
156
    /**
157
     * Callback sent when the "StartMap" section end.
158
     *
159
     * @param int     $count     Each time this section is played, this number is incremented by one
160
     * @param int     $time      Server time when the callback was sent
161
     * @param boolean $restarted true if the map was restarted, false otherwise
162
     * @param Map     $map       Map started with.
163
     *
164
     * @return mixed
165
     */
166 1
    public function onStartMapEnd($count, $time, $restarted, Map $map)
167
    {
168
        // Nothing to do.
169 1
    }
170
171
    /**
172
     * Callback sent when the "EndMap" section start.
173
     *
174
     * @param int     $count     Each time this section is played, this number is incremented by one
175
     * @param int     $time      Server time when the callback was sent
176
     * @param boolean $restarted true if the map was restarted, false otherwise
177
     * @param Map     $map       Map started with.
178
     *
179
     * @return mixed
180
     */
181 1
    public function onEndMapStart($count, $time, $restarted, Map $map)
182
    {
183
        // Nothing to do.
184 1
    }
185
186 1
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
187
    {
188
        // Nothing to do.
189 1
    }
190
191 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
192
    {
193
        // Nothing to do.
194 1
    }
195
196 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
197
    {
198
        // Nothing to do.
199 1
    }
200
201
    /**
202
     * Dispatch a record event.
203
     *
204
     * @param $eventData
205
     */
206 3
    protected function dispatchEvent($eventData)
207
    {
208 3
        $event = $this->eventPrefix . '.' . $eventData['event'];
209 3
        unset($eventData['event']);
210
211 3
        $this->dispatcher->dispatch($event, [$eventData]);
212 3
    }
213
214
    /**
215
     * Get number of laps
216
     *
217
     * @return int
218
     */
219 2
    protected function getNbLaps()
220
    {
221 2
        return 1;
222
    }
223
}