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\Storage\PlayerStorage; |
13
|
|
|
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyMap; |
14
|
|
|
use eXpansionExperimantal\Bundle\Dedimania\DataProviders\Listener\DedimaniaDataListener; |
15
|
|
|
use eXpansionExperimantal\Bundle\Dedimania\Structures\DedimaniaPlayer; |
16
|
|
|
use eXpansionExperimantal\Bundle\Dedimania\Structures\DedimaniaRecord; |
17
|
|
|
use Maniaplanet\DedicatedServer\Structures\Map; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class BestCheckpoints implements StatusAwarePluginInterface, RecordsDataListener, DedimaniaDataListener, ListenerInterfaceMpLegacyMap |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var PlayerStorage |
24
|
|
|
*/ |
25
|
|
|
private $playerStorage; |
26
|
|
|
/** |
27
|
|
|
* @var BestCheckpointsWidgetFactory |
28
|
|
|
*/ |
29
|
|
|
private $widget; |
30
|
|
|
/** |
31
|
|
|
* @var Group |
32
|
|
|
*/ |
33
|
|
|
private $players; |
34
|
|
|
/** |
35
|
|
|
* @var UpdaterWidgetFactory |
36
|
|
|
*/ |
37
|
|
|
private $updater; |
38
|
|
|
/** |
39
|
|
|
* @var Group |
40
|
|
|
*/ |
41
|
|
|
private $allPlayers; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* BestCheckpoints constructor. |
46
|
|
|
* |
47
|
|
|
* @param PlayerStorage $playerStorage |
48
|
|
|
* @param BestCheckpointsWidgetFactory $widget |
49
|
|
|
* @param UpdaterWidgetFactory $updater |
50
|
|
|
* @param Group $players |
51
|
|
|
* @param Group $allPlayers |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
PlayerStorage $playerStorage, |
55
|
|
|
BestCheckPointsWidgetFactory $widget, |
56
|
|
|
UpdaterWidgetFactory $updater, |
57
|
|
|
Group $players, |
58
|
|
|
Group $allPlayers |
59
|
|
|
) { |
60
|
|
|
$this->playerStorage = $playerStorage; |
61
|
|
|
$this->widget = $widget; |
62
|
|
|
$this->players = $players; |
63
|
|
|
$this->updater = $updater; |
64
|
|
|
$this->allPlayers = $allPlayers; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the status of the plugin |
69
|
|
|
* |
70
|
|
|
* @param boolean $status |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function setStatus($status) |
75
|
|
|
{ |
76
|
|
|
if ($status) { |
77
|
|
|
$this->widget->create($this->players); |
78
|
|
|
$this->updater->create($this->allPlayers); |
79
|
|
|
} else { |
80
|
|
|
$this->widget->destroy($this->players); |
81
|
|
|
$this->updater->destroy($this->allPlayers); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Called when local records are loaded. |
87
|
|
|
* |
88
|
|
|
* @param Record[] $records |
89
|
|
|
*/ |
90
|
|
|
public function onLocalRecordsLoaded($records, BaseRecords $baseRecords) |
91
|
|
|
{ |
92
|
|
|
if (!$this->checkRecordPlugin($baseRecords)) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (count($records) > 0) { |
97
|
|
|
$this->updater->setLocalRecord($records[0]->getPlayer()->getNickname(), $records[0]->getCheckpoints()); |
98
|
|
|
} else { |
99
|
|
|
$this->updater->setLocalRecord("-", []); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Called when a player finishes map for the very first time (basically first record). |
105
|
|
|
* |
106
|
|
|
* @param Record $record |
107
|
|
|
* @param Record[] $records |
108
|
|
|
* @param $position |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function onLocalRecordsFirstRecord(Record $record, $records, $position, BaseRecords $baseRecords) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
if (!$this->checkRecordPlugin($baseRecords)) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->updater->setLocalRecord($record->getPlayer()->getNickname(), $record->getCheckpoints()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Called when a player finishes map and does same time as before. |
121
|
|
|
* |
122
|
|
|
* @param Record $record |
123
|
|
|
* @param Record $oldRecord |
124
|
|
|
* @param Record[] $records |
125
|
|
|
*/ |
126
|
|
|
public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records, BaseRecords $baseRecords) |
127
|
|
|
{ |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Called when a player finishes map with better time and has better position. |
133
|
|
|
* |
134
|
|
|
* @param Record $record |
135
|
|
|
* @param Record $oldRecord |
136
|
|
|
* @param Record[] $records |
137
|
|
|
* @param int $position |
138
|
|
|
* @param int $oldPosition |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function onLocalRecordsBetterPosition( |
|
|
|
|
141
|
|
|
Record $record, |
142
|
|
|
Record $oldRecord, |
143
|
|
|
$records, |
144
|
|
|
$position, |
145
|
|
|
$oldPosition, |
146
|
|
|
BaseRecords $baseRecords |
147
|
|
|
) { |
148
|
|
|
if (!$this->checkRecordPlugin($baseRecords)) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($position == 1) { |
153
|
|
|
$this->updater->setLocalRecord($record->getPlayer()->getNickname(), $record->getCheckpoints()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Called when a player finishes map with better time but keeps same position. |
159
|
|
|
* |
160
|
|
|
* @param Record $record |
161
|
|
|
* @param Record $oldRecord |
162
|
|
|
* @param Record[] $records |
163
|
|
|
* @param $position |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function onLocalRecordsSamePosition( |
|
|
|
|
166
|
|
|
Record $record, |
167
|
|
|
Record $oldRecord, |
168
|
|
|
$records, |
169
|
|
|
$position, |
170
|
|
|
BaseRecords $baseRecords |
171
|
|
|
) { |
172
|
|
|
if (!$this->checkRecordPlugin($baseRecords)) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ($position == 1) { |
177
|
|
|
$this->updater->setLocalRecord($record->getPlayer()->getNickname(), $record->getCheckpoints()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Check if we can use the data for this plugin. |
183
|
|
|
* |
184
|
|
|
* @param BaseRecords $baseRecords |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
protected function checkRecordPlugin(BaseRecords $baseRecords) |
189
|
|
|
{ |
190
|
|
|
return $baseRecords->getRecordsHandler()->getCurrentNbLaps() == 1; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param Map $map |
195
|
|
|
* |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function onBeginMap(Map $map) |
199
|
|
|
{ |
200
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param Map $map |
205
|
|
|
* |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
|
|
public function onEndMap(Map $map) |
209
|
|
|
{ |
210
|
|
|
$this->updater->setLocalRecord("-", []); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Called when dedimania records are loaded. |
215
|
|
|
* |
216
|
|
|
* @param DedimaniaRecord[] $records |
217
|
|
|
*/ |
218
|
|
|
public function onDedimaniaRecordsLoaded($records) |
219
|
|
|
{ |
220
|
|
|
if (count($records) > 1) { |
221
|
|
|
$this->updater->setDedimaniaRecord($records[0]->nickName, $records[0]->getCheckpoints()); |
222
|
|
|
} else { |
223
|
|
|
$this->updater->setDedimaniaRecord("-", []); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param DedimaniaRecord $record |
229
|
|
|
* @param DedimaniaRecord $oldRecord |
230
|
|
|
* @param DedimaniaRecord[] $records |
231
|
|
|
* @param int $position |
232
|
|
|
* @param int $oldPosition |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
public function onDedimaniaRecordsUpdate( |
236
|
|
|
DedimaniaRecord $record, |
237
|
|
|
DedimaniaRecord $oldRecord, |
238
|
|
|
$records, |
239
|
|
|
$position, |
240
|
|
|
$oldPosition |
241
|
|
|
) { |
242
|
|
|
if ($position == 1) { |
243
|
|
|
$this->updater->setDedimaniaRecord($record->nickName, $record->getCheckpoints()); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param DedimaniaPlayer $player |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public function onDedimaniaPlayerConnect(DedimaniaPlayer $player) |
252
|
|
|
{ |
253
|
|
|
// do nothing |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param DedimaniaPlayer $player |
258
|
|
|
* @return void |
259
|
|
|
*/ |
260
|
|
|
public function onDedimaniaPlayerDisconnect(DedimaniaPlayer $player) |
261
|
|
|
{ |
262
|
|
|
// do nothing |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
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.