1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @property integer $uid |
4
|
|
|
* @property array $blackBait |
5
|
|
|
* @property array $packagesSms |
6
|
|
|
*/ |
7
|
|
|
class Store extends CModel |
8
|
|
|
{ |
9
|
|
|
private $uid; |
10
|
|
|
private $missingSetItemPrice = 100; |
11
|
|
|
|
12
|
|
|
public function attributeNames() |
13
|
|
|
{ |
14
|
|
|
return []; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function getUid() |
18
|
|
|
{ |
19
|
|
|
return $this->uid; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getPackagesSms() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
return Yii::app()->params['packagesSms']; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getMissingSetItemPrice() |
28
|
|
|
{ |
29
|
|
|
return $this->missingSetItemPrice; |
30
|
|
|
} |
31
|
|
|
public function setUid($uid) |
32
|
|
|
{ |
33
|
|
|
$this->uid = (int)$uid; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function refillEnergy() |
37
|
|
|
{ |
38
|
|
|
$player = Yii::app()->player->model; |
39
|
|
|
|
40
|
|
|
$logger = new Logger; |
|
|
|
|
41
|
|
|
$logger->key = 'refillEnergy:'.date('Y-m-d').':'.$this->uid; |
|
|
|
|
42
|
|
|
$logger->addToSet('----start: '.date('H:i:s').'----'); |
43
|
|
|
$logger->addToSet('gold:'.$player->gold.', energy: ' . $player->energy.'/'.$player->energy_max); |
44
|
|
|
|
45
|
|
|
if ($player->gold < 20) { |
46
|
|
|
throw new CFlashException('Nincs elég aranyad az energiaital kifizetésére.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$logger->addToSet('gold > 20'); |
50
|
|
|
if ($player->energy_missing < 3) { |
51
|
|
|
throw new CFlashException('Kevesebb, mint 3 energiára van szükséged. Emiatt nem érdemes energiaitalt innod.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$logger->addToSet('energy_missing > 3'); |
55
|
|
|
|
56
|
|
|
$player->updateAttributes(['energy'=>$player->energy_missing], ['gold'=>20]); |
57
|
|
|
$logger->addToSet('energy increased, gold decreased'); |
58
|
|
|
|
59
|
|
|
$logger->addToSet('---- end: '.date('H:i:s').'----'); |
60
|
|
|
|
61
|
|
|
Yii::app()->badge->model->triggerSimple('energy_drink'); |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function listMissingSetItems() |
66
|
|
|
{ |
67
|
|
|
$shop = new Shop; |
|
|
|
|
68
|
|
|
$shop->item_type = Shop::TYPE_PART; |
|
|
|
|
69
|
|
|
$shop->fetchSets(); |
70
|
|
|
$sets = $shop->items; |
|
|
|
|
71
|
|
|
|
72
|
|
|
$missing = []; |
73
|
|
|
foreach ($sets as $set) { |
74
|
|
|
foreach ($set['items'] as $item) { |
75
|
|
|
if (!$item->owned) { |
76
|
|
|
$missing[] = $item; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $missing; |
82
|
|
|
} |
83
|
|
|
public function buySetItem($id) |
84
|
|
|
{ |
85
|
|
|
$player = Yii::app()->player->model; |
|
|
|
|
86
|
|
|
$userName = $player->user; |
87
|
|
|
|
88
|
|
|
//check is we have the selected item in the missing list |
89
|
|
|
$missingList = $this->listMissingSetItems(); |
90
|
|
|
$buyItem = null; |
|
|
|
|
91
|
|
|
foreach ($missingList as $item) { |
92
|
|
|
if ($item['id'] == $id) { |
93
|
|
|
$buyItem = $item; |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
//check it it is an existing item |
98
|
|
|
if ($buyItem == null) { |
99
|
|
|
throw new CFlashException('A keresett elem nem létezik.'); |
100
|
|
|
} |
101
|
|
|
//check the amount of gold |
102
|
|
|
if ($player->gold < $this->missingSetItemPrice) { |
103
|
|
|
throw new CFlashException('Nincs elég aranyad a szett elem kifizetésére.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
//check is we bought one this week of this type |
107
|
|
|
$setItemType = strtok($buyItem['title'], ' '); |
108
|
|
|
$logKey = 'debug:buySetItem:' . date('Y:W') . ':' . strtolower($setItemType); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$hash = new ARedisHash($logKey); |
111
|
|
|
if ($hash->offsetExists($userName)) { |
112
|
|
|
throw new CFlashException($setItemType .' típusú szett elemet már vettél a héten. Jövő héten vásárolhatsz belőle újból.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
//buy the item |
116
|
|
|
$i = new Item; |
|
|
|
|
117
|
|
|
$i->id = $buyItem['id']; |
|
|
|
|
118
|
|
|
$i->item_type = Item::TYPE_PART; |
|
|
|
|
119
|
|
|
$i->fetch(); |
120
|
|
|
$i->buy(1); |
121
|
|
|
|
122
|
|
|
//pay the price |
123
|
|
|
$player->updateAttributes([], ['gold'=>$this->missingSetItemPrice]); |
124
|
|
|
|
125
|
|
|
//log the purchase |
126
|
|
|
$hash->add($userName, date('H:i:s') . ', ' . $buyItem['title']); |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
} |
|
|
|
|
131
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.