Completed
Branch master (220ce5)
by De Cramer
16:11
created

Jukebox::add()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 23
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 2
crap 20
1
<?php
2
3
namespace eXpansion\Bundle\Maps\Plugins;
4
5
use eXpansion\Bundle\Maps\Plugins\Gui\JukeboxWindowFactory;
6
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
7
use eXpansion\Framework\Core\Helpers\ChatNotification;
8
use eXpansion\Bundle\Maps\Services\JukeboxService;
9
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyMap;
10
use eXpansion\Framework\Core\Storage\MapStorage;
11
use eXpansion\Framework\Core\Storage\PlayerStorage;
12
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptPodium;
13
use Maniaplanet\DedicatedServer\Connection;
14
use Maniaplanet\DedicatedServer\Structures\Map;
15
16
class Jukebox implements ListenerInterfaceMpScriptPodium, ListenerInterfaceMpLegacyMap
17
{
18
    /**
19
     * @var JukeboxService
20
     */
21
    private $jukeboxService;
22
    /**
23
     * @var Connection
24
     */
25
    private $connetion;
26
    /**
27
     * @var ChatNotification
28
     */
29
    private $chatNotification;
30
    /**
31
     * @var AdminGroups
32
     */
33
    private $adminGroups;
34
    /**
35
     * @var PlayerStorage
36
     */
37
    private $playerStorage;
38
    /**
39
     * @var MapStorage
40
     */
41
    private $mapStorage;
42
    /**
43
     * @var JukeboxWindowFactory
44
     */
45
    private $jukeboxWindowFactory;
46
47
    /**
48
     * Jukebox constructor.
49
     * @param Connection $connection
50
     * @param ChatNotification $chatNotification
51
     * @param JukeboxService $jukeboxService
52
     * @param AdminGroups $adminGroups
53
     * @param PlayerStorage $playerStorage
54
     * @param MapStorage $mapStorage
55
     * @param JukeboxWindowFactory $jukeboxWindowFactory
56
     */
57
    public function __construct(
58
        Connection $connection,
59
        ChatNotification $chatNotification,
60
        JukeboxService $jukeboxService,
61
        AdminGroups $adminGroups,
62
        PlayerStorage $playerStorage,
63
        MapStorage $mapStorage,
64
        JukeboxWindowFactory $jukeboxWindowFactory
65
    ) {
66
67
        $this->jukeboxService = $jukeboxService;
68
        $this->connetion = $connection;
69
        $this->chatNotification = $chatNotification;
70
        $this->adminGroups = $adminGroups;
71
        $this->playerStorage = $playerStorage;
72
        $this->mapStorage = $mapStorage;
73
        $this->jukeboxWindowFactory = $jukeboxWindowFactory;
74
    }
75
76
77
    public function jukeboxCommand($login, $action)
78
    {
79
        switch ($action) {
80
            case "list":
81
            case "show":
82
                $this->view($login);
83
                break;
84
            case "drop":
85
                $this->drop($login);
86
                break;
87
            case "clear":
88
                $this->clear($login);
89
                break;
90
            default:
91
                $this->add($login, $action);
92
                break;
93
        }
94
95
    }
96
97
    public function view($login)
98
    {
99
        $this->jukeboxWindowFactory->setJukeboxPlugin($this);
100
        $this->jukeboxWindowFactory->updateMaps();
101
        $this->jukeboxWindowFactory->create($login);
102
103
    }
104
105
    public function add($login, $index)
106
    {
107
        if (is_numeric($index)) {
108
            $map = $this->mapStorage->getMapByIndex($index - 1);
109
            if ($map) {
110
                if ($this->jukeboxService->addMap($map, $login)) {
111
                    $player = $this->playerStorage->getPlayerInfo($login);
112
                    $length = count($this->jukeboxService->getMapQueue());
113
                    $this->chatNotification->sendMessage('expansion_jukebox.chat.addmap', null, [
114
                        "%mapname%" => $map->name,
115
                        "%nickname%" => $player->getNickName(),
116
                        "%length%" => $length,
117
                    ]);
118
119
                    return;
120
                } else {
121
                    $this->chatNotification->sendMessage('expansion_jukebox.chat.noadd', $login,
122
                        ["%mapname%" => $map->name]);
123
124
                    return;
125
                }
126
            }
127
        }
128
        $this->chatNotification->sendMessage('expansion_jukebox.chat.nomap', $login);
129
    }
130
131
    public function drop($login, $map = null)
132
    {
133
        if ($map === null) {
134
            $map = $this->jukeboxService->getMap($login);
135
        }
136
137
        if ($map) {
138
            if ($this->jukeboxService->removeMap($map->getMap(), $login)) {
139
                $length = count($this->jukeboxService->getMapQueue());
140
                $this->chatNotification->sendMessage('expansion_jukebox.chat.removemap', null, [
141
                    "%mapname%" => $map->getMap()->name,
142
                    "%nickname%" => $map->getPlayer()->getNickName(),
143
                    "%length%" => $length,
144
                ]);
145
146
                return;
147
            }
148
            $this->chatNotification->sendMessage('expansion_jukebox.chat.noremove', $login,
149
                ["%mapname%" => $map->getMap()->name]);
150
151
            return;
152
        }
153
        $this->chatNotification->sendMessage('expansion_jukebox.chat.nomap', $login);
154
    }
155
156
    public function clear($login)
157
    {
158
        if ($this->adminGroups->hasPermission($login, 'jukebox')) {
159
            $group = $this->adminGroups->getLoginUserGroups($login)->getName();
160
            $level = $this->adminGroups->getGroupLabel($group);
161
            $player = $this->playerStorage->getPlayerInfo($login);
162
            $this->jukeboxService->clearMapQueue();
163
            $this->chatNotification->sendMessage('expansion_jukebox.chat.clear', null,
164
                ["%adminlevel%" => $level, "%admin%" => $player->getNickName()]);
165
        } else {
166
            $this->chatNotification->sendMessage('expansion_jukebox.chat.nopermission', $login);
167
        }
168
    }
169
170
    /**
171
     * Set the status of the plugin
172
     *
173
     * @param boolean $status
174
     *
175
     * @return null
176
     */
177
    public function setStatus($status)
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178
    {
179
        // TODO: Implement setStatus() method.
180
    }
181
182
    /**
183
     * @param Map $map
184
     *
185
     * @return mixed
186
     */
187
    public function onBeginMap(Map $map)
188
    {
189
        $this->chatNotification->sendMessage('expansion_jukebox.chat.beginmap', null,
190
            ['%name%' => $map->name, '%author%' => $map->author]);
191
    }
192
193
    /**
194
     * @param Map $map
195
     *
196
     * @return mixed
197
     */
198
    public function onEndMap(Map $map)
199
    {
200
        $jbmap = $this->jukeboxService->getFirst();
201
        if ($jbmap) {
202
            $this->connetion->setNextMapIdent($jbmap->getMap()->uId);
203
            $this->jukeboxService->removeMap($jbmap->getMap(), null, true);
204
        }
205
    }
206
207
    /**
208
     * Callback sent when the "onPodiumStart" section start.
209
     *
210
     * @param int $time Server time when the callback was sent
211
     * @return mixed
212
     */
213
    public function onPodiumStart($time)
214
    {
215
        $jbMap = $this->jukeboxService->getFirst();
216
        if ($jbMap) {
217
            $length = count($this->jukeboxService->getMapQueue());
218
            $this->chatNotification->sendMessage('expansion_jukebox.chat.nextjbmap', null, [
219
                "%mapname%" => $jbMap->getMap()->name,
220
                "%mapauthor%" => $jbMap->getMap()->author,
221
                "%nickname%" => $jbMap->getPlayer()->getNickName(),
222
                "%length%" => $length,
223
            ]);
224
        } else {
225
            $map = $this->mapStorage->getNextMap();
226
            $this->chatNotification->sendMessage('expansion_jukebox.chat.nextmap', null, [
227
                "%name%" => $map->name,
228
                "%author%" => $map->author,
229
            ]);
230
        }
231
    }
232
233
    /**
234
     * Callback sent when the "onPodiumEnd" section end.
235
     *
236
     * @param int $time Server time when the callback was sent
237
     *
238
     * @return mixed
239
     */
240
    public function onPodiumEnd($time)
241
    {
242
243
    }
244
}
245