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:
26: /**
27: * Teeworlds Protocol class
28: *
29: * Only supports versions > 0.5
30: *
31: * @author Austin Bischoff <austin@codebeard.com>
32: * @author Marcel Bößendörfer <m.boessendoerfer@marbis.net>
33: */
34: class Teeworlds extends Protocol
35: {
36: /**
37: * Array of packets we want to look up.
38: * Each key should correspond to a defined method in this or a parent class
39: *
40: * @var array
41: */
42: protected $packets = [
43: self::PACKET_ALL => "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05",
44: // 0.5 Packet (not compatible, maybe some wants to implement "Teeworldsold")
45: //self::PACKET_STATUS => "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFFgief",
46: ];
47:
48: /**
49: * Use the response flag to figure out what method to run
50: *
51: * @var array
52: */
53: protected $responses = [
54: "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffinf35" => "processAll",
55: ];
56:
57: /**
58: * The query protocol used to make the call
59: *
60: * @var string
61: */
62: protected $protocol = 'teeworlds';
63:
64: /**
65: * String name of this protocol class
66: *
67: * @var string
68: */
69: protected $name = 'teeworlds';
70:
71: /**
72: * Longer string name of this protocol class
73: *
74: * @var string
75: */
76: protected $name_long = "Teeworlds Server";
77:
78: /**
79: * The client join link
80: *
81: * @var string
82: */
83: protected $join_link = "steam://connect/%s:%d/";
84:
85: /**
86: * Normalize settings for this protocol
87: *
88: * @var array
89: */
90: protected $normalize = [
91: // General
92: 'general' => [
93: // target => source
94: 'dedicated' => 'dedicated',
95: 'hostname' => 'hostname',
96: 'mapname' => 'map',
97: 'maxplayers' => 'num_players_total',
98: ],
99: // Individual
100: 'player' => [
101: 'name' => 'name',
102: 'score' => 'score',
103: ],
104: ];
105:
106: /**
107: * Process the response
108: *
109: * @return array
110: * @throws Exception
111: * @throws \GameQ\Exception\Protocol
112: */
113: public function processResponse()
114: {
115: // Holds the results
116: $results = [];
117:
118: // Iterate over the packets
119: foreach ($this->packets_response as $response) {
120: // Make a buffer
121: $buffer = new Buffer($response);
122:
123: // Grab the header
124: $header = $buffer->readString();
125:
126: // Figure out which packet response this is
127: if (!array_key_exists($header, $this->responses)) {
128: throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
129: }
130:
131: // Now we need to call the proper method
132: $results = array_merge(
133: $results,
134: call_user_func_array([$this, $this->responses[$header]], [$buffer])
135: );
136: }
137:
138: unset($buffer);
139:
140: return $results;
141: }
142:
143: /**
144: * Handle processing all of the data returned
145: *
146: * @param Buffer $buffer
147: * @return array
148: * @throws \GameQ\Exception\Protocol
149: */
150: protected function processAll(Buffer $buffer)
151: {
152: // Set the result to a new result instance
153: $result = new Result();
154:
155: // Always dedicated
156: $result->add('dedicated', 1);
157:
158: $result->add('version', $buffer->readString());
159: $result->add('hostname', $buffer->readString());
160: $result->add('map', $buffer->readString());
161: $result->add('game_descr', $buffer->readString());
162: $result->add('flags', $buffer->readString()); // not sure about that
163: $result->add('num_players', $buffer->readString());
164: $result->add('maxplayers', $buffer->readString());
165: $result->add('num_players_total', $buffer->readString());
166: $result->add('maxplayers_total', $buffer->readString());
167:
168: // Players
169: while ($buffer->getLength()) {
170: $result->addPlayer('name', $buffer->readString());
171: $result->addPlayer('clan', $buffer->readString());
172: $result->addPlayer('flag', $buffer->readString());
173: $result->addPlayer('score', $buffer->readString());
174: $result->addPlayer('team', $buffer->readString());
175: }
176:
177: unset($buffer);
178:
179: return $result->fetch();
180: }
181: }
182: