|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\Maps\Services; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Bundle\Maps\Structure\JukeboxMap; |
|
6
|
|
|
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups; |
|
7
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
|
8
|
|
|
use Maniaplanet\DedicatedServer\Structures\Map; |
|
9
|
|
|
|
|
10
|
|
|
class JukeboxService |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var JukeboxMap[] */ |
|
13
|
|
|
protected $mapQueue = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var PlayerStorage |
|
17
|
|
|
*/ |
|
18
|
|
|
private $playerStorage; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var AdminGroups |
|
21
|
|
|
*/ |
|
22
|
|
|
private $adminGroups; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* JukeboxService constructor. |
|
26
|
|
|
* @param PlayerStorage $playerStorage |
|
27
|
|
|
* @param AdminGroups $adminGroups |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(PlayerStorage $playerStorage, AdminGroups $adminGroups) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->playerStorage = $playerStorage; |
|
32
|
|
|
$this->adminGroups = $adminGroups; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return JukeboxMap[] |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getMapQueue() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->mapQueue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return JukeboxMap|null |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getFirst() |
|
47
|
|
|
{ |
|
48
|
|
|
reset($this->mapQueue); |
|
49
|
|
|
|
|
50
|
|
|
return current($this->mapQueue); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Adds map as last item |
|
55
|
|
|
* @param Map $map |
|
56
|
|
|
* @param null $login |
|
57
|
|
|
* @param bool $force |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
|
|
61
|
|
View Code Duplication |
public function addMap(Map $map, $login = null, $force = false) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$player = null; |
|
|
|
|
|
|
64
|
|
|
if (!$login) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$player = $this->playerStorage->getPlayerInfo($login); |
|
69
|
|
|
$jbMap = new JukeboxMap($map, $player); |
|
70
|
|
|
|
|
71
|
|
|
if ($force) { |
|
72
|
|
|
$this->add($jbMap); |
|
73
|
|
|
|
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
// no some restrictions for admin |
|
77
|
|
|
if ($this->adminGroups->hasPermission($login, "jukebox")) { |
|
78
|
|
|
if ($this->checkMap($map) === false) { |
|
79
|
|
|
$this->add($jbMap); |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} else { |
|
85
|
|
|
// restrict 1 map per 1 login |
|
86
|
|
|
if ($this->checkLogin($login) === false && $this->checkMap($map) === false) { |
|
87
|
|
|
$this->add($jbMap); |
|
88
|
|
|
|
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Adds Map as first item |
|
98
|
|
|
* @param Map $map |
|
99
|
|
|
* @param null $login |
|
100
|
|
|
* @param bool $force |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
|
|
104
|
|
View Code Duplication |
public function addMapFirst(Map $map, $login = null, $force = false) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$player = null; |
|
|
|
|
|
|
107
|
|
|
if (!$login) { |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$player = $this->playerStorage->getPlayerInfo($login); |
|
112
|
|
|
$jbMap = new JukeboxMap($map, $player); |
|
113
|
|
|
|
|
114
|
|
|
if ($force) { |
|
115
|
|
|
$this->addFirst($jbMap); |
|
116
|
|
|
|
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
// no some restrictions for admin |
|
120
|
|
|
if ($this->adminGroups->hasPermission($login, "jukebox")) { |
|
121
|
|
|
if ($this->checkMap($map) === false) { |
|
122
|
|
|
$this->add($jbMap); |
|
123
|
|
|
|
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} else { |
|
128
|
|
|
// restrict 1 map per 1 login |
|
129
|
|
|
if ($this->checkLogin($login) === false && $this->checkMap($map) === false) { |
|
130
|
|
|
$this->add($jbMap); |
|
131
|
|
|
|
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param Map $map |
|
141
|
|
|
* @param $login |
|
142
|
|
|
* @param bool $force |
|
143
|
|
|
* @return false; |
|
|
|
|
|
|
144
|
|
|
*/ |
|
145
|
|
|
public function removeMap(Map $map, $login = null, $force = false) |
|
146
|
|
|
{ |
|
147
|
|
|
if ($force) { |
|
148
|
|
|
return $this->remove($map); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (!$login) { |
|
152
|
|
|
|
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
// no some restrictions for admin |
|
156
|
|
|
if ($this->adminGroups->hasPermission($login, "jukebox")) { |
|
157
|
|
|
return $this->remove($map); |
|
158
|
|
|
} else { |
|
159
|
|
|
// restrict 1 map per 1 login |
|
160
|
|
|
$check = $this->getMap($login); |
|
161
|
|
|
if ($check && $check->getMap() === $map) { |
|
162
|
|
|
return $this->remove($map); |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param JukeboxMap $map |
|
173
|
|
|
*/ |
|
174
|
|
|
private function add(JukeboxMap $map) |
|
175
|
|
|
{ |
|
176
|
|
|
array_push($this->mapQueue, $map); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param JukeboxMap $map |
|
181
|
|
|
*/ |
|
182
|
|
|
private function addFirst(JukeboxMap $map) |
|
183
|
|
|
{ |
|
184
|
|
|
array_unshift($this->mapQueue, $map); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param $login |
|
189
|
|
|
* @return bool|JukeboxMap |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getMap($login) |
|
192
|
|
|
{ |
|
193
|
|
|
foreach ($this->mapQueue as $jukeboxMap) { |
|
194
|
|
|
if ($jukeboxMap->getPlayer()->getLogin() == $login) { |
|
195
|
|
|
return $jukeboxMap; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param Map $map |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
|
View Code Duplication |
private function remove(Map $map) |
|
|
|
|
|
|
208
|
|
|
{ |
|
209
|
|
|
foreach ($this->mapQueue as $idx => $jukeboxMap) { |
|
210
|
|
|
if ($jukeboxMap->getMap() === $map) { |
|
211
|
|
|
unset($this->mapQueue[$idx]); |
|
212
|
|
|
|
|
213
|
|
|
return true; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return false; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* checks if login exists on queue |
|
223
|
|
|
* @param string $login |
|
224
|
|
|
* @return bool |
|
225
|
|
|
*/ |
|
226
|
|
|
private function checkLogin($login) |
|
227
|
|
|
{ |
|
228
|
|
|
foreach ($this->mapQueue as $jukeboxMap) { |
|
229
|
|
|
if ($jukeboxMap->getPlayer()->getLogin() == $login) { |
|
230
|
|
|
return true; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return false; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* checks if map exists on queue |
|
239
|
|
|
* @param Map $map |
|
240
|
|
|
* @return bool |
|
241
|
|
|
*/ |
|
242
|
|
View Code Duplication |
private function checkMap(Map $map) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
foreach ($this->mapQueue as $jukeboxMap) { |
|
245
|
|
|
if ($jukeboxMap->getMap() === $map) { |
|
246
|
|
|
return true; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return false; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* |
|
255
|
|
|
*/ |
|
256
|
|
|
public function clearMapQueue() |
|
257
|
|
|
{ |
|
258
|
|
|
$this->mapQueue = []; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
|
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.