Tshock.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Exception\Protocol as Exception;
  20. use GameQ\Result;
  21. /**
  22. * Tshock Protocol Class
  23. *
  24. * Result from this call should be a header + JSON response
  25. *
  26. * References:
  27. * - https://tshock.atlassian.net/wiki/display/TSHOCKPLUGINS/REST+API+Endpoints#RESTAPIEndpoints-/status
  28. * - http://tshock.co/xf/index.php?threads/rest-tshock-server-status-image.430/
  29. *
  30. * Special thanks to intradox and Ruok2bu for game & protocol references
  31. *
  32. * @author Austin Bischoff <[email protected]>
  33. */
  34. class Tshock extends Http
  35. {
  36. /**
  37. * Packets to send
  38. *
  39. * @var array
  40. */
  41. protected $packets = [
  42. self::PACKET_STATUS => "GET /v2/server/status?players=true&rules=true HTTP/1.0\r\nAccept: */*\r\n\r\n",
  43. ];
  44. /**
  45. * The protocol being used
  46. *
  47. * @var string
  48. */
  49. protected $protocol = 'tshock';
  50. /**
  51. * String name of this protocol class
  52. *
  53. * @var string
  54. */
  55. protected $name = 'tshock';
  56. /**
  57. * Longer string name of this protocol class
  58. *
  59. * @var string
  60. */
  61. protected $name_long = "Tshock";
  62. /**
  63. * Normalize some items
  64. *
  65. * @var array
  66. */
  67. protected $normalize = [
  68. // General
  69. 'general' => [
  70. // target => source
  71. 'dedicated' => 'dedicated',
  72. 'hostname' => 'hostname',
  73. 'mapname' => 'world',
  74. 'maxplayers' => 'maxplayers',
  75. 'numplayers' => 'numplayers',
  76. 'password' => 'password',
  77. ],
  78. // Individual
  79. 'player' => [
  80. 'name' => 'nickname',
  81. 'team' => 'team',
  82. ],
  83. ];
  84. /**
  85. * Process the response
  86. *
  87. * @return array
  88. * @throws Exception
  89. */
  90. public function processResponse()
  91. {
  92. if (empty($this->packets_response)) {
  93. return [];
  94. }
  95. // Implode and rip out the JSON
  96. preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
  97. // Return should be JSON, let's validate
  98. if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
  99. throw new Exception("JSON response from Tshock protocol is invalid.");
  100. }
  101. // Check the status response
  102. if ($json->status != 200) {
  103. throw new Exception("JSON status from Tshock protocol response was '{$json->status}', expected '200'.");
  104. }
  105. $result = new Result();
  106. // Server is always dedicated
  107. $result->add('dedicated', 1);
  108. // Add server items
  109. $result->add('hostname', $json->name);
  110. $result->add('game_port', $json->port);
  111. $result->add('serverversion', $json->serverversion);
  112. $result->add('world', $json->world);
  113. $result->add('uptime', $json->uptime);
  114. $result->add('password', (int)$json->serverpassword);
  115. $result->add('numplayers', $json->playercount);
  116. $result->add('maxplayers', $json->maxplayers);
  117. // Parse players
  118. foreach ($json->players as $player) {
  119. $result->addPlayer('nickname', $player->nickname);
  120. $result->addPlayer('username', $player->username);
  121. $result->addPlayer('group', $player->group);
  122. $result->addPlayer('active', (int)$player->active);
  123. $result->addPlayer('state', $player->state);
  124. $result->addPlayer('team', $player->team);
  125. }
  126. // Make rules into simple array
  127. $rules = [];
  128. // Parse rules
  129. foreach ($json->rules as $rule => $value) {
  130. // Add rule but convert boolean into int (0|1)
  131. $rules[$rule] = (is_bool($value)) ? (int)$value : $value;
  132. }
  133. // Add rules
  134. $result->add('rules', $rules);
  135. unset($rules, $rule, $player, $value);
  136. return $result->fetch();
  137. }
  138. }