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\Protocol; |
24: | use GameQ\Result; |
25: | use GameQ\Server; |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | class Cfx extends Protocol |
40: | { |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | protected $packets = [ |
48: | self::PACKET_STATUS => "\xFF\xFF\xFF\xFFgetinfo xxx", |
49: | ]; |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | protected $responses = [ |
57: | "\xFF\xFF\xFF\xFFinfoResponse" => "processStatus", |
58: | ]; |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | protected $protocol = 'cfx'; |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | protected $name = 'cfx'; |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | protected $name_long = "CitizenFX"; |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | protected $PlayerList = []; |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | protected $normalize = [ |
94: | |
95: | 'general' => [ |
96: | |
97: | 'gametype' => 'gametype', |
98: | 'hostname' => 'hostname', |
99: | 'mapname' => 'mapname', |
100: | 'maxplayers' => 'sv_maxclients', |
101: | 'mod' => 'gamename', |
102: | 'numplayers' => 'clients', |
103: | 'password' => 'privateClients', |
104: | ], |
105: | ]; |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | public function beforeSend(Server $server) |
113: | { |
114: | $GameQ = new \GameQ\GameQ(); |
115: | $GameQ->addServer([ |
116: | 'type' => 'cfxplayers', |
117: | 'host' => "$server->ip:$server->port_query", |
118: | ]); |
119: | $results = $GameQ->process(); |
120: | $this->PlayerList = isset($results[0]) && isset($results[0][0]) ? $results[0][0] : []; |
121: | } |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | public function processResponse() |
130: | { |
131: | |
132: | $buffer = new Buffer(implode('', $this->packets_response)); |
133: | |
134: | |
135: | $response_type = $buffer->readString(PHP_EOL); |
136: | |
137: | |
138: | if (empty($response_type) || !array_key_exists($response_type, $this->responses)) { |
139: | throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid"); |
140: | } |
141: | |
142: | |
143: | $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]); |
144: | |
145: | return $results; |
146: | } |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | protected function processStatus(Buffer $buffer) |
158: | { |
159: | |
160: | $result = new Result(); |
161: | |
162: | |
163: | if ($buffer->lookAhead(1) == '\\') { |
164: | |
165: | $buffer->skip(1); |
166: | } |
167: | |
168: | |
169: | $data = explode('\\', $buffer->getBuffer()); |
170: | |
171: | |
172: | unset($buffer); |
173: | |
174: | $itemCount = count($data); |
175: | |
176: | |
177: | for ($x = 0; $x < $itemCount; $x += 2) { |
178: | |
179: | $key = $data[$x]; |
180: | $val = $data[$x + 1]; |
181: | |
182: | if (in_array($key, ['challenge'])) { |
183: | continue; |
184: | } |
185: | |
186: | |
187: | $result->add($key, $val); |
188: | } |
189: | |
190: | |
191: | if ($this->PlayerList) { |
192: | $result->add('players', $this->PlayerList); |
193: | } |
194: | |
195: | return $result->fetch(); |
196: | } |
197: | } |
198: | |