|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @property integer $id |
|
4
|
|
|
* @property integer $uid |
|
5
|
|
|
* @property string $collect |
|
6
|
|
|
* @property integer $prize |
|
7
|
|
|
* @property string $desciptionId |
|
8
|
|
|
* @property boolean $isValid |
|
9
|
|
|
* @property boolean $isActive |
|
10
|
|
|
* @property integer $secUntilEnd |
|
11
|
|
|
* @property array $winners |
|
12
|
|
|
* @property array $list |
|
13
|
|
|
* @property array $history |
|
14
|
|
|
* @property string $rankDescription |
|
15
|
|
|
* @property integer $maxScore |
|
16
|
|
|
* @property integer $lastId |
|
17
|
|
|
* @property integer $prizePerWinner |
|
18
|
|
|
*/ |
|
19
|
|
|
class ContestList extends CModel |
|
20
|
|
|
{ |
|
21
|
|
|
const LIFETIME = 172800; //2 days |
|
|
|
|
|
|
22
|
|
|
const BOARD_RANGE = 7; |
|
23
|
|
|
|
|
24
|
|
|
private $id; |
|
25
|
|
|
private $uid; |
|
26
|
|
|
private $isValid; |
|
27
|
|
|
private $collect; |
|
28
|
|
|
private $prize; |
|
29
|
|
|
private $maxScore; |
|
30
|
|
|
private $list; |
|
31
|
|
|
private $winners; |
|
32
|
|
|
private $history; |
|
33
|
|
|
private $collectTypes = [ |
|
34
|
|
|
'xp','xp_duel','xp_mission', |
|
35
|
|
|
'dollar','dollar_duel','dollar_mission' |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
public function attributeNames() |
|
39
|
|
|
{ |
|
40
|
|
|
return []; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getId() |
|
44
|
|
|
{ |
|
45
|
|
|
return (int)$this->id; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getCollect() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
return $this->collect; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getPrize() |
|
54
|
|
|
{ |
|
55
|
|
|
return (int)$this->prize; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getDescriptionId() |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
if (!in_array($this->collect, $this->collectTypes)) { |
|
61
|
|
|
return 'xp'; |
|
62
|
|
|
} |
|
63
|
|
|
return $this->collect; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getIsValid() |
|
67
|
|
|
{ |
|
68
|
|
|
if (is_null($this->isValid)) { |
|
69
|
|
|
$this->isValid = Yii::app()->redis->getClient()->exists('contest:list:'.$this->getId().':created'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return (bool)$this->isValid; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getIsActive() |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$this->id) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$active = (int)Yii::app()->redis->getClient()->get('contest:active'); |
|
82
|
|
|
return $active == $this->id; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getSecUntilEnd() |
|
86
|
|
|
{ |
|
87
|
|
|
$sue = ($this->id + self::LIFETIME - time()); |
|
88
|
|
|
return $sue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function hasWinner() |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
return Yii::app()->redis->getClient()->exists('contest:list:'.$this->id.':winners'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getWinners() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->winners; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getList() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->list; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getHistory() |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
if (!$this->history) { |
|
109
|
|
|
$this->history = Yii::app()->redis->getClient()->lRange('contest:log', 0, 5); |
|
110
|
|
|
} |
|
111
|
|
|
return $this->history; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getRankDescription() |
|
115
|
|
|
{ |
|
116
|
|
|
if (!$this->getIsActive()) { |
|
117
|
|
|
return ''; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
121
|
|
|
|
|
122
|
|
|
$key = 'contest:list:'.$this->id.':points'; |
|
|
|
|
|
|
123
|
|
|
$total = $redis->zCard($key); |
|
124
|
|
|
$rank = $redis->zRevRank($key, $this->uid) + 1; |
|
125
|
|
|
|
|
126
|
|
|
if ($rank == 1) { |
|
127
|
|
|
return 'Te vagy a király!'; |
|
128
|
|
|
} else { |
|
129
|
|
|
$percent = round((1 - ($rank / $total)) * 100, 1); |
|
130
|
|
|
if (!$percent) { |
|
131
|
|
|
return 'Húzz bele, ha nyerni szeretnél!'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return 'Jobb vagy, mint a versenytársaid ' . $percent . '%-a!'; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function getMaxScore() |
|
139
|
|
|
{ |
|
140
|
|
|
if (!$this->maxScore) { |
|
141
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
142
|
|
|
$key = 'contest:list:'.$this->id.':points'; |
|
|
|
|
|
|
143
|
|
|
$max = $redis->zRevRange($key, 0, 0, true); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
if (count($max)) { |
|
146
|
|
|
$this->maxScore = array_values($max)[0]; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return (int)$this->maxScore; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function getLastId() |
|
154
|
|
|
{ |
|
155
|
|
|
$id = Yii::app()->redis->getClient()->lINDEX('contest:log', 0); |
|
156
|
|
|
return (int)$id; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function getPrizePerWinner() |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
if (!count($this->winners)) { |
|
162
|
|
|
return Contest::PRIZE; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return ceil(Contest::PRIZE / count($this->winners)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function setId($id) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->id = (int)$id; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function setUid($uid) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->uid = (int)$uid; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function fetchDetails() |
|
179
|
|
|
{ |
|
180
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
|
|
|
|
|
181
|
|
|
$this->collect = $redis->get('contest:list:'.$this->id.':collect'); |
|
182
|
|
|
$this->prize = $redis->get('contest:list:'.$this->id.':prize'); |
|
|
|
|
|
|
183
|
|
|
$this->getMaxScore(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function fetchList() |
|
187
|
|
|
{ |
|
188
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
189
|
|
|
|
|
190
|
|
|
$myRank = $redis->zRevRank('contest:list:'.$this->getId().':points', $this->uid); |
|
191
|
|
|
|
|
192
|
|
|
$range = self::BOARD_RANGE; |
|
193
|
|
|
$min = $myRank - $range > 0 ? $myRank - $range : 0; |
|
|
|
|
|
|
194
|
|
|
$max = $myRank + $range + ($range-$myRank+$min); |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
$item = new Player; |
|
197
|
|
|
$i = $min+1; |
|
|
|
|
|
|
198
|
|
View Code Duplication |
foreach ($redis->zRevRange('contest:list:'.$this->id.':points', $min, $max, true) as $id => $score) { |
|
|
|
|
|
|
199
|
|
|
$item->subjectId = $id; |
|
200
|
|
|
|
|
201
|
|
|
$this->list[$i] = [ |
|
202
|
|
|
'id'=>$id, |
|
203
|
|
|
'name'=>$item->getSubjectName(), |
|
204
|
|
|
'score'=>$score, |
|
205
|
|
|
]; |
|
206
|
|
|
$i++; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function listBestPlayers() |
|
211
|
|
|
{ |
|
212
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
213
|
|
|
if ($this->getIsActive()) { |
|
214
|
|
|
$res = $redis->zRevRangeByScore('contest:list:'.$this->id.':points', $this->maxScore, $this->maxScore); |
|
215
|
|
|
} else { |
|
216
|
|
|
$res = $redis->smembers('contest:list:'.$this->id.':winners'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$list = []; |
|
220
|
|
|
$item = new Player; |
|
221
|
|
|
foreach ($res as $id) { |
|
222
|
|
|
$item->subjectId = $id; |
|
223
|
|
|
|
|
224
|
|
|
$list[$id] = [ |
|
225
|
|
|
'name'=>$item->getSubjectName(), |
|
226
|
|
|
'score'=>$this->maxScore, |
|
227
|
|
|
]; |
|
228
|
|
|
} |
|
229
|
|
|
$this->winners = $list; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function canClaimPrize() |
|
233
|
|
|
{ |
|
234
|
|
|
//is active? |
|
235
|
|
|
if (!$this->hasWinner()) { |
|
236
|
|
|
return false; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
//she is winner? |
|
240
|
|
|
if (!array_key_exists($this->uid, $this->winners)) { |
|
241
|
|
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
//have we the prize/winner? |
|
245
|
|
|
if ($this->prizePerWinner < 1) { |
|
246
|
|
|
return false; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
//she has claimed the prize already? |
|
250
|
|
|
return !Yii::app()->redis->getClient()->exists('contest:list:'.$this->id.':claimed-'.$this->uid); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function claimPrize() |
|
254
|
|
|
{ |
|
255
|
|
|
if (!$this->canClaimPrize()) { |
|
256
|
|
|
return false; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
Yii::app()->player->model->updateAttributes(['dollar'=>$this->prizePerWinner], []); |
|
260
|
|
|
Yii::app()->redis->getClient()->set('contest:list:'.$this->id.':claimed-'.$this->uid, date('Y.m.d. H:i:s')); |
|
261
|
|
|
return true; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function seeContest() |
|
265
|
|
|
{ |
|
266
|
|
|
if (Yii::app()->player->newContest) { |
|
267
|
|
|
Yii::app()->redis->getClient()->set('contest:lastcheck:'.$this->uid, time()); |
|
268
|
|
|
echo 'checked'; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
|
|
|
|
|
272
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.