|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @property int $id |
|
4
|
|
|
* @property boolean $active |
|
5
|
|
|
* @property string $opponentLink |
|
6
|
|
|
* @property integer $caller |
|
7
|
|
|
* @property integer $opponent |
|
8
|
|
|
* @property string $name_caller |
|
9
|
|
|
* @property string $name_opponent |
|
10
|
|
|
* @property integer $cnt_won_caller |
|
11
|
|
|
* @property integer $cnt_won_opponent |
|
12
|
|
|
* @property integer $point_caller |
|
13
|
|
|
* @property integer $point_opponent |
|
14
|
|
|
* @property integer $loot_caller |
|
15
|
|
|
* @property integer $loot_opponent |
|
16
|
|
|
* @property integer $startTime |
|
17
|
|
|
* @property integer $endTime |
|
18
|
|
|
* @property array $listDuels |
|
19
|
|
|
* @property string $winner |
|
20
|
|
|
*/ |
|
21
|
|
|
class Challenge extends CModel |
|
22
|
|
|
{ |
|
23
|
|
|
const TIME_LIMIT_HOURS = 8; |
|
|
|
|
|
|
24
|
|
|
const TIME_LIMIT_LASTCALL_HOURS = 4; |
|
25
|
|
|
|
|
26
|
|
|
private $id; |
|
27
|
|
|
private $active = false; |
|
28
|
|
|
private $caller; |
|
29
|
|
|
private $opponent; |
|
30
|
|
|
private $loot_caller; |
|
31
|
|
|
private $loot_opponent; |
|
32
|
|
|
private $cnt_won_caller; |
|
33
|
|
|
private $cnt_won_opponent; |
|
34
|
|
|
private $point_caller; |
|
35
|
|
|
private $point_opponent; |
|
36
|
|
|
private $name_caller; |
|
37
|
|
|
private $name_opponent; |
|
38
|
|
|
private $winner; |
|
39
|
|
|
private $created; |
|
40
|
|
|
private $listDuels = []; |
|
41
|
|
|
|
|
42
|
|
|
public function attributeNames() |
|
43
|
|
|
{ |
|
44
|
|
|
return []; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getId() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->id; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getActive() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->active; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getOpponentLink($clubID) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$oppID = $clubID == $this->caller ? $this->opponent : $this->caller; |
|
|
|
|
|
|
60
|
|
|
$oppName = $clubID == $this->caller ? $this->name_opponent : $this->name_caller; |
|
61
|
|
|
|
|
62
|
|
|
return CHtml::link($oppName, ['club/details', 'id'=>$oppID], ['data-ajax'=>'false']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getCaller() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->caller; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getOpponent() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->opponent; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getName_caller() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
return $this->name_caller; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getName_opponent() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
return $this->name_opponent; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getCnt_won_caller() |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
return $this->cnt_won_caller; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getCnt_won_opponent() |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
return $this->cnt_won_opponent; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getPoint_caller() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
return $this->point_caller; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getPoint_opponent() |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
return $this->point_opponent; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getLoot_caller() |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
return $this->loot_caller; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getLoot_opponent() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
return $this->loot_opponent; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getStartTime() |
|
116
|
|
|
{ |
|
117
|
|
|
return strtotime($this->created) + 1800; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getEndTime() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->startTime + 1800; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getListDuels() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->listDuels; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getWinner() |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
return $this->winner; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function setId($id) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->id = (int)$id; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setCaller($clubID) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->caller = (int)$clubID; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function setOpponent($clubID) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->opponent = (int)$clubID; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
View Code Duplication |
public function fetch() |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$res = Yii::app()->db->createCommand() |
|
153
|
|
|
->select('*') |
|
154
|
|
|
->from('challenge') |
|
155
|
|
|
->where('id=:id', [':id'=>$this->id]) |
|
156
|
|
|
->queryRow(); |
|
157
|
|
|
if (!$res) { |
|
158
|
|
|
throw new CHttpException(404, 'A lekért verseny nem található.'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
foreach ($res as $k => $v) { |
|
162
|
|
|
$this->$k = $v; |
|
163
|
|
|
} |
|
164
|
|
|
$this->active = !$this->winner; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
View Code Duplication |
public function fetchActiveChallenge() |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$res = Yii::app()->db->createCommand() |
|
170
|
|
|
->select('*') |
|
171
|
|
|
->from('challenge') |
|
172
|
|
|
->where('caller=:id OR opponent=:id', [':id'=>$this->opponent]) |
|
173
|
|
|
->order('created DESC') |
|
174
|
|
|
->limit(1) |
|
175
|
|
|
->queryRow(); |
|
176
|
|
|
if (!$res) { |
|
177
|
|
|
return false; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
foreach ($res as $k => $v) { |
|
181
|
|
|
$this->$k = $v; |
|
182
|
|
|
} |
|
183
|
|
|
$this->active = !$this->winner; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function hasActiveChallenge($clubID) |
|
187
|
|
|
{ |
|
188
|
|
|
$res = Yii::app()->db->createCommand() |
|
189
|
|
|
->select('id, winner') |
|
190
|
|
|
->from('challenge') |
|
191
|
|
|
->where('caller=:id OR opponent=:id', [':id'=>(int)$clubID]) |
|
192
|
|
|
->order('created DESC') |
|
193
|
|
|
->limit(1) |
|
194
|
|
|
->queryRow(); |
|
195
|
|
|
return $res['id'] && !$res['winner']; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param integer $opponentID |
|
200
|
|
|
*/ |
|
201
|
|
View Code Duplication |
public function underCallTimeLimit($clubID, $opponentID) |
|
|
|
|
|
|
202
|
|
|
{ |
|
203
|
|
|
$res = Yii::app()->db->createCommand() |
|
204
|
|
|
->select('id, created') |
|
205
|
|
|
->from('challenge') |
|
206
|
|
|
->where('caller=:cid AND opponent=:oid', [':cid'=>(int)$clubID, ':oid'=>$opponentID]) |
|
207
|
|
|
->order('created DESC') |
|
208
|
|
|
->limit(1) |
|
209
|
|
|
->queryRow(); |
|
210
|
|
|
return time() - strtotime($res['created']) <= self::TIME_LIMIT_HOURS * 3600; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param integer $clubID |
|
215
|
|
|
*/ |
|
216
|
|
View Code Duplication |
public function underLastCallTimeLimit($clubID) |
|
|
|
|
|
|
217
|
|
|
{ |
|
218
|
|
|
$res = Yii::app()->db->createCommand() |
|
219
|
|
|
->select('id, created') |
|
220
|
|
|
->from('challenge') |
|
221
|
|
|
->where('caller=:cid', [':cid'=>(int)$clubID]) |
|
222
|
|
|
->order('created DESC') |
|
223
|
|
|
->limit(1) |
|
224
|
|
|
->queryRow(); |
|
225
|
|
|
return time() - strtotime($res['created']) <= self::TIME_LIMIT_LASTCALL_HOURS * 3600; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param Club $opponent |
|
230
|
|
|
*/ |
|
231
|
|
|
public function callToChallenge($opponent) |
|
232
|
|
|
{ |
|
233
|
|
|
$player = Yii::app()->player->model; |
|
234
|
|
|
|
|
235
|
|
|
//requirements |
|
236
|
|
|
if (!$player->in_club) { |
|
237
|
|
|
throw new CFlashException('Csak egy klub tagjaként vagy alapítójaként hívhatsz ki versenyre másik klubot.'); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if ($this->active) { |
|
241
|
|
|
throw new CFlashException('Ez a klub már részt vesz egy másik versenyben.'); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if (!$opponent->would_compete) { |
|
|
|
|
|
|
245
|
|
|
throw new CFlashException('Ez a klub nem szeretne versenyezni.'); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
if ($this->hasActiveChallenge($player->in_club)) { |
|
249
|
|
|
throw new CFlashException('A klubod már részt vesz egy versenyben.'); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if ($this->underCallTimeLimit($player->in_club, $this->opponent)) { |
|
253
|
|
|
throw new CFlashException('Az elmúlt '. self::TIME_LIMIT_HOURS .' órában már kihívtátok ezt a klubot. Unalmas volna ilyen gyakran játszani ellenük. :)'); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
//caller club |
|
257
|
|
|
$callerClub = Yii::app()->db->createCommand() |
|
258
|
|
|
->select('name, would_compete') |
|
259
|
|
|
->from('club') |
|
260
|
|
|
->where('id=:clubID', [':clubID'=>$player->in_club]) |
|
261
|
|
|
->queryRow(); |
|
262
|
|
|
if (!$callerClub['would_compete']) { |
|
263
|
|
|
throw new CFlashException('Mielőtt versenyre hívsz egy klubot, kapcsold be a saját klubodban a \'versenyezne\' beállítást.'); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
Yii::app()->db->createCommand() |
|
267
|
|
|
->insert('challenge', [ |
|
268
|
|
|
'caller'=>$this->caller, |
|
269
|
|
|
'opponent'=>$this->opponent, |
|
270
|
|
|
'name_caller'=>$callerClub['name'], |
|
271
|
|
|
'name_opponent'=>$opponent->name, |
|
|
|
|
|
|
272
|
|
|
]); |
|
273
|
|
|
//set properties |
|
274
|
|
|
$this->fetchActiveChallenge(); |
|
275
|
|
|
|
|
276
|
|
|
$this->addCommandToStack([ |
|
277
|
|
|
'id'=>$this->id, |
|
278
|
|
|
]); |
|
279
|
|
|
|
|
280
|
|
|
//add reminder |
|
281
|
|
|
$this->addReminder(); |
|
282
|
|
|
|
|
283
|
|
|
return true; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
public function fetchListDuels() |
|
287
|
|
|
{ |
|
288
|
|
|
$res = Yii::app()->db->createCommand() |
|
|
|
|
|
|
289
|
|
|
->select('*') |
|
290
|
|
|
->from('duel') |
|
291
|
|
|
->where('challenge_id=:id', [':id'=>$this->id]) |
|
292
|
|
|
->order('id DESC') |
|
293
|
|
|
->queryAll(); |
|
294
|
|
|
$player = new Player; |
|
295
|
|
|
foreach ($res as $d) { |
|
296
|
|
|
$player->subjectId = $d['caller']; |
|
297
|
|
|
$d['name_caller'] = $player->getSubjectName(); |
|
|
|
|
|
|
298
|
|
|
|
|
299
|
|
|
$player->subjectId = $d['opponent']; |
|
|
|
|
|
|
300
|
|
|
$d['name_opponent'] = $player->getSubjectName(); |
|
301
|
|
|
|
|
302
|
|
|
$d['awards'] = $this->getAwards($d['id'], $d['winner']); |
|
303
|
|
|
|
|
304
|
|
|
$this->listDuels[] = $d; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
private function getAwards($id, $role) |
|
|
|
|
|
|
309
|
|
|
{ |
|
310
|
|
|
$res = Yii::app()->db->createCommand() |
|
311
|
|
|
->select('award_dollar, duel_points, club') |
|
312
|
|
|
->from('duel_player') |
|
313
|
|
|
->where('duel_id=:id AND role=:role', [':id'=>(int)$id, ':role'=>$role]) |
|
314
|
|
|
->queryRow(); |
|
315
|
|
|
return $res; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
private function addCommandToStack($params) |
|
319
|
|
|
{ |
|
320
|
|
|
Yii::app()->db->createCommand() |
|
321
|
|
|
->insert('command_stack', [ |
|
322
|
|
|
'command'=>'endChallenge', |
|
323
|
|
|
'process_time'=>date('Y-m-d H:i:s', time()+3600), //1800+1800 |
|
324
|
|
|
'params'=>CJSON::encode($params) |
|
325
|
|
|
]); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
private function addReminder() |
|
329
|
|
|
{ |
|
330
|
|
|
$redis = Yii::app()->redis->getClient(); |
|
331
|
|
|
|
|
332
|
|
|
$redis->set('reminder:challenge:'.$this->caller, time()+3600); |
|
333
|
|
|
$redis->set('reminder:challenge:'.$this->opponent, time()+3600); |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
|
|
|
|
|
336
|
|
|
|
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.