|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @property integer $activeId |
|
4
|
|
|
* @property string $recommendedCollect |
|
5
|
|
|
* @property integer $recommendedPrize |
|
6
|
|
|
* @property string $collect |
|
7
|
|
|
*/ |
|
8
|
|
|
class Contest extends CModel |
|
9
|
|
|
{ |
|
10
|
|
|
const LIFETIME = 172800; //2 days |
|
11
|
|
|
const PRIZE = 500; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
const ID_ACTIVE = 'contest:active'; |
|
|
|
|
|
|
14
|
|
|
const ID_LIST = 'contest:list:'; |
|
|
|
|
|
|
15
|
|
|
const ID_RECOMMENDED_COLLECT = 'contest:r_collect'; |
|
16
|
|
|
const ID_RECOMMENDED_PRIZE = 'contest:r_prize'; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
const ACT_MISSION = 1; |
|
19
|
|
|
const ACT_DUEL = 2; |
|
|
|
|
|
|
20
|
|
|
const ACT_CLUB = 3; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
private $activeId; |
|
23
|
|
|
private $recommendedCollect; |
|
24
|
|
|
private $recommendedPrize; |
|
25
|
|
|
private $collect; |
|
26
|
|
|
private $collectParam; |
|
27
|
|
|
private $collectTypes = [ |
|
28
|
|
|
'xp','xp_duel','xp_mission', |
|
29
|
|
|
'dollar','dollar_duel','dollar_mission' |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
public function attributeNames() |
|
33
|
|
|
{ |
|
34
|
|
|
return []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getCollect() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
if (!$this->collect) { |
|
40
|
|
|
$this->collect = Yii::app()->redis->getClient()->get(self::ID_LIST . $this->activeId . ':collect'); |
|
41
|
|
|
} |
|
42
|
|
|
return $this->collect; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getActiveId() |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
if (!$this->activeId) { |
|
48
|
|
|
$this->activeId = Yii::app()->redis->getClient()->get(self::ID_ACTIVE); |
|
49
|
|
|
} |
|
50
|
|
|
return $this->activeId; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getRecommendedCollect() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
View Code Duplication |
if (!$this->recommendedCollect) { |
|
|
|
|
|
|
56
|
|
|
$this->recommendedCollect = Yii::app()->redis->getClient()->get(self::ID_RECOMMENDED_COLLECT); |
|
57
|
|
|
Yii::app()->redis->getClient()->del(self::ID_RECOMMENDED_COLLECT); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!in_array($this->recommendedCollect, $this->collectTypes)) { |
|
61
|
|
|
$randomId = array_rand($this->collectTypes); |
|
|
|
|
|
|
62
|
|
|
$this->recommendedCollect = $this->collectTypes[$randomId]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->recommendedCollect; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getRecommendedPrize() |
|
69
|
|
|
{ |
|
70
|
|
View Code Duplication |
if (!$this->recommendedPrize) { |
|
|
|
|
|
|
71
|
|
|
$this->recommendedPrize = Yii::app()->redis->getClient()->get(self::ID_RECOMMENDED_PRIZE); |
|
72
|
|
|
Yii::app()->redis->getClient()->del(self::ID_RECOMMENDED_PRIZE); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (!$this->recommendedPrize) { |
|
76
|
|
|
$this->recommendedPrize = self::PRIZE; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return (int)$this->recommendedPrize; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function create() |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->activeId) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
89
|
|
|
|
|
90
|
|
|
$this->activeId = time(); |
|
91
|
|
|
|
|
92
|
|
|
$redis->set('contest:active', $this->activeId); |
|
93
|
|
|
$redis->lPush('contest:log', $this->activeId); //log of contests |
|
94
|
|
|
|
|
95
|
|
|
$redis->set(self::ID_LIST . $this->activeId.':collect', $this->getRecommendedCollect()); |
|
96
|
|
|
$redis->set(self::ID_LIST . $this->activeId.':prize', $this->getRecommendedPrize()); |
|
97
|
|
|
$redis->set(self::ID_LIST . $this->activeId.':created', date('Y.m.d. H:i:s', $this->getActiveId())); |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param integer $energy |
|
103
|
|
|
*/ |
|
104
|
|
|
public function addPoints($uid, $activity, $energy, $xp, $dollar) |
|
105
|
|
|
{ |
|
106
|
|
|
//todo: log common collecting |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
if (!$this->getActiveId()) { |
|
109
|
|
|
return false; //no active contest |
|
110
|
|
|
} |
|
111
|
|
|
if (!$this->validateContest($activity, $xp, $dollar)) { |
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
//collect |
|
116
|
|
|
$points = 0; |
|
117
|
|
|
if ($this->collectParam == 'xp') { |
|
118
|
|
|
$points = $xp; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ($this->collectParam == 'dollar') { |
|
122
|
|
|
$points = $dollar; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
Yii::app()->redis->getClient()->zIncrBy(self::ID_LIST . $this->getActiveId() . ':points', $points, $uid); |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function complete() |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
132
|
|
|
|
|
133
|
|
|
$completed = $redis->del(self::ID_ACTIVE); |
|
134
|
|
|
if ($completed) { |
|
135
|
|
|
$this->logWinners(); |
|
136
|
|
|
$this->activeId = 0; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $completed; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function logWinners() |
|
143
|
|
|
{ |
|
144
|
|
|
$winners = $this->fetchWinners(); |
|
145
|
|
|
if (!$winners) { |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
150
|
|
|
$b = Yii::app()->badge->model; |
|
|
|
|
|
|
151
|
|
|
foreach ($winners as $winner) { |
|
152
|
|
|
//add badges |
|
153
|
|
|
$b->uid = $winner; |
|
154
|
|
|
$b->triggerSimple('win_contest'); |
|
155
|
|
|
|
|
156
|
|
|
//add winner to the log |
|
157
|
|
|
$redis->sadd(self::ID_LIST . $this->activeId . ':winners', $winner); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
private function fetchWinners() |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
164
|
|
|
//get max point |
|
165
|
|
|
$key = self::ID_LIST . $this->activeId . ':points'; |
|
166
|
|
|
$max = $redis->zRevRange($key, 0, 0, true); |
|
167
|
|
|
|
|
168
|
|
|
if (count($max)) { |
|
169
|
|
|
$maxScore = array_values($max)[0]; |
|
170
|
|
|
$winners = $redis->zRevRangeByScore($key, $maxScore, $maxScore); |
|
|
|
|
|
|
171
|
|
|
return $winners; |
|
172
|
|
|
} |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function validateContest($activity, $xp, $dollar) |
|
177
|
|
|
{ |
|
178
|
|
|
$toCollect = $this->getCollect(); |
|
179
|
|
|
|
|
180
|
|
|
//check the lifetime |
|
181
|
|
|
if (time() > $this->activeId + self::LIFETIME) { |
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$valid = true; |
|
186
|
|
|
//check activity+toCollect |
|
187
|
|
|
switch ($toCollect) { |
|
188
|
|
|
case 'xp': |
|
189
|
|
|
if (!$xp) { |
|
190
|
|
|
$valid = false; |
|
191
|
|
|
} |
|
192
|
|
|
$this->collectParam = 'xp'; |
|
193
|
|
|
break; |
|
194
|
|
|
case 'xp_duel': |
|
195
|
|
|
if (!$xp || $activity != self::ACT_DUEL) { |
|
196
|
|
|
$valid = false; |
|
197
|
|
|
} |
|
198
|
|
|
$this->collectParam = 'xp'; |
|
199
|
|
|
break; |
|
200
|
|
|
case 'xp_mission': |
|
201
|
|
|
if (!$xp || $activity != self::ACT_MISSION) { |
|
202
|
|
|
$valid = false; |
|
203
|
|
|
} |
|
204
|
|
|
$this->collectParam = 'xp'; |
|
205
|
|
|
break; |
|
206
|
|
|
case 'dollar': |
|
207
|
|
|
if (!$dollar) { |
|
208
|
|
|
$valid = false; |
|
209
|
|
|
} |
|
210
|
|
|
$this->collectParam = 'dollar'; |
|
211
|
|
|
break; |
|
212
|
|
|
case 'dollar_duel': |
|
213
|
|
|
if (!$dollar || $activity != self::ACT_DUEL) { |
|
214
|
|
|
$valid = false; |
|
215
|
|
|
} |
|
216
|
|
|
$this->collectParam = 'dollar'; |
|
217
|
|
|
break; |
|
218
|
|
|
case 'dollar_mission': |
|
219
|
|
|
if (!$dollar || $activity != self::ACT_MISSION) { |
|
220
|
|
|
$valid = false; |
|
221
|
|
|
} |
|
222
|
|
|
$this->collectParam = 'dollar'; |
|
223
|
|
|
break; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $valid; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
|
|
|
|
|
229
|
|
|
|
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.