Completed
Push — master ( 947184...ad3618 )
by
unknown
9s
created

BestCheckpoints::checkRecordPlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Bundle\WidgetBestCheckpoints\Plugins;
4
5
use eXpansion\Bundle\LocalRecords\DataProviders\Listener\RecordsDataListener;
6
use eXpansion\Bundle\LocalRecords\Model\Record;
7
use eXpansion\Bundle\LocalRecords\Plugins\BaseRecords;
8
use eXpansion\Bundle\WidgetBestCheckpoints\Plugins\Gui\BestCheckpointsWidgetFactory;
9
use eXpansion\Bundle\WidgetBestCheckpoints\Plugins\Gui\UpdaterWidgetFactory;
10
use eXpansion\Framework\Core\Model\UserGroups\Group;
11
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
12
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
13
use eXpansion\Framework\Core\Storage\PlayerStorage;
14
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyMap;
15
use Maniaplanet\DedicatedServer\Structures\Map;
16
17
18
class BestCheckpoints implements StatusAwarePluginInterface, RecordsDataListener, ListenerInterfaceMpLegacyMap
19
{
20
    /**
21
     * @var PlayerStorage
22
     */
23
    private $playerStorage;
24
    /**
25
     * @var BestCheckpointsWidgetFactory
26
     */
27
    private $widget;
28
    /**
29
     * @var Group
30
     */
31
    private $players;
32
    /**
33
     * @var UpdaterWidgetFactory
34
     */
35
    private $updater;
36
    /**
37
     * @var Group
38
     */
39
    private $allPlayers;
40
41
42
    /**
43
     * BestCheckpoints constructor.
44
     *
45
     * @param Factory $factory
0 ignored issues
show
Bug introduced by
There is no parameter named $factory. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     * @param PlayerStorage $playerStorage
47
     * @param BestCheckpointsWidgetFactory $widget
48
     * @param UpdaterWidgetFactory $updater
49
     * @param Group $players
50
     * @param Group $allPlayers
51
     */
52 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
        PlayerStorage $playerStorage,
54
        BestCheckPointsWidgetFactory $widget,
55
        UpdaterWidgetFactory $updater,
56
        Group $players,
57
        Group $allPlayers
58
    ) {
59
        $this->playerStorage = $playerStorage;
60
        $this->widget = $widget;
61
        $this->players = $players;
62
        $this->updater = $updater;
63
        $this->allPlayers = $allPlayers;
64
    }
65
66
    /**
67
     * Set the status of the plugin
68
     *
69
     * @param boolean $status
70
     *
71
     * @return void
72
     */
73
    public function setStatus($status)
74
    {
75
        if ($status) {
76
            $this->widget->create($this->players);
77
            $this->updater->create($this->allPlayers);
78
        } else {
79
            $this->widget->destroy($this->players);
80
            $this->updater->destroy($this->allPlayers);
81
        }
82
    }
83
84
    /**
85
     * Called when local records are loaded.
86
     *
87
     * @param Record[] $records
88
     */
89
    public function onLocalRecordsLoaded($records, BaseRecords $baseRecords)
90
    {
91
        if (!$this->checkRecordPlugin($baseRecords)) {
92
            return;
93
        }
94
95
        if (count($records) > 0) {
96
            $this->updater->setLocalRecord($records[0]->getCheckpoints());
97
        } else {
98
            $this->updater->setLocalRecord([]);
99
        }
100
    }
101
102
    /**
103
     * Called when a player finishes map for the very first time (basically first record).
104
     *
105
     * @param Record   $record
106
     * @param Record[] $records
107
     * @param          $position
108
     */
109
    public function onLocalRecordsFirstRecord(Record $record, $records, $position, BaseRecords $baseRecords)
110
    {
111
        if (!$this->checkRecordPlugin($baseRecords)) {
112
            return;
113
        }
114
115
        $this->updater->setLocalRecord($record->getCheckpoints());
116
    }
117
118
    /**
119
     * Called when a player finishes map and does same time as before.
120
     *
121
     * @param Record   $record
122
     * @param Record   $oldRecord
123
     * @param Record[] $records
124
     */
125
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records, BaseRecords $baseRecords)
126
    {
127
128
    }
129
130
    /**
131
     * Called when a player finishes map with better time and has better position.
132
     *
133
     * @param Record   $record
134
     * @param Record   $oldRecord
135
     * @param Record[] $records
136
     * @param int      $position
137
     * @param int      $oldPosition
138
     */
139 View Code Duplication
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition, BaseRecords $baseRecords)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        if (!$this->checkRecordPlugin($baseRecords)) {
142
            return;
143
        }
144
145
        if ($position == 1) {
146
            $this->updater->setLocalRecord($record->getCheckpoints());
147
        }
148
    }
149
150
    /**
151
     * Called when a player finishes map with better time but keeps same position.
152
     *
153
     * @param Record   $record
154
     * @param Record   $oldRecord
155
     * @param Record[] $records
156
     * @param          $position
157
     */
158 View Code Duplication
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position, BaseRecords $baseRecords)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160
        if (!$this->checkRecordPlugin($baseRecords)) {
161
            return;
162
        }
163
164
        if ($position == 1) {
165
            $this->updater->setLocalRecord($record->getCheckpoints());
166
        }
167
    }
168
169
    /**
170
     * Check if we can use the data for this plugin.
171
     *
172
     * @param BaseRecords $baseRecords
173
     *
174
     * @return bool
175
     */
176
    protected function checkRecordPlugin(BaseRecords $baseRecords)
177
    {
178
        return $baseRecords->getRecordsHandler()->getCurrentNbLaps() == 1;
179
    }
180
181
    /**
182
     * @param Map $map
183
     *
184
     * @return void
185
     */
186
    public function onBeginMap(Map $map)
187
    {
188
189
    }
190
191
    /**
192
     * @param Map $map
193
     *
194
     * @return void
195
     */
196
    public function onEndMap(Map $map)
197
    {
198
        $this->updater->setLocalRecord([]);
199
    }
200
}
201