Total Complexity | 62 |
Total Lines | 466 |
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) |
||
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 | * Returns an array with a list of all the Items from the database |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getItems() |
||
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) |
||
207 | { |
||
208 | $database = []; |
||
209 | $row = 0; |
||
210 | if (($handle = fopen($this->config['bot_db'], "r")) !== false) { |
||
211 | while (($data = fgetcsv($handle, 1024, "\t")) !== false) { |
||
212 | $row++; |
||
213 | if ($row == 1) { |
||
214 | continue; |
||
215 | } |
||
216 | |||
217 | if ($nick !== null && strcmp($data[0], $nick) !== 0) { |
||
218 | continue; |
||
219 | } |
||
220 | |||
221 | $record = [ |
||
222 | 'nick' => $data[0], // nick |
||
223 | 'level' => $data[3], // level |
||
224 | 'admin' => ($data[2] ? 'Yes' : 'No'), // admin |
||
225 | 'class' => $data[4], // class |
||
226 | 'ttl' => [ |
||
227 | 'display' => $this->secondsToTime((int) $data[5]), |
||
228 | 'numeric' => (int) $data[5], // ttl |
||
229 | ], |
||
230 | 'nick_host' => $data[7], // nick and host |
||
231 | 'online' => ($data[8] ? 'Yes' : 'No'), // online |
||
232 | 'idled' => [ |
||
233 | 'display' => $this->secondsToTime((int) $data[9]), // idled |
||
234 | 'numeric' => (int) $data[9], |
||
235 | ], |
||
236 | 'x_pos' => (int) $data[10], // x pos |
||
237 | 'y_pos' => (int) $data[11], // y pos |
||
238 | 'msg_pen' => [ |
||
239 | 'display' => $this->secondsToTime((int) $data[12]), // msg pen |
||
240 | 'numeric' => (int) $data[12], |
||
241 | ], |
||
242 | 'nick_pen' => [ |
||
243 | 'display' => $this->secondsToTime((int) $data[13]), // nick pen |
||
244 | 'numeric' => (int) $data[13], |
||
245 | ], |
||
246 | 'part_pen' => [ |
||
247 | 'display' => $this->secondsToTime((int) $data[14]), // part pen |
||
248 | 'numeric' => (int) $data[14], |
||
249 | ], |
||
250 | 'kick_pen' => [ |
||
251 | 'display' => $this->secondsToTime((int) $data[15]), // kick pen |
||
252 | 'numeric' => (int) $data[15], |
||
253 | ], |
||
254 | 'quit_pen' => [ |
||
255 | 'display' => $this->secondsToTime((int) $data[16]), // quit pen |
||
256 | 'numeric' => (int) $data[16], |
||
257 | ], |
||
258 | 'quest_pen' => [ |
||
259 | 'display' => $this->secondsToTime((int) $data[17]), // quest pen |
||
260 | 'numeric' => (int) $data[17], |
||
261 | ], |
||
262 | 'logout_pen' => [ |
||
263 | 'display' => $this->secondsToTime((int) $data[18]), // logout pen |
||
264 | 'numeric' => (int) $data[18], |
||
265 | ], |
||
266 | 'total_pen' => [ |
||
267 | 'display' => $this->secondsToTime($this->sumFields($data, 12, 18)), |
||
268 | 'numeric' => $this->sumFields($data, 12, 18), |
||
269 | ], |
||
270 | 'created' => [ |
||
271 | 'display' => date('Y-m-d H:i:s', (int) $data[19]), // created |
||
272 | 'numeric' => (int) $data[19], |
||
273 | ], |
||
274 | 'last_login' => [ |
||
275 | 'display' => date('Y-m-d H:i:s', (int) $data[20]), // last login |
||
276 | 'numeric' => (int) $data[20], |
||
277 | ], |
||
278 | 'amulet' => [ |
||
279 | 'display' => $data[21], // amulet |
||
280 | 'numeric' => (int) $data[21], |
||
281 | 'unique' => $this->parseUniqueItem($data[21]) |
||
282 | ], |
||
283 | 'charm' => [ |
||
284 | 'display' => $data[22], // charm |
||
285 | 'numeric' => (int) $data[22], |
||
286 | 'unique' => $this->parseUniqueItem($data[22]) |
||
287 | ], |
||
288 | 'helm' => [ |
||
289 | 'display' => $data[23], // helm |
||
290 | 'numeric' => (int) $data[23], |
||
291 | 'unique' => $this->parseUniqueItem($data[23]) |
||
292 | ], |
||
293 | 'boots' => [ |
||
294 | 'display' => $data[24], // boots |
||
295 | 'numeric' => (int) $data[24], |
||
296 | 'unique' => $this->parseUniqueItem($data[24]) |
||
297 | ], |
||
298 | 'gloves' => [ |
||
299 | 'display' => $data[25], // gloves |
||
300 | 'numeric' => (int) $data[25], |
||
301 | 'unique' => $this->parseUniqueItem($data[25]) |
||
302 | ], |
||
303 | 'ring' => [ |
||
304 | 'display' => $data[26], // ring |
||
305 | 'numeric' => (int) $data[26], |
||
306 | 'unique' => $this->parseUniqueItem($data[26]) |
||
307 | ], |
||
308 | 'leggings' => [ |
||
309 | 'display' => $data[27], // leggings |
||
310 | 'numeric' => (int) $data[27], |
||
311 | 'unique' => $this->parseUniqueItem($data[27]) |
||
312 | ], |
||
313 | 'shield' => [ |
||
314 | 'display' => $data[28], // shield |
||
315 | 'numeric' => (int) $data[28], |
||
316 | 'unique' => $this->parseUniqueItem($data[28]) |
||
317 | ], |
||
318 | 'tunic' => [ |
||
319 | 'display' => $data[29], // tunic |
||
320 | 'numeric' => (int) $data[29], |
||
321 | 'unique' => $this->parseUniqueItem($data[29]) |
||
322 | ], |
||
323 | 'weapon' => [ |
||
324 | 'display' => $data[30], // weapon |
||
325 | 'numeric' => (int) $data[30], |
||
326 | 'unique' => $this->parseUniqueItem($data[30]) |
||
327 | ], |
||
328 | 'sum' => $this->sumFields($data, 21, 30), |
||
329 | 'alignment' => $this->parseAlignment($data[31]), // alignment |
||
330 | ]; |
||
331 | |||
332 | if ($nick != null) { |
||
333 | return $record; |
||
334 | } |
||
335 | |||
336 | $database[] = $record; |
||
337 | } |
||
338 | fclose($handle); |
||
339 | } |
||
340 | |||
341 | if ($nick != null) { |
||
342 | return 0; |
||
343 | } |
||
344 | |||
345 | return $database; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Returns the last $limit events [from the user $nick] |
||
350 | * If $limit is 0 returns all |
||
351 | * @param int $limit |
||
352 | * @param string|null $nick |
||
353 | * @return array|mixed |
||
354 | */ |
||
355 | public function getEvents(int $limit, string $nick = null) |
||
356 | { |
||
357 | $modifiers = [ |
||
358 | 'items' => [], |
||
359 | 'total' => 0, |
||
360 | ]; |
||
361 | |||
362 | $tmp = []; |
||
363 | $handle = fopen($this->config['bot_mod'], "r"); |
||
364 | if ($handle) { |
||
365 | while (($line = fgets($handle)) !== false) { |
||
366 | if ($nick != null && strpos($line, $nick) !== false) { |
||
367 | $tmp[] = $line; |
||
368 | } |
||
369 | |||
370 | if ($nick == null) { |
||
371 | $tmp[] = $line; |
||
372 | } |
||
373 | } |
||
374 | fclose($handle); |
||
375 | } |
||
376 | |||
377 | $tmp = array_reverse($tmp); |
||
378 | $modifiers['total'] = count($tmp); |
||
379 | $modifiers['items'] = array_slice($tmp, 0, ($limit > 0 ) ? $limit : null); |
||
380 | |||
381 | return $modifiers; |
||
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 | /** |
||
453 | * Returns an array with all the Players nicks |
||
454 | * @return array |
||
455 | */ |
||
456 | public function getPlayers() |
||
475 |