Passed
Pull Request — v3 (#656)
by Jesse
21:20 queued 19:12
created

Gta5m::beforeSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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('%s:%s', $server->ip, $server->port_query);
116 30
        $ch = curl_init();
117 30
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
118 30
        curl_setopt($ch, CURLOPT_URL, sprintf('http://%s/players.json', $connection));
119 30
        $data = curl_exec($ch);
120 30
        curl_close($ch);
121 30
        $this->PlayerList = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data can also be of type true. However, the property $PlayerList is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
122 15
    }
123
124
    /**
125
     * Process the response
126
     *
127
     * @return array
128
     * @throws \GameQ\Exception\Protocol
129
     */
130 30
    public function processResponse()
131
    {
132
        // In case it comes back as multiple packets (it shouldn't)
133 30
        $buffer = new Buffer(implode('', $this->packets_response));
134
135
        // Figure out what packet response this is for
136 30
        $response_type = $buffer->readString(PHP_EOL);
137
138
        // Figure out which packet response this is
139 30
        if (empty($response_type) || !array_key_exists($response_type, $this->responses)) {
140 12
            throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid");
141
        }
142
143
        // Offload the call
144 18
        $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]);
145
146 18
        return $results;
147
    }
148
149
    /*
150
     * Internal methods
151
     */
152
153
    /**
154
     * Handle processing the status response
155
     *
156
     * @param Buffer $buffer
157
     *
158
     * @return array
159
     */
160 18
    protected function processStatus(Buffer $buffer)
161
    {
162
        // Set the result to a new result instance
163 18
        $result = new Result();
164
165
        // Lets peek and see if the data starts with a \
166 18
        if ($buffer->lookAhead(1) == '\\') {
167
            // Burn the first one
168 18
            $buffer->skip(1);
169 3
        }
170
171
        // Explode the data
172 18
        $data = explode('\\', $buffer->getBuffer());
173
174
        // No longer needed
175 18
        unset($buffer);
176
177 18
        $itemCount = count($data);
178
179
        // Now lets loop the array
180 18
        for ($x = 0; $x < $itemCount; $x += 2) {
181
            // Set some local vars
182 18
            $key = $data[$x];
183 18
            $val = $data[$x + 1];
184
185 18
            if (in_array($key, ['challenge'])) {
186 18
                continue; // skip
187
            }
188
189
            // Regular variable so just add the value.
190 18
            $result->add($key, $val);
191 3
        }
192
193 18
        if ($this->PlayerList) {
194
            $players = [];
195
            $data = json_decode($this->PlayerList);
196
            foreach ($data as $player) {
197
                $players[] = array("id"=>$player->id,"name"=>$player->name,"ping"=>$player->ping,"identifiers"=>$player->identifiers);
198
            }
199
            $result->add('players', $players);
200
        }
201
202
        /*var_dump($data);
203
        var_dump($result->fetch());
204
205
        exit;*/
206
207 18
        return $result->fetch();
208
    }
209
}
210