1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @property integer $uid |
4
|
|
|
* @property integer $skill |
5
|
|
|
* @property integer $chance |
6
|
|
|
* @property integer $energy |
7
|
|
|
* @property integer $avgEnergy |
8
|
|
|
* @property integer $dollar |
9
|
|
|
* @property string $club |
10
|
|
|
* @property boolean $isCaller |
11
|
|
|
* @property boolean $winner |
12
|
|
|
* @property integer $opponent |
13
|
|
|
* @property integer $reqEnergy |
14
|
|
|
* @property integer $reqDollar |
15
|
|
|
* @property integer $awardXp |
16
|
|
|
* @property integer $awardDollar |
17
|
|
|
* @property integer $awardPoints |
18
|
|
|
* @property integer $duelId |
19
|
|
|
*/ |
20
|
|
|
class Competitor extends CModel |
21
|
|
|
{ |
22
|
|
|
protected $uid; |
23
|
|
|
protected $skill; |
24
|
|
|
protected $chance; |
25
|
|
|
protected $energy; |
26
|
|
|
protected $avgEnergy; |
27
|
|
|
protected $dollar; |
28
|
|
|
protected $club; |
29
|
|
|
|
30
|
|
|
protected $isCaller = false; |
31
|
|
|
protected $winner = false; |
|
|
|
|
32
|
|
|
|
33
|
|
|
protected $duelId; |
34
|
|
|
protected $opponent; |
35
|
|
|
|
36
|
|
|
//reqs, awards |
37
|
|
|
protected $reqEnergy = 0; |
|
|
|
|
38
|
|
|
protected $reqDollar = 0; |
|
|
|
|
39
|
|
|
protected $awardXp = 0; |
|
|
|
|
40
|
|
|
protected $awardDollar = 0; |
41
|
|
|
protected $awardPoints = 0; |
42
|
|
|
|
43
|
|
|
protected $mapReqs = ['energy'=>'reqEnergy', 'dollar'=>'reqDollar']; |
|
|
|
|
44
|
|
|
protected $mapAwards = ['xp_all'=>'awardXp', 'xp_delta'=>'awardXp', 'dollar'=>'awardDollar', 'duel_points'=>'awardPoints']; |
45
|
|
|
|
46
|
|
|
public function attributeNames() |
47
|
|
|
{ |
48
|
|
|
return []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getUid() |
52
|
|
|
{ |
53
|
|
|
return $this->uid; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getSkill() |
57
|
|
|
{ |
58
|
|
|
return $this->skill; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getChance() |
62
|
|
|
{ |
63
|
|
|
return $this->chance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getEnergy() |
67
|
|
|
{ |
68
|
|
|
return $this->energy; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getDollar() |
72
|
|
|
{ |
73
|
|
|
return $this->dollar; |
74
|
|
|
} |
75
|
|
|
public function getIsCaller() |
76
|
|
|
{ |
77
|
|
|
return $this->isCaller; |
78
|
|
|
} |
79
|
|
|
public function getWinner() |
80
|
|
|
{ |
81
|
|
|
return $this->winner; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getOpponent() |
85
|
|
|
{ |
86
|
|
|
return $this->opponent; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getAvgEnergy() |
90
|
|
|
{ |
91
|
|
|
return $this->avgEnergy; |
92
|
|
|
} |
93
|
|
|
public function getReqEnergy() |
94
|
|
|
{ |
95
|
|
|
return $this->reqEnergy; |
96
|
|
|
} |
97
|
|
|
public function getReqDollar() |
98
|
|
|
{ |
99
|
|
|
return $this->reqDollar; |
100
|
|
|
} |
101
|
|
|
public function getAwardXp() |
102
|
|
|
{ |
103
|
|
|
return $this->awardXp; |
104
|
|
|
} |
105
|
|
|
public function getAwardDollar() |
106
|
|
|
{ |
107
|
|
|
return $this->awardDollar; |
108
|
|
|
} |
109
|
|
|
public function getAwardPoints() |
110
|
|
|
{ |
111
|
|
|
return $this->awardPoints; |
112
|
|
|
} |
113
|
|
|
public function getClub() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
return $this->club; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
public function setUid($uid) |
120
|
|
|
{ |
121
|
|
|
$this->uid = (int)$uid; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setSkill($skill) |
125
|
|
|
{ |
126
|
|
|
$this->skill = (int)$skill; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function setChance($chance) |
130
|
|
|
{ |
131
|
|
|
$this->chance = (int)$chance; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setEnergy($energy) |
135
|
|
|
{ |
136
|
|
|
$this->energy = (int)$energy; |
137
|
|
|
} |
138
|
|
|
public function setAvgEnergy($avgEnergy) |
139
|
|
|
{ |
140
|
|
|
$this->avgEnergy = (int)$avgEnergy; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setDollar($dollar) |
144
|
|
|
{ |
145
|
|
|
$this->dollar = (int)$dollar; |
146
|
|
|
} |
147
|
|
|
public function setIsCaller($isCaller) |
148
|
|
|
{ |
149
|
|
|
$this->isCaller = (bool)$isCaller; |
150
|
|
|
} |
151
|
|
|
public function setWinner($isWinner) |
152
|
|
|
{ |
153
|
|
|
$this->winner = (int)$isWinner; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setDuelId($id) |
157
|
|
|
{ |
158
|
|
|
$this->duelId = (int)$id; |
159
|
|
|
} |
160
|
|
|
public function setOpponent($opponent) |
161
|
|
|
{ |
162
|
|
|
$this->opponent = $opponent; |
163
|
|
|
} |
164
|
|
|
public function setClub($club) |
165
|
|
|
{ |
166
|
|
|
$this->club = $club; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param integer $duelId |
171
|
|
|
*/ |
172
|
|
|
public function fetchFromLog($duelId) |
173
|
|
|
{ |
174
|
|
|
$role = $this->isCaller ? 'caller' : 'opponent'; |
175
|
|
|
|
176
|
|
|
$res = Yii::app()->db->createCommand() |
177
|
|
|
->select('dp.*, m.user') |
178
|
|
|
->from('duel_player dp') |
179
|
|
|
->join('main m', 'dp.uid=m.uid') |
180
|
|
|
->where('dp.duel_id = :id AND dp.role=:role', [':id'=>$duelId, ':role'=>$role]) |
181
|
|
|
->queryRow(); |
182
|
|
|
|
183
|
|
|
$this->skill = $res['skill']; |
|
|
|
|
184
|
|
|
$this->chance = $res['chance']; |
|
|
|
|
185
|
|
|
$this->energy = $res['energy']; |
|
|
|
|
186
|
|
|
$this->dollar = $res['dollar']; |
|
|
|
|
187
|
|
|
$this->reqEnergy = $res['req_energy']; |
|
|
|
|
188
|
|
|
$this->reqDollar = $res['req_dollar']; |
|
|
|
|
189
|
|
|
$this->awardXp = $res['award_xp']; |
|
|
|
|
190
|
|
|
$this->awardDollar = $res['award_dollar']; |
191
|
|
|
$this->awardPoints = $res['duel_points']; |
192
|
|
|
$this->winner = $res['winner']; |
|
|
|
|
193
|
|
|
$this->club = $res['club']; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
public function play($isWinner) |
196
|
|
|
{ |
197
|
|
|
$this->winner = $isWinner; |
198
|
|
|
|
199
|
|
|
if ($this->winner) { |
200
|
|
|
$this->winPrize(); |
201
|
|
|
$this->updateLeaderboard(); |
202
|
|
|
} else { |
203
|
|
|
$this->losePrize(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function finish($player) |
208
|
|
|
{ |
209
|
|
|
$this->updateAttributes($player); |
210
|
|
|
|
211
|
|
|
$contest = new Contest; |
212
|
|
|
$contest->addPoints($this->uid, Contest::ACT_DUEL, $this->reqEnergy, $this->awardXp, $this->awardDollar); |
213
|
|
|
|
214
|
|
|
$this->log(); |
215
|
|
|
$stat = $this->incrementCounters($player); |
216
|
|
|
$this->addBadges($player, $stat); |
217
|
|
|
|
218
|
|
|
if (!$this->isCaller) { |
219
|
|
|
$this->sendWallMessage(); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
protected function updateAttributes($player) |
223
|
|
|
{ |
224
|
|
|
$player->updateAttributes( |
225
|
|
|
$this->membersToArray($this->mapAwards), |
226
|
|
|
$this->membersToArray($this->mapReqs) |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
protected function membersToArray($map) |
231
|
|
|
{ |
232
|
|
|
$result = []; |
233
|
|
|
foreach ($map as $sql => $member) { |
234
|
|
|
if ($this->$member) { |
235
|
|
|
$result[$sql] = $this->$member; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
return $result; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function resetAwards() |
242
|
|
|
{ |
243
|
|
|
$this->reqEnergy = 0; |
|
|
|
|
244
|
|
|
$this->reqDollar = 0; |
|
|
|
|
245
|
|
|
$this->awardXp = 0; |
|
|
|
|
246
|
|
|
$this->awardDollar = 0; |
247
|
|
|
$this->awardPoints = 0; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function winPrize() |
251
|
|
|
{ |
252
|
|
|
$this->reqEnergy = $this->energy; |
|
|
|
|
253
|
|
|
$this->awardXp = round($this->avgEnergy * ($this->opponent['chance'] / 100)); |
|
|
|
|
254
|
|
|
$this->awardDollar = round($this->opponent['dollar'] * ($this->opponent['chance'] / 100)); |
|
|
|
|
255
|
|
|
$this->awardPoints = round($this->avgEnergy * ($this->compensator($this->opponent['chance']) / 100)); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
protected function losePrize() |
259
|
|
|
{ |
260
|
|
|
$this->reqEnergy = $this->energy; |
261
|
|
|
$this->reqDollar = round($this->dollar * $this->chance / 100); |
|
|
|
|
262
|
|
|
$this->awardXp = round($this->avgEnergy * ($this->chance / 100) / 5); //20% of winners prize |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function compensator($value) |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
$min = Yii::app()->params['competitorCompensatorMin']; |
268
|
|
|
$max = Yii::app()->params['competitorCompensatorMax']; |
269
|
|
|
|
270
|
|
|
if ($value < $min) { |
271
|
|
|
$value = $min; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ($value > $max) { |
275
|
|
|
$value = $max; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $value; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
protected function updateLeaderboard() |
282
|
|
|
{ |
283
|
|
|
if (!$this->awardPoints) { |
284
|
|
|
return false; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
Yii::app()->redis->getClient()->zIncrBy('board_p:'.date('Ym'), $this->awardPoints, $this->uid); |
288
|
|
|
return true; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
protected function log() |
292
|
|
|
{ |
293
|
|
|
$role = $this->isCaller ? 'caller' : 'opponent'; |
294
|
|
|
|
295
|
|
|
$parameters = [ |
296
|
|
|
'duel_id'=>$this->duelId, |
297
|
|
|
'role'=>$role, |
298
|
|
|
'uid'=>$this->uid, |
299
|
|
|
'skill'=>$this->skill, |
300
|
|
|
'chance'=>$this->chance, |
301
|
|
|
'energy'=>$this->energy, |
302
|
|
|
'dollar'=>$this->dollar, |
303
|
|
|
'req_energy'=>$this->reqEnergy, |
304
|
|
|
'req_dollar'=>$this->reqDollar, |
305
|
|
|
'award_xp'=>$this->awardXp, |
306
|
|
|
'award_dollar'=>$this->awardDollar, |
307
|
|
|
'duel_points'=>$this->awardPoints, |
308
|
|
|
'winner'=>(int)$this->winner, |
309
|
|
|
'club'=>$this->club |
310
|
|
|
]; |
311
|
|
|
|
312
|
|
|
Yii::app()->db->createCommand()->insert('duel_player', $parameters); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
protected function incrementCounters($player) |
|
|
|
|
316
|
|
|
{ |
317
|
|
|
//log mission counter |
318
|
|
|
$cell = 'duel_' . ($this->winner ? 'success' : 'fail'); |
|
|
|
|
319
|
|
|
$logger = new Logger; |
|
|
|
|
320
|
|
|
$logger->uid = $this->uid; |
|
|
|
|
321
|
|
|
$logger->level = $player->level; |
|
|
|
|
322
|
|
|
$logger->increment($cell, 1); |
323
|
|
|
return $logger->getCounters(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
protected function addBadges($player, $stat) |
327
|
|
|
{ |
328
|
|
|
$role = $this->isCaller ? 'caller' : 'opponent'; |
329
|
|
|
|
330
|
|
|
$b = new DuelBadgeActivator(); |
|
|
|
|
331
|
|
|
$b->uid = $this->uid; |
332
|
|
|
|
333
|
|
|
$b->triggerDuelFirstWin($this->winner, $role); |
334
|
|
|
$b->triggerDuelSuccess((int)@$stat['duel_success']); |
335
|
|
|
$b->triggerDuelFail((int)@$stat['duel_fail']); |
336
|
|
|
$b->triggerDuelRate((int)@$stat['duel_success'], (int)@$stat['duel_fail']); |
337
|
|
|
$b->triggerDuelMoney($this->awardDollar); |
338
|
|
|
$b->triggerDuelWinChance($this->winner, $this->chance); |
339
|
|
|
$b->triggerDuelLoseChance($this->winner, $this->chance); |
340
|
|
|
$b->triggerDuel2h($role); |
341
|
|
|
|
342
|
|
|
$b->uid = $player->uid; //reset uid |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
protected function sendWallMessage() |
346
|
|
|
{ |
347
|
|
|
$wall = new Wall; |
|
|
|
|
348
|
|
|
$wall->content_type = Wall::TYPE_DUEL; |
|
|
|
|
349
|
|
|
$wall->uid = $this->uid; |
|
|
|
|
350
|
|
|
|
351
|
|
|
$wall->add([ |
352
|
|
|
'duel_id'=>$this->duelId, |
353
|
|
|
'caller_uid'=>$this->opponent['uid'], |
354
|
|
|
'caller_user'=>$this->opponent['user'], |
355
|
|
|
'req_energy'=>$this->reqEnergy, |
356
|
|
|
'req_dollar'=>$this->reqDollar, |
357
|
|
|
'award_xp'=>$this->awardXp, |
358
|
|
|
'award_dollar'=>$this->awardDollar, |
359
|
|
|
'winner'=>$this->winner |
360
|
|
|
]); |
361
|
|
|
} |
362
|
|
|
} |
|
|
|
|
363
|
|
|
|
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.