Etqw.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Buffer;
  20. use GameQ\Exception\Protocol as Exception;
  21. use GameQ\Protocol;
  22. use GameQ\Result;
  23. /**
  24. * Enemy Territory Quake Wars Protocol Class
  25. *
  26. * @author Austin Bischoff <[email protected]>
  27. */
  28. class Etqw extends Protocol
  29. {
  30. /**
  31. * Array of packets we want to look up.
  32. * Each key should correspond to a defined method in this or a parent class
  33. *
  34. * @type array
  35. */
  36. protected $packets = [
  37. self::PACKET_STATUS => "\xFF\xFFgetInfoEx\x00\x00\x00\x00",
  38. //self::PACKET_STATUS => "\xFF\xFFgetInfo\x00\x00\x00\x00\x00",
  39. ];
  40. /**
  41. * Use the response flag to figure out what method to run
  42. *
  43. * @type array
  44. */
  45. protected $responses = [
  46. "\xFF\xFFinfoExResponse" => "processStatus",
  47. ];
  48. /**
  49. * The query protocol used to make the call
  50. *
  51. * @type string
  52. */
  53. protected $protocol = 'etqw';
  54. /**
  55. * String name of this protocol class
  56. *
  57. * @type string
  58. */
  59. protected $name = 'etqw';
  60. /**
  61. * Longer string name of this protocol class
  62. *
  63. * @type string
  64. */
  65. protected $name_long = "Enemy Territory Quake Wars";
  66. /**
  67. * Normalize settings for this protocol
  68. *
  69. * @type array
  70. */
  71. protected $normalize = [
  72. // General
  73. 'general' => [
  74. // target => source
  75. 'gametype' => 'campaign',
  76. 'hostname' => 'name',
  77. 'mapname' => 'map',
  78. 'maxplayers' => 'maxPlayers',
  79. 'mod' => 'gamename',
  80. 'numplayers' => 'numplayers',
  81. 'password' => 'privateClients',
  82. ],
  83. // Individual
  84. 'player' => [
  85. 'name' => 'name',
  86. 'score' => 'score',
  87. 'time' => 'time',
  88. ],
  89. ];
  90. /**
  91. * Process the response
  92. *
  93. * @return array
  94. * @throws \GameQ\Exception\Protocol
  95. */
  96. public function processResponse()
  97. {
  98. // In case it comes back as multiple packets (it shouldn't)
  99. $buffer = new Buffer(implode('', $this->packets_response));
  100. // Figure out what packet response this is for
  101. $response_type = $buffer->readString();
  102. // Figure out which packet response this is
  103. if (!array_key_exists($response_type, $this->responses)) {
  104. throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid");
  105. }
  106. // Offload the call
  107. $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]);
  108. return $results;
  109. }
  110. /*
  111. * Internal methods
  112. */
  113. /**
  114. * Handle processing the status response
  115. *
  116. * @param Buffer $buffer
  117. *
  118. * @return array
  119. */
  120. protected function processStatus(Buffer $buffer)
  121. {
  122. // Set the result to a new result instance
  123. $result = new Result();
  124. // Defaults
  125. $result->add('dedicated', 1);
  126. // Now burn the challenge, version and size
  127. $buffer->skip(16);
  128. // Key / value pairs
  129. while ($buffer->getLength()) {
  130. $var = str_replace('si_', '', $buffer->readString());
  131. $val = $buffer->readString();
  132. if (empty($var) && empty($val)) {
  133. break;
  134. }
  135. // Add the server prop
  136. $result->add($var, $val);
  137. }
  138. // Now let's do the basic player info
  139. $this->parsePlayers($buffer, $result);
  140. // Now grab the rest of the server info
  141. $result->add('osmask', $buffer->readInt32());
  142. $result->add('ranked', $buffer->readInt8());
  143. $result->add('timeleft', $buffer->readInt32());
  144. $result->add('gamestate', $buffer->readInt8());
  145. $result->add('servertype', $buffer->readInt8());
  146. // 0: regular server
  147. if ($result->get('servertype') == 0) {
  148. $result->add('interested_clients', $buffer->readInt8());
  149. } else {
  150. // 1: tv server
  151. $result->add('connected_clients', $buffer->readInt32());
  152. $result->add('max_clients', $buffer->readInt32());
  153. }
  154. // Now let's parse the extended player info
  155. $this->parsePlayersExtra($buffer, $result);
  156. unset($buffer);
  157. return $result->fetch();
  158. }
  159. /**
  160. * Parse players out of the status ex response
  161. *
  162. * @param Buffer $buffer
  163. * @param Result $result
  164. */
  165. protected function parsePlayers(Buffer &$buffer, Result &$result)
  166. {
  167. // By default there are 0 players
  168. $players = 0;
  169. // Iterate over the players until we run out
  170. while (($id = $buffer->readInt8()) != 32) {
  171. $result->addPlayer('id', $id);
  172. $result->addPlayer('ping', $buffer->readInt16());
  173. $result->addPlayer('name', $buffer->readString());
  174. $result->addPlayer('clantag_pos', $buffer->readInt8());
  175. $result->addPlayer('clantag', $buffer->readString());
  176. $result->addPlayer('bot', $buffer->readInt8());
  177. $players++;
  178. }
  179. // Let's add in the current players as a result
  180. $result->add('numplayers', $players);
  181. // Free some memory
  182. unset($id);
  183. }
  184. /**
  185. * Handle parsing extra player data
  186. *
  187. * @param Buffer $buffer
  188. * @param Result $result
  189. */
  190. protected function parsePlayersExtra(Buffer &$buffer, Result &$result)
  191. {
  192. // Iterate over the extra player info
  193. while (($id = $buffer->readInt8()) != 32) {
  194. $result->addPlayer('total_xp', $buffer->readFloat32());
  195. $result->addPlayer('teamname', $buffer->readString());
  196. $result->addPlayer('total_kills', $buffer->readInt32());
  197. $result->addPlayer('total_deaths', $buffer->readInt32());
  198. }
  199. // @todo: Add team stuff
  200. // Free some memory
  201. unset($id);
  202. }
  203. }