1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @property integer $id |
4
|
|
|
* @property array $items |
5
|
|
|
* @property string $item_type |
6
|
|
|
* @property integer $page |
7
|
|
|
* @property integer $owned_baits |
8
|
|
|
* @property integer $owned_items |
9
|
|
|
* @property array $success |
10
|
|
|
* @property CPagination $pagination |
11
|
|
|
* @property integer $count |
12
|
|
|
* @property integer $transactionId |
13
|
|
|
* @property integer $levelLimit |
14
|
|
|
* @property integer $nextItemsLevel |
15
|
|
|
*/ |
16
|
|
|
class Shop extends CModel |
17
|
|
|
{ |
18
|
|
|
const TYPE_BAIT = 'bait'; |
19
|
|
|
const TYPE_ITEM = 'item'; |
20
|
|
|
const TYPE_PART = 'part'; |
21
|
|
|
|
22
|
|
|
private $id; |
23
|
|
|
private $items = []; |
24
|
|
|
private $item_type; |
25
|
|
|
private $page = 0; |
26
|
|
|
private $pagination; |
27
|
|
|
private $count; |
28
|
|
|
private $transactionId; |
29
|
|
|
private $success = ['setSold'=>false]; |
30
|
|
|
|
31
|
|
|
public function attributeNames() |
32
|
|
|
{ |
33
|
|
|
return []; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getId() |
37
|
|
|
{ |
38
|
|
|
return $this->id; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getItems() |
42
|
|
|
{ |
43
|
|
|
return $this->items; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getOwned_baits() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return Yii::app()->player->model->owned_baits; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getOwned_items() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
return Yii::app()->player->model->owned_items; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getSuccess() |
57
|
|
|
{ |
58
|
|
|
return $this->success; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getPagination() |
62
|
|
|
{ |
63
|
|
|
return $this->pagination; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getCount() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return $this->count; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getTransactionId() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
return $this->transactionId; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getLevelLimit() |
77
|
|
|
{ |
78
|
|
|
$player = Yii::app()->player->model; |
|
|
|
|
79
|
|
|
$levelLimit = $player->level; |
80
|
|
|
if ($player->black_market) { |
81
|
|
|
$levelLimit += 2; |
82
|
|
|
} |
83
|
|
|
return (int)$levelLimit; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function getNextItemsLevel() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$nextLevel = Yii::app()->db->createCommand() |
89
|
|
|
->select('level') |
90
|
|
|
->from($this->item_type.'s') |
91
|
|
|
->where('level > :level', [':level'=>Yii::app()->player->model->level]) |
92
|
|
|
->order('level ASC') |
93
|
|
|
->limit(1) |
94
|
|
|
->queryScalar(); |
95
|
|
|
return (int)$nextLevel; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setItem_type($type) |
99
|
|
|
{ |
100
|
|
|
$this->item_type = $type; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setPage($page) |
104
|
|
|
{ |
105
|
|
|
$this->page = $page; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setId($id) |
109
|
|
|
{ |
110
|
|
|
$this->id = (int)$id; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function fetchSets() |
114
|
|
|
{ |
115
|
|
|
$res = Yii::app()->db->createCommand() |
116
|
|
|
->select('id, parts, title') |
117
|
|
|
->from('itemsets') |
118
|
|
|
->order('id DESC') |
119
|
|
|
->queryAll(); |
120
|
|
|
|
121
|
|
|
foreach ($res as $item) { |
122
|
|
|
$parts = explode(',', $item['parts']); |
123
|
|
|
|
124
|
|
|
$ownedOne = false; |
|
|
|
|
125
|
|
|
$skill = 0; |
|
|
|
|
126
|
|
|
$constructable = true; |
127
|
|
|
foreach ($parts as $part) { |
128
|
|
|
$i = new Item(); |
|
|
|
|
129
|
|
|
$i->id = $part; |
|
|
|
|
130
|
|
|
$i->item_type = $this->item_type; |
|
|
|
|
131
|
|
|
$i->fetch(); |
132
|
|
|
$skill += $i->skill; |
|
|
|
|
133
|
|
|
$item['items'][$part] = $i; |
134
|
|
|
|
135
|
|
|
if ($i->owned) { |
|
|
|
|
136
|
|
|
$ownedOne = true; |
137
|
|
|
} else { |
138
|
|
|
$constructable = false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
$item['skill_multiplicator'] = $skill; |
143
|
|
|
$item['constructable'] = $constructable; |
|
|
|
|
144
|
|
|
$item['sold'] = false; |
|
|
|
|
145
|
|
|
if ($ownedOne) { |
146
|
|
|
$this->items[$item['id']] = $item; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function fetchItems() |
152
|
|
|
{ |
153
|
|
|
$limit = Yii::app()->params['shopItemsPerPage']; |
|
|
|
|
154
|
|
|
$levelLimit = $this->levelLimit; |
155
|
|
|
|
156
|
|
|
$this->count = Yii::app()->db->createCommand() |
157
|
|
|
->select('COUNT(*) AS count') |
158
|
|
|
->from($this->item_type.'s') |
159
|
|
|
->where('level <= :level', [':level'=>$levelLimit]) |
160
|
|
|
->queryScalar(); |
161
|
|
|
|
162
|
|
|
$res = Yii::app()->db->createCommand() |
163
|
|
|
->select('id') |
164
|
|
|
->from($this->item_type.'s') |
165
|
|
|
->where('level <= :level', [':level'=>$levelLimit]) |
166
|
|
|
->order('skill DESC, level DESC') |
167
|
|
|
->limit($limit, ($this->page * $limit) - $limit) // the trick is here! |
168
|
|
|
->queryAll(); |
169
|
|
|
|
170
|
|
|
$this->pagination = new CPagination($this->count); |
171
|
|
|
$this->pagination->setPageSize(Yii::app()->params['shopItemsPerPage']); |
172
|
|
|
|
173
|
|
|
foreach ($res as $item) { |
174
|
|
|
$i = new Item(); |
|
|
|
|
175
|
|
|
$i->id = $item['id']; |
|
|
|
|
176
|
|
|
$i->item_type = $this->item_type; |
|
|
|
|
177
|
|
|
$i->fetch(); |
178
|
|
|
|
179
|
|
|
$this->items[$item['id']] = $i; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function fetchPlayersItems() |
184
|
|
|
{ |
185
|
|
|
$limit = Yii::app()->params['shopItemsPerPage']; |
186
|
|
|
|
187
|
|
|
$this->count = Yii::app()->db->createCommand() |
188
|
|
|
->select('COUNT(*) AS count') |
189
|
|
|
->from('users_'.$this->item_type.'s') |
190
|
|
|
->where('uid=:uid AND item_count > 0', [':uid'=>Yii::app()->player->uid]) |
191
|
|
|
->queryScalar(); |
192
|
|
|
|
193
|
|
|
$res = Yii::app()->db->createCommand() |
194
|
|
|
->select('item_id') |
195
|
|
|
->from('users_'.$this->item_type.'s') |
196
|
|
|
->where('uid=:uid AND item_count > 0', [':uid'=>Yii::app()->player->uid]) |
197
|
|
|
->order('skill DESC, item_id DESC') |
198
|
|
|
->limit($limit, ($this->page * $limit) - $limit) // the trick is here! |
199
|
|
|
->queryAll(); |
200
|
|
|
|
201
|
|
|
$this->pagination = new CPagination($this->count); |
202
|
|
|
$this->pagination->setPageSize(Yii::app()->params['shopItemsPerPage']); |
203
|
|
|
|
204
|
|
|
foreach ($res as $item) { |
205
|
|
|
$i = new Item(); |
|
|
|
|
206
|
|
|
$i->id = $item['item_id']; |
|
|
|
|
207
|
|
|
if ($i->id < 1000) { |
|
|
|
|
208
|
|
|
$i->item_type = $this->item_type; |
|
|
|
|
209
|
|
|
$i->fetch(); |
210
|
|
|
} else { |
211
|
|
|
$i->item_type = $this->item_type; |
|
|
|
|
212
|
|
|
$i->fetchSet(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->items[$item['item_id']] = $i; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function buyItem($id, $amount) |
220
|
|
|
{ |
221
|
|
|
if (!isset($this->items[$id])) { |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$this->transactionId = $id; |
226
|
|
|
$i = $this->items[$id]; |
|
|
|
|
227
|
|
|
$i->buy($amount); |
228
|
|
|
if ($i->success) { |
229
|
|
|
$b = Yii::app()->badge->model; |
230
|
|
|
|
231
|
|
View Code Duplication |
if ($i->item_type == Item::TYPE_BAIT) { |
|
|
|
|
232
|
|
|
Yii::app()->player->model->owned_baits = Yii::app()->player->model->owned_baits + $amount; |
233
|
|
|
$b->triggerBaits(Yii::app()->player->model->owned_baits); |
234
|
|
|
} |
235
|
|
View Code Duplication |
if ($i->item_type == Item::TYPE_ITEM) { |
|
|
|
|
236
|
|
|
Yii::app()->player->model->owned_items = Yii::app()->player->model->owned_items + $amount; |
237
|
|
|
$b->triggerItems(Yii::app()->player->model->owned_items); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
(new Skill)->updateExtended(); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function sellItem($id, $amount) |
245
|
|
|
{ |
246
|
|
|
if (!isset($this->items[$id])) { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$this->transactionId = $id; |
251
|
|
|
$i = $this->items[$id]; |
|
|
|
|
252
|
|
|
$i->sell($amount); |
253
|
|
|
if ($i->success) { |
254
|
|
View Code Duplication |
if ($i->item_type == Item::TYPE_BAIT) { |
|
|
|
|
255
|
|
|
Yii::app()->player->model->owned_baits = Yii::app()->player->model->owned_baits - $amount; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
View Code Duplication |
if ($i->item_type == Item::TYPE_ITEM) { |
|
|
|
|
259
|
|
|
Yii::app()->player->model->owned_items = Yii::app()->player->model->owned_items - $amount; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
(new Skill)->updateExtended(); |
263
|
|
|
|
264
|
|
|
$setId = $id > 999 ? $id[0] : 0; |
265
|
|
|
if ($setId) { |
266
|
|
|
Yii::app()->badge->model->triggerSet($setId, true); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function constructItem($id) |
272
|
|
|
{ |
273
|
|
|
if (!isset($this->items[$id])) { |
274
|
|
|
throw new CFlashException('A kiválasztott szett nem létezik.'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$set = $this->items[$id]; |
278
|
|
|
if (!$set['constructable']) { |
279
|
|
|
throw new CFlashException('Nem rendelkezel a kiválasztott szett összes elemével.'); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (!Yii::app()->player->model->freeSlots) { |
283
|
|
|
throw new CFlashException('Nincs szabad helyed, most nem tudod összeszerelni a szettet. Tipp: adj el egy felszerelést vagy növeld meg a teherbírásodat.'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$player = Yii::app()->player->model; |
287
|
|
|
if ($player->energy < $player->energy_max) { |
288
|
|
|
throw new CFlashException('A szett elkészítéséhez az összes energiádra szükség lesz.'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
foreach ($set['items'] as $i) { |
292
|
|
|
$remaining = $i->owned - 1; |
293
|
|
|
//remove from inventory |
294
|
|
|
Yii::app()->db |
295
|
|
|
->createCommand("UPDATE users_parts SET item_count=:remaining WHERE uid=:uid AND item_id=:item_id") |
|
|
|
|
296
|
|
|
->bindValues([':uid'=>$player->uid, 'item_id'=>(int)$i->id, ':remaining'=>$remaining]) |
297
|
|
|
->execute(); |
298
|
|
|
$set['items'][$i->id]->owned--; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
//the id of new item |
302
|
|
|
$itemId = $set['id'] . sprintf('%03d', $player->level); |
303
|
|
|
|
304
|
|
|
//add created item to inventory |
305
|
|
|
$update = Yii::app()->db |
306
|
|
|
->createCommand("UPDATE users_items SET item_count=item_count+:amount WHERE uid=:uid AND item_id=:item_id") |
|
|
|
|
307
|
|
|
->bindValues([':uid'=>$player->uid, 'item_id'=>(int)$itemId, ':amount'=>1]) |
308
|
|
|
->execute(); |
309
|
|
|
|
310
|
|
|
if (!$update) { |
311
|
|
|
$best = Yii::app()->db->createCommand() |
312
|
|
|
->select('skill, price') |
313
|
|
|
->from('items') |
314
|
|
|
->where('level <= :level', [':level'=>Yii::app()->player->model->level]) |
315
|
|
|
->order('skill DESC, level DESC') |
316
|
|
|
->limit(1) |
317
|
|
|
->queryRow(); |
318
|
|
|
|
319
|
|
|
Yii::app()->db->createCommand() |
320
|
|
|
->insert('users_items', [ |
321
|
|
|
'uid'=>$player->uid, |
322
|
|
|
'item_id'=>(int)$itemId, |
323
|
|
|
'item_count'=>1, |
324
|
|
|
'skill'=>(int)$best['skill'] * $set['skill_multiplicator'], |
325
|
|
|
'price'=>(int)$best['price'] * $set['skill_multiplicator'] * 2, |
326
|
|
|
]); |
327
|
|
|
} |
328
|
|
|
$this->items[$id]['sold'] = true; |
329
|
|
|
$this->success['setSold'] = true; |
330
|
|
|
|
331
|
|
|
(new Skill)->updateExtended(); |
332
|
|
|
|
333
|
|
|
//decrement energy |
334
|
|
|
$player->rewriteAttributes(['energy'=>0]); |
335
|
|
|
|
336
|
|
|
Yii::app()->badge->model->triggerSet($set['id']); |
337
|
|
|
} |
338
|
|
|
} |
|
|
|
|
339
|
|
|
|
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.