Passed
Push — master ( 6627c9...55f346 )
by Pedro
02:07
created

BotParser::getPenaltiesList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Application\Service;
4
5
use Carbon\Carbon;
6
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)
62
    {
63
        $align = "";
64
        switch ($alignment) {
65
            case 'n':
66
                $align = "Neutral";
67
                break;
68
            case 'e':
69
                $align = "Evil";
70
                break;
71
            case 'g':
72
                $align = "Good";
73
                break;
74
        }
75
        return $align;
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
     * @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);
0 ignored issues
show
Bug introduced by
array_column($players, 'level') cannot be passed to array_multisort() as the parameter $arr expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

210
        array_multisort(/** @scrutinizer ignore-type */ array_column($players, 'level'), SORT_DESC, $players);
Loading history...
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)
223
    {
224
        $database = [];
225
        $row = 0;
226
        if (($handle = fopen($this->config['bot_db'], "r")) !== false) {
227
            while (($data = fgetcsv($handle, 1024, "\t")) !== false) {
228
                $row++;
229
                if ($row == 1) {
230
                    continue;
231
                }
232
233
                if ($nick !== null && strcmp($data[0], $nick) !== 0) {
234
                    continue;
235
                }
236
237
                $record = [
238
                    'nick' => $data[0], // nick
239
                    'level' => $data[3], // level
240
                    'admin' => ($data[2] ? 'Yes' : 'No'), // admin
241
                    'class' => $data[4], // class
242
                    'ttl' => [
243
                        'display' => $this->secondsToTime((int) $data[5]),
244
                        'numeric' => (int) $data[5], // ttl
245
                    ],
246
                    'nick_host' => $data[7], // nick and host
247
                    'online' => ($data[8] ? 'Yes' : 'No'), // online
248
                    'idled' => [
249
                        'display' => $this->secondsToTime((int) $data[9]), // idled
250
                        'numeric' => (int) $data[9],
251
                    ],
252
                    'x_pos' => (int) $data[10], // x pos
253
                    'y_pos' => (int) $data[11], // y pos
254
                    'msg_pen' => [
255
                        'display' => $this->secondsToTime((int) $data[12]), // msg pen
256
                        'numeric' => (int) $data[12],
257
                    ],
258
                    'nick_pen' => [
259
                        'display' => $this->secondsToTime((int) $data[13]), // nick pen
260
                        'numeric' => (int) $data[13],
261
                    ],
262
                    'part_pen' => [
263
                        'display' => $this->secondsToTime((int) $data[14]), // part pen
264
                        'numeric' => (int) $data[14],
265
                    ],
266
                    'kick_pen' => [
267
                        'display' => $this->secondsToTime((int) $data[15]), // kick pen
268
                        'numeric' => (int) $data[15],
269
                    ],
270
                    'quit_pen' => [
271
                        'display' => $this->secondsToTime((int) $data[16]), // quit pen
272
                        'numeric' => (int) $data[16],
273
                    ],
274
                    'quest_pen' => [
275
                        'display' => $this->secondsToTime((int) $data[17]), // quest pen
276
                        'numeric' => (int) $data[17],
277
                    ],
278
                    'logout_pen' => [
279
                        'display' => $this->secondsToTime((int) $data[18]), // logout pen
280
                        'numeric' => (int) $data[18],
281
                    ],
282
                    'total_pen' => [
283
                        'display' => $this->secondsToTime($this->sumFields($data, 12, 18)),
284
                        'numeric' => $this->sumFields($data, 12, 18),
285
                    ],
286
                    'created' => [
287
                        'display' => date('Y-m-d H:i:s', (int) $data[19]), // created
288
                        'numeric' => (int) $data[19],
289
                    ],
290
                    'last_login' => [
291
                        'display' => date('Y-m-d H:i:s', (int) $data[20]), // last login
292
                        'numeric' => (int) $data[20],
293
                    ],
294
                    'amulet' => [
295
                        'display' => $data[21], // amulet
296
                        'numeric' => (int) $data[21],
297
                        'unique' => $this->parseUniqueItem($data[21])
298
                    ],
299
                    'charm' => [
300
                        'display' => $data[22], // charm
301
                        'numeric' => (int) $data[22],
302
                        'unique' => $this->parseUniqueItem($data[22])
303
                    ],
304
                    'helm' => [
305
                        'display' => $data[23], // helm
306
                        'numeric' => (int) $data[23],
307
                        'unique' => $this->parseUniqueItem($data[23])
308
                    ],
309
                    'boots' => [
310
                        'display' => $data[24], // boots
311
                        'numeric' => (int) $data[24],
312
                        'unique' => $this->parseUniqueItem($data[24])
313
                    ],
314
                    'gloves' => [
315
                        'display' => $data[25], // gloves
316
                        'numeric' => (int) $data[25],
317
                        'unique' => $this->parseUniqueItem($data[25])
318
                    ],
319
                    'ring' => [
320
                        'display' => $data[26], // ring
321
                        'numeric' => (int) $data[26],
322
                        'unique' => $this->parseUniqueItem($data[26])
323
                    ],
324
                    'leggings' => [
325
                        'display' => $data[27], // leggings
326
                        'numeric' => (int) $data[27],
327
                        'unique' => $this->parseUniqueItem($data[27])
328
                    ],
329
                    'shield' => [
330
                        'display' => $data[28], // shield
331
                        'numeric' => (int) $data[28],
332
                        'unique' => $this->parseUniqueItem($data[28])
333
                    ],
334
                    'tunic' => [
335
                        'display' => $data[29], // tunic
336
                        'numeric' => (int) $data[29],
337
                        'unique' => $this->parseUniqueItem($data[29])
338
                    ],
339
                    'weapon' => [
340
                        'display' => $data[30], // weapon
341
                        'numeric' => (int) $data[30],
342
                        'unique' => $this->parseUniqueItem($data[30])
343
                    ],
344
                    'sum' => $this->sumFields($data, 21, 30),
345
                    'alignment' => $this->parseAlignment($data[31]), // alignment
346
                ];
347
348
                if ($nick !== null) {
349
                    return $record;
350
                }
351
352
                $database[] = $record;
353
            }
354
            fclose($handle);
355
        }
356
357
        if ($nick !== null) {
358
            return 0;
359
        }
360
361
        return $database;
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)
372
    {
373
        $modifiers = [
374
            'items' => [],
375
            'total' => 0,
376
        ];
377
378
        $tmp = [];
379
        $handle = fopen($this->config['bot_mod'], "r");
380
        if ($handle) {
0 ignored issues
show
introduced by
$handle is of type resource|false, thus it always evaluated to false.
Loading history...
381
            while (($line = fgets($handle)) !== false) {
382
                if ($nick != null && strpos($line, $nick) !== false) {
383
                    $tmp[] = $line;
384
                }
385
386
                if ($nick == null) {
387
                    $tmp[] = $line;
388
                }
389
            }
390
            fclose($handle);
391
        }
392
393
        $tmp = array_reverse($tmp);
394
        $modifiers['total'] = count($tmp);
395
        $modifiers['items'] = array_slice($tmp, 0, ($limit > 0 ) ? $limit : null);
396
397
        return $modifiers;
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()
479
    {
480
        $players = [];
481
482
        $row = 0;
483
        if (($handle = fopen($this->config['bot_db'], "r")) !== false) {
484
            while (($data = fgetcsv($handle, 1024, "\t")) !== false) {
485
                $row++;
486
                if ($row == 1) {
487
                    continue;
488
                }
489
490
                $players[] = $data[0]; // nick
491
            }
492
        }
493
494
        return $players;
495
    }
496
}
497