1 | <?php |
||
35 | class Gamespy2 extends Protocol |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * Define the state of this class |
||
40 | * |
||
41 | * @type int |
||
42 | */ |
||
43 | protected $state = self::STATE_BETA; |
||
44 | |||
45 | /** |
||
46 | * Array of packets we want to look up. |
||
47 | * Each key should correspond to a defined method in this or a parent class |
||
48 | * |
||
49 | * @type array |
||
50 | */ |
||
51 | protected $packets = [ |
||
52 | self::PACKET_DETAILS => "\xFE\xFD\x00\x43\x4F\x52\x59\xFF\x00\x00", |
||
53 | self::PACKET_PLAYERS => "\xFE\xFD\x00\x43\x4F\x52\x58\x00\xFF\xFF", |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Use the response flag to figure out what method to run |
||
58 | * |
||
59 | * @type array |
||
60 | */ |
||
61 | protected $responses = [ |
||
62 | "\x00\x43\x4F\x52\x59" => "processDetails", |
||
63 | "\x00\x43\x4F\x52\x58" => "processPlayers", |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * The query protocol used to make the call |
||
68 | * |
||
69 | * @type string |
||
70 | */ |
||
71 | protected $protocol = 'gamespy2'; |
||
72 | |||
73 | /** |
||
74 | * String name of this protocol class |
||
75 | * |
||
76 | * @type string |
||
77 | */ |
||
78 | protected $name = 'gamespy2'; |
||
79 | |||
80 | /** |
||
81 | * Longer string name of this protocol class |
||
82 | * |
||
83 | * @type string |
||
84 | */ |
||
85 | protected $name_long = "GameSpy2 Server"; |
||
86 | |||
87 | /** |
||
88 | * The client join link |
||
89 | * |
||
90 | * @type string |
||
91 | */ |
||
92 | protected $join_link = null; |
||
93 | |||
94 | /** |
||
95 | * Normalize settings for this protocol |
||
96 | * |
||
97 | * @type array |
||
98 | */ |
||
99 | protected $normalize = [ |
||
100 | // General |
||
101 | 'general' => [ |
||
102 | // target => source |
||
103 | 'dedicated' => 'dedicated', |
||
104 | 'gametype' => 'gametype', |
||
105 | 'hostname' => 'hostname', |
||
106 | 'mapname' => 'mapname', |
||
107 | 'maxplayers' => 'maxplayers', |
||
108 | 'mod' => 'mod', |
||
109 | 'numplayers' => 'numplayers', |
||
110 | 'password' => 'password', |
||
111 | ], |
||
112 | ]; |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Process the response |
||
117 | * |
||
118 | * @return array |
||
119 | * @throws Exception |
||
120 | */ |
||
121 | 2 | public function processResponse() |
|
122 | { |
||
123 | |||
124 | // Will hold the packets after sorting |
||
125 | 2 | $packets = []; |
|
126 | |||
127 | // We need to pre-sort these for split packets so we can do extra work where needed |
||
128 | 2 | foreach ($this->packets_response as $response) { |
|
129 | 2 | $buffer = new Buffer($response); |
|
130 | |||
131 | // Pull out the header |
||
132 | 2 | $header = $buffer->read(5); |
|
133 | |||
134 | // Add the packet to the proper section, we will combine later |
||
135 | 2 | $packets[$header][] = $buffer->getBuffer(); |
|
136 | } |
||
137 | |||
138 | 2 | unset($buffer); |
|
139 | |||
140 | 2 | $results = []; |
|
141 | |||
142 | // Now let's iterate and process |
||
143 | 2 | foreach ($packets as $header => $packetGroup) { |
|
144 | // Figure out which packet response this is |
||
145 | 2 | if (!array_key_exists($header, $this->responses)) { |
|
146 | 2 | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
|
147 | } |
||
148 | |||
149 | // Now we need to call the proper method |
||
150 | $results = array_merge( |
||
151 | $results, |
||
152 | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | unset($packets); |
||
157 | |||
158 | return $results; |
||
159 | } |
||
160 | |||
161 | /* |
||
162 | * Internal methods |
||
163 | */ |
||
164 | |||
165 | /** |
||
166 | * Handles processing the details data into a usable format |
||
167 | * |
||
168 | * @param \GameQ\Buffer $buffer |
||
169 | * |
||
170 | * @return array |
||
171 | * @throws Exception |
||
172 | */ |
||
173 | protected function processDetails(Buffer $buffer) |
||
192 | |||
193 | /** |
||
194 | * Handles processing the players data into a usable format |
||
195 | * |
||
196 | * @param \GameQ\Buffer $buffer |
||
197 | * |
||
198 | * @return array |
||
199 | * @throws Exception |
||
200 | */ |
||
201 | protected function processPlayers(Buffer $buffer) |
||
220 | |||
221 | /** |
||
222 | * Parse the player/team info returned from the player call |
||
223 | * |
||
224 | * @param string $dataType |
||
225 | * @param \GameQ\Buffer $buffer |
||
226 | * @param \GameQ\Result $result |
||
227 | * |
||
228 | * @throws Exception |
||
229 | */ |
||
230 | protected function parsePlayerTeam($dataType, Buffer &$buffer, Result &$result) |
||
269 | } |
||
270 |