Completed
Pull Request — master (#152)
by
unknown
02:44
created

VoteManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 7
dl 0
loc 189
rs 10
c 0
b 0
f 0
ccs 0
cts 56
cp 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A onVoteNew() 0 18 4
A onVoteCancelled() 0 7 2
A onVotePassed() 0 17 4
A onVoteFailed() 0 7 2
A onPreLoop() 0 4 1
A onPostLoop() 0 4 1
A onEverySecond() 0 6 2
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Plugins;
4
5
use eXpansion\Bundle\Maps\Services\JukeboxService;
6
use eXpansion\Bundle\VoteManager\Plugins\Gui\Widget\UpdateVoteWidgetFactory;
7
use eXpansion\Bundle\VoteManager\Plugins\Gui\Widget\VoteWidgetFactory;
8
use eXpansion\Bundle\VoteManager\Services\VoteService;
9
use eXpansion\Bundle\VoteManager\Structures\Vote;
10
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
11
use eXpansion\Framework\Core\Helpers\ChatNotification;
12
use eXpansion\Framework\Core\Model\UserGroups\Group;
13
use eXpansion\Framework\Core\Services\Console;
14
use eXpansion\Framework\Core\Storage\Data\Player;
15
use eXpansion\Framework\Core\Storage\MapStorage;
16
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyVote;
17
use Maniaplanet\DedicatedServer\Connection;
18
19
class VoteManager implements ListenerInterfaceMpLegacyVote, ListenerInterfaceExpTimer
20
{
21
22
    const YES = "yes";
23
    const NO = "no";
24
25
    /**
26
     * @var Console
27
     */
28
    private $console;
29
    /**
30
     * @var Connection
31
     */
32
    private $connection;
33
    /**
34
     * @var ChatNotification
35
     */
36
    private $chatNotification;
37
38
    /** @var Vote|null */
39
    public static $currentVote = null;
40
41
    /**
42
     * @var VoteWidgetFactory
43
     */
44
    private $voteWidgetFactory;
45
    /**
46
     * @var UpdateVoteWidgetFactory
47
     */
48
    private $updateVoteWidgetFactory;
49
    /**
50
     * @var Group
51
     */
52
    private $players;
53
    /**
54
     * @var JukeboxService
55
     */
56
    private $jukebox;
57
    /**
58
     * @var MapStorage
59
     */
60
    private $mapStorage;
61
62
    /** @var array */
63
    private $voteStarted = [];
0 ignored issues
show
Unused Code introduced by
The property $voteStarted is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
64
    /**
65
     * @var VoteService
66
     */
67
    private $voteService;
68
69
    /**
70
     * VoteManager constructor.
71
     * @param Console $console
72
     * @param Connection $connection
73
     * @param ChatNotification $chatNotification
74
     * @param VoteWidgetFactory $voteWidgetFactory
75
     * @param UpdateVoteWidgetFactory $updateVoteWidgetFactory
76
     * @param Group $players
77
     * @param JukeboxService $jukebox
78
     * @param MapStorage $mapStorage
79
     * @param VoteService $voteService
80
     */
81
    public function __construct(
82
        Console $console,
83
        Connection $connection,
84
        ChatNotification $chatNotification,
85
        VoteWidgetFactory $voteWidgetFactory,
86
        UpdateVoteWidgetFactory $updateVoteWidgetFactory,
87
        Group $players,
88
        JukeboxService $jukebox,
89
        MapStorage $mapStorage,
90
        VoteService $voteService
91
    ) {
92
        $this->console = $console;
93
        $this->connection = $connection;
94
        $this->chatNotification = $chatNotification;
95
        $this->voteWidgetFactory = $voteWidgetFactory;
96
        $this->players = $players;
97
        $this->jukebox = $jukebox;
98
99
        $this->mapStorage = $mapStorage;
100
        $this->voteService = $voteService;
101
        $this->updateVoteWidgetFactory = $updateVoteWidgetFactory;
102
    }
103
104
    /**
105
     * When a new vote is addressed
106
     *
107
     * @param Player $player
108
     * @param string $cmdName
109
     * @param string $cmdValue
110
     *
111
     * @return void
112
     */
113
    public function onVoteNew(Player $player, $cmdName, $cmdValue)
114
    {
115
        if ($cmdValue instanceof Vote) {
116
            $text = "Unknown Vote";
117
            switch ($cmdValue->getType()) {
118
                case "Exp_RestartMap":
119
                    $text = "Restart Map ?";
120
                    break;
121
                case "Exp_NextMap":
122
                    $text = "Skip Map ?";
123
                    break;
124
            }
125
126
            $this->updateVoteWidgetFactory->create($this->players);
127
            $this->voteWidgetFactory->create($this->players);
128
            $this->voteWidgetFactory->setMessage($text);
129
        }
130
    }
131
132
    /**
133
     * When vote gets cancelled
134
     *
135
     * @param Player $player
136
     * @param string $cmdName
137
     * @param string $cmdValue
138
     *
139
     * @return void
140
     */
141
    public function onVoteCancelled(Player $player, $cmdName, $cmdValue)
142
    {
143
        if ($cmdValue instanceof Vote) {
144
            $this->voteWidgetFactory->destroy($this->players);
145
            $this->updateVoteWidgetFactory->destroy($this->players);
146
        }
147
    }
148
149
    /**
150
     * When vote Passes
151
     * @param Player $player
152
     * @param string $cmdName
153
     * @param string $cmdValue
154
     *
155
     * @return void
156
     */
157
    public function onVotePassed(Player $player, $cmdName, $cmdValue)
158
    {
159
        if ($cmdValue instanceof Vote) {
160
            switch ($cmdName) {
161
                case "Exp_RestartMap":
162
                    $this->chatNotification->sendMessage("|info| Vote passed. Map will replay.");
163
                    $this->jukebox->addMap($this->mapStorage->getCurrentMap(), $cmdValue->getPlayer()->getLogin(), true);
164
                    break;
165
                case "Exp_NextMap":
166
                    $this->connection->nextMap(false);
167
                    $this->chatNotification->sendMessage("|info| Vote passed. Skipping map!");
168
                    break;
169
            }
170
            $this->voteWidgetFactory->destroy($this->players);
171
            $this->updateVoteWidgetFactory->destroy($this->players);
172
        }
173
    }
174
175
    /**
176
     * When vote Fails
177
     * @param Player $player
178
     * @param string $cmdName
179
     * @param string $cmdValue
180
     *
181
     * @return void
182
     */
183
    public function onVoteFailed(Player $player, $cmdName, $cmdValue)
184
    {
185
        if ($cmdValue instanceof Vote) {
186
            $this->voteWidgetFactory->destroy($this->players);
187
            $this->updateVoteWidgetFactory->destroy($this->players);
188
        }
189
    }
190
191
    public function onPreLoop()
192
    {
193
        // TODO: Implement onPreLoop() method.
194
    }
195
196
    public function onPostLoop()
197
    {
198
        // TODO: Implement onPostLoop() method.
199
    }
200
201
    public function onEverySecond()
202
    {
203
        if ($this->voteService->getCurrentVote() instanceof Vote) {
204
            $this->updateVoteWidgetFactory->update($this->players);
205
        }
206
    }
207
}
208
209