Openttd.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * OpenTTD Protocol Class
  25. *
  26. * Handles processing Open Transport Tycoon Deluxe servers
  27. *
  28. * @package GameQ\Protocols
  29. * @author Wilson Jesus <>
  30. */
  31. class Openttd extends Protocol
  32. {
  33. /**
  34. * Array of packets we want to look up.
  35. * Each key should correspond to a defined method in this or a parent class
  36. *
  37. * @type array
  38. */
  39. protected $packets = [
  40. self::PACKET_ALL => "\x03\x00\x00",
  41. ];
  42. /**
  43. * The query protocol used to make the call
  44. *
  45. * @type string
  46. */
  47. protected $protocol = 'openttd';
  48. /**
  49. * String name of this protocol class
  50. *
  51. * @type string
  52. */
  53. protected $name = 'openttd';
  54. /**
  55. * Longer string name of this protocol class
  56. *
  57. * @type string
  58. */
  59. protected $name_long = "Open Transport Tycoon Deluxe";
  60. /**
  61. * The client join link
  62. *
  63. * @type string
  64. */
  65. protected $join_link = null;
  66. /**
  67. * Normalize settings for this protocol
  68. *
  69. * @type array
  70. */
  71. protected $normalize = [
  72. // General
  73. 'general' => [
  74. // target => source
  75. 'hostname' => 'hostname',
  76. 'mapname' => 'map',
  77. 'maxplayers' => 'max_clients',
  78. 'numplayers' => 'clients',
  79. 'password' => 'password',
  80. 'dedicated' => 'dedicated',
  81. ],
  82. ];
  83. /**
  84. * Handle response from the server
  85. *
  86. * @return mixed
  87. * @throws Exception
  88. */
  89. public function processResponse()
  90. {
  91. // Make a buffer
  92. $buffer = new Buffer(implode('', $this->packets_response));
  93. // Get the length of the packet
  94. $packetLength = $buffer->getLength();
  95. // Grab the header
  96. $length = $buffer->readInt16();
  97. //$type = $buffer->readInt8();
  98. $buffer->skip(1); // Skip the "$type" as its not used in the code, and to comply with phpmd it cant be assigned and not used.
  99. // Header
  100. // Figure out which packet response this is
  101. if ($packetLength != $length) {
  102. throw new Exception(__METHOD__ . " response type '" . bin2hex($length) . "' is not valid");
  103. }
  104. return call_user_func_array([$this, 'processServerInfo'], [$buffer]);
  105. }
  106. /**
  107. * Handle processing the server information
  108. *
  109. * @param Buffer $buffer
  110. *
  111. * @return array
  112. */
  113. protected function processServerInfo(Buffer $buffer)
  114. {
  115. // Set the result to a new result instance
  116. $result = new Result();
  117. $protocol_version = $buffer->readInt8();
  118. $result->add('protocol_version', $protocol_version);
  119. switch ($protocol_version) {
  120. case 4:
  121. $num_grfs = $buffer->readInt8(); #number of grfs
  122. $result->add('num_grfs', $num_grfs);
  123. //$buffer->skip ($num_grfs * 20); #skip grfs id and md5 hash
  124. for ($i=0; $i<$num_grfs; $i++) {
  125. $result->add('grfs_'.$i.'_ID', strtoupper(bin2hex($buffer->read(4))));
  126. $result->add('grfs_'.$i.'_MD5', strtoupper(bin2hex($buffer->read(16))));
  127. }
  128. // No break, cascades all the down even if case is meet
  129. case 3:
  130. $result->add('game_date', $buffer->readInt32());
  131. $result->add('start_date', $buffer->readInt32());
  132. // Cascades all the way down even if case is meet
  133. case 2:
  134. $result->add('companies_max', $buffer->readInt8());
  135. $result->add('companies_on', $buffer->readInt8());
  136. $result->add('spectators_max', $buffer->readInt8());
  137. // Cascades all the way down even if case is meet
  138. case 1:
  139. $result->add('hostname', $buffer->readString());
  140. $result->add('version', $buffer->readString());
  141. $language = $buffer->readInt8();
  142. $result->add('language', $language);
  143. $result->add('language_icon', '//media.openttd.org/images/server/'.$language.'_lang.gif');
  144. $result->add('password', $buffer->readInt8());
  145. $result->add('max_clients', $buffer->readInt8());
  146. $result->add('clients', $buffer->readInt8());
  147. $result->add('spectators', $buffer->readInt8());
  148. if ($protocol_version < 3) {
  149. $days = ( 365 * 1920 + 1920 / 4 - 1920 / 100 + 1920 / 400 );
  150. $result->add('game_date', $buffer->readInt16() + $days);
  151. $result->add('start_date', $buffer->readInt16() + $days);
  152. }
  153. $result->add('map', $buffer->readString());
  154. $result->add('map_width', $buffer->readInt16());
  155. $result->add('map_height', $buffer->readInt16());
  156. $result->add('map_type', $buffer->readInt8());
  157. $result->add('dedicated', $buffer->readInt8());
  158. // Cascades all the way down even if case is meet
  159. }
  160. unset($buffer);
  161. return $result->fetch();
  162. }
  163. }