Completed
Pull Request — master (#210)
by
unknown
08:31
created

BestCheckpoints::onLocalRecordsLoaded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 1
f 0
ccs 0
cts 6
cp 0
cc 2
eloc 6
nc 2
nop 1
crap 6
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\WidgetBestCheckpoints\Plugins\Gui\BestCheckpointsWidgetFactory;
8
use eXpansion\Bundle\WidgetBestCheckpoints\Plugins\Gui\UpdaterWidgetFactory;
9
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
10
use eXpansion\Framework\Core\Model\UserGroups\Group;
11
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
12
use eXpansion\Framework\Core\Storage\PlayerStorage;
13
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyMap;
14
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMatch;
15
use Maniaplanet\DedicatedServer\Connection;
16
use Maniaplanet\DedicatedServer\Structures\Map;
17
18
19
class BestCheckpoints implements StatusAwarePluginInterface, RecordsDataListener,ListenerInterfaceMpLegacyMap
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "ListenerInterfaceMpLegacyMap"; 0 found
Loading history...
20
{
21
    /** @var Connection */
22
    protected $connection;
23
    /**
24
     * @var PlayerStorage
25
     */
26
    private $playerStorage;
27
    /**
28
     * @var BestCheckpointsWidgetFactory
29
     */
30
    private $widget;
31
    /**
32
     * @var Group
33
     */
34
    private $players;
35
    /**
36
     * @var UpdaterWidgetFactory
37
     */
38
    private $updater;
39
    /**
40
     * @var Group
41
     */
42
    private $allPlayers;
43
44
45
    /**
46
     * Debug constructor.
47
     *
48
     * @param Connection                   $connection
49
     * @param PlayerStorage                $playerStorage
50
     * @param BestCheckPointsWidgetFactory $widget
51
     * @param UpdaterWidgetFactory         $updater
52
     * @param Group                        $players
53
     * @param Group                        $allPlayers
54
     */
55
    public function __construct(
56
        Connection $connection,
57
        PlayerStorage $playerStorage,
58
        BestCheckPointsWidgetFactory $widget,
59
        UpdaterWidgetFactory $updater,
60
        Group $players,
61
        Group $allPlayers
62
    ) {
63
        $this->connection = $connection;
64
        $this->playerStorage = $playerStorage;
65
        $this->widget = $widget;
66
        $this->players = $players;
67
        $this->updater = $updater;
68
        $this->allPlayers = $allPlayers;
69
    }
70
71
    /**
72
     * Set the status of the plugin
73
     *
74
     * @param boolean $status
75
     *
76
     * @return void
77
     */
78
    public function setStatus($status)
79
    {
80
        if ($status) {
81
            $this->widget->create($this->players);
82
            $this->updater->create($this->allPlayers);
83
        } else {
84
            $this->widget->destroy($this->players);
85
            $this->updater->destroy($this->allPlayers);
86
        }
87
    }
88
89
    /**
90
     * Called when local records are loaded.
91
     *
92
     * @param Record[] $records
93
     */
94
    public function onLocalRecordsLoaded($records)
95
    {
96
        if (count($records) > 0) {
97
            $this->updater->setLocalRecord($records[0]->getCheckpoints());
98
        } else {
99
            $this->updater->setLocalRecord([]);
100
        }
101
        $this->updater->update($this->allPlayers);
102
    }
103
104
    /**
105
     * Called when a player finishes map for the very first time (basically first record).
106
     *
107
     * @param Record   $record
108
     * @param Record[] $records
109
     * @param          $position
110
     */
111
    public function onLocalRecordsFirstRecord(Record $record, $records, $position)
112
    {
113
        $rec = $record->getCheckpoints();
114
        $this->updater->setLocalRecord($rec);
115
        $this->updater->update($this->allPlayers);
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)
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)
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 ($position == 1) {
142
            $this->updater->setLocalRecord($record->getCheckpoints());
143
            $this->updater->update($this->allPlayers);
144
        }
145
    }
146
147
    /**
148
     * Called when a player finishes map with better time but keeps same position.
149
     *
150
     * @param Record   $record
151
     * @param Record   $oldRecord
152
     * @param Record[] $records
153
     * @param          $position
154
     */
155 View Code Duplication
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position)
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...
156
    {
157
        if ($position == 1) {
158
            $this->updater->setLocalRecord($record->getCheckpoints());
159
            $this->updater->update($this->allPlayers);
160
        }
161
    }
162
163
164
    /**
165
     * @param Map $map
166
     *
167
     * @return void
168
     */
169
    public function onBeginMap(Map $map)
170
    {
171
172
    }
173
174
    /**
175
     * @param Map $map
176
     *
177
     * @return void
178
     */
179
    public function onEndMap(Map $map)
180
    {
181
        $this->updater->setLocalRecord([]);
182
    }
183
}
184