1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @property integer $caller |
4
|
|
|
* @property integer $opponent |
5
|
|
|
* @property boolean $isChallenge |
6
|
|
|
* @property boolean $played |
7
|
|
|
* @property array $competitors |
8
|
|
|
*/ |
9
|
|
|
class DuelShield extends CModel |
10
|
|
|
{ |
11
|
|
|
private $uid; |
12
|
|
|
private $last = 0; |
13
|
|
|
|
14
|
|
|
public function attributeNames() |
15
|
|
|
{ |
16
|
|
|
return []; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function setUid($uid) |
20
|
|
|
{ |
21
|
|
|
$this->uid = (int)$uid; |
22
|
|
|
$this->fetch(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get the time, until the shield is active |
27
|
|
|
*/ |
28
|
|
|
public function getLifetime() |
29
|
|
|
{ |
30
|
|
|
return $this->last - time(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function fetch() |
34
|
|
|
{ |
35
|
|
|
$last = Yii::app()->db->createCommand() |
36
|
|
|
->select('until') |
37
|
|
|
->from('duel_shield') |
38
|
|
|
->where('uid = :uid', [':uid'=>$this->uid]) |
39
|
|
|
->order('until DESC') |
40
|
|
|
->limit(1) |
41
|
|
|
->queryScalar(); |
42
|
|
|
$this->last = strtotime($last); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getPrices() |
46
|
|
|
{ |
47
|
|
|
// 10min/gold |
48
|
|
|
return [ |
49
|
|
|
15 => ['price'=>2, 'label'=>'15 perc'], //interval => price |
50
|
|
|
30 => ['price'=>3, 'label'=>'30 perc'], |
51
|
|
|
60 => ['price'=>6, 'label'=>'1 óra'], |
52
|
|
|
120 => ['price'=>12, 'label'=>'2 óra'], |
53
|
|
|
180 => ['price'=>18, 'label'=>'3 óra'], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function activate($time) |
58
|
|
|
{ |
59
|
|
|
$player = Yii::app()->player->model; |
60
|
|
|
$logger = new Logger; |
61
|
|
|
$logger->key = 'shield:'.date('Y-m-d').':'.$this->uid; |
|
|
|
|
62
|
|
|
$logger->addToSet('----start: '.date('H:i:s').'----'); |
63
|
|
|
$logger->addToSet('gold:'.$player->gold.', time: ' . $time); |
64
|
|
|
|
65
|
|
|
$prices = $this->getPrices(); |
66
|
|
|
if (!array_key_exists($time, $prices)) { |
67
|
|
|
throw new CFlashException('A pajzsot csak a listában megadott időkre aktiválhatod.'); |
68
|
|
|
} |
69
|
|
|
$price = $prices[$time]['price']; |
70
|
|
|
|
71
|
|
|
$logger->addToSet('price found: ' . $price); |
72
|
|
|
|
73
|
|
|
if ($player->gold < $price) { |
74
|
|
|
throw new CFlashException('Nincs elég aranyad a kiválasztott pajzs aktiválásához.'); |
75
|
|
|
} |
76
|
|
|
$logger->addToSet('gold > price'); |
77
|
|
|
|
78
|
|
|
if ($this->getLifetime() > 0) { |
79
|
|
|
throw new CFlashException('A korábban aktivált pajzsod még érvényes.'); |
80
|
|
|
} |
81
|
|
|
$logger->addToSet('lifeTime = 0, can activate new'); |
82
|
|
|
|
83
|
|
|
$player->updateAttributes([], ['gold'=>$price]); |
84
|
|
|
$logger->addToSet('gold decreased'); |
85
|
|
|
|
86
|
|
|
Yii::app()->db->createCommand()->insert('duel_shield', [ |
87
|
|
|
'uid' => $this->uid, |
88
|
|
|
'until' => date('Y-m-d H:i:s', time()+($time*60)), |
89
|
|
|
]); |
90
|
|
|
$logger->addToSet('shield activated, time:' . $time . ', price:' . $price); |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.