Ase.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // Check for valid response
  101. if ($buffer->getLength() < 4) {
  102. throw new \GameQ\Exception\Protocol(sprintf('%s The response from the server was empty.', __METHOD__));
  103. }
  104. // Read the header
  105. $header = $buffer->read(4);
  106. // Verify header
  107. if ($header !== 'EYE1') {
  108. throw new \GameQ\Exception\Protocol(sprintf('%s The response header "%s" does not match expected "EYE1"', __METHOD__, $header));
  109. }
  110. // Create a new result
  111. $result = new Result();
  112. // Variables
  113. $result->add('gamename', $buffer->readPascalString(1, true));
  114. $result->add('port', $buffer->readPascalString(1, true));
  115. $result->add('servername', $buffer->readPascalString(1, true));
  116. $result->add('gametype', $buffer->readPascalString(1, true));
  117. $result->add('map', $buffer->readPascalString(1, true));
  118. $result->add('version', $buffer->readPascalString(1, true));
  119. $result->add('password', $buffer->readPascalString(1, true));
  120. $result->add('num_players', $buffer->readPascalString(1, true));
  121. $result->add('max_players', $buffer->readPascalString(1, true));
  122. $result->add('dedicated', 1);
  123. // Offload the key/value pair processing
  124. $this->processKeyValuePairs($buffer, $result);
  125. // Offload processing player and team info
  126. $this->processPlayersAndTeams($buffer, $result);
  127. unset($buffer);
  128. return $result->fetch();
  129. }
  130. /*
  131. * Internal methods
  132. */
  133. /**
  134. * Handles processing the extra key/value pairs for server settings
  135. *
  136. * @param \GameQ\Buffer $buffer
  137. * @param \GameQ\Result $result
  138. */
  139. protected function processKeyValuePairs(Buffer &$buffer, Result &$result)
  140. {
  141. // Key / value pairs
  142. while ($buffer->getLength()) {
  143. $key = $buffer->readPascalString(1, true);
  144. // If we have an empty key, we've reached the end
  145. if (empty($key)) {
  146. break;
  147. }
  148. // Otherwise, add the pair
  149. $result->add(
  150. $key,
  151. $buffer->readPascalString(1, true)
  152. );
  153. }
  154. unset($key);
  155. }
  156. /**
  157. * Handles processing the player and team data into a usable format
  158. *
  159. * @param \GameQ\Buffer $buffer
  160. * @param \GameQ\Result $result
  161. */
  162. protected function processPlayersAndTeams(Buffer &$buffer, Result &$result)
  163. {
  164. // Players and team info
  165. while ($buffer->getLength()) {
  166. // Get the flags
  167. $flags = $buffer->readInt8();
  168. // Get data according to the flags
  169. if ($flags & 1) {
  170. $result->addPlayer('name', $buffer->readPascalString(1, true));
  171. }
  172. if ($flags & 2) {
  173. $result->addPlayer('team', $buffer->readPascalString(1, true));
  174. }
  175. if ($flags & 4) {
  176. $result->addPlayer('skin', $buffer->readPascalString(1, true));
  177. }
  178. if ($flags & 8) {
  179. $result->addPlayer('score', $buffer->readPascalString(1, true));
  180. }
  181. if ($flags & 16) {
  182. $result->addPlayer('ping', $buffer->readPascalString(1, true));
  183. }
  184. if ($flags & 32) {
  185. $result->addPlayer('time', $buffer->readPascalString(1, true));
  186. }
  187. }
  188. }
  189. }