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

AbstractVotePlugin::getQuestion()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Plugins\Votes;
4
5
use eXpansion\Bundle\VoteManager\Structures\Vote;
6
use eXpansion\Framework\Core\Storage\Data\Player;
7
use eXpansion\Framework\Core\Storage\PlayerStorage;
8
9
/**
10
 * Class AbstractVotePlugin
11
 *
12
 * @author    de Cramer Oliver<[email protected]>
13
 * @copyright 2017 eXpansion
14
 * @package eXpansion\Bundle\VoteManager\Plugins\Votes
15
 */
16
abstract class AbstractVotePlugin
17
{
18
    /** @var PlayerStorage */
19
    protected $playerStorage;
20
21
    /** @var int */
22
    protected $duration;
23
24
    /** @var float */
25
    protected $ratio;
26
27
    /** @var Vote|null */
28
    protected $currentVote = null;
29
30
    /**
31
     * AbstractVotePlugin constructor.
32
     *
33
     * @param PlayerStorage $playerStorage
34
     * @param int $duration
35
     * @param float $ratio
36
     */
37 10
    public function __construct(PlayerStorage $playerStorage, int $duration, float $ratio)
38
    {
39 10
        $this->playerStorage = $playerStorage;
40 10
        $this->duration = $duration;
41 10
        $this->ratio = $ratio;
42 10
    }
43
44
    /**
45
     * Start a new vote.
46
     *
47
     * @param Player $player
48
     *
49
     * @return Vote|null
50
     */
51 7
    public function start(Player $player, $params)
52
    {
53 7
        $this->currentVote = new Vote($player, $this->getCode(), $params);
54 7
        return $this->currentVote;
55
    }
56
57
    /**
58
     * Reset current vote session.
59
     */
60 1
    public function reset()
61
    {
62 1
        $this->currentVote = null;
63 1
    }
64
65
    /**
66
     * User votes yes
67
     *
68
     * @param string $login
69
     */
70 5
    public function castYes($login)
71
    {
72 5
        if ($this->currentVote) {
73 5
            $this->currentVote->castYes($login);
74
        }
75 5
    }
76
77
    /**
78
     * User votes no
79
     *
80
     * @param string $login
81
     */
82 3
    public function castNo($login)
83
    {
84 3
        if ($this->currentVote) {
85 3
            $this->currentVote->castNo($login);
86
        }
87 3
    }
88
89
    /**
90
     * Cancel ongoing vote.
91
     */
92 1
    public function cancel()
93
    {
94 1
        if (!$this->currentVote) {
95 1
            return;
96
        }
97
98 1
        $this->currentVote->setStatus(Vote::STATUS_CANCEL);
99 1
    }
100
101
    /**
102
     * Update the status of the vote, and execute actions if vote passed.
103
     *
104
     * @param int $time
105
     */
106 8
    public function update($time = null)
107
    {
108 8
        if (!$this->currentVote || $this->currentVote->getStatus() == Vote::STATUS_CANCEL) {
109 1
            return;
110
        }
111
112 7
        if (is_null($time)) {
113 2
            $time = time();
114
        }
115
116 7
        $playerCount = count($this->playerStorage->getOnline());
117
118
        // Check if vote passes when we suppose that all palyers that didn't vote would vote NO.
119 7 View Code Duplication
        if ($playerCount > 0 && ($this->currentVote->getYes() / $playerCount) > $this->ratio) {
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...
120 2
            $this->votePassed();
121 2
            return;
122
        }
123
124
        // If the vote is still not decided wait for the end to decide.
125 5
        if (($time - $this->currentVote->getStartTime()) > $this->duration) {
126 3
            $totalVotes = $this->currentVote->getYes() + $this->currentVote->getNo() * 1.0;
127
128 3 View Code Duplication
            if ($totalVotes >= 1 && ($this->currentVote->getYes()/$totalVotes) > $this->ratio) {
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...
129 1
                $this->votePassed();
130
            } else {
131 2
                $this->voteFailed();
132
            }
133
        }
134 5
    }
135
136
    /**
137
     * @return Vote|null
138
     */
139 6
    public function getCurrentVote()
140
    {
141 6
        return $this->currentVote;
142
    }
143
144
    /**
145
     * Called when a vote passed.
146
     */
147 3
    protected function votePassed()
148
    {
149 3
        $this->currentVote->setStatus(Vote::STATUS_PASSED);
150 3
        $this->executeVotePassed();
151 3
    }
152
153
    /**
154
     * Called when a vote failed.
155
     */
156 2
    protected function voteFailed()
157
    {
158 2
        $this->currentVote->setStatus(Vote::STATUS_FAILED);
159 2
        $this->executeVoteFailed();
160 2
    }
161
162
    /**
163
     * @return int
164
     */
165
    public function getDuration(): int
166
    {
167
        return $this->duration;
168
    }
169
170
    /**
171
     * @return float
172
     */
173
    public function getRatio(): float
174
    {
175
        return $this->ratio;
176
    }
177
178
    /**
179
     * @return int
180
     */
181
    public function getElapsedTime() : int
182
    {
183
        return time() - $this->currentVote->getStartTime();
184
    }
185
186
    /**
187
     * Get question text to display for this vote.
188
     *
189
     * @return string
190
     */
191
    abstract public function getQuestion(): string;
192
193
    /**
194
     * Get type code of this vote.
195
     *
196
     * @return string
197
     */
198
    abstract public function getCode(): string;
199
200
    /**
201
     * Get native votes this votes replaces.
202
     *
203
     * @return string[]
204
     */
205
    abstract public function getReplacementTypes(): array;
206
207
    /**
208
     * Called when vote is passed.
209
     *
210
     * @return void
211
     */
212
    abstract protected function executeVotePassed();
213
214
    /**
215
     * Called when vote is failed.
216
     */
217
    abstract protected function executeVoteFailed();
218
}
219