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:
24: /**
25: * ECO Global Survival Protocol Class
26: *
27: * @author Austin Bischoff <austin@codebeard.com>
28: */
29: class Eco extends Http
30: {
31: /**
32: * Packets to send
33: *
34: * @var array
35: */
36: protected $packets = [
37: self::PACKET_STATUS => "GET /frontpage HTTP/1.0\r\nAccept: */*\r\n\r\n",
38: ];
39:
40: /**
41: * Http protocol is SSL
42: *
43: * @var string
44: */
45: protected $transport = self::TRANSPORT_TCP;
46:
47: /**
48: * The protocol being used
49: *
50: * @var string
51: */
52: protected $protocol = 'eco';
53:
54: /**
55: * String name of this protocol class
56: *
57: * @var string
58: */
59: protected $name = 'eco';
60:
61: /**
62: * Longer string name of this protocol class
63: *
64: * @var string
65: */
66: protected $name_long = "ECO Global Survival";
67:
68: /**
69: * query_port = client_port + 1
70: *
71: * @type int
72: */
73: protected $port_diff = 1;
74:
75: /**
76: * Normalize some items
77: *
78: * @var array
79: */
80: protected $normalize = [
81: // General
82: 'general' => [
83: // target => source
84: 'dedicated' => 'dedicated',
85: 'hostname' => 'description',
86: 'maxplayers' => 'totalplayers',
87: 'numplayers' => 'onlineplayers',
88: 'password' => 'haspassword',
89: ],
90: ];
91:
92: /**
93: * Process the response
94: *
95: * @return array
96: * @throws Exception
97: */
98: public function processResponse()
99: {
100: if (empty($this->packets_response)) {
101: return [];
102: }
103:
104: // Implode and rip out the JSON
105: preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
106:
107: // Return should be JSON, let's validate
108: if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
109: throw new Exception("JSON response from Eco server is invalid.");
110: }
111:
112: $result = new Result();
113:
114: // Server is always dedicated
115: $result->add('dedicated', 1);
116:
117: foreach ($json->Info as $info => $setting) {
118: $result->add(strtolower($info), $setting);
119: }
120:
121: return $result->fetch();
122: }
123: }
124: