Doom3.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * Doom3 Protocol Class
  25. *
  26. * Handles processing DOOM 3 servers
  27. *
  28. * @package GameQ\Protocols
  29. * @author Wilson Jesus <>
  30. */
  31. class Doom3 extends Protocol
  32. {
  33. /**
  34. * Array of packets we want to look up.
  35. * Each key should correspond to a defined method in this or a parent class
  36. *
  37. * @type array
  38. */
  39. protected $packets = [
  40. self::PACKET_ALL => "\xFF\xFFgetInfo\x00PiNGPoNG\x00",
  41. ];
  42. /**
  43. * Use the response flag to figure out what method to run
  44. *
  45. * @type array
  46. */
  47. protected $responses = [
  48. "\xFF\xFFinfoResponse" => 'processStatus',
  49. ];
  50. /**
  51. * The query protocol used to make the call
  52. *
  53. * @type string
  54. */
  55. protected $protocol = 'doom3';
  56. /**
  57. * String name of this protocol class
  58. *
  59. * @type string
  60. */
  61. protected $name = 'doom3';
  62. /**
  63. * Longer string name of this protocol class
  64. *
  65. * @type string
  66. */
  67. protected $name_long = "Doom 3";
  68. /**
  69. * The client join link
  70. *
  71. * @type string
  72. */
  73. protected $join_link = null;
  74. /**
  75. * Normalize settings for this protocol
  76. *
  77. * @type array
  78. */
  79. protected $normalize = [
  80. // General
  81. 'general' => [
  82. // target => source
  83. 'hostname' => 'si_name',
  84. 'gametype' => 'gamename',
  85. 'mapname' => 'si_map',
  86. 'maxplayers' => 'si_maxPlayers',
  87. 'numplayers' => 'clients',
  88. 'password' => 'si_usepass',
  89. ],
  90. // Individual
  91. 'player' => [
  92. 'name' => 'name',
  93. 'ping' => 'ping',
  94. ],
  95. ];
  96. /**
  97. * Handle response from the server
  98. *
  99. * @return mixed
  100. * @throws Exception
  101. */
  102. public function processResponse()
  103. {
  104. // Make a buffer
  105. $buffer = new Buffer(implode('', $this->packets_response));
  106. // Grab the header
  107. $header = $buffer->readString();
  108. // Header
  109. // Figure out which packet response this is
  110. if (empty($header) || !array_key_exists($header, $this->responses)) {
  111. throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
  112. }
  113. return call_user_func_array([$this, $this->responses[$header]], [$buffer]);
  114. }
  115. /**
  116. * Process the status response
  117. *
  118. * @param Buffer $buffer
  119. *
  120. * @return array
  121. */
  122. protected function processStatus(Buffer $buffer)
  123. {
  124. // We need to split the data and offload
  125. $results = $this->processServerInfo($buffer);
  126. $results = array_merge_recursive(
  127. $results,
  128. $this->processPlayers($buffer)
  129. );
  130. unset($buffer);
  131. // Return results
  132. return $results;
  133. }
  134. /**
  135. * Handle processing the server information
  136. *
  137. * @param Buffer $buffer
  138. *
  139. * @return array
  140. */
  141. protected function processServerInfo(Buffer $buffer)
  142. {
  143. // Set the result to a new result instance
  144. $result = new Result();
  145. $result->add('version', $buffer->readInt8() . '.' . $buffer->readInt8());
  146. // Key / value pairs, delimited by an empty pair
  147. while ($buffer->getLength()) {
  148. $key = trim($buffer->readString());
  149. $val = utf8_encode(trim($buffer->readString()));
  150. // Something is empty so we are done
  151. if (empty($key) && empty($val)) {
  152. break;
  153. }
  154. $result->add($key, $val);
  155. }
  156. unset($buffer);
  157. return $result->fetch();
  158. }
  159. /**
  160. * Handle processing of player data
  161. *
  162. * @param Buffer $buffer
  163. *
  164. * @return array
  165. */
  166. protected function processPlayers(Buffer $buffer)
  167. {
  168. // Some games do not have a number of current players
  169. $playerCount = 0;
  170. // Set the result to a new result instance
  171. $result = new Result();
  172. // Parse players
  173. // Loop thru the buffer until we run out of data
  174. while (($id = $buffer->readInt8()) != 32) {
  175. // Add player info results
  176. $result->addPlayer('id', $id);
  177. $result->addPlayer('ping', $buffer->readInt16());
  178. $result->addPlayer('rate', $buffer->readInt32());
  179. // Add player name, encoded
  180. $result->addPlayer('name', utf8_encode(trim($buffer->readString())));
  181. // Increment
  182. $playerCount++;
  183. }
  184. // Add the number of players to the result
  185. $result->add('clients', $playerCount);
  186. // Clear
  187. unset($buffer, $playerCount);
  188. return $result->fetch();
  189. }
  190. }