Bfbc2.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 Bad Company 2 Protocol Class
  25. *
  26. * NOTE: There are no qualifiers to the response packets sent back from the server as to which response packet
  27. * belongs to which query request. For now this class assumes the responses are in the same order as the order in
  28. * which the packets were sent to the server. If this assumption turns out to be wrong there is easy way to tell which
  29. * response belongs to which query. Hopefully this assumption will hold true as it has in my testing.
  30. *
  31. * @package GameQ\Protocols
  32. * @author Austin Bischoff <[email protected]>
  33. */
  34. class Bfbc2 extends Protocol
  35. {
  36. /**
  37. * Array of packets we want to query.
  38. *
  39. * @type array
  40. */
  41. protected $packets = [
  42. self::PACKET_VERSION => "\x00\x00\x00\x00\x18\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00version\x00",
  43. self::PACKET_STATUS => "\x00\x00\x00\x00\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00",
  44. self::PACKET_PLAYERS => "\x00\x00\x00\x00\x24\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00listPlayers\x00\x03\x00\x00\x00\x61ll\x00",
  45. ];
  46. /**
  47. * Use the response flag to figure out what method to run
  48. *
  49. * @type array
  50. */
  51. protected $responses = [
  52. "processVersion",
  53. "processDetails",
  54. "processPlayers",
  55. ];
  56. /**
  57. * The transport mode for this protocol is TCP
  58. *
  59. * @type string
  60. */
  61. protected $transport = self::TRANSPORT_TCP;
  62. /**
  63. * The query protocol used to make the call
  64. *
  65. * @type string
  66. */
  67. protected $protocol = 'bfbc2';
  68. /**
  69. * String name of this protocol class
  70. *
  71. * @type string
  72. */
  73. protected $name = 'bfbc2';
  74. /**
  75. * Longer string name of this protocol class
  76. *
  77. * @type string
  78. */
  79. protected $name_long = "Battlefield Bad Company 2";
  80. /**
  81. * The client join link
  82. *
  83. * @type string
  84. */
  85. protected $join_link = null;
  86. /**
  87. * query_port = client_port + 29321
  88. * 48888 = 19567 + 29321
  89. *
  90. * @type int
  91. */
  92. protected $port_diff = 29321;
  93. /**
  94. * Normalize settings for this protocol
  95. *
  96. * @type array
  97. */
  98. protected $normalize = [
  99. // General
  100. 'general' => [
  101. // target => source
  102. 'dedicated' => 'dedicated',
  103. 'hostname' => 'hostname',
  104. 'mapname' => 'map',
  105. 'maxplayers' => 'max_players',
  106. 'numplayers' => 'num_players',
  107. 'password' => 'password',
  108. ],
  109. 'player' => [
  110. 'name' => 'name',
  111. 'score' => 'score',
  112. 'ping' => 'ping',
  113. ],
  114. 'team' => [
  115. 'score' => 'tickets',
  116. ],
  117. ];
  118. /**
  119. * Process the response for the StarMade server
  120. *
  121. * @return array
  122. * @throws \GameQ\Exception\Protocol
  123. */
  124. public function processResponse()
  125. {
  126. //print_r($this->packets_response);
  127. // Holds the results sent back
  128. $results = [];
  129. // Iterate over the response packets
  130. // @todo: This protocol has no packet ordering, ids or anyway to identify which packet coming back belongs to which initial call.
  131. foreach ($this->packets_response as $i => $packet) {
  132. // Create a new buffer
  133. $buffer = new Buffer($packet);
  134. // Burn first 4 bytes, same across all packets
  135. $buffer->skip(4);
  136. // Get the packet length
  137. $packetLength = $buffer->getLength();
  138. // Check to make sure the expected length matches the real length
  139. // Subtract 4 for the header burn
  140. if ($packetLength != ($buffer->readInt32() - 4)) {
  141. throw new Exception(__METHOD__ . " packet length does not match expected length!");
  142. }
  143. // We assume the packets are coming back in the same order as sent, this maybe incorrect...
  144. $results = array_merge(
  145. $results,
  146. call_user_func_array([$this, $this->responses[$i]], [$buffer])
  147. );
  148. }
  149. unset($buffer, $packetLength);
  150. return $results;
  151. }
  152. /*
  153. * Internal Methods
  154. */
  155. /**
  156. * Decode the buffer into a usable format
  157. *
  158. * @param \GameQ\Buffer $buffer
  159. *
  160. * @return array
  161. */
  162. protected function decode(Buffer $buffer)
  163. {
  164. $items = [];
  165. // Get the number of words in this buffer
  166. $itemCount = $buffer->readInt32();
  167. // Loop over the number of items
  168. for ($i = 0; $i < $itemCount; $i++) {
  169. // Length of the string
  170. $buffer->readInt32();
  171. // Just read the string
  172. $items[$i] = $buffer->readString();
  173. }
  174. return $items;
  175. }
  176. /**
  177. * Process the server details
  178. *
  179. * @param \GameQ\Buffer $buffer
  180. *
  181. * @return array
  182. */
  183. protected function processDetails(Buffer $buffer)
  184. {
  185. // Decode into items
  186. $items = $this->decode($buffer);
  187. // Set the result to a new result instance
  188. $result = new Result();
  189. // Server is always dedicated
  190. $result->add('dedicated', 1);
  191. // These are the same no matter what mode the server is in
  192. $result->add('hostname', $items[1]);
  193. $result->add('num_players', (int)$items[2]);
  194. $result->add('max_players', (int)$items[3]);
  195. $result->add('gametype', $items[4]);
  196. $result->add('map', $items[5]);
  197. $result->add('roundsplayed', (int)$items[6]);
  198. $result->add('roundstotal', (int)$items[7]);
  199. $result->add('num_teams', (int)$items[8]);
  200. // Set the current index
  201. $index_current = 9;
  202. // Pull the team count
  203. $teamCount = $result->get('num_teams');
  204. // Loop for the number of teams found, increment along the way
  205. for ($id = 1; $id <= $teamCount; $id++, $index_current++) {
  206. // Shows the tickets
  207. $result->addTeam('tickets', $items[$index_current]);
  208. // We add an id so we know which team this is
  209. $result->addTeam('id', $id);
  210. }
  211. // Get and set the rest of the data points.
  212. $result->add('targetscore', (int)$items[$index_current]);
  213. $result->add('online', 1); // Forced true, shows accepting players
  214. $result->add('ranked', (($items[$index_current + 2] == 'true') ? 1 : 0));
  215. $result->add('punkbuster', (($items[$index_current + 3] == 'true') ? 1 : 0));
  216. $result->add('password', (($items[$index_current + 4] == 'true') ? 1 : 0));
  217. $result->add('uptime', (int)$items[$index_current + 5]);
  218. $result->add('roundtime', (int)$items[$index_current + 6]);
  219. $result->add('mod', $items[$index_current + 7]);
  220. $result->add('ip_port', $items[$index_current + 9]);
  221. $result->add('punkbuster_version', $items[$index_current + 10]);
  222. $result->add('join_queue', (($items[$index_current + 11] == 'true') ? 1 : 0));
  223. $result->add('region', $items[$index_current + 12]);
  224. unset($items, $index_current, $teamCount, $buffer);
  225. return $result->fetch();
  226. }
  227. /**
  228. * Process the server version
  229. *
  230. * @param \GameQ\Buffer $buffer
  231. *
  232. * @return array
  233. */
  234. protected function processVersion(Buffer $buffer)
  235. {
  236. // Decode into items
  237. $items = $this->decode($buffer);
  238. // Set the result to a new result instance
  239. $result = new Result();
  240. $result->add('version', $items[2]);
  241. unset($buffer, $items);
  242. return $result->fetch();
  243. }
  244. /**
  245. * Process the players
  246. *
  247. * @param \GameQ\Buffer $buffer
  248. *
  249. * @return array
  250. */
  251. protected function processPlayers(Buffer $buffer)
  252. {
  253. // Decode into items
  254. $items = $this->decode($buffer);
  255. // Set the result to a new result instance
  256. $result = new Result();
  257. // Number of data points per player
  258. $numTags = $items[1];
  259. // Grab the tags for each player
  260. $tags = array_slice($items, 2, $numTags);
  261. // Get the player count
  262. $playerCount = $items[$numTags + 2];
  263. // Iterate over the index until we run out of players
  264. for ($i = 0, $x = $numTags + 3; $i < $playerCount; $i++, $x += $numTags) {
  265. // Loop over the player tags and extract the info for that tag
  266. foreach ($tags as $index => $tag) {
  267. $result->addPlayer($tag, $items[($x + $index)]);
  268. }
  269. }
  270. return $result->fetch();
  271. }
  272. }