1 | <?php |
||
33 | class Lhmp extends Protocol |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Array of packets we want to look up. |
||
38 | * Each key should correspond to a defined method in this or a parent class |
||
39 | * |
||
40 | * @type array |
||
41 | */ |
||
42 | protected $packets = [ |
||
43 | self::PACKET_DETAILS => "LHMPo", |
||
44 | self::PACKET_PLAYERS => "LHMPp", |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Use the response flag to figure out what method to run |
||
49 | * |
||
50 | * @type array |
||
51 | */ |
||
52 | protected $responses = [ |
||
53 | "LHMPo" => "processDetails", |
||
54 | "LHMPp" => "processPlayers", |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * The query protocol used to make the call |
||
59 | * |
||
60 | * @type string |
||
61 | */ |
||
62 | protected $protocol = 'lhmp'; |
||
63 | |||
64 | /** |
||
65 | * String name of this protocol class |
||
66 | * |
||
67 | * @type string |
||
68 | */ |
||
69 | protected $name = 'lhmp'; |
||
70 | |||
71 | /** |
||
72 | * Longer string name of this protocol class |
||
73 | * |
||
74 | * @type string |
||
75 | */ |
||
76 | protected $name_long = "Lost Heaven"; |
||
77 | |||
78 | /** |
||
79 | * query_port = client_port + 1 |
||
80 | * |
||
81 | * @type int |
||
82 | */ |
||
83 | protected $port_diff = 1; |
||
84 | |||
85 | /** |
||
86 | * Normalize settings for this protocol |
||
87 | * |
||
88 | * @type array |
||
89 | */ |
||
90 | protected $normalize = [ |
||
91 | // General |
||
92 | 'general' => [ |
||
93 | // target => source |
||
94 | 'gametype' => 'gamemode', |
||
95 | 'hostname' => 'servername', |
||
96 | 'mapname' => 'mapname', |
||
97 | 'maxplayers' => 'maxplayers', |
||
98 | 'numplayers' => 'numplayers', |
||
99 | 'password' => 'password', |
||
100 | ], |
||
101 | // Individual |
||
102 | 'player' => [ |
||
103 | 'name' => 'name', |
||
104 | ], |
||
105 | ]; |
||
106 | |||
107 | /** |
||
108 | * Process the response |
||
109 | * |
||
110 | * @return array |
||
111 | * @throws \GameQ\Exception\Protocol |
||
112 | */ |
||
113 | 2 | public function processResponse() |
|
114 | { |
||
115 | // Will hold the packets after sorting |
||
116 | 2 | $packets = []; |
|
117 | |||
118 | // We need to pre-sort these for split packets so we can do extra work where needed |
||
119 | 2 | foreach ($this->packets_response as $response) { |
|
120 | 2 | $buffer = new Buffer($response); |
|
121 | |||
122 | // Pull out the header |
||
123 | 2 | $header = $buffer->read(5); |
|
124 | |||
125 | // Add the packet to the proper section, we will combine later |
||
126 | 2 | $packets[$header][] = $buffer->getBuffer(); |
|
127 | 2 | } |
|
128 | |||
129 | 2 | unset($buffer); |
|
130 | |||
131 | 2 | $results = []; |
|
132 | |||
133 | // Now let's iterate and process |
||
134 | 2 | foreach ($packets as $header => $packetGroup) { |
|
135 | // Figure out which packet response this is |
||
136 | 2 | if (!array_key_exists($header, $this->responses)) { |
|
137 | 2 | throw new Exception(__METHOD__ . " response type '{$header}' is not valid"); |
|
138 | } |
||
139 | |||
140 | // Now we need to call the proper method |
||
141 | 2 | $results = array_merge( |
|
142 | 2 | $results, |
|
143 | 2 | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
|
144 | 2 | ); |
|
145 | 2 | } |
|
146 | |||
147 | unset($packets); |
||
148 | |||
149 | return $results; |
||
150 | } |
||
151 | |||
152 | /* |
||
153 | * Internal methods |
||
154 | */ |
||
155 | |||
156 | /** |
||
157 | * Handles processing the details data into a usable format |
||
158 | * |
||
159 | * @param Buffer $buffer |
||
160 | * |
||
161 | * @return array |
||
162 | * @throws Exception |
||
163 | */ |
||
164 | protected function processDetails(Buffer $buffer) |
||
183 | |||
184 | /** |
||
185 | * Handles processing the player data into a usable format |
||
186 | * |
||
187 | * @param Buffer $buffer |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | 2 | protected function processPlayers(Buffer $buffer) |
|
214 | } |
||
215 |