Completed
Push — master ( 47c10a...c931cd )
by De Cramer
13s
created

VoteManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 1
eloc 9
nc 1
nop 4
crap 1
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\Plugins\Votes\AbstractVotePlugin;
9
use eXpansion\Bundle\VoteManager\Services\VoteService;
10
use eXpansion\Bundle\VoteManager\Structures\Vote;
11
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
12
use eXpansion\Framework\Core\Helpers\ChatNotification;
13
use eXpansion\Framework\Core\Model\UserGroups\Group;
14
use eXpansion\Framework\Core\Services\Console;
15
use eXpansion\Framework\Core\Storage\Data\Player;
16
use eXpansion\Framework\Core\Storage\MapStorage;
17
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyVote;
18
use Maniaplanet\DedicatedServer\Connection;
19
20
class VoteManager implements ListenerInterfaceMpLegacyVote, ListenerInterfaceExpTimer
21
{
22
    const YES = "yes";
23
    const NO = "no";
24
25
    /**
26
     * @var VoteWidgetFactory
27
     */
28
    private $voteWidgetFactory;
29
30
    /**
31
     * @var UpdateVoteWidgetFactory
32
     */
33
    private $updateVoteWidgetFactory;
34
35
    /**
36
     * @var Group
37
     */
38
    private $players;
39
40
    /**
41
     * @var VoteService
42
     */
43
    private $voteService;
44
45
    /**
46
     * VoteManager constructor.
47
     *
48
     * @param VoteWidgetFactory $voteWidgetFactory
49
     * @param UpdateVoteWidgetFactory $updateVoteWidgetFactory
50
     * @param Group $players
51
     * @param VoteService $voteService
52
     */
53 7
    public function __construct(
54
        VoteWidgetFactory $voteWidgetFactory,
55
        UpdateVoteWidgetFactory $updateVoteWidgetFactory,
56
        Group $players,
57
        VoteService $voteService
58
    ) {
59 7
        $this->voteWidgetFactory = $voteWidgetFactory;
60 7
        $this->players = $players;
61 7
        $this->voteService = $voteService;
62 7
        $this->updateVoteWidgetFactory = $updateVoteWidgetFactory;
63 7
    }
64
65
    /**
66
     * When a new vote is addressed
67
     *
68
     * @param Player $player
69
     * @param string $cmdName
70
     * @param string $cmdValue
71
     *
72
     * @return void
73
     */
74 2
    public function onVoteNew(Player $player, $cmdName, $cmdValue)
75
    {
76 2
        if ($cmdValue instanceof Vote) {
77 1
            $this->updateVoteWidgetFactory->create($this->players);
78 1
            $this->voteWidgetFactory->create($this->players);
79 1
            $this->voteWidgetFactory->setMessage($this->voteService->getCurrentVote()->getQuestion());
80
        } else {
81 1
            $this->voteService->startVote($player, $cmdName, ['value' => $cmdValue]);
82
        }
83 2
    }
84
85
    /**
86
     * When vote gets cancelled
87
     *
88
     * @param Player $player
89
     * @param string $cmdName
90
     * @param string $cmdValue
91
     *
92
     * @return void
93
     */
94 1
    public function onVoteCancelled(Player $player, $cmdName, $cmdValue)
95
    {
96
97 1
        if ($cmdValue instanceof Vote) {
98 1
            $this->voteWidgetFactory->destroy($this->players);
99 1
            $this->updateVoteWidgetFactory->destroy($this->players);
100
        } else {
101
            $this->voteService->cancel();
102
        }
103 1
    }
104
105
    /**
106
     * When vote Passes
107
     * @param Player $player
108
     * @param string $cmdName
109
     * @param string $cmdValue
110
     *
111
     * @return void
112
     */
113 1
    public function onVotePassed(Player $player, $cmdName, $cmdValue)
114
    {
115 1
        if ($cmdValue instanceof Vote) {
116 1
            $this->voteWidgetFactory->destroy($this->players);
117 1
            $this->updateVoteWidgetFactory->destroy($this->players);
118
        }
119 1
    }
120
121
    /**
122
     * When vote Fails
123
     * @param Player $player
124
     * @param string $cmdName
125
     * @param string $cmdValue
126
     *
127
     * @return void
128
     */
129 1
    public function onVoteFailed(Player $player, $cmdName, $cmdValue)
130
    {
131 1
        if ($cmdValue instanceof Vote) {
132 1
            $this->voteWidgetFactory->destroy($this->players);
133 1
            $this->updateVoteWidgetFactory->destroy($this->players);
134
        }
135 1
    }
136
137 1
    public function onEverySecond()
138
    {
139 1
        if ($this->voteService->getCurrentVote() instanceof AbstractVotePlugin) {
140 1
            $this->voteService->update();
141 1
            $this->updateVoteWidgetFactory->update($this->players);
142
        }
143 1
    }
144
145 1
    public function onPreLoop()
146
    {
147
        // Nothing
148 1
    }
149
150 1
    public function onPostLoop()
151
    {
152
        // Nothing
153 1
    }
154
}
155
156