1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @property integer $id |
4
|
|
|
* @property string $item_type |
5
|
|
|
* @property integer $skill |
6
|
|
|
* @property integer $level |
7
|
|
|
* @property string $title |
8
|
|
|
* @property integer $price |
9
|
|
|
* @property integer $price_sell |
10
|
|
|
* @property integer $owned |
11
|
|
|
* @property integer $buy_amount |
12
|
|
|
* @property integer $sell_amount |
13
|
|
|
* @property array $errors |
14
|
|
|
* @property boolean $success |
15
|
|
|
*/ |
16
|
|
|
class Item extends CModel |
17
|
|
|
{ |
18
|
|
|
const TYPE_BAIT = 'bait'; |
|
|
|
|
19
|
|
|
const TYPE_ITEM = 'item'; |
|
|
|
|
20
|
|
|
const TYPE_ITEMSET = 'itemset'; |
21
|
|
|
const TYPE_PART = 'part'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
private $id; |
24
|
|
|
private $item_type; |
25
|
|
|
private $skill; |
26
|
|
|
private $level; |
27
|
|
|
private $title; |
28
|
|
|
private $price; |
29
|
|
|
private $parts; |
|
|
|
|
30
|
|
|
private $owned; |
31
|
|
|
private $buy_amount = []; |
|
|
|
|
32
|
|
|
private $sell_amount = []; |
33
|
|
|
|
34
|
|
|
private $errors = ['dollar'=>false, 'amount'=>false, 'owned'=>false, 'isLast'=>false, 'freeSlots'=>false]; |
35
|
|
|
private $success; |
36
|
|
|
|
37
|
|
|
public function attributeNames() |
38
|
|
|
{ |
39
|
|
|
return []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getId() |
43
|
|
|
{ |
44
|
|
|
return $this->id; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getItem_type() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return $this->item_type; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getSkill() |
53
|
|
|
{ |
54
|
|
|
return $this->skill; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getLevel() |
58
|
|
|
{ |
59
|
|
|
return $this->level; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getTitle() |
63
|
|
|
{ |
64
|
|
|
return $this->title; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getPrice() |
68
|
|
|
{ |
69
|
|
|
return $this->price; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getPrice_sell() |
73
|
|
|
{ |
74
|
|
|
return floor($this->price / 2); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getOwned() |
78
|
|
|
{ |
79
|
|
|
return $this->owned; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getBuy_amount() |
83
|
|
|
{ |
84
|
|
|
return $this->buy_amount; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getSell_amount() |
88
|
|
|
{ |
89
|
|
|
return $this->sell_amount; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getErrors($attribute = NULL) |
93
|
|
|
{ |
94
|
|
|
return $this->errors; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getSuccess() |
98
|
|
|
{ |
99
|
|
|
return $this->success; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setId($id) |
103
|
|
|
{ |
104
|
|
|
$this->id = (int)$id; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function setItem_type($type) |
108
|
|
|
{ |
109
|
|
|
$this->item_type = $type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function setOwned($owned) |
113
|
|
|
{ |
114
|
|
|
$this->owned = (int)$owned; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function fetch() |
118
|
|
|
{ |
119
|
|
|
if (!$this->id) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$uid = Yii::app()->player->uid; |
124
|
|
|
|
125
|
|
|
//read all from db |
126
|
|
|
$dependency = new CExpressionDependency("Yii::app()->params['{$this->item_type}s_version']"); |
|
|
|
|
127
|
|
|
$res = Yii::app()->db->cache(Yii::app()->params['cacheDuration'], $dependency)->createCommand() |
|
|
|
|
128
|
|
|
->select('*') |
129
|
|
|
->from($this->item_type.'s') |
130
|
|
|
->where('id=:id', [':id'=>$this->id]) |
131
|
|
|
->queryRow(); |
132
|
|
|
|
133
|
|
|
if (!$res) { |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($res as $k => $v) { |
138
|
|
|
if ($k == 'id') { |
139
|
|
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->$k = $v; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$own = Yii::app()->db->createCommand() |
|
|
|
|
146
|
|
|
->select('item_count') |
147
|
|
|
->from('users_'.$this->item_type.'s') |
148
|
|
|
->where('uid=:uid and item_id=:item_id', [':uid'=>$uid, ':item_id'=>$this->id]) |
149
|
|
|
->queryRow(); |
150
|
|
|
$this->owned = (int)$own['item_count']; |
151
|
|
|
|
152
|
|
|
$this->setBuyAmount(); |
153
|
|
|
$this->setSellAmount(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function fetchSet() |
157
|
|
|
{ |
158
|
|
|
if (!$this->id) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$uid = Yii::app()->player->uid; |
163
|
|
|
|
164
|
|
|
$combinedId = (string)$this->id; |
|
|
|
|
165
|
|
|
$setId = (int)substr($combinedId, 0, -3); |
|
|
|
|
166
|
|
|
$this->level = (int)substr($combinedId, -3); |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
//read all from db |
170
|
|
|
$dependency = new CExpressionDependency("Yii::app()->params['{$this->item_type}s_version']"); |
|
|
|
|
171
|
|
|
$res = Yii::app()->db->cache(Yii::app()->params['cacheDuration'], $dependency)->createCommand() |
|
|
|
|
172
|
|
|
->select('*') |
173
|
|
|
->from('itemsets') |
174
|
|
|
->where('id=:id', [':id'=>$setId]) |
175
|
|
|
->queryRow(); |
176
|
|
|
|
177
|
|
|
if (!$res) { |
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
foreach ($res as $k => $v) { |
182
|
|
|
if ($k == 'id') { |
183
|
|
|
continue; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->$k = $v; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$own = Yii::app()->db->createCommand() |
|
|
|
|
190
|
|
|
->select('skill, item_count, price') |
191
|
|
|
->from('users_items') |
192
|
|
|
->where('uid=:uid and item_id=:item_id', [':uid'=>$uid, ':item_id'=>$this->id]) |
193
|
|
|
->queryRow(); |
194
|
|
|
$this->owned = (int)$own['item_count']; |
195
|
|
|
|
196
|
|
|
//read skill from users tbl |
197
|
|
|
$this->title = $this->level . '. szintű ' . $this->title; |
198
|
|
|
$this->skill = (int)$own['skill']; |
199
|
|
|
$this->price = (int)$own['price']; |
200
|
|
|
$this->level = 1; |
201
|
|
|
|
202
|
|
|
$this->setBuyAmount(); |
203
|
|
|
$this->setSellAmount(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param integer $amount |
208
|
|
|
*/ |
209
|
|
|
public function buy($amount) |
210
|
|
|
{ |
211
|
|
|
$decr = []; |
|
|
|
|
212
|
|
|
$amount = (int)$amount; |
213
|
|
|
if ($amount < 1) { |
214
|
|
|
$this->errors['amount'] = true; |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
View Code Duplication |
if ($this->price * $amount > Yii::app()->player->model->dollar) { |
|
|
|
|
219
|
|
|
$this->errors['dollar'] = true; |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
View Code Duplication |
if ($this->item_type !== self::TYPE_PART) { |
|
|
|
|
224
|
|
|
if ($amount > Yii::app()->player->model->freeSlots) { |
225
|
|
|
$this->errors['freeSlots'] = true; |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$uid = Yii::app()->player->uid; |
231
|
|
|
|
232
|
|
|
//add to inventory |
233
|
|
|
$update = Yii::app()->db |
234
|
|
|
->createCommand("UPDATE users_{$this->item_type}s SET item_count=item_count+:amount WHERE uid=:uid AND item_id=:item_id") |
|
|
|
|
235
|
|
|
->bindValues([':uid'=>$uid, ':item_id'=>(int)$this->id, ':amount'=>$amount]) |
236
|
|
|
->execute(); |
237
|
|
|
|
238
|
|
View Code Duplication |
if (!$update) { |
|
|
|
|
239
|
|
|
Yii::app()->db->createCommand() |
240
|
|
|
->insert('users_'.$this->item_type.'s', [ |
241
|
|
|
'uid'=>$uid, |
242
|
|
|
'item_id'=>(int)$this->id, |
243
|
|
|
'item_count'=>$amount, |
244
|
|
|
'skill'=>(int)$this->skill, |
245
|
|
|
]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
//pay for it |
249
|
|
|
if ($this->price > 0) { |
250
|
|
|
$decr['dollar'] = $amount * $this->price; |
251
|
|
|
Yii::app()->player->model->updateAttributes([], $decr); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$this->success = true; |
255
|
|
|
$this->owned += $amount; |
|
|
|
|
256
|
|
|
$this->setBuyAmount(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function sell($amount) |
260
|
|
|
{ |
261
|
|
|
$incr = []; |
|
|
|
|
262
|
|
|
$amount = (int)$amount; |
263
|
|
|
if ($amount < 1) { |
264
|
|
|
$this->errors['amount'] = true; |
265
|
|
|
return false; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if ($this->owned < $amount) { |
269
|
|
|
$this->errors['owned'] = true; |
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$player = Yii::app()->player->model; |
274
|
|
|
|
275
|
|
|
$owned = $this->item_type == self::TYPE_BAIT ? $player->owned_baits : $player->owned_items; |
276
|
|
|
if ($owned == 1) { |
277
|
|
|
$this->errors['isLast'] = true; |
278
|
|
|
return false; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
//remove from inventory |
282
|
|
|
Yii::app()->db |
283
|
|
|
->createCommand("UPDATE users_{$this->item_type}s SET item_count=item_count-:amount WHERE uid=:uid AND item_id=:item_id") |
|
|
|
|
284
|
|
|
->bindValues([':uid'=>$player->uid, 'item_id'=>(int)$this->id, ':amount'=>$amount]) |
285
|
|
|
->execute(); |
286
|
|
|
|
287
|
|
|
//give money for it |
288
|
|
|
$incr['dollar'] = $amount * $this->price_sell; |
289
|
|
|
Yii::app()->player->model->updateAttributes($incr, []); |
290
|
|
|
|
291
|
|
|
$this->success = true; |
292
|
|
|
$this->owned -= $amount; |
|
|
|
|
293
|
|
|
$this->setSellAmount(); |
294
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
private function setBuyAmount() |
298
|
|
|
{ |
299
|
|
|
foreach ([1,5,10] as $amount) { |
300
|
|
|
$this->buy_amount[$amount] = (bool)($this->price * $amount <= Yii::app()->player->model->dollar); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
private function setSellAmount() |
305
|
|
|
{ |
306
|
|
|
foreach ([1,5,10] as $amount) { |
307
|
|
|
$this->sell_amount[$amount] = (bool)($this->owned >= $amount); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function __toString() |
312
|
|
|
{ |
313
|
|
|
$attributes = ['id','item_type','skill','level','price','title','owned']; |
314
|
|
|
$ret = ''; |
|
|
|
|
315
|
|
|
foreach ($attributes as $attribute) { |
316
|
|
|
$ret .= $attribute . ': ' . $this->$attribute . "\n"; |
317
|
|
|
} |
318
|
|
|
return $ret; |
319
|
|
|
} |
320
|
|
|
} |
|
|
|
|
321
|
|
|
|
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.