Passed
Pull Request — v3 (#707)
by
unknown
33:17
created

Beammp::processResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 22
rs 9.7666
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\Exception\Protocol as Exception;
22
use GameQ\Protocol;
23
use GameQ\Result;
24
use GameQ\Server;
25
26
/**
27
 * BeamMP (An BeamNG multiplayer client)
28
 * https://beammp.com/
29
 *
30
 * @author iTeeLion <[email protected]>
31
 * @author Austin Bischoff <[email protected]>
32
 */
33
class Beammp extends Protocol
34
{
35
    /**
36
     * The protocol being used
37
     *
38
     * @var string
39
     */
40
    protected $protocol = 'beammp';
41
42
    /**
43
     * String name of this protocol class
44
     *
45
     * @var string
46
     */
47
    protected $name = 'beammp';
48
49
    /**
50
     * Longer string name of this protocol class
51
     *
52
     * @var string
53
     */
54
    protected $name_long = "BeamMP";
55
56
    /**
57
     * Normalize some items
58
     *
59
     * @var array
60
     */
61
    protected $normalize = [
62
        // General
63
        'general' => [
64
            'online'        => 'online',
65
            'dedicated'     => 'dedicated',
66
            'hostname'      => 'hostname',
67
            'mod'           => 'mod',
68
            'address'       => 'ip',
69
            'port'          => 'port',
70
            'maxplayers'    => 'maxplayers',
71
            'numplayers'    => 'players_count',
72
            'players'       => 'players_list',
73
            'dport'         => 'dport',
74
            'mapname'       => 'map',
75
            'version'       => 'version',
76
            'cversion'      => 'cversion',
77
            'official'      => 'official',
78
            'featured'      => 'featured',
79
            'owner'         => 'owner',
80
            'sdesc'         => 'sdesc',
81
            'pps'           => 'pps',
82
            'modlist'       => 'modlist',
83
            'modstotal'     => 'modstotal',
84
            'modstotalsize' => 'modstotalsize',
85
        ],
86
    ];
87
88
    /**
89
     * Holds the real ip so we can overwrite it back
90
     *
91
     * @var string
92
     */
93
    protected $realIp = null;
94
95
    protected $realPortQuery = null;
96
97
    /**
98
     * Cache beammp backend result
99
     *
100
     * @var array
101
     */
102
    public static $backendResult = [];
103
104
    /**
105
     * Do request with builtin php methods
106
     *
107
     * @param Server $server
108
     */
109
    public function beforeSend(Server $server)
110
    {
111
        if (empty(self::$backendResult)) {
112
            $context = stream_context_create(['http' => ['method' => 'POST', 'content' => '']]);
113
            self::$backendResult = json_decode(file_get_contents('https://backend.beammp.com/servers/', false, $context), true);
114
            if (self::$backendResult === null) {
115
                self::$backendResult = [];
116
            }
117
        }
118
119
        $this->packets_response = current(array_filter(self::$backendResult, function ($s) use ($server) {
120
            return ($s['ip'] == $server->ip && $s['port'] == $server->port_query);
121
        }));
122
        $this->realIp = $server->ip;
123
        $this->realPortQuery = $server->port_query;
124
    }
125
126
    /**
127
     * Process the response
128
     *
129
     * @return array
130
     * @throws Exception
131
     */
132
    public function processResponse()
133
    {
134
        $result = new Result();
135
        $result->add('dedicated', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type array|string expected by parameter $value of GameQ\Result::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        $result->add('dedicated', /** @scrutinizer ignore-type */ true);
Loading history...
136
        $result->add('mod', 'beammp');
137
        $result->add('ip', $this->realIp);
138
        $result->add('port', $this->realPortQuery);
139
140
        if (empty($this->packets_response)) {
141
            $result->add('online', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array|string expected by parameter $value of GameQ\Result::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
            $result->add('online', /** @scrutinizer ignore-type */ false);
Loading history...
142
        } else {
143
            $this->packets_response['players_list'] = explode(';', preg_replace('/;$/u', '', $this->packets_response['playerslist']));
144
            $this->packets_response['players_count'] = count($this->packets_response['players_list']);
145
            unset($this->packets_response['players']);
146
147
            $result->add('online', true);
148
            foreach ($this->packets_response as $k => $v) {
149
                $result->add($k, $v);
150
            }
151
        }
152
153
        return $result->fetch();
154
    }
155
}
156