Passed
Pull Request — v3 (#716)
by
unknown
60:45 queued 26:18
created

Rust   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 14
eloc 73
c 10
b 0
f 0
dl 0
loc 152
ccs 8
cts 8
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parseKeywords() 0 36 8
A processDetails() 0 16 6
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Protocols;
20
21
use GameQ\Buffer;
22
23
/**
24
 * Class Rust
25
 *
26
 * @package GameQ\Protocols
27
 * @author  Austin Bischoff <[email protected]>
28
 * @author  Nikolay Ipanyuk <[email protected]>
29
 */
30
class Rust extends Source
31
{
32
    /**
33
     * Server keywords
34
     *
35
     * mp - Max players
36
     * cp - Current players
37
     * qp - Queue players
38
     * born - Time to create a new save / Wipe time (unixtime)
39
     * pt - Protocol type (rak - RakNet, sw - SteamNetworking)
40
     * h - Hash Assembly-CSharp.dll
41
     * v - Protocol version
42
     * cs - Build version
43
     * st - Status server (ok - Normal work, rst - Server restarting)
44
     * gm - Game mode
45
     * oxide - Oxide/uMod (https://umod.org/)
46
     * carbon - Carbon (https://carbonmod.gg/)
47
     * modded - Modded flag
48
     *
49
     * @type array
50
     */
51 18
    private $server_keywords = [
52
        'mp',
53 18
        'cp',
54
        'qp',
55 18
        'born',
56
        'pt',
57 18
        'h',
58 18
        'v',
59 18
        'cs',
60 3
        'st',
61
        'gm',
62 18
        'oxide',
63
        'carbon',
64
        'modded'
65
    ];
66
67
    /**
68
     * Server tags (https://wiki.facepunch.com/rust/server-browser-tags)
69
     *
70
     * @type array
71
     */
72
    private $server_tags = [
73
        'monthly',
74
        'biweekly',
75
        'weekly',
76
        'vanilla',
77
        'hardcore',
78
        'softcore',
79
        'pve',
80
        'roleplay',
81
        'creative',
82
        'minigame',
83
        'training',
84
        'battlefield',
85
        'broyale',
86
        'builds'
87
    ];
88
89
    /**
90
     * Region tags (https://wiki.facepunch.com/rust/server-browser-tags)
91
     *
92
     * @type array
93
     */
94
    private $region_tags = [
95
        'na',
96
        'sa',
97
        'eu',
98
        'wa',
99
        'ea',
100
        'oc',
101
        'af'
102
    ];
103
104
    /**
105
     * String name of this protocol class
106
     *
107
     * @type string
108
     */
109
    protected $name = 'rust';
110
111
    /**
112
     * Longer string name of this protocol class
113
     *
114
     * @type string
115
     */
116
    protected $name_long = "Rust";
117
118
    /**
119
     * Processing of server tags and more correct indication of the current number of players and the maximum number of players
120
     *
121
     * @param Buffer $buffer
122
     */
123
    protected function processDetails(Buffer $buffer)
124
    {
125
        $results = parent::processDetails($buffer);
126
        if (isset($results['keywords']) and strlen($results['keywords']) > 0) {
127
            $keywords = explode(',', $results['keywords']);
128
            if (sizeof($keywords) > 0) {
129
                $results = array_merge($results, $this->parseKeywords($keywords));
130
                foreach (['cp' => 'num_players', 'mp' => 'max_players'] as $keyword => $key) {
131
                    if (isset($results['server.keywords'][$keyword])) {
132
                        $results[$key] = intval($results['server.keywords'][$keyword]);
133
                    }
134
                }
135
            }
136
        }
137
138
        return $results;
139
    }
140
141
    /**
142
     * Parse keywords
143
     *
144
     * @param array $keywords
145
     */
146
    protected function parseKeywords(array $keywords = [])
147
    {
148
        $result = [
149
            'server.keywords' => [],
150
            'unhandled.tags' => [],
151
            'server.tags' => []
152
        ];
153
154
        foreach ($keywords as $gametag) {
155
            $parsed = false;
156
            $gametag = trim(mb_strtolower($gametag));
157
            if (in_array($gametag, $this->server_tags)) {
158
                $parsed = true;
159
                $result['server.tags'][] = $gametag;
160
            } elseif (in_array($gametag, $this->region_tags)) {
161
                $parsed = true;
162
                $result['region'] = mb_strtoupper($gametag);
163
            } else {
164
                foreach ($this->server_keywords as $server_keyword) {
165
                    if (strpos($gametag, $server_keyword) === 0) {
166
                        $parsed = true;
167
                        if ($gametag == $server_keyword) {
168
                            $result['server.keywords'][$gametag] = true;
169
                        } else {
170
                            $result['server.keywords'][$server_keyword] = mb_substr($gametag, mb_strlen($server_keyword));
171
                        }
172
                    }
173
                }
174
            }
175
176
            if (!$parsed) {
177
                $result['unhandled.tags'][] = $gametag;
178
            }
179
        }
180
181
        return $result;
182
    }
183
}
184