1
0

Tibia.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * Tibia Protocol Class
  25. *
  26. * Tibia server query protocol class
  27. *
  28. * Credit to Ahmad Fatoum for providing Perl based querying as a roadmap
  29. *
  30. * @author Yive <[email protected]>
  31. * @author Austin Bischoff <[email protected]>
  32. */
  33. class Tibia extends Protocol
  34. {
  35. /**
  36. * Array of packets we want to query.
  37. *
  38. * @type array
  39. */
  40. protected $packets = [
  41. self::PACKET_STATUS => "\x06\x00\xFF\xFF\x69\x6E\x66\x6F",
  42. ];
  43. /**
  44. * The transport mode for this protocol is TCP
  45. *
  46. * @type string
  47. */
  48. protected $transport = self::TRANSPORT_TCP;
  49. /**
  50. * The query protocol used to make the call
  51. *
  52. * @type string
  53. */
  54. protected $protocol = 'tibia';
  55. /**
  56. * String name of this protocol class
  57. *
  58. * @type string
  59. */
  60. protected $name = 'tibia';
  61. /**
  62. * Longer string name of this protocol class
  63. *
  64. * @type string
  65. */
  66. protected $name_long = "Tibia";
  67. /**
  68. * The client join link
  69. *
  70. * @type string
  71. */
  72. protected $join_link = "otserv://%s/%d/";
  73. /**
  74. * Normalize settings for this protocol
  75. *
  76. * @type array
  77. */
  78. protected $normalize = [
  79. // General
  80. 'general' => [
  81. // target => source
  82. 'dedicated' => 'dedicated',
  83. 'gametype' => 'server',
  84. 'hostname' => 'servername',
  85. 'motd' => 'motd',
  86. 'maxplayers' => 'players_max',
  87. 'numplayers' => 'players_online',
  88. 'map' => 'map_name',
  89. ],
  90. ];
  91. /**
  92. * Process the response for the Tibia server
  93. *
  94. * @return array
  95. * @throws \GameQ\Exception\Protocol
  96. */
  97. public function processResponse()
  98. {
  99. // Merge the response packets
  100. $xmlString = implode('', $this->packets_response);
  101. // Check to make sure this is will decode into a valid XML Document
  102. if (($xmlDoc = @simplexml_load_string($xmlString)) === false) {
  103. throw new Exception(__METHOD__ . " Unable to load XML string.");
  104. }
  105. // Set the result to a new result instance
  106. $result = new Result();
  107. // All servers are dedicated as far as I can tell
  108. $result->add('dedicated', 1);
  109. // Iterate over the info
  110. foreach (['serverinfo', 'owner', 'map', 'npcs', 'monsters', 'players'] as $property) {
  111. foreach ($xmlDoc->{$property}->attributes() as $key => $value) {
  112. if (!in_array($property, ['serverinfo'])) {
  113. $key = $property . '_' . $key;
  114. }
  115. // Add the result
  116. $result->add($key, (string)$value);
  117. }
  118. }
  119. $result->add("motd", (string)$xmlDoc->motd);
  120. unset($xmlDoc, $xmlDoc);
  121. return $result->fetch();
  122. }
  123. }