Total Complexity | 57 |
Total Lines | 443 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BotParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BotParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class BotParser |
||
8 | { |
||
9 | const ITEMS = [ |
||
10 | 'amulet', |
||
11 | 'boots', |
||
12 | 'charm', |
||
13 | 'gloves', |
||
14 | 'helm', |
||
15 | 'leggings', |
||
16 | 'ring', |
||
17 | 'shield', |
||
18 | 'tunic', |
||
19 | 'weapon', |
||
20 | ]; |
||
21 | const PENALTIES = [ |
||
22 | 'kick', |
||
23 | 'logout', |
||
24 | 'msg', |
||
25 | 'nick', |
||
26 | 'part', |
||
27 | 'quest', |
||
28 | 'quit', |
||
29 | ]; |
||
30 | private $config; |
||
31 | |||
32 | public function __construct(array $config) |
||
33 | { |
||
34 | $this->config = $config; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Converts a DateInterval to a human readable format |
||
39 | * Returns 'None' if the difference is zero |
||
40 | * @param int $seconds |
||
41 | * @return string |
||
42 | */ |
||
43 | private function secondsToTime(int $seconds) |
||
44 | { |
||
45 | $result = 'None'; |
||
46 | |||
47 | if ($seconds > 0) { |
||
48 | $dtF = new Carbon('@0'); |
||
49 | $dtT = new Carbon("@$seconds"); |
||
50 | $result = $dtF->diffForHumans($dtT, true, false, 2); |
||
51 | } |
||
52 | |||
53 | return $result; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns the alignment |
||
58 | * @param string $alignment |
||
59 | * @return string |
||
60 | */ |
||
61 | private function parseAlignment(string $alignment) |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Returns the name of item if it is unique, or null if it isn't |
||
80 | * @param mixed $item_value |
||
81 | * @return null|string |
||
82 | */ |
||
83 | private function parseUniqueItem($item_value) |
||
84 | { |
||
85 | $result = null; |
||
86 | |||
87 | $regex = '/\d+([a-h])/'; |
||
88 | preg_match($regex, $item_value, $matches); |
||
89 | |||
90 | if (isset($matches[1])) { |
||
91 | switch ($matches[1]) { |
||
92 | case 'a': |
||
93 | $result = "Mattt's Omniscience Grand Crown"; |
||
94 | break; |
||
95 | case 'b': |
||
96 | $result = "Res0's Protectorate Plate Mail"; |
||
97 | break; |
||
98 | case 'c': |
||
99 | $result = "Dwyn's Storm Magic Amulet"; |
||
100 | break; |
||
101 | case 'd': |
||
102 | $result = "Jotun's Fury Colossal Sword"; |
||
103 | break; |
||
104 | case 'e': |
||
105 | $result = "Drdink's Cane of Blind Rage"; |
||
106 | break; |
||
107 | case 'f': |
||
108 | $result = "Mrquick's Magical Boots of Swiftness"; |
||
109 | break; |
||
110 | case 'g': |
||
111 | $result = "Jeff's Cluehammer of Doom"; |
||
112 | break; |
||
113 | case 'h': |
||
114 | $result = "Juliet's Glorious Ring of Sparkliness"; |
||
115 | break; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | return $result; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Sums the values from the keys of the array $record from $start till $end |
||
124 | * @param array $record |
||
125 | * @param int $start |
||
126 | * @param int $end |
||
127 | * @return int |
||
128 | */ |
||
129 | private function sumFields(array $record, int $start, int $end) |
||
130 | { |
||
131 | $total = 0; |
||
132 | for ($i = $start; $i <= $end; $i++) { |
||
133 | $total += (int) $record[$i]; |
||
134 | } |
||
135 | return $total; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Returns an array with a list of all the Items from the database |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getItems() |
||
143 | { |
||
144 | $items = []; |
||
145 | $row = 0; |
||
146 | if (($handle = fopen($this->config['bot_item'], "r")) !== false) { |
||
147 | while (($data = fgetcsv($handle, 1024, "\t")) !== false) { |
||
148 | $row++; |
||
149 | if ($row == 1) { |
||
150 | continue; |
||
151 | } |
||
152 | |||
153 | $record = [ |
||
154 | 'x_pos' => (int)$data[0], |
||
155 | 'y_pos' => (int)$data[1], |
||
156 | 'type' => $data[2], |
||
157 | 'level' => $data[3], |
||
158 | 'age' => $data[4] |
||
159 | ]; |
||
160 | |||
161 | $items[] = $record; |
||
162 | } |
||
163 | fclose($handle); |
||
164 | } |
||
165 | |||
166 | return $items; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Returns an array with the Players, sorted by level |
||
171 | * Includes the fields needed for the scoreboard page |
||
172 | * @return array |
||
173 | */ |
||
174 | public function getScoreboard() |
||
175 | { |
||
176 | $players = []; |
||
177 | $row = 0; |
||
178 | if (($handle = fopen($this->config['bot_db'], "r")) !== false) { |
||
179 | while (($data = fgetcsv($handle, 1024, "\t")) !== false) { |
||
180 | $row++; |
||
181 | if ($row == 1) { |
||
182 | continue; |
||
183 | } |
||
184 | $players[] = [ |
||
185 | 'nick' => $data[0], |
||
186 | 'level' => (int) $data[3], |
||
187 | 'class' => $data[4], |
||
188 | 'ttl' => (int) $data[5], |
||
189 | 'status' => (bool) $data[8], |
||
190 | ]; |
||
191 | } |
||
192 | fclose($handle); |
||
193 | } |
||
194 | array_multisort(array_column($players, 'level'), SORT_DESC, $players); |
||
|
|||
195 | |||
196 | return $players; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Returns a list with (almost) all the fields from the Players database |
||
201 | * If the parameter $nick is used, only returns the data for that Player |
||
202 | * or 0 if that Player doesn't exist |
||
203 | * @param string|null $nick |
||
204 | * @return array|int|mixed |
||
205 | */ |
||
206 | public function getDatabase(string $nick = null) |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Returns the last $limit events [from the user $nick] |
||
346 | * If $limit is 0 returns all |
||
347 | * @param int $limit |
||
348 | * @param string|null $nick |
||
349 | * @return array|mixed |
||
350 | */ |
||
351 | public function getEvents(int $limit, string $nick = null) |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Returns an array with the coordinates and name of all Players and Items |
||
386 | * @return array |
||
387 | */ |
||
388 | public function getCoordinates() |
||
389 | { |
||
390 | $coordinates = []; |
||
391 | |||
392 | $players = $this->getDatabase(); |
||
393 | $items = $this->getItems(); |
||
394 | |||
395 | foreach ($players as $player) { |
||
396 | $coordinates[] = ['x' => $player['x_pos'], 'y' => $player['y_pos'], 'text' => $player['nick']]; |
||
397 | } |
||
398 | |||
399 | foreach ($items as $item) { |
||
400 | $coordinates[] = ['x' => $item['x_pos'], 'y' => $item['y_pos'], 'text' => $item['type']]; |
||
401 | } |
||
402 | |||
403 | return $coordinates; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Returns an array with all the data associated with the current quest |
||
408 | * @return array |
||
409 | */ |
||
410 | public function getQuestData() |
||
450 | } |
||
451 | } |
||
452 |