Total Complexity | 68 |
Total Lines | 522 |
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 ONLINE_COLOR = '#0080e1'; |
||
10 | const OFFLINE_COLOR = '#d30000'; |
||
11 | const ITEM_COLOR = '#ff8000'; |
||
12 | const UNIQUE_ITEM_COLOR = '#ffc000'; |
||
13 | private $items = [ |
||
14 | 'amulet', |
||
15 | 'boots', |
||
16 | 'charm', |
||
17 | 'gloves', |
||
18 | 'helm', |
||
19 | 'leggings', |
||
20 | 'ring', |
||
21 | 'shield', |
||
22 | 'tunic', |
||
23 | 'weapon', |
||
24 | ]; |
||
25 | private $penalties = [ |
||
26 | 'kick', |
||
27 | 'logout', |
||
28 | 'msg', |
||
29 | 'nick', |
||
30 | 'part', |
||
31 | 'quest', |
||
32 | 'quit', |
||
33 | ]; |
||
34 | private $config; |
||
35 | |||
36 | public function __construct(array $config) |
||
37 | { |
||
38 | $this->config = $config; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Converts a DateInterval to a human readable format |
||
43 | * Returns 'None' if the difference is zero |
||
44 | * @param int $seconds |
||
45 | * @return string |
||
46 | */ |
||
47 | private function secondsToTime(int $seconds) |
||
48 | { |
||
49 | $result = 'None'; |
||
50 | |||
51 | if ($seconds > 0) { |
||
52 | $dtF = new Carbon('@0'); |
||
53 | $dtT = new Carbon("@$seconds"); |
||
54 | $result = $dtF->diffForHumans($dtT, true, false, 2); |
||
55 | } |
||
56 | |||
57 | return $result; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns the alignment |
||
62 | * @param string $alignment |
||
63 | * @return string |
||
64 | */ |
||
65 | private function parseAlignment(string $alignment) |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Returns the name of item if it is unique, or null if it isn't |
||
84 | * @param mixed $item_value |
||
85 | * @return null|string |
||
86 | */ |
||
87 | private function parseUniqueItem($item_value) |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Sums the values from the keys of the array $record from $start till $end |
||
128 | * @param array $record |
||
129 | * @param int $start |
||
130 | * @param int $end |
||
131 | * @return int |
||
132 | */ |
||
133 | private function sumFields(array $record, int $start, int $end) |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getPenaltiesList() |
||
146 | { |
||
147 | return $this->penalties; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return array |
||
152 | */ |
||
153 | public function getItemsList() |
||
154 | { |
||
155 | return $this->items; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns an array with a list of all the Items from the database |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getItems() |
||
163 | { |
||
164 | $items = []; |
||
165 | $row = 0; |
||
166 | if (($handle = fopen($this->config['bot_item'], "r")) !== false) { |
||
167 | while (($data = fgetcsv($handle, 1024, "\t")) !== false) { |
||
168 | $row++; |
||
169 | if ($row == 1) { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | $record = [ |
||
174 | 'x_pos' => (int) $data[0], |
||
175 | 'y_pos' => (int) $data[1], |
||
176 | 'type' => $data[2], |
||
177 | 'level' => $data[3], |
||
178 | 'age' => $data[4] |
||
179 | ]; |
||
180 | |||
181 | $items[] = $record; |
||
182 | } |
||
183 | fclose($handle); |
||
184 | } |
||
185 | |||
186 | return $items; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Returns an array with the Players, sorted by level |
||
191 | * Includes the fields needed for the scoreboard page |
||
192 | * @return array |
||
193 | */ |
||
194 | public function getScoreboard() |
||
195 | { |
||
196 | $players = []; |
||
197 | $row = 0; |
||
198 | if (($handle = fopen($this->config['bot_db'], "r")) !== false) { |
||
199 | while (($data = fgetcsv($handle, 1024, "\t")) !== false) { |
||
200 | $row++; |
||
201 | if ($row == 1) { |
||
202 | continue; |
||
203 | } |
||
204 | $players[] = [ |
||
205 | 'nick' => $data[0], |
||
206 | 'level' => (int) $data[3], |
||
207 | 'class' => $data[4], |
||
208 | 'ttl' => $this->secondsToTime((int) $data[5]), |
||
209 | 'status' => (bool) $data[8], |
||
210 | ]; |
||
211 | } |
||
212 | fclose($handle); |
||
213 | } |
||
214 | array_multisort(array_column($players, 'level'), SORT_DESC, $players); |
||
|
|||
215 | |||
216 | return $players; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns a list with (almost) all the fields from the Players database |
||
221 | * If the parameter $nick is used, only returns the data for that Player |
||
222 | * or 0 if that Player doesn't exist |
||
223 | * @param string|null $nick |
||
224 | * @return array|int|mixed |
||
225 | */ |
||
226 | public function getDatabase(string $nick = null) |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Returns the last $limit events [from the user $nick] |
||
370 | * If $limit is 0 returns all |
||
371 | * @param int $limit |
||
372 | * @param string|null $nick |
||
373 | * @return array|mixed |
||
374 | */ |
||
375 | public function getEvents(int $limit, string $nick = null) |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Returns an array with the coordinates and name of all Players and Items |
||
406 | * @return array |
||
407 | */ |
||
408 | public function getCoordinates() |
||
409 | { |
||
410 | $coordinates = []; |
||
411 | |||
412 | $players = $this->getDatabase(); |
||
413 | $items = $this->getItems(); |
||
414 | |||
415 | foreach ($players as $player) { |
||
416 | $coordinates[] = [ |
||
417 | 'x' => $player['x_pos'], |
||
418 | 'y' => $player['y_pos'], |
||
419 | 'text' => $player['nick'], |
||
420 | 'color' => ($player['online'] == 'Yes' ? self::ONLINE_COLOR : self::OFFLINE_COLOR) |
||
421 | ]; |
||
422 | } |
||
423 | |||
424 | foreach ($items as $item) { |
||
425 | $coordinates[] = [ |
||
426 | 'x' => $item['x_pos'], |
||
427 | 'y' => $item['y_pos'], |
||
428 | 'text' => $item['type'], |
||
429 | 'color' => self::ITEM_COLOR, |
||
430 | ]; |
||
431 | } |
||
432 | |||
433 | return $coordinates; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Returns an array with all the data associated with the current quest |
||
438 | * @return array |
||
439 | */ |
||
440 | public function getQuestData() |
||
441 | { |
||
442 | $quest = []; |
||
443 | if (($handle = fopen($this->config['bot_quest'], "r")) !== false) { |
||
444 | while (($data = fgets($handle, 1024)) !== false) { |
||
445 | // T - title |
||
446 | if (! isset($data['title']) && $data[0] == "T") { |
||
447 | $quest['title'] = substr($data, 2); |
||
448 | } |
||
449 | // Y - type. 1 for time based, 2 for stages |
||
450 | if (! isset($data['type']) && $data[0] == "Y") { |
||
451 | $quest['type'] = (int) substr($data, 2); |
||
452 | } |
||
453 | // S - objective |
||
454 | if ($data[0] == "S") { |
||
455 | if ($quest['type'] == 1) { |
||
456 | // Time to end |
||
457 | $quest['objective'] = $this->secondsToTime((int) substr($data, 2)); |
||
458 | } elseif ($quest['type'] == 2) { |
||
459 | // Stage |
||
460 | $quest['objective'] = (int) substr($data, 2); |
||
461 | } |
||
462 | } |
||
463 | if ($data[0] == "P") { |
||
464 | $data_exploded = explode(" ", $data); |
||
465 | // P - stages position |
||
466 | if ($data_exploded[0] == "P") { |
||
467 | $quest['stages'] = [ |
||
468 | [ |
||
469 | 'x_pos' => (int) $data_exploded[1], |
||
470 | 'y_pos' => (int) $data_exploded[2], |
||
471 | 'color' => self::OFFLINE_COLOR, |
||
472 | ], |
||
473 | [ |
||
474 | 'x_pos' => (int) $data_exploded[3], |
||
475 | 'y_pos' => (int) $data_exploded[4], |
||
476 | 'color' => self::OFFLINE_COLOR, |
||
477 | ], |
||
478 | ]; |
||
479 | } |
||
480 | // P{1-4} - player position |
||
481 | if (isset($data_exploded[0][1])) { |
||
482 | $quest['players'][] = [ |
||
483 | 'nick' => $data_exploded[1], |
||
484 | 'x_pos' => (int) $data_exploded[2], |
||
485 | 'y_pos' => (int) $data_exploded[3], |
||
486 | 'color' => self::ONLINE_COLOR, |
||
487 | ]; |
||
488 | } |
||
489 | } |
||
490 | } |
||
491 | fclose($handle); |
||
492 | } |
||
493 | |||
494 | return $quest; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Returns an array with all the Players nicks |
||
499 | * @return array |
||
500 | */ |
||
501 | public function getPlayers() |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Returns an array with the dimensions of the map image |
||
522 | * @return array |
||
523 | */ |
||
524 | public function getMapDimensions() |
||
529 | ]; |
||
530 | } |
||
531 | } |
||
532 |