1: | <?php |
2: | |
3: | |
4: | namespace GameQ\Protocols; |
5: | |
6: | use GameQ\Buffer; |
7: | use GameQ\Exception\Protocol as Exception; |
8: | use GameQ\Helpers\Str; |
9: | use GameQ\Protocol; |
10: | use GameQ\Result; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | class Quake2 extends Protocol |
20: | { |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | protected $packets = [ |
28: | self::PACKET_STATUS => "\xFF\xFF\xFF\xFFstatus\x00", |
29: | ]; |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | protected $responses = [ |
37: | "\xFF\xFF\xFF\xFF\x70\x72\x69\x6e\x74" => 'processStatus', |
38: | ]; |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | protected $protocol = 'quake2'; |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | protected $name = 'quake2'; |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | protected $name_long = "Quake 2 Server"; |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | protected $normalize = [ |
67: | |
68: | 'general' => [ |
69: | |
70: | 'gametype' => 'gamename', |
71: | 'hostname' => 'hostname', |
72: | 'mapname' => 'mapname', |
73: | 'maxplayers' => 'maxclients', |
74: | 'mod' => 'g_gametype', |
75: | 'numplayers' => 'clients', |
76: | 'password' => 'password', |
77: | ], |
78: | |
79: | 'player' => [ |
80: | 'name' => 'name', |
81: | 'ping' => 'ping', |
82: | 'score' => 'frags', |
83: | ], |
84: | ]; |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | public function processResponse() |
94: | { |
95: | |
96: | $buffer = new Buffer(implode('', $this->packets_response)); |
97: | |
98: | |
99: | $header = $buffer->readString("\x0A"); |
100: | |
101: | |
102: | if (empty($header) || !array_key_exists($header, $this->responses)) { |
103: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
104: | } |
105: | |
106: | return call_user_func_array([$this, $this->responses[$header]], [$buffer]); |
107: | } |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | protected function processStatus(Buffer $buffer) |
117: | { |
118: | |
119: | $results = $this->processServerInfo(new Buffer($buffer->readString("\x0A"))); |
120: | |
121: | $results = array_merge_recursive( |
122: | $results, |
123: | $this->processPlayers(new Buffer($buffer->getBuffer())) |
124: | ); |
125: | |
126: | unset($buffer); |
127: | |
128: | |
129: | return $results; |
130: | } |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | protected function processServerInfo(Buffer $buffer) |
140: | { |
141: | |
142: | $result = new Result(); |
143: | |
144: | |
145: | $buffer->readString('\\'); |
146: | |
147: | |
148: | while ($buffer->getLength()) { |
149: | |
150: | $result->add( |
151: | trim($buffer->readString('\\')), |
152: | Str::isoToUtf8(trim($buffer->readStringMulti(['\\', "\x0a"]))) |
153: | ); |
154: | } |
155: | |
156: | $result->add('password', 0); |
157: | $result->add('mod', 0); |
158: | |
159: | unset($buffer); |
160: | |
161: | return $result->fetch(); |
162: | } |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: | protected function processPlayers(Buffer $buffer) |
172: | { |
173: | |
174: | $playerCount = 0; |
175: | |
176: | |
177: | $result = new Result(); |
178: | |
179: | |
180: | while ($buffer->getLength()) { |
181: | |
182: | $playerInfo = new Buffer($buffer->readString("\x0A")); |
183: | |
184: | |
185: | $result->addPlayer('frags', $playerInfo->readString("\x20")); |
186: | $result->addPlayer('ping', $playerInfo->readString("\x20")); |
187: | |
188: | |
189: | $playerInfo->skip(1); |
190: | |
191: | |
192: | $result->addPlayer('name', Str::isoToUtf8(trim(($playerInfo->readString('"'))))); |
193: | |
194: | |
195: | $playerInfo->skip(2); |
196: | |
197: | |
198: | $result->addPlayer('address', trim($playerInfo->readString('"'))); |
199: | |
200: | |
201: | $playerCount++; |
202: | |
203: | |
204: | unset($playerInfo); |
205: | } |
206: | |
207: | $result->add('clients', $playerCount); |
208: | |
209: | |
210: | unset($buffer, $playerCount); |
211: | |
212: | return $result->fetch(); |
213: | } |
214: | } |
215: | |