Completed
Push — v3 ( d10fe9...93e9ec )
by Austin
04:15
created

Gta5m::processStatus()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 40
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 16
cts 16
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 1
crap 4
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
use GameQ\Exception\Protocol as Exception;
23
use GameQ\Protocol;
24
use GameQ\Result;
25
26
/**
27
 * GTA Five M Protocol Class
28
 *
29
 * Server base can be found at https://fivem.net/
30
 *
31
 * Based on code found at https://github.com/LiquidObsidian/fivereborn-query
32
 *
33
 * @author Austin Bischoff <[email protected]>
34
 */
35
class Gta5m extends Protocol
36
{
37
38
    /**
39
     * Array of packets we want to look up.
40
     * Each key should correspond to a defined method in this or a parent class
41
     *
42
     * @type array
43
     */
44
    protected $packets = [
45
        self::PACKET_STATUS => "\xFF\xFF\xFF\xFFgetinfo xxx",
46
    ];
47
48
    /**
49
     * Use the response flag to figure out what method to run
50
     *
51
     * @type array
52
     */
53
    protected $responses = [
54
        "\xFF\xFF\xFF\xFFinfoResponse" => "processStatus",
55
    ];
56
57
    /**
58
     * The query protocol used to make the call
59
     *
60
     * @type string
61
     */
62
    protected $protocol = 'gta5m';
63
64
    /**
65
     * String name of this protocol class
66
     *
67
     * @type string
68
     */
69
    protected $name = 'gta5m';
70
71
    /**
72
     * Longer string name of this protocol class
73
     *
74
     * @type string
75
     */
76
    protected $name_long = "GTA Five M";
77
78
    /**
79
     * Normalize settings for this protocol
80
     *
81
     * @type array
82
     */
83
    protected $normalize = [
84
        // General
85
        'general' => [
86
            // target       => source
87
            'gametype'   => 'gametype',
88
            'hostname'   => 'hostname',
89
            'mapname'    => 'mapname',
90
            'maxplayers' => 'sv_maxclients',
91
            'mod'        => 'gamename',
92
            'numplayers' => 'clients',
93
            'password'   => 'privateClients',
94
        ],
95
    ];
96
97
    /**
98
     * Process the response
99
     *
100
     * @return array
101
     * @throws \GameQ\Exception\Protocol
102
     */
103 5
    public function processResponse()
104
    {
105
        // In case it comes back as multiple packets (it shouldn't)
106 5
        $buffer = new Buffer(implode('', $this->packets_response));
107
108
        // Figure out what packet response this is for
109 5
        $response_type = $buffer->readString(PHP_EOL);
110
111
        // Figure out which packet response this is
112 5
        if (empty($response_type) || !array_key_exists($response_type, $this->responses)) {
113 2
            throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid");
114
        }
115
116
        // Offload the call
117 3
        $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]);
118
119 3
        return $results;
120
    }
121
122
    /*
123
     * Internal methods
124
     */
125
126
    /**
127
     * Handle processing the status response
128
     *
129
     * @param Buffer $buffer
130
     *
131
     * @return array
132
     */
133 3
    protected function processStatus(Buffer $buffer)
134
    {
135
        // Set the result to a new result instance
136 3
        $result = new Result();
137
138
        // Lets peek and see if the data starts with a \
139 3
        if ($buffer->lookAhead(1) == '\\') {
140
            // Burn the first one
141 3
            $buffer->skip(1);
142 3
        }
143
144
        // Explode the data
145 3
        $data = explode('\\', $buffer->getBuffer());
146
147
        // No longer needed
148 3
        unset($buffer);
149
150 3
        $itemCount = count($data);
151
152
        // Now lets loop the array
153 3
        for ($x = 0; $x < $itemCount; $x += 2) {
154
            // Set some local vars
155 3
            $key = $data[$x];
156 3
            $val = $data[$x + 1];
157
158 3
            if (in_array($key, ['challenge'])) {
159 3
                continue; // skip
160
            }
161
162
            // Regular variable so just add the value.
163 3
            $result->add($key, $val);
164 3
        }
165
166
        /*var_dump($data);
167
        var_dump($result->fetch());
168
169
        exit;*/
170
171 3
        return $result->fetch();
172
    }
173
}
174