Character::inventory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace FreedomCore\TrinityCore\Character;
2
3
use FreedomCore\TrinityCore\Character\Classes\Boost;
4
use FreedomCore\TrinityCore\Console\Client as SOAPClient;
5
use FreedomCore\TrinityCore\Support\Classes\Durability;
6
use FreedomCore\TrinityCore\Support\Classes\Item;
7
use FreedomCore\TrinityCore\Support\Classes\Profession;
8
use FreedomCore\TrinityCore\Support\Common\Helper;
9
use FreedomCore\TrinityCore\Support\DB2Reader;
10
use Illuminate\Filesystem\Filesystem;
11
use FreedomCore\TrinityCore\Character\Models\Character as CharacterModel;
12
use Illuminate\Support\Collection;
13
14
/**
15
 * Class Character
16
 * @package FreedomCore\TrinityCore\Character
17
 */
18
class Character
19
{
20
21
    /**
22
     * Package Version
23
     * @var string|null
24
     */
25
    protected $version = null;
26
27
    /**
28
     * FileSystem Instance
29
     * @var Filesystem|\Illuminate\Foundation\Application|mixed|null
30
     */
31
    protected $fileSystem = null;
32
33
    /**
34
     * DB2Reader Instance
35
     * @var null|DB2Reader
36
     */
37
    protected $reader = null;
38
39
    /**
40
     * Character Model Instance
41
     * @var null|CharacterModel
42
     */
43
    protected $character = null;
44
45
    /**
46
     * Character Inventory Array
47
     * @var array
48
     */
49
    protected $inventory = [];
50
51
    /**
52
     * Character professions collection
53
     * @var \Illuminate\Support\Collection
54
     */
55
    protected $professions = [];
56
57
    /**
58
     * Character constructor.
59
     * @param string $characterName
60
     * @param DB2Reader|null $reader
61
     * @throws \Exception
62
     */
63
    public function __construct(string $characterName = '', DB2Reader $reader = null)
64
    {
65
        $this->fileSystem = app(FileSystem::class);
66
        $this->reader = $reader;
67
        $this->findPackageVersion();
68
        if (strlen($characterName) > 1) {
69
            $this->loadCharacterData($characterName);
70
        }
71
    }
72
73
    /**
74
     * Load character data
75
     * @param string $characterName
76
     * @throws \Exception
77
     */
78
    public function loadCharacterData(string $characterName)
79
    {
80
        $this->loadCharacterModelData($characterName)->loadCharacterInventory()->loadCharacterProfessions();
81
    }
82
83
    /**
84
     * Get character model
85
     * @return CharacterModel
86
     */
87
    public function model() : CharacterModel
88
    {
89
        return $this->character;
90
    }
91
92
    /**
93
     * Get reader instance
94
     * @return DB2Reader
95
     */
96
    public function reader() : DB2Reader
97
    {
98
        if ($this->reader === null) {
99
            Helper::throwRuntimeException('DB2Reader has not been instantiated!');
100
        }
101
        return $this->reader;
102
    }
103
104
    /**
105
     * Get character inventory
106
     * @return array
107
     */
108
    public function inventory() : array
109
    {
110
        return $this->inventory;
111
    }
112
113
    /**
114
     * Get character professions
115
     * @return Collection
116
     */
117
    public function professions() : Collection
118
    {
119
        return $this->professions;
120
    }
121
122
    /**
123
     * Character boost interface
124
     * @param SOAPClient $client
125
     * @return Boost
126
     */
127
    public function boost(SOAPClient $client) : Boost
128
    {
129
        return (new Boost($this, $client));
130
    }
131
132
    /**
133
     * Find Package Version
134
     */
135
    protected function findPackageVersion()
136
    {
137
        if (!is_null($this->version)) {
138
            return;
139
        }
140
        if ($this->fileSystem->exists(base_path('composer.lock'))) {
141
            $file = json_decode(
142
                $this->fileSystem->get(base_path('composer.lock'))
143
            );
144
            foreach ($file->packages as $package) {
145
                if ($package->name == 'freedomcore/trinitycore-character') {
146
                    $this->version = $package->version;
147
                    break;
148
                }
149
            }
150
        }
151
    }
152
153
    /**
154
     * Load character data
155
     * @param string $characterName
156
     * @return Character
157
     */
158
    protected function loadCharacterModelData(string $characterName) : Character
159
    {
160
        $characterName = Helper::getCharacterName($characterName);
161
        $characterData = CharacterModel::where('name', $characterName)->first();
162
        if ($characterData === null) {
163
            throw new \RuntimeException('Unable to find character with name: ' . $characterName);
164
        } else {
165
            $this->character = $characterData;
166
        }
167
        return $this;
168
    }
169
170
    /**
171
     * Load character inventory
172
     * @return Character
173
     * @throws \Exception
174
     */
175
    protected function loadCharacterInventory() : Character
176
    {
177
        $characterInventory = $this->character->inventory;
178
        $characterItems = [];
179
        foreach ($characterInventory as $inventoryEntry) {
180
            $reference = $inventoryEntry->reference;
181
            $item = new Item($inventoryEntry, $reference);
182
            if ($this->reader !== null) {
183
                $item->attachReader($this->reader)->autoloadItemData(true);
184
                $item->setItemID($reference->itemEntry);
185
            }
186
            $characterItems[] = $item;
187
        }
188
        $this->inventory = $characterItems;
189
        return $this;
190
    }
191
192
    /**
193
     * Load character professions
194
     * @return Character
195
     * @throws \Exception
196
     */
197
    protected function loadCharacterProfessions() : Character
198
    {
199
        $profession = new Profession();
200
        $profession->loadProfessions(new DB2Reader(true));
201
        $profession->loadCharacterProfessions($this->character);
202
        $this->professions = $profession->getCharacterProfessions();
203
        return $this;
204
    }
205
}
206