Lhmp.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * This file is part of GameQ.
  4. *
  5. * GameQ is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GameQ is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. namespace GameQ\Protocols;
  19. use GameQ\Protocol;
  20. use GameQ\Buffer;
  21. use GameQ\Result;
  22. use GameQ\Exception\Protocol as Exception;
  23. /**
  24. * Lost Heaven Protocol class
  25. *
  26. * Reference: http://lh-mp.eu/wiki/index.php/Query_System
  27. *
  28. * @author Austin Bischoff <[email protected]>
  29. */
  30. class Lhmp extends Protocol
  31. {
  32. /**
  33. * Array of packets we want to look up.
  34. * Each key should correspond to a defined method in this or a parent class
  35. *
  36. * @type array
  37. */
  38. protected $packets = [
  39. self::PACKET_DETAILS => "LHMPo",
  40. self::PACKET_PLAYERS => "LHMPp",
  41. ];
  42. /**
  43. * Use the response flag to figure out what method to run
  44. *
  45. * @type array
  46. */
  47. protected $responses = [
  48. "LHMPo" => "processDetails",
  49. "LHMPp" => "processPlayers",
  50. ];
  51. /**
  52. * The query protocol used to make the call
  53. *
  54. * @type string
  55. */
  56. protected $protocol = 'lhmp';
  57. /**
  58. * String name of this protocol class
  59. *
  60. * @type string
  61. */
  62. protected $name = 'lhmp';
  63. /**
  64. * Longer string name of this protocol class
  65. *
  66. * @type string
  67. */
  68. protected $name_long = "Lost Heaven";
  69. /**
  70. * query_port = client_port + 1
  71. *
  72. * @type int
  73. */
  74. protected $port_diff = 1;
  75. /**
  76. * Normalize settings for this protocol
  77. *
  78. * @type array
  79. */
  80. protected $normalize = [
  81. // General
  82. 'general' => [
  83. // target => source
  84. 'gametype' => 'gamemode',
  85. 'hostname' => 'servername',
  86. 'mapname' => 'mapname',
  87. 'maxplayers' => 'maxplayers',
  88. 'numplayers' => 'numplayers',
  89. 'password' => 'password',
  90. ],
  91. // Individual
  92. 'player' => [
  93. 'name' => 'name',
  94. ],
  95. ];
  96. /**
  97. * Process the response
  98. *
  99. * @return array
  100. * @throws \GameQ\Exception\Protocol
  101. */
  102. public function processResponse()
  103. {
  104. // Will hold the packets after sorting
  105. $packets = [];
  106. // We need to pre-sort these for split packets so we can do extra work where needed
  107. foreach ($this->packets_response as $response) {
  108. $buffer = new Buffer($response);
  109. // Pull out the header
  110. $header = $buffer->read(5);
  111. // Add the packet to the proper section, we will combine later
  112. $packets[$header][] = $buffer->getBuffer();
  113. }
  114. unset($buffer);
  115. $results = [];
  116. // Now let's iterate and process
  117. foreach ($packets as $header => $packetGroup) {
  118. // Figure out which packet response this is
  119. if (!array_key_exists($header, $this->responses)) {
  120. throw new Exception(__METHOD__ . " response type '{$header}' is not valid");
  121. }
  122. // Now we need to call the proper method
  123. $results = array_merge(
  124. $results,
  125. call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))])
  126. );
  127. }
  128. unset($packets);
  129. return $results;
  130. }
  131. /*
  132. * Internal methods
  133. */
  134. /**
  135. * Handles processing the details data into a usable format
  136. *
  137. * @param Buffer $buffer
  138. *
  139. * @return array
  140. * @throws Exception
  141. */
  142. protected function processDetails(Buffer $buffer)
  143. {
  144. // Set the result to a new result instance
  145. $result = new Result();
  146. $result->add('protocol', $buffer->readString());
  147. $result->add('password', $buffer->readString());
  148. $result->add('numplayers', $buffer->readInt16());
  149. $result->add('maxplayers', $buffer->readInt16());
  150. $result->add('servername', utf8_encode($buffer->readPascalString()));
  151. $result->add('gamemode', $buffer->readPascalString());
  152. $result->add('website', utf8_encode($buffer->readPascalString()));
  153. $result->add('mapname', utf8_encode($buffer->readPascalString()));
  154. unset($buffer);
  155. return $result->fetch();
  156. }
  157. /**
  158. * Handles processing the player data into a usable format
  159. *
  160. * @param Buffer $buffer
  161. *
  162. * @return array
  163. */
  164. protected function processPlayers(Buffer $buffer)
  165. {
  166. // Set the result to a new result instance
  167. $result = new Result();
  168. // Get the number of players
  169. $result->add('numplayers', $buffer->readInt16());
  170. // Parse players
  171. while ($buffer->getLength()) {
  172. // Player id
  173. if (($id = $buffer->readInt16()) !== 0) {
  174. // Add the results
  175. $result->addPlayer('id', $id);
  176. $result->addPlayer('name', utf8_encode($buffer->readPascalString()));
  177. }
  178. }
  179. unset($buffer, $id);
  180. return $result->fetch();
  181. }
  182. }