Item::setBagSlot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php namespace FreedomCore\TrinityCore\Support\Classes;
2
3
use FreedomCore\TrinityCore\Character\Models\CharacterInventory;
4
use FreedomCore\TrinityCore\Character\Models\ItemInstance;
5
use FreedomCore\TrinityCore\Support\Common\Helper;
6
use FreedomCore\TrinityCore\Support\DB2Reader;
7
8
/**
9
 * Class Item
10
 * @package FreedomCore\TrinityCore\Support\Classes
11
 */
12
class Item
13
{
14
15
    /**
16
     * Character Inventory Model Instance
17
     * @var CharacterInventory|null
18
     */
19
    protected $inventory = null;
20
21
    /**
22
     * Item Instance Model Instance
23
     * @var ItemInstance|null
24
     */
25
    protected $instance = null;
26
27
    /**
28
     * DB2Reader Instance
29
     * @var null|DB2Reader
30
     */
31
    protected $reader = null;
32
33
    /**
34
     * Guid of the inventory reference
35
     * @var null|integer
36
     */
37
    protected $inventoryGuid = null;
38
39
    /**
40
     * Bag id
41
     * @var int
42
     */
43
    protected $bag = 0;
44
45
    /**
46
     * Item slot
47
     * @var null|integer
48
     */
49
    protected $slot = null;
50
51
    /**
52
     * Guid of the item
53
     * @var null|integer
54
     */
55
    protected $itemGuid = null;
56
57
    /**
58
     * Item id
59
     * @var null|integer
60
     */
61
    protected $itemEntry = null;
62
63
    /**
64
     * Who owns this item
65
     * @var null|integer
66
     */
67
    protected $ownerGuid = null;
68
69
    /**
70
     * Who created this item
71
     * @var int
72
     */
73
    protected $creatorGuid = 0;
74
75
    /**
76
     * Who gifted that item to the character
77
     * @var int
78
     */
79
    protected $giftCreatorGuid = 0;
80
81
    /**
82
     * Amount of item of that type in inventory
83
     * @var int
84
     */
85
    protected $count = 1;
86
87
    /**
88
     * When this item will be removed from inventory
89
     * @var int
90
     */
91
    protected $duration = 0;
92
93
    /**
94
     * Amount of charges left
95
     * @var null|string|integer
96
     */
97
    protected $charges = '';
98
99
    /**
100
     * Flags variable
101
     * @var int
102
     */
103
    protected $flags = 1;
104
105
    /**
106
     * Enchantments String
107
     * @var null|string
108
     */
109
    protected $enchantments = '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ';
110
111
    /**
112
     * Type of random property
113
     * @var int
114
     */
115
    protected $randomPropertyType = 0;
116
117
    /**
118
     * ID of random property
119
     * @var int
120
     */
121
    protected $randomPropertyId = 0;
122
123
    /**
124
     * Item durability
125
     * @var int
126
     */
127
    protected $durability = 0;
128
129
    /**
130
     * How long player used this item for
131
     * @var int
132
     */
133
    protected $playedTime = 0;
134
135
    /**
136
     * Text on the item
137
     * @var null|string
138
     */
139
    protected $text = '';
140
141
    /**
142
     * Transmogrigication id
143
     * @var int
144
     */
145
    protected $transmogrification = 0;
146
147
    /**
148
     * Upgrade id
149
     * @var int
150
     */
151
    protected $upgradeId = 0;
152
153
    /**
154
     * Enchant illusion id
155
     * @var int
156
     */
157
    protected $enchantIllusion = 0;
158
159
    /**
160
     * Battle pet species id
161
     * @var int
162
     */
163
    protected $battlePetSpeciesId = 0;
164
165
    /**
166
     * Battle pet breed data
167
     * @var int
168
     */
169
    protected $battlePetBreedData = 0;
170
171
    /**
172
     * Battle pet level
173
     * @var int
174
     */
175
    protected $battlePetLevel = 0;
176
177
    /**
178
     * Battle pet display id
179
     * @var int
180
     */
181
    protected $battlePetDisplayId = 0;
182
183
    /**
184
     * Unknown
185
     * @var int
186
     */
187
    protected $context = 0;
188
189
    /**
190
     * List of bonuses applied to the item
191
     * @var null|string
192
     */
193
    protected $bonusListIDs = '';
194
195
    /**
196
     * Should we automatically load item data once it is entered
197
     * @var bool
198
     */
199
    protected $enableDynamicDataLoading = true;
200
201
    /**
202
     * Should we load item data as soon as user provides Item ID
203
     * @var bool
204
     */
205
    protected $autoloadItemData = false;
206
207
    /**
208
     * Automatically Loaded Item Data
209
     * @var array
210
     */
211
    protected $autoloadedItemData = [];
212
213
    /**
214
     * Item constructor.
215
     * @param CharacterInventory|null $inventory
216
     * @param ItemInstance|null $instance
217
     */
218 16
    public function __construct(CharacterInventory $inventory = null, ItemInstance $instance = null)
219
    {
220 16
        $this->inventory = $inventory;
221 16
        $this->instance = $instance;
222 16
        if ($this->inventory !== null && $this->instance !== null) {
223
            $this->processPassedData();
224
        }
225 16
    }
226
227
    /**
228
     * Attach DB2Reader to get the item info
229
     * @param DB2Reader|null $reader
230
     * @return Item
231
     */
232
    public function attachReader(DB2Reader $reader = null) : Item
233
    {
234
        if ($reader === null) {
235
            $reader = new DB2Reader(true);
236
            $this->enableDynamicDataLoading = false;
237
        }
238
        if (!$reader->isFileOpened()) {
239
            Helper::throwRuntimeException('You need to pass instance of the DB2Reader with opened ItemSparse file!');
240
        }
241
        $this->reader = $reader;
242
        return $this;
243
    }
244
245
    /**
246
     * Automatically load item data once the id is provided
247
     * @param bool $increaseMemoryLimit
248
     * @return Item
249
     * @throws \Exception
250
     */
251
    public function autoloadItemData(bool $increaseMemoryLimit = false) : Item
252
    {
253
        if ($increaseMemoryLimit) {
254
            ini_set('memory_limit', '512M');
255
            set_time_limit(0);
256
        }
257
        $memoryLimit = ini_get('memory_limit');
258
        if (strstr($memoryLimit, 'M')) {
259
            $memoryLimit = intval(str_replace('M', '', $memoryLimit));
260
        } elseif (strstr($memoryLimit, 'G')) {
261
            $memoryLimit = intval(str_replace('G', '', $memoryLimit)  * 1024);
262
        } else {
263
            Helper::throwRuntimeException('We are unable to process memory limit: ' . $memoryLimit);
264
        }
265
        if ($memoryLimit < 512) {
266
            Helper::throwRuntimeException('Outside of the console this method requires at least 512M of memory dedicated to PHP. We can try to automatically increase this parameter if you call this method in the following manner autoloadItemData(true)');
267
        }
268
        $this->autoloadItemData = true;
269
        return $this;
270
    }
271
272
    /**
273
     * Set Inventory Guid
274
     * @param int $inventoryGuid
275
     * @return Item
276
     */
277
    public function setInventoryGuid(int $inventoryGuid) : Item
278
    {
279
        $this->inventoryGuid = $inventoryGuid;
280
        return $this;
281
    }
282
283
    /**
284
     * Get inventory Guid
285
     * @return int
286
     */
287
    public function getInventoryGuid() : int
288
    {
289
        return $this->inventoryGuid;
290
    }
291
292
    /**
293
     * Set bag slot
294
     * @param int $bagSlot
295
     * @return Item
296
     */
297
    public function setBagSlot(int $bagSlot) : Item
298
    {
299
        $this->bag = $bagSlot;
300
        return $this;
301
    }
302
303
    /**
304
     * Get bag slot
305
     * @return int
306
     */
307
    public function getBagSlot() : int
308
    {
309
        return $this->bag;
310
    }
311
312
    /**
313
     * Set item slot
314
     * @param int $slot
315
     * @return Item
316
     */
317
    public function setSlot(int $slot) : Item
318
    {
319
        $this->slot = $slot;
320
        return $this;
321
    }
322
323
    /**
324
     * Get item slot
325
     * @return int
326
     */
327
    public function getSlot() : int
328
    {
329
        return $this->slot;
330
    }
331
332
    /**
333
     * Set item guid
334
     * @param int $itemGuid
335
     * @return Item
336
     */
337
    public function setItemGuid(int $itemGuid) : Item
338
    {
339
        $this->itemGuid = $itemGuid;
340
        return $this;
341
    }
342
343
    /**
344
     * Get item guid
345
     * @return int
346
     */
347
    public function getItemGuid() : int
348
    {
349
        return $this->itemGuid;
350
    }
351
352
    /**
353
     * Set item id
354
     * @param int $itemID
355
     * @return Item
356
     * @throws \Exception
357
     */
358 16
    public function setItemID(int $itemID) : Item
359
    {
360 16
        $this->itemEntry = $itemID;
361 16
        if ($this->autoloadItemData) {
362
            if ($this->enableDynamicDataLoading) {
363
                $this->autoloadedItemData = $this->reader->getRecord($this->itemEntry);
364
            } else {
365
                $this->autoloadItemData();
366
                $this->autoloadedItemData = $this->reader->getRecord($this->itemEntry);
367
            }
368
        }
369 16
        return $this;
370
    }
371
372
    /**
373
     * Get item id
374
     * @return int
375
     */
376 6
    public function getItemID() : int
377
    {
378 6
        return $this->itemEntry;
379
    }
380
381
    /**
382
     * Set owner guid
383
     * @param int $ownerGuid
384
     * @return Item
385
     */
386
    public function setOwnerGuid(int $ownerGuid) : Item
387
    {
388
        $this->ownerGuid = $ownerGuid;
389
        return $this;
390
    }
391
392
    /**
393
     * Get owner guid
394
     * @return int
395
     */
396
    public function getOwnerGuid() : int
397
    {
398
        return $this->ownerGuid;
399
    }
400
401
    /**
402
     * Set creator guid
403
     * @param int $creatorGuid
404
     * @return Item
405
     */
406
    public function setCreatorGuid(int $creatorGuid) : Item
407
    {
408
        $this->creatorGuid = $creatorGuid;
409
        return $this;
410
    }
411
412
    /**
413
     * Get creator guid
414
     * @return int
415
     */
416
    public function getCreatorGuid() : int
417
    {
418
        return $this->creatorGuid;
419
    }
420
421
    /**
422
     * Set gift creator guid
423
     * @param int $creatorGuid
424
     * @return Item
425
     */
426
    public function setGiftCreatorGuid(int $creatorGuid) : Item
427
    {
428
        $this->giftCreatorGuid = $creatorGuid;
429
        return $this;
430
    }
431
432
    /**
433
     * Get gift creator guid
434
     * @return int
435
     */
436
    public function getGiftCreatorGuid() : int
437
    {
438
        return $this->giftCreatorGuid;
439
    }
440
441
    /**
442
     * Set count
443
     * @param int $count
444
     * @return Item
445
     */
446 8
    public function setCount(int $count) : Item
447
    {
448 8
        $this->count = $count;
449 8
        return $this;
450
    }
451
452
    /**
453
     * Get count
454
     * @return int
455
     */
456 6
    public function getCount() : int
457
    {
458 6
        return $this->count;
459
    }
460
461
    /**
462
     * Set duration
463
     * @param int $duration
464
     * @return Item
465
     */
466
    public function setDuration(int $duration) : Item
467
    {
468
        $this->duration = $duration;
469
        return $this;
470
    }
471
472
    /**
473
     * Set charges
474
     * @param string $charges
475
     * @return Item
476
     */
477
    public function setCharges(string $charges) : Item
478
    {
479
        $this->charges = $charges;
480
        return $this;
481
    }
482
483
    /**
484
     * Set flags
485
     * @param int $flags
486
     * @return Item
487
     */
488
    public function setFlags(int $flags) : Item
489
    {
490
        $this->flags = $flags;
491
        return $this;
492
    }
493
494
    /**
495
     * Set enchantments
496
     * @param string $enchantments
497
     * @return Item
498
     */
499
    public function setEnchantments(string $enchantments) : Item
500
    {
501
        $this->enchantments = $enchantments;
502
        return $this;
503
    }
504
505
    /**
506
     * Set random property
507
     * @param int $type
508
     * @param int $id
509
     * @return Item
510
     */
511
    public function setRandomProperty(int $type, int $id) : Item
512
    {
513
        $this->randomPropertyType = $type;
514
        $this->randomPropertyId = $id;
515
        return $this;
516
    }
517
518
    /**
519
     * Set durability
520
     * @param int $durability
521
     * @return Item
522
     */
523
    public function setDurability(int $durability) : Item
524
    {
525
        $this->durability = $durability;
526
        return $this;
527
    }
528
529
    /**
530
     * Set played time
531
     * @param int $playTime
532
     * @return Item
533
     */
534
    public function setPlayedTime(int $playTime) : Item
535
    {
536
        $this->playedTime = $playTime;
537
        return $this;
538
    }
539
540
    /**
541
     * Set text
542
     * @param string $text
543
     * @return Item
544
     */
545
    public function setText(string $text) : Item
546
    {
547
        $this->text = $text;
548
        return $this;
549
    }
550
551
    /**
552
     * Set transmogrification
553
     * @param int $transmog
554
     * @return Item
555
     */
556
    public function setTransmogrification(int $transmog) : Item
557
    {
558
        $this->transmogrification = $transmog;
559
        return $this;
560
    }
561
562
    /**
563
     * Set upgrade id
564
     * @param int $upgradeID
565
     * @return Item
566
     */
567
    public function setUpgradeID(int $upgradeID) : Item
568
    {
569
        $this->upgradeId = $upgradeID;
570
        return $this;
571
    }
572
573
    /**
574
     * Set enchant illusion
575
     * @param int $illusionID
576
     * @return Item
577
     */
578
    public function setEnchantIllusion(int $illusionID) : Item
579
    {
580
        $this->enchantIllusion = $illusionID;
581
        return $this;
582
    }
583
584
    /**
585
     * Set bonus list
586
     * @param array $bonuses
587
     * @return Item
588
     */
589
    public function setBonusList(array $bonuses) : Item
590
    {
591
        $this->bonusListIDs = implode(',', $bonuses);
592
        return $this;
593
    }
594
595
    /**
596
     * Get Character Inventory Entry
597
     * @return CharacterInventory
598
     */
599
    public function getInventory() : CharacterInventory
600
    {
601
        return $this->inventory;
602
    }
603
604
    /**
605
     * Get updated Character Inventory Entry
606
     * @return CharacterInventory
607
     */
608
    public function getUpdatedInventory() : CharacterInventory
609
    {
610
        return new CharacterInventory([
611
            'guid'      =>  $this->inventoryGuid,
612
            'bag'       =>  $this->bag,
613
            'slot'      =>  $this->slot,
614
            'item'      =>  $this->itemGuid
615
        ]);
616
    }
617
618
    /**
619
     * Get Item Instance
620
     * @return ItemInstance
621
     */
622
    public function getInstance() : ItemInstance
623
    {
624
        return $this->instance;
625
    }
626
627
    /**
628
     * Get updated Item Instance
629
     * @return ItemInstance
630
     */
631
    public function getUpdatedInstance() : ItemInstance
632
    {
633
        return new ItemInstance([
634
            'guid'                  =>  $this->itemGuid,
635
            'itemEntry'             =>  $this->itemEntry,
636
            'owner_guid'            =>  $this->ownerGuid,
637
            'creatorGuid'           =>  $this->creatorGuid,
638
            'giftCreatorGuid'       =>  $this->giftCreatorGuid,
639
            'count'                 =>  $this->count,
640
            'duration'              =>  $this->duration,
641
            'charges'               =>  $this->charges,
642
            'flags'                 =>  $this->flags,
643
            'enchantments'          =>  $this->enchantments,
644
            'randomPropertyType'    =>  $this->randomPropertyType,
645
            'randomPropertyId'      =>  $this->randomPropertyId,
646
            'durability'            =>  $this->durability,
647
            'playedTime'            =>  $this->playedTime,
648
            'text'                  =>  $this->text,
649
            'transmogrification'    =>  $this->transmogrification,
650
            'upgradeId'             =>  $this->upgradeId,
651
            'enchantIllusion'       =>  $this->enchantIllusion,
652
            'battlePetSpeciesId'    =>  $this->battlePetSpeciesId,
653
            'battlePetBreedData'    =>  $this->battlePetBreedData,
654
            'battlePetLevel'        =>  $this->battlePetLevel,
655
            'battlePetDisplayId'    =>  $this->battlePetDisplayId,
656
            'context'               =>  $this->context,
657
            'bonusListIDs'          =>  $this->bonusListIDs
658
        ]);
659
    }
660
661
    /**
662
     * Are we working with the armor gear piece
663
     * @return bool
664
     */
665
    public function isArmor() : bool
666
    {
667
        if ($this->slot === null) {
668
            Helper::throwRuntimeException('Unable to determine if piece of gear is belongs to armor type without slot specified!');
669
        }
670
        $armorSlots = [0, 2, 4, 5, 6, 7, 8, 9, 14];
671
        if (in_array($this->slot, $armorSlots)) {
672
            return true;
673
        }
674
        return false;
675
    }
676
677
    /**
678
     * Are we working with the weapon gear piece
679
     * @return bool
680
     */
681
    public function isWeapon() : bool
682
    {
683
        if ($this->slot === null) {
684
            Helper::throwRuntimeException('Unable to determine if piece of gear is belongs to weapon type without slot specified!');
685
        }
686
        $weaponSlots = [15, 16, 17];
687
        if (in_array($this->slot, $weaponSlots)) {
688
            return true;
689
        }
690
        return false;
691
    }
692
693
    /**
694
     * Get item quality
695
     * @return int
696
     */
697
    public function getQuality() : int
698
    {
699
        if (empty($this->autoloadedItemData)) {
700
            Helper::throwRuntimeException('Item must be loaded automatically to enable use of this method!');
701
        }
702
        return $this->autoloadedItemData['quality'];
703
    }
704
705
    /**
706
     * Get item level
707
     * @return int
708
     */
709
    public function getItemLevel() : int
710
    {
711
        if (empty($this->autoloadedItemData)) {
712
            Helper::throwRuntimeException('Item must be loaded automatically to enable use of this method!');
713
        }
714
        return $this->autoloadedItemData['item_level'];
715
    }
716
717
    /**
718
     * Get level required for this item to be able to equip it
719
     * @return int
720
     */
721
    public function getRequiredLevel() : int
722
    {
723
        if (empty($this->autoloadedItemData)) {
724
            Helper::throwRuntimeException('Item must be loaded automatically to enable use of this method!');
725
        }
726
        return $this->autoloadedItemData['required_level'];
727
    }
728
729
    /**
730
     * Get inventory type
731
     * @return int
732
     */
733
    public function getInventoryType() : int
734
    {
735
        if (empty($this->autoloadedItemData)) {
736
            Helper::throwRuntimeException('Item must be loaded automatically to enable use of this method!');
737
        }
738
        return $this->autoloadedItemData['inventory_type'];
739
    }
740
741
    /**
742
     * Get item sub class
743
     * @return int
744
     */
745
    public function getSubClass() : int
746
    {
747
        Helper::throwRuntimeException('This method is not implemented!');
748
    }
749
750
    /**
751
     * Manually set autoloaded item data
752
     * @param array $itemData
753
     * @return Item
754
     */
755
    public function debugSetAutoloadedItemData(array $itemData) : Item
756
    {
757
        $this->autoloadedItemData = $itemData;
758
        return $this;
759
    }
760
761
    /**
762
     * Get data for freedomcore/trinitycore-console as array
763
     * @return array
764
     */
765 8
    public function getDataForConsoleAsArray() : array
766
    {
767
        return [
768 8
            'id'    =>  $this->itemEntry,
769 8
            'count' =>  $this->count
770
        ];
771
    }
772
773
    /**
774
     * Get data for freedomcore/trinitycore-console as command attribute
775
     * @return string
776
     */
777 4
    public function getDataForConsoleAsCommandAttribute() : string
778
    {
779 4
        return implode(':', $this->getDataForConsoleAsArray());
780
    }
781
782
    /**
783
     * Process data passed by the user
784
     */
785
    private function processPassedData()
786
    {
787
        foreach ($this->instance->toArray() as $key => $value) {
788
            if ($key === 'guid') {
789
                $this->itemGuid = $value;
790
            } elseif ($key === 'owner_guid') {
791
                $this->ownerGuid = $value;
792
            } elseif (property_exists($this, $key)) {
793
                $this->$key = $value;
794
            }
795
        }
796
        foreach ($this->inventory->toArray() as $key => $value) {
797
            if ($key === 'guid') {
798
                $this->inventoryGuid = $value;
799
            } elseif (property_exists($this, $key)) {
800
                $this->$key = $value;
801
            }
802
        }
803
    }
804
}
805