Ase.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /**
  23. * All-Seeing Eye Protocol class
  24. *
  25. * @author Marcel Bößendörfer <[email protected]>
  26. * @author Austin Bischoff <[email protected]>
  27. */
  28. class Ase 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_ALL => "s",
  38. ];
  39. /**
  40. * The query protocol used to make the call
  41. *
  42. * @type string
  43. */
  44. protected $protocol = 'ase';
  45. /**
  46. * String name of this protocol class
  47. *
  48. * @type string
  49. */
  50. protected $name = 'ase';
  51. /**
  52. * Longer string name of this protocol class
  53. *
  54. * @type string
  55. */
  56. protected $name_long = "All-Seeing Eye";
  57. /**
  58. * The client join link
  59. *
  60. * @type string
  61. */
  62. protected $join_link = null;
  63. /**
  64. * Normalize settings for this protocol
  65. *
  66. * @type array
  67. */
  68. protected $normalize = [
  69. // General
  70. 'general' => [
  71. // target => source
  72. 'dedicated' => 'dedicated',
  73. 'gametype' => 'gametype',
  74. 'hostname' => 'servername',
  75. 'mapname' => 'map',
  76. 'maxplayers' => 'max_players',
  77. 'mod' => 'game_dir',
  78. 'numplayers' => 'num_players',
  79. 'password' => 'password',
  80. ],
  81. // Individual
  82. 'player' => [
  83. 'name' => 'name',
  84. 'score' => 'score',
  85. 'team' => 'team',
  86. 'ping' => 'ping',
  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. // Create a new buffer
  99. $buffer = new Buffer(implode('', $this->packets_response));
  100. // Burn the header
  101. $buffer->skip(4);
  102. // Create a new result
  103. $result = new Result();
  104. // Variables
  105. $result->add('gamename', $buffer->readPascalString(1, true));
  106. $result->add('port', $buffer->readPascalString(1, true));
  107. $result->add('servername', $buffer->readPascalString(1, true));
  108. $result->add('gametype', $buffer->readPascalString(1, true));
  109. $result->add('map', $buffer->readPascalString(1, true));
  110. $result->add('version', $buffer->readPascalString(1, true));
  111. $result->add('password', $buffer->readPascalString(1, true));
  112. $result->add('num_players', $buffer->readPascalString(1, true));
  113. $result->add('max_players', $buffer->readPascalString(1, true));
  114. $result->add('dedicated', 1);
  115. // Offload the key/value pair processing
  116. $this->processKeyValuePairs($buffer, $result);
  117. // Offload processing player and team info
  118. $this->processPlayersAndTeams($buffer, $result);
  119. unset($buffer);
  120. return $result->fetch();
  121. }
  122. /*
  123. * Internal methods
  124. */
  125. /**
  126. * Handles processing the extra key/value pairs for server settings
  127. *
  128. * @param \GameQ\Buffer $buffer
  129. * @param \GameQ\Result $result
  130. */
  131. protected function processKeyValuePairs(Buffer &$buffer, Result &$result)
  132. {
  133. // Key / value pairs
  134. while ($buffer->getLength()) {
  135. $key = $buffer->readPascalString(1, true);
  136. // If we have an empty key, we've reached the end
  137. if (empty($key)) {
  138. break;
  139. }
  140. // Otherwise, add the pair
  141. $result->add(
  142. $key,
  143. $buffer->readPascalString(1, true)
  144. );
  145. }
  146. unset($key);
  147. }
  148. /**
  149. * Handles processing the player and team data into a usable format
  150. *
  151. * @param \GameQ\Buffer $buffer
  152. * @param \GameQ\Result $result
  153. */
  154. protected function processPlayersAndTeams(Buffer &$buffer, Result &$result)
  155. {
  156. // Players and team info
  157. while ($buffer->getLength()) {
  158. // Get the flags
  159. $flags = $buffer->readInt8();
  160. // Get data according to the flags
  161. if ($flags & 1) {
  162. $result->addPlayer('name', $buffer->readPascalString(1, true));
  163. }
  164. if ($flags & 2) {
  165. $result->addPlayer('team', $buffer->readPascalString(1, true));
  166. }
  167. if ($flags & 4) {
  168. $result->addPlayer('skin', $buffer->readPascalString(1, true));
  169. }
  170. if ($flags & 8) {
  171. $result->addPlayer('score', $buffer->readPascalString(1, true));
  172. }
  173. if ($flags & 16) {
  174. $result->addPlayer('ping', $buffer->readPascalString(1, true));
  175. }
  176. if ($flags & 32) {
  177. $result->addPlayer('time', $buffer->readPascalString(1, true));
  178. }
  179. }
  180. }
  181. }