1: | <?php |
2: | |
3: | |
4: | namespace GameQ\Protocols; |
5: | |
6: | use GameQ\Buffer; |
7: | use GameQ\Exception\Protocol as Exception; |
8: | use GameQ\Protocol; |
9: | use GameQ\Result; |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | class Ffow extends Protocol |
22: | { |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | protected $packets = [ |
30: | self::PACKET_CHALLENGE => "\xFF\xFF\xFF\xFF\x57", |
31: | self::PACKET_RULES => "\xFF\xFF\xFF\xFF\x56%s", |
32: | self::PACKET_PLAYERS => "\xFF\xFF\xFF\xFF\x55%s", |
33: | self::PACKET_INFO => "\xFF\xFF\xFF\xFF\x46\x4C\x53\x51", |
34: | ]; |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | protected $responses = [ |
42: | "\xFF\xFF\xFF\xFF\x49\x02" => 'processInfo', |
43: | "\xFF\xFF\xFF\xFF\x45\x00" => 'processRules', |
44: | "\xFF\xFF\xFF\xFF\x44\x00" => 'processPlayers', |
45: | ]; |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | protected $protocol = 'ffow'; |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | protected $name = 'ffow'; |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | protected $name_long = "Frontlines Fuel of War"; |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | protected $port_diff = 2; |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | protected $normalize = [ |
81: | |
82: | 'general' => [ |
83: | |
84: | 'gametype' => 'gamemode', |
85: | 'hostname' => 'servername', |
86: | 'mapname' => 'mapname', |
87: | 'maxplayers' => 'max_players', |
88: | 'mod' => 'modname', |
89: | 'numplayers' => 'num_players', |
90: | 'password' => 'password', |
91: | ], |
92: | |
93: | 'player' => [ |
94: | 'name' => 'name', |
95: | 'ping' => 'ping', |
96: | 'score' => 'frags', |
97: | ], |
98: | ]; |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | public function challengeParseAndApply(Buffer $challenge_buffer) |
109: | { |
110: | |
111: | $challenge_buffer->skip(5); |
112: | |
113: | |
114: | return $this->challengeApply($challenge_buffer->read(4)); |
115: | } |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | public function processResponse() |
124: | { |
125: | |
126: | $results = []; |
127: | |
128: | foreach ($this->packets_response as $response) { |
129: | $buffer = new Buffer($response); |
130: | |
131: | |
132: | $response_type = $buffer->read(6); |
133: | |
134: | |
135: | if (!array_key_exists($response_type, $this->responses)) { |
136: | throw new Exception(__METHOD__ . " response type '" . bin2hex($response_type) . "' is not valid"); |
137: | } |
138: | |
139: | |
140: | $results = array_merge( |
141: | $results, |
142: | call_user_func_array([$this, $this->responses[$response_type]], [$buffer]) |
143: | ); |
144: | |
145: | unset($buffer); |
146: | } |
147: | |
148: | return $results; |
149: | } |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | protected function processInfo(Buffer $buffer) |
160: | { |
161: | |
162: | $result = new Result(); |
163: | |
164: | $result->add('servername', $buffer->readString()); |
165: | $result->add('mapname', $buffer->readString()); |
166: | $result->add('modname', $buffer->readString()); |
167: | $result->add('gamemode', $buffer->readString()); |
168: | $result->add('description', $buffer->readString()); |
169: | $result->add('version', $buffer->readString()); |
170: | $result->add('port', $buffer->readInt16()); |
171: | $result->add('num_players', $buffer->readInt8()); |
172: | $result->add('max_players', $buffer->readInt8()); |
173: | $result->add('dedicated', $buffer->readInt8()); |
174: | $result->add('os', $buffer->readInt8()); |
175: | $result->add('password', $buffer->readInt8()); |
176: | $result->add('anticheat', $buffer->readInt8()); |
177: | $result->add('average_fps', $buffer->readInt8()); |
178: | $result->add('round', $buffer->readInt8()); |
179: | $result->add('max_rounds', $buffer->readInt8()); |
180: | $result->add('time_left', $buffer->readInt16()); |
181: | |
182: | unset($buffer); |
183: | |
184: | return $result->fetch(); |
185: | } |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | protected function processRules(Buffer $buffer) |
196: | { |
197: | |
198: | $result = new Result(); |
199: | |
200: | |
201: | $buffer->skip(1); |
202: | |
203: | |
204: | while ($buffer->getLength()) { |
205: | $key = $buffer->readString(); |
206: | |
207: | if (strstr($key, "Map:")) { |
208: | $result->addSub("maplist", "name", $buffer->readString()); |
209: | } else { |
210: | $result->add($key, $buffer->readString()); |
211: | } |
212: | } |
213: | |
214: | unset($buffer); |
215: | |
216: | return $result->fetch(); |
217: | } |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | protected function processPlayers(Buffer $buffer) |
229: | { |
230: | |
231: | $result = new Result(); |
232: | |
233: | unset($buffer); |
234: | |
235: | return $result->fetch(); |
236: | } |
237: | } |
238: | |