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\Protocol; |
22: | use GameQ\Buffer; |
23: | use GameQ\Result; |
24: | use GameQ\Exception\Protocol as Exception; |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | class Doom3 extends Protocol |
35: | { |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | protected $packets = [ |
43: | self::PACKET_ALL => "\xFF\xFFgetInfo\x00PiNGPoNG\x00", |
44: | ]; |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | protected $responses = [ |
52: | "\xFF\xFFinfoResponse" => 'processStatus', |
53: | ]; |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | protected $protocol = 'doom3'; |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | protected $name = 'doom3'; |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | protected $name_long = "Doom 3"; |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | protected $join_link = null; |
82: | |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | protected $normalize = [ |
89: | |
90: | 'general' => [ |
91: | |
92: | 'hostname' => 'si_name', |
93: | 'gametype' => 'gamename', |
94: | 'mapname' => 'si_map', |
95: | 'maxplayers' => 'si_maxPlayers', |
96: | 'numplayers' => 'clients', |
97: | 'password' => 'si_usepass', |
98: | ], |
99: | |
100: | 'player' => [ |
101: | 'name' => 'name', |
102: | 'ping' => 'ping', |
103: | ], |
104: | ]; |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | public function processResponse() |
113: | { |
114: | |
115: | $buffer = new Buffer(implode('', $this->packets_response)); |
116: | |
117: | |
118: | $header = $buffer->readString(); |
119: | |
120: | |
121: | |
122: | if (empty($header) || !array_key_exists($header, $this->responses)) { |
123: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
124: | } |
125: | |
126: | return call_user_func_array([$this, $this->responses[$header]], [$buffer]); |
127: | } |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | protected function processStatus(Buffer $buffer) |
137: | { |
138: | |
139: | $results = $this->processServerInfo($buffer); |
140: | |
141: | $results = array_merge_recursive( |
142: | $results, |
143: | $this->processPlayers($buffer) |
144: | ); |
145: | |
146: | unset($buffer); |
147: | |
148: | |
149: | return $results; |
150: | } |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | protected function processServerInfo(Buffer $buffer) |
160: | { |
161: | |
162: | $result = new Result(); |
163: | |
164: | $result->add('version', $buffer->readInt8() . '.' . $buffer->readInt8()); |
165: | |
166: | |
167: | while ($buffer->getLength()) { |
168: | $key = trim($buffer->readString()); |
169: | $val = utf8_encode(trim($buffer->readString())); |
170: | |
171: | |
172: | if (empty($key) && empty($val)) { |
173: | break; |
174: | } |
175: | |
176: | $result->add($key, $val); |
177: | } |
178: | |
179: | unset($buffer); |
180: | |
181: | return $result->fetch(); |
182: | } |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | protected function processPlayers(Buffer $buffer) |
192: | { |
193: | |
194: | $playerCount = 0; |
195: | |
196: | |
197: | $result = new Result(); |
198: | |
199: | |
200: | |
201: | while (($id = $buffer->readInt8()) != 32) { |
202: | |
203: | $result->addPlayer('id', $id); |
204: | $result->addPlayer('ping', $buffer->readInt16()); |
205: | $result->addPlayer('rate', $buffer->readInt32()); |
206: | |
207: | $result->addPlayer('name', utf8_encode(trim($buffer->readString()))); |
208: | |
209: | |
210: | $playerCount++; |
211: | } |
212: | |
213: | |
214: | $result->add('clients', $playerCount); |
215: | |
216: | |
217: | unset($buffer, $playerCount); |
218: | |
219: | return $result->fetch(); |
220: | } |
221: | } |
222: | |