Total Complexity | 66 |
Total Lines | 488 |
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 | private $items = [ |
||
10 | 'amulet', |
||
11 | 'boots', |
||
12 | 'charm', |
||
13 | 'gloves', |
||
14 | 'helm', |
||
15 | 'leggings', |
||
16 | 'ring', |
||
17 | 'shield', |
||
18 | 'tunic', |
||
19 | 'weapon', |
||
20 | ]; |
||
21 | private $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) |
||
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) |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getPenaltiesList() |
||
142 | { |
||
143 | return $this->penalties; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getItemsList() |
||
150 | { |
||
151 | return $this->items; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns an array with a list of all the Items from the database |
||
156 | * @return array |
||
157 | */ |
||
158 | public function getItems() |
||
159 | { |
||
160 | $items = []; |
||
161 | $row = 0; |
||
162 | if (($handle = fopen($this->config['bot_item'], "r")) !== false) { |
||
163 | while (($data = fgetcsv($handle, 1024, "\t")) !== false) { |
||
164 | $row++; |
||
165 | if ($row == 1) { |
||
166 | continue; |
||
167 | } |
||
168 | |||
169 | $record = [ |
||
170 | 'x_pos' => (int) $data[0], |
||
171 | 'y_pos' => (int) $data[1], |
||
172 | 'type' => $data[2], |
||
173 | 'level' => $data[3], |
||
174 | 'age' => $data[4] |
||
175 | ]; |
||
176 | |||
177 | $items[] = $record; |
||
178 | } |
||
179 | fclose($handle); |
||
180 | } |
||
181 | |||
182 | return $items; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns an array with the Players, sorted by level |
||
187 | * Includes the fields needed for the scoreboard page |
||
188 | * @return array |
||
189 | */ |
||
190 | public function getScoreboard() |
||
191 | { |
||
192 | $players = []; |
||
193 | $row = 0; |
||
194 | if (($handle = fopen($this->config['bot_db'], "r")) !== false) { |
||
195 | while (($data = fgetcsv($handle, 1024, "\t")) !== false) { |
||
196 | $row++; |
||
197 | if ($row == 1) { |
||
198 | continue; |
||
199 | } |
||
200 | $players[] = [ |
||
201 | 'nick' => $data[0], |
||
202 | 'level' => (int) $data[3], |
||
203 | 'class' => $data[4], |
||
204 | 'ttl' => (int) $data[5], |
||
205 | 'status' => (bool) $data[8], |
||
206 | ]; |
||
207 | } |
||
208 | fclose($handle); |
||
209 | } |
||
210 | array_multisort(array_column($players, 'level'), SORT_DESC, $players); |
||
|
|||
211 | |||
212 | return $players; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Returns a list with (almost) all the fields from the Players database |
||
217 | * If the parameter $nick is used, only returns the data for that Player |
||
218 | * or 0 if that Player doesn't exist |
||
219 | * @param string|null $nick |
||
220 | * @return array|int|mixed |
||
221 | */ |
||
222 | public function getDatabase(string $nick = null) |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Returns the last $limit events [from the user $nick] |
||
366 | * If $limit is 0 returns all |
||
367 | * @param int $limit |
||
368 | * @param string|null $nick |
||
369 | * @return array|mixed |
||
370 | */ |
||
371 | public function getEvents(int $limit, string $nick = null) |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Returns an array with the coordinates and name of all Players and Items |
||
402 | * @return array |
||
403 | */ |
||
404 | public function getCoordinates() |
||
405 | { |
||
406 | $coordinates = []; |
||
407 | |||
408 | $players = $this->getDatabase(); |
||
409 | $items = $this->getItems(); |
||
410 | |||
411 | foreach ($players as $player) { |
||
412 | $coordinates[] = ['x' => $player['x_pos'], 'y' => $player['y_pos'], 'text' => $player['nick']]; |
||
413 | } |
||
414 | |||
415 | foreach ($items as $item) { |
||
416 | $coordinates[] = ['x' => $item['x_pos'], 'y' => $item['y_pos'], 'text' => $item['type']]; |
||
417 | } |
||
418 | |||
419 | return $coordinates; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Returns an array with all the data associated with the current quest |
||
424 | * @return array |
||
425 | */ |
||
426 | public function getQuestData() |
||
427 | { |
||
428 | $quest = []; |
||
429 | if (($handle = fopen($this->config['bot_quest'], "r")) !== false) { |
||
430 | while (($data = fgets($handle, 1024)) !== false) { |
||
431 | // T - title |
||
432 | if (! isset($data['title']) && $data[0] == "T") { |
||
433 | $quest['title'] = substr($data, 2); |
||
434 | } |
||
435 | // Y - type. 1 for time based, 2 for stages |
||
436 | if (! isset($data['type']) && $data[0] == "Y") { |
||
437 | $quest['type'] = (int) substr($data, 2); |
||
438 | } |
||
439 | // S - objective |
||
440 | if ($data[0] == "S") { |
||
441 | if ($quest['type'] == 1) { |
||
442 | // Time to end |
||
443 | $quest['objective'] = $this->secondsToTime((int) substr($data, 2)); |
||
444 | } elseif ($quest['type'] == 2) { |
||
445 | // Stage |
||
446 | $quest['objective'] = (int) substr($data, 2); |
||
447 | } |
||
448 | } |
||
449 | if ($data[0] == "P") { |
||
450 | $data_exploded = explode(" ", $data); |
||
451 | // P - stages position |
||
452 | if ($data_exploded[0] == "P") { |
||
453 | $quest['stages'] = [ |
||
454 | ['x_pos' => (int) $data_exploded[1], 'y_pos' => (int) $data_exploded[2]], |
||
455 | ['x_pos' => (int) $data_exploded[3], 'y_pos' => (int) $data_exploded[4]], |
||
456 | ]; |
||
457 | } |
||
458 | // P{1-4} - player position |
||
459 | if (isset($data_exploded[0][1])) { |
||
460 | $quest['players'][] = [ |
||
461 | 'nick' => $data_exploded[1], |
||
462 | 'x_pos' => (int) $data_exploded[2], |
||
463 | 'y_pos' => (int) $data_exploded[3] |
||
464 | ]; |
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | fclose($handle); |
||
469 | } |
||
470 | |||
471 | return $quest; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Returns an array with all the Players nicks |
||
476 | * @return array |
||
477 | */ |
||
478 | public function getPlayers() |
||
495 | } |
||
496 | } |
||
497 |