Passed
Pull Request — v3 (#656)
by Jesse
29:57 queued 27:42
created

Gta5m::beforeSend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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