1
0

Bf3.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. * Battlefield 3 Protocol Class
  25. *
  26. * Good place for doc status and info is http://www.fpsadmin.com/forum/showthread.php?t=24134
  27. *
  28. * @package GameQ\Protocols
  29. * @author Austin Bischoff <[email protected]>
  30. */
  31. class Bf3 extends Protocol
  32. {
  33. /**
  34. * Array of packets we want to query.
  35. *
  36. * @type array
  37. */
  38. protected $packets = [
  39. self::PACKET_STATUS => "\x00\x00\x00\x21\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00",
  40. self::PACKET_VERSION => "\x00\x00\x00\x22\x18\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00version\x00",
  41. self::PACKET_PLAYERS =>
  42. "\x00\x00\x00\x23\x24\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00listPlayers\x00\x03\x00\x00\x00\x61ll\x00",
  43. ];
  44. /**
  45. * Use the response flag to figure out what method to run
  46. *
  47. * @type array
  48. */
  49. protected $responses = [
  50. 1627389952 => "processDetails", // a
  51. 1644167168 => "processVersion", // b
  52. 1660944384 => "processPlayers", // c
  53. ];
  54. /**
  55. * The transport mode for this protocol is TCP
  56. *
  57. * @type string
  58. */
  59. protected $transport = self::TRANSPORT_TCP;
  60. /**
  61. * The query protocol used to make the call
  62. *
  63. * @type string
  64. */
  65. protected $protocol = 'bf3';
  66. /**
  67. * String name of this protocol class
  68. *
  69. * @type string
  70. */
  71. protected $name = 'bf3';
  72. /**
  73. * Longer string name of this protocol class
  74. *
  75. * @type string
  76. */
  77. protected $name_long = "Battlefield 3";
  78. /**
  79. * The client join link
  80. *
  81. * @type string
  82. */
  83. protected $join_link = null;
  84. /**
  85. * query_port = client_port + 22000
  86. * 47200 = 25200 + 22000
  87. *
  88. * @type int
  89. */
  90. protected $port_diff = 22000;
  91. /**
  92. * Normalize settings for this protocol
  93. *
  94. * @type array
  95. */
  96. protected $normalize = [
  97. // General
  98. 'general' => [
  99. // target => source
  100. 'dedicated' => 'dedicated',
  101. 'hostname' => 'hostname',
  102. 'mapname' => 'map',
  103. 'maxplayers' => 'max_players',
  104. 'numplayers' => 'num_players',
  105. 'password' => 'password',
  106. ],
  107. 'player' => [
  108. 'name' => 'name',
  109. 'score' => 'score',
  110. 'ping' => 'ping',
  111. ],
  112. 'team' => [
  113. 'score' => 'tickets',
  114. ],
  115. ];
  116. /**
  117. * Process the response for the StarMade server
  118. *
  119. * @return array
  120. * @throws \GameQ\Exception\Protocol
  121. */
  122. public function processResponse()
  123. {
  124. // Holds the results sent back
  125. $results = [];
  126. // Holds the processed packets after having been reassembled
  127. $processed = [];
  128. // Start up the index for the processed
  129. $sequence_id_last = 0;
  130. foreach ($this->packets_response as $packet) {
  131. // Create a new buffer
  132. $buffer = new Buffer($packet);
  133. // Each "good" packet begins with sequence_id (32-bit)
  134. $sequence_id = $buffer->readInt32();
  135. // Sequence id is a response
  136. if (array_key_exists($sequence_id, $this->responses)) {
  137. $processed[$sequence_id] = $buffer->getBuffer();
  138. $sequence_id_last = $sequence_id;
  139. } else {
  140. // This is a continuation of the previous packet, reset the buffer and append
  141. $buffer->jumpto(0);
  142. // Append
  143. $processed[$sequence_id_last] .= $buffer->getBuffer();
  144. }
  145. }
  146. unset($buffer, $sequence_id_last, $sequence_id);
  147. // Iterate over the combined packets and do some work
  148. foreach ($processed as $sequence_id => $data) {
  149. // Create a new buffer
  150. $buffer = new Buffer($data);
  151. // Get the length of the packet
  152. $packetLength = $buffer->getLength();
  153. // Check to make sure the expected length matches the real length
  154. // Subtract 4 for the sequence_id pulled out earlier
  155. if ($packetLength != ($buffer->readInt32() - 4)) {
  156. throw new Exception(__METHOD__ . " packet length does not match expected length!");
  157. }
  158. // Now we need to call the proper method
  159. $results = array_merge(
  160. $results,
  161. call_user_func_array([$this, $this->responses[$sequence_id]], [$buffer])
  162. );
  163. }
  164. return $results;
  165. }
  166. /*
  167. * Internal Methods
  168. */
  169. /**
  170. * Decode the buffer into a usable format
  171. *
  172. * @param \GameQ\Buffer $buffer
  173. *
  174. * @return array
  175. */
  176. protected function decode(Buffer $buffer)
  177. {
  178. $items = [];
  179. // Get the number of words in this buffer
  180. $itemCount = $buffer->readInt32();
  181. // Loop over the number of items
  182. for ($i = 0; $i < $itemCount; $i++) {
  183. // Length of the string
  184. $buffer->readInt32();
  185. // Just read the string
  186. $items[$i] = $buffer->readString();
  187. }
  188. return $items;
  189. }
  190. /**
  191. * Process the server details
  192. *
  193. * @param \GameQ\Buffer $buffer
  194. *
  195. * @return array
  196. */
  197. protected function processDetails(Buffer $buffer)
  198. {
  199. // Decode into items
  200. $items = $this->decode($buffer);
  201. // Set the result to a new result instance
  202. $result = new Result();
  203. // Server is always dedicated
  204. $result->add('dedicated', 1);
  205. // These are the same no matter what mode the server is in
  206. $result->add('hostname', $items[1]);
  207. $result->add('num_players', (int)$items[2]);
  208. $result->add('max_players', (int)$items[3]);
  209. $result->add('gametype', $items[4]);
  210. $result->add('map', $items[5]);
  211. $result->add('roundsplayed', (int)$items[6]);
  212. $result->add('roundstotal', (int)$items[7]);
  213. $result->add('num_teams', (int)$items[8]);
  214. // Set the current index
  215. $index_current = 9;
  216. // Pull the team count
  217. $teamCount = $result->get('num_teams');
  218. // Loop for the number of teams found, increment along the way
  219. for ($id = 1; $id <= $teamCount; $id++, $index_current++) {
  220. // Shows the tickets
  221. $result->addTeam('tickets', $items[$index_current]);
  222. // We add an id so we know which team this is
  223. $result->addTeam('id', $id);
  224. }
  225. // Get and set the rest of the data points.
  226. $result->add('targetscore', (int)$items[$index_current]);
  227. $result->add('online', 1); // Forced true, it seems $words[$index_current + 1] is always empty
  228. $result->add('ranked', (int)$items[$index_current + 2]);
  229. $result->add('punkbuster', (int)$items[$index_current + 3]);
  230. $result->add('password', (int)$items[$index_current + 4]);
  231. $result->add('uptime', (int)$items[$index_current + 5]);
  232. $result->add('roundtime', (int)$items[$index_current + 6]);
  233. // Added in R9
  234. $result->add('ip_port', $items[$index_current + 7]);
  235. $result->add('punkbuster_version', $items[$index_current + 8]);
  236. $result->add('join_queue', (int)$items[$index_current + 9]);
  237. $result->add('region', $items[$index_current + 10]);
  238. $result->add('pingsite', $items[$index_current + 11]);
  239. $result->add('country', $items[$index_current + 12]);
  240. // Added in R29, No docs as of yet
  241. $result->add('quickmatch', (int)$items[$index_current + 13]); // Guessed from research
  242. unset($items, $index_current, $teamCount, $buffer);
  243. return $result->fetch();
  244. }
  245. /**
  246. * Process the server version
  247. *
  248. * @param \GameQ\Buffer $buffer
  249. *
  250. * @return array
  251. */
  252. protected function processVersion(Buffer $buffer)
  253. {
  254. // Decode into items
  255. $items = $this->decode($buffer);
  256. // Set the result to a new result instance
  257. $result = new Result();
  258. $result->add('version', $items[2]);
  259. unset($buffer, $items);
  260. return $result->fetch();
  261. }
  262. /**
  263. * Process the players
  264. *
  265. * @param \GameQ\Buffer $buffer
  266. *
  267. * @return array
  268. */
  269. protected function processPlayers(Buffer $buffer)
  270. {
  271. // Decode into items
  272. $items = $this->decode($buffer);
  273. // Set the result to a new result instance
  274. $result = new Result();
  275. // Number of data points per player
  276. $numTags = $items[1];
  277. // Grab the tags for each player
  278. $tags = array_slice($items, 2, $numTags);
  279. // Get the player count
  280. $playerCount = $items[$numTags + 2];
  281. // Iterate over the index until we run out of players
  282. for ($i = 0, $x = $numTags + 3; $i < $playerCount; $i++, $x += $numTags) {
  283. // Loop over the player tags and extract the info for that tag
  284. foreach ($tags as $index => $tag) {
  285. $result->addPlayer($tag, $items[($x + $index)]);
  286. }
  287. }
  288. return $result->fetch();
  289. }
  290. }