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\Result; |
23
|
|
|
use GameQ\Server; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Stationeers Protocol Class |
27
|
|
|
* |
28
|
|
|
* This protocol uses a server list from a JSON response to find the server and parse the server's status information |
29
|
|
|
* |
30
|
|
|
* @author Austin Bischoff <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Stationeers extends Http |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The host (address) of the server to query to get the list of servers |
36
|
|
|
*/ |
37
|
|
|
const SERVER_LIST_HOST = '40.82.200.175'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The port of the server to query to get the list of servers |
41
|
|
|
*/ |
42
|
|
|
const SERVER_LIST_PORT = 8081; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Packets to send |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $packets = [ |
50
|
|
|
self::PACKET_STATUS => "GET /list HTTP/1.0\r\nAccept: */*\r\n\r\n", |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The protocol being used |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $protocol = 'stationeers'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* String name of this protocol class |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $name = 'stationeers'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Longer string name of this protocol class |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $name_long = "Stationeers"; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The client join link |
76
|
|
|
* |
77
|
|
|
* @type string |
78
|
|
|
*/ |
79
|
|
|
protected $join_link = ""; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Normalize some items |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $normalize = [ |
87
|
|
|
// General |
88
|
|
|
'general' => [ |
89
|
|
|
// target => source |
90
|
|
|
'dedicated' => 'dedicated', |
91
|
|
|
'hostname' => 'hostname', |
92
|
|
|
'mapname' => 'map', |
93
|
|
|
'maxplayers' => 'maxplayers', |
94
|
|
|
'numplayers' => 'numplayers', |
95
|
|
|
'password' => 'password', |
96
|
|
|
], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Handle changing the call to call a central server rather than the server directly |
101
|
|
|
* |
102
|
|
|
* @param Server $server |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
12 |
|
public function beforeSend(Server $server) |
107
|
|
|
{ |
108
|
|
|
// Save the passed IP information so we can use it later for the response |
109
|
12 |
|
$this->realIp = $server->ip; |
|
|
|
|
110
|
12 |
|
$this->realPortQuery = $server->port_query; |
|
|
|
|
111
|
|
|
|
112
|
|
|
// Override the existing settings with the query host information |
113
|
12 |
|
$server->ip = self::SERVER_LIST_HOST; |
114
|
12 |
|
$server->port_query = self::SERVER_LIST_PORT; |
115
|
4 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Process the response |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
* @throws Exception |
122
|
|
|
*/ |
123
|
12 |
|
public function processResponse() |
124
|
|
|
{ |
125
|
12 |
|
if (empty($this->packets_response)) { |
126
|
|
|
return []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Implode and rip out the JSON |
130
|
12 |
|
preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); |
131
|
|
|
|
132
|
|
|
// Return should be JSON, let's validate |
133
|
12 |
|
if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) { |
134
|
|
|
throw new Exception(__METHOD__ . " JSON response from Stationeers protocol is invalid."); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// By default no server is found |
138
|
12 |
|
$server = null; |
139
|
|
|
|
140
|
|
|
// Find the server on this list by iterating over the entire list. |
141
|
12 |
|
foreach ($json->GameSessions as $serverEntry) { |
142
|
|
|
// Server information passed matches an entry on this list |
143
|
12 |
|
if ($serverEntry->Address === $this->realIp && (int)$serverEntry->Port === $this->realPortQuery) { |
144
|
12 |
|
$server = $serverEntry; |
145
|
12 |
|
break; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// No longer needed, be free! |
150
|
12 |
|
unset($matches, $serverEntry, $json); |
151
|
|
|
|
152
|
|
|
// The server information passed was not found in this host's list |
153
|
12 |
|
if (!$server) { |
154
|
|
|
throw new Exception(sprintf( |
155
|
|
|
'%s Unable to find the server "%s:%d" in the Stationeers server list', |
156
|
|
|
__METHOD__, |
157
|
|
|
$this->realIp, |
158
|
|
|
$this->realPortQuery |
159
|
|
|
)); |
160
|
|
|
} |
161
|
|
|
|
162
|
12 |
|
$result = new Result(); |
163
|
|
|
|
164
|
|
|
// Server is always dedicated |
165
|
12 |
|
$result->add('dedicated', 1); |
166
|
|
|
|
167
|
|
|
// Add server items |
168
|
12 |
|
$result->add('hostname', $server->Name); |
169
|
12 |
|
$result->add('gq_address', $server->Address); |
170
|
12 |
|
$result->add('gq_port_query', $server->Port); |
171
|
12 |
|
$result->add('version', $server->Version); |
172
|
12 |
|
$result->add('map', $server->MapName); |
173
|
12 |
|
$result->add('uptime', $server->UpTime); |
174
|
12 |
|
$result->add('password', (int)$server->Password); |
175
|
12 |
|
$result->add('numplayers', $server->Players); |
176
|
12 |
|
$result->add('maxplayers', $server->MaxPlayers); |
177
|
12 |
|
$result->add('type', $server->Type); |
178
|
|
|
|
179
|
12 |
|
unset($server); |
180
|
|
|
|
181
|
12 |
|
return $result->fetch(); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|