1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | namespace GameQ\Protocols; |
20: | |
21: | use GameQ\Exception\Protocol as Exception; |
22: | use GameQ\Helpers\Arr; |
23: | use GameQ\Result; |
24: | use GameQ\Server; |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | class Stationeers extends Http |
35: | { |
36: | |
37: | |
38: | |
39: | const SERVER_LIST_HOST = '40.82.200.175'; |
40: | |
41: | |
42: | |
43: | |
44: | const SERVER_LIST_PORT = 8081; |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | protected $packets = [ |
52: | self::PACKET_STATUS => "GET /list HTTP/1.0\r\nAccept: */*\r\n\r\n", |
53: | ]; |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | protected $protocol = 'stationeers'; |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | protected $name = 'stationeers'; |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | protected $name_long = "Stationeers"; |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | protected $normalize = [ |
82: | |
83: | 'general' => [ |
84: | |
85: | 'dedicated' => 'dedicated', |
86: | 'hostname' => 'hostname', |
87: | 'mapname' => 'map', |
88: | 'maxplayers' => 'maxplayers', |
89: | 'numplayers' => 'numplayers', |
90: | 'password' => 'password', |
91: | ], |
92: | ]; |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | protected $realIp = null; |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | protected $realPortQuery = null; |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | public function beforeSend(Server $server) |
120: | { |
121: | |
122: | $metaServerHost = $server->getOption('meta_host') ? $server->getOption('meta_host') : self::SERVER_LIST_HOST; |
123: | $metaServerPort = $server->getOption('meta_port') ? $server->getOption('meta_port') : self::SERVER_LIST_PORT; |
124: | |
125: | |
126: | Arr::shift($this->realIp, $server->ip, $metaServerHost); |
127: | Arr::shift($this->realPortQuery, $server->port_query, $metaServerPort); |
128: | } |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | public function processResponse() |
137: | { |
138: | |
139: | if (empty($this->packets_response)) { |
140: | return []; |
141: | } |
142: | |
143: | |
144: | preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); |
145: | |
146: | |
147: | if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) { |
148: | throw new Exception(__METHOD__ . " JSON response from Stationeers Metaserver is invalid."); |
149: | } |
150: | |
151: | |
152: | $server = null; |
153: | |
154: | |
155: | foreach ($json->GameSessions as $serverEntry) { |
156: | |
157: | if ($serverEntry->Address === $this->realIp && (int)$serverEntry->Port === $this->realPortQuery) { |
158: | $server = $serverEntry; |
159: | break; |
160: | } |
161: | } |
162: | |
163: | |
164: | unset($matches, $serverEntry, $json); |
165: | |
166: | |
167: | if (! $server) { |
168: | throw new Exception(sprintf( |
169: | '%s Unable to find the server "%s:%d" in the Stationeer Metaservers server list', |
170: | __METHOD__, |
171: | $this->realIp, |
172: | $this->realPortQuery |
173: | )); |
174: | } |
175: | |
176: | |
177: | $result = new Result(); |
178: | $result->add('dedicated', 1); |
179: | $result->add('hostname', $server->Name); |
180: | $result->add('gq_address', $server->Address); |
181: | $result->add('gq_port_query', $server->Port); |
182: | $result->add('version', $server->Version); |
183: | $result->add('map', $server->MapName); |
184: | $result->add('uptime', $server->UpTime); |
185: | $result->add('password', (int)$server->Password); |
186: | $result->add('numplayers', $server->Players); |
187: | $result->add('maxplayers', $server->MaxPlayers); |
188: | $result->add('type', $server->Type); |
189: | |
190: | |
191: | unset($server); |
192: | |
193: | return $result->fetch(); |
194: | } |
195: | } |
196: | |