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