Gamespy.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * GameSpy Protocol class
  25. *
  26. * @author Austin Bischoff <[email protected]>
  27. */
  28. class Gamespy 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 => "\x5C\x73\x74\x61\x74\x75\x73\x5C",
  38. ];
  39. /**
  40. * The query protocol used to make the call
  41. *
  42. * @type string
  43. */
  44. protected $protocol = 'gamespy';
  45. /**
  46. * String name of this protocol class
  47. *
  48. * @type string
  49. */
  50. protected $name = 'gamespy';
  51. /**
  52. * Longer string name of this protocol class
  53. *
  54. * @type string
  55. */
  56. protected $name_long = "GameSpy Server";
  57. /**
  58. * The client join link
  59. *
  60. * @type string
  61. */
  62. protected $join_link = null;
  63. /**
  64. * Process the response for this protocol
  65. *
  66. * @return array
  67. * @throws Exception
  68. */
  69. public function processResponse()
  70. {
  71. // Holds the processed packets so we can sort them in case they come in an unordered
  72. $processed = [];
  73. // Iterate over the packets
  74. foreach ($this->packets_response as $response) {
  75. // Check to see if we had a preg_match error
  76. if (($match = preg_match("#^(.*)\\\\queryid\\\\([^\\\\]+)(\\\\|$)#", $response, $matches)) === false
  77. || $match != 1
  78. ) {
  79. throw new Exception(__METHOD__ . " An error occurred while parsing the packets for 'queryid'");
  80. }
  81. // Multiply so we move the decimal point out of the way, if there is one
  82. $key = (int)(floatval($matches[2]) * 1000);
  83. // Add this packet to the processed
  84. $processed[$key] = $matches[1];
  85. }
  86. // Sort the new array to make sure the keys (query ids) are in the proper order
  87. ksort($processed, SORT_NUMERIC);
  88. // Create buffer and offload processing
  89. return $this->processStatus(new Buffer(implode('', $processed)));
  90. }
  91. /*
  92. * Internal methods
  93. */
  94. /**
  95. * Handle processing the status buffer
  96. *
  97. * @param Buffer $buffer
  98. *
  99. * @return array
  100. */
  101. protected function processStatus(Buffer $buffer)
  102. {
  103. // Set the result to a new result instance
  104. $result = new Result();
  105. // By default dedicted
  106. $result->add('dedicated', 1);
  107. // Lets peek and see if the data starts with a \
  108. if ($buffer->lookAhead(1) == '\\') {
  109. // Burn the first one
  110. $buffer->skip(1);
  111. }
  112. // Explode the data
  113. $data = explode('\\', $buffer->getBuffer());
  114. // No longer needed
  115. unset($buffer);
  116. // Init some vars
  117. $numPlayers = 0;
  118. $numTeams = 0;
  119. $itemCount = count($data);
  120. // Check to make sure we have more than 1 item in the array before trying to loop
  121. if (count($data) > 1) {
  122. // Now lets loop the array since we have items
  123. for ($x = 0; $x < $itemCount; $x += 2) {
  124. // Set some local vars
  125. $key = $data[$x];
  126. $val = $data[$x + 1];
  127. // Check for <variable>_<count> variable (i.e players)
  128. if (($suffix = strrpos($key, '_')) !== false && is_numeric(substr($key, $suffix + 1))) {
  129. // See if this is a team designation
  130. if (substr($key, 0, $suffix) == 'teamname') {
  131. $result->addTeam('teamname', $val);
  132. $numTeams++;
  133. } else {
  134. // Its a player
  135. if (substr($key, 0, $suffix) == 'playername') {
  136. $numPlayers++;
  137. }
  138. $result->addPlayer(substr($key, 0, $suffix), utf8_encode($val));
  139. }
  140. } else {
  141. // Regular variable so just add the value.
  142. $result->add($key, $val);
  143. }
  144. }
  145. }
  146. // Add the player and team count
  147. $result->add('num_players', $numPlayers);
  148. $result->add('num_teams', $numTeams);
  149. // Unset some stuff to free up memory
  150. unset($data, $key, $val, $suffix, $x, $itemCount);
  151. // Return the result
  152. return $result->fetch();
  153. }
  154. }