Passed
Pull Request — v3 (#656)
by Jesse
30:32
created

Gta5m::beforeSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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