|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\VoteManager\Structures; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\Storage\Data\Player; |
|
6
|
|
|
|
|
7
|
|
|
class Vote |
|
8
|
|
|
{ |
|
9
|
|
|
const VOTE_YES = "yes"; |
|
10
|
|
|
const VOTE_NO = "no"; |
|
11
|
|
|
|
|
12
|
|
|
const STATUS_FAILED = -1; |
|
13
|
|
|
const STATUS_CANCEL = 0; |
|
14
|
|
|
const STATUS_RUNNING = 1; |
|
15
|
|
|
const STATUS_PASSED = 2; |
|
16
|
|
|
|
|
17
|
|
|
protected $status = 1; |
|
18
|
|
|
|
|
19
|
|
|
protected $elapsedTime = 0; |
|
20
|
|
|
protected $totalTime = 30; |
|
21
|
|
|
|
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
protected $startTime = 0; |
|
24
|
|
|
|
|
25
|
|
|
protected $votes = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $type; |
|
29
|
|
|
|
|
30
|
|
|
protected $ratio = 0.57; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Player |
|
33
|
|
|
*/ |
|
34
|
|
|
private $player; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(Player $player, $type) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->startTime = time(); |
|
39
|
|
|
$this->type = $type; |
|
40
|
|
|
$this->totalTime = 30; |
|
41
|
|
|
$this->player = $player; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
public function castYes($login) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->votes[$login] = self::VOTE_YES; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function castNo($login) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->votes[$login] = self::VOTE_NO; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
public function getYes() |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$value = 0; |
|
61
|
|
|
foreach ($this->votes as $login => $vote) { |
|
62
|
|
|
if ($vote === self::VOTE_YES) { |
|
63
|
|
|
$value++; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
function updateVote($time) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$this->elapsedTime = $time - $this->startTime; |
|
|
|
|
|
|
73
|
|
|
if ($this->elapsedTime > $this->totalTime) { |
|
74
|
|
|
$this->status = self::STATUS_FAILED; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$total = $this->getYes() + $this->getNo(); |
|
78
|
|
|
if ($total > 0) { |
|
79
|
|
|
if (($this->getYes() / $total) > $this->ratio) { |
|
80
|
|
|
$this->status = self::STATUS_PASSED; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (1 - ($this->getYes() / $total) > 0.9) { |
|
84
|
|
|
$this->status = self::STATUS_FAILED; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public function getNo() |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$value = 0; |
|
97
|
|
|
foreach ($this->votes as $login => $vote) { |
|
98
|
|
|
if ($vote === self::VOTE_NO) { |
|
99
|
|
|
$value++; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $value; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getStartTime(): int |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->startTime; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return float |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getTotalTime(): int |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->totalTime; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return int |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getElapsedTime(): int |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->elapsedTime; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return float |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getRatio(): float |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->ratio; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param float $ratio |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setRatio(float $ratio) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->ratio = $ratio; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getType(): string |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->type; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return Player |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getPlayer(): Player |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->player; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return int |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getStatus(): int |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->status; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param int $status |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setStatus(int $status) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->status = $status; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
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.