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

VoteService::onVoteFailed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 2
eloc 3
nc 2
nop 3
crap 6
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Services;
4
5
use eXpansion\Bundle\VoteManager\Structures\Vote;
6
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
7
use eXpansion\Framework\Core\Helpers\ChatNotification;
8
use eXpansion\Framework\Core\Services\Application\Dispatcher;
9
use eXpansion\Framework\Core\Services\Console;
10
use eXpansion\Framework\Core\Storage\Data\Player;
11
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyVote;
12
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap;
13
use Maniaplanet\DedicatedServer\Connection;
14
use Maniaplanet\DedicatedServer\Structures\Map;
15
16
class VoteService implements ListenerInterfaceMpLegacyVote, ListenerInterfaceExpTimer, ListenerInterfaceMpScriptMap
17
{
18
    /**
19
     * @var Connection
20
     */
21
    public $connection;
22
23
    /** @var Console */
24
    public $console;
25
26
    public $removeVote = false;
27
28
    /** @var null|Vote */
29
    private $currentVote = null;
30
31
    /** @var array */
32
    private $votesStarted = [];
33
34
    /**
35
     * @var Dispatcher
36
     */
37
    private $dispatcher;
38
    /**
39
     * @var ChatNotification
40
     */
41
    private $chatNotification;
42
43
    /**
44
     * VoteManager constructor.
45
     * @param Console $console
46
     * @param Connection $connection
47
     * @param ChatNotification $chatNotification
48
     * @param Dispatcher $dispatcher
49
     */
50
    public function __construct(
51
        Console $console,
52
        Connection $connection,
53
        ChatNotification $chatNotification,
54
        Dispatcher $dispatcher
55
    ) {
56
        $this->console = $console;
57
        $this->connection = $connection;
58
        $this->dispatcher = $dispatcher;
59
        $this->chatNotification = $chatNotification;
60
    }
61
62
63
    /**
64
     * When a new vote is addressed
65
     *
66
     * @param Player $player
67
     * @param string $cmdName
68
     * @param string $cmdValue
69
     *
70
     * @return void
71
     */
72
    public function onVoteNew(Player $player, $cmdName, $cmdValue)
73
    {
74
        switch ($cmdName) {
75
            case "NextMap":
76
                // disable default vote
77
                $this->connection->cancelVote();
78
                $this->startVote($player, "Exp_NextMap");
79
                break;
80
            case "RestartMap":
81
                // disable default vote
82
                $this->connection->cancelVote();
83
                $this->startVote($player, "Exp_RestartMap");
84
                break;
85
        }
86
    }
87
88
    /**
89
     * When vote gets cancelled
90
     *
91
     * @param Player $player
92
     * @param string $cmdName
93
     * @param string $cmdValue
94
     *
95
     * @return void
96
     */
97
    public function onVoteCancelled(Player $player, $cmdName, $cmdValue)
98
    {
99
        if ($cmdValue instanceof Vote) {
100
            $this->currentVote = null;
101
        }
102
    }
103
104
    /**
105
     * When vote Passes
106
     * @param Player $player
107
     * @param string $cmdName
108
     * @param string $cmdValue
109
     *
110
     * @return void
111
     */
112
    public function onVotePassed(Player $player, $cmdName, $cmdValue)
113
    {
114
        if ($cmdValue instanceof Vote) {
115
            $this->currentVote = null;
116
        }
117
    }
118
119
    /**
120
     * When vote Fails
121
     * @param Player $player
122
     * @param string $cmdName
123
     * @param string $cmdValue
124
     *
125
     * @return void
126
     */
127
    public function onVoteFailed(Player $player, $cmdName, $cmdValue)
128
    {
129
        if ($cmdValue instanceof Vote) {
130
            $this->currentVote = null;
131
        }
132
    }
133
134
    public function onPreLoop()
135
    {
136
        // do nothing
137
    }
138
139
    public function onPostLoop()
140
    {
141
        //do nothing
142
    }
143
144
    public function updateVote($login, $type)
145
    {
146
        if ($this->currentVote instanceof Vote) {
147
            switch ($type) {
148
                case Vote::VOTE_YES:
149
                    $this->currentVote->castYes($login);
150
                    break;
151
                case Vote::VOTE_NO:
152
                    $this->currentVote->castNo($login);
153
                    break;
154
            }
155
        }
156
    }
157
158
    public function onEverySecond()
159
    {
160
        $vote = $this->currentVote;
161
162
        if ($vote !== null) {
163
            $vote->updateVote(time());
164
165
            switch ($vote->getStatus()) {
166 View Code Duplication
                case Vote::STATUS_CANCEL:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
167
                    $this->dispatcher->dispatch("votemanager.votecancelled",
168
                        [$vote->getPlayer(), $vote->getType(), $this->currentVote]);
169
                    break;
170 View Code Duplication
                case Vote::STATUS_FAILED:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
171
                    $this->dispatcher->dispatch("votemanager.votefailed",
172
                        [$vote->getPlayer(), $vote->getType(), $this->currentVote]);
173
                    break;
174 View Code Duplication
                case Vote::STATUS_PASSED:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
175
                    $this->dispatcher->dispatch("votemanager.votepassed",
176
                        [$vote->getPlayer(), $vote->getType(), $this->currentVote]);
177
                    break;
178
            }
179
        }
180
    }
181
182
    /**
183
     * @return Vote
184
     */
185
    public function getCurrentVote()
186
    {
187
        return $this->currentVote;
188
    }
189
190
    public function startVote(Player $player, $type)
191
    {
192
        if ($this->getCurrentVote() !== null) {
193
            $this->chatNotification->sendMessage("|error| Vote already progressing.");
194
195
            return;
196
        }
197
198
        if (array_key_exists($type, $this->votesStarted) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
199
            $this->votesStarted[$type] = "yep";
200
            $this->currentVote = new Vote($player, $type);
201
            $this->dispatcher->dispatch("votemanager.votenew", [$player, $type, $this->currentVote]);
202
        } else {
203
            /** @todo change this to toast when the service is ready */
204
            $this->chatNotification->sendMessage("|error| Vote of this type has already started.");
205
        }
206
    }
207
208
209
    /**
210
     * Callback sent when the "StartMap" section start.
211
     *
212
     * @param int $count Each time this section is played, this number is incremented by one
213
     * @param int $time Server time when the callback was sent
214
     * @param boolean $restarted true if the map was restarted, false otherwise
215
     * @param Map $map Map started with.
216
     *
217
     * @return void
218
     */
219
    public function onStartMapStart($count, $time, $restarted, Map $map)
220
    {
221
        $this->votesStarted = [];
222
    }
223
224
    /**
225
     * Callback sent when the "StartMap" section end.
226
     *
227
     * @param int $count Each time this section is played, this number is incremented by one
228
     * @param int $time Server time when the callback was sent
229
     * @param boolean $restarted true if the map was restarted, false otherwise
230
     * @param Map $map Map started with.
231
     *
232
     * @return void
233
     */
234
    public function onStartMapEnd($count, $time, $restarted, Map $map)
235
    {
236
        // TODO: Implement onStartMapEnd() method.
237
    }
238
239
    /**
240
     * Callback sent when the "EndMap" section start.
241
     *
242
     * @param int $count Each time this section is played, this number is incremented by one
243
     * @param int $time Server time when the callback was sent
244
     * @param boolean $restarted true if the map was restarted, false otherwise
245
     * @param Map $map Map started with.
246
     *
247
     * @return void
248
     */
249
    public function onEndMapStart($count, $time, $restarted, Map $map)
250
    {
251
        // TODO: Implement onEndMapStart() method.
252
    }
253
254
    /**
255
     * Callback sent when the "EndMap" section end.
256
     *
257
     * @param int $count Each time this section is played, this number is incremented by one
258
     * @param int $time Server time when the callback was sent
259
     * @param boolean $restarted true if the map was restarted, false otherwise
260
     * @param Map $map Map started with.
261
     *
262
     * @return void
263
     */
264
    public function onEndMapEnd($count, $time, $restarted, Map $map)
265
    {
266
        // TODO: Implement onEndMapEnd() method.
267
    }
268
}
269