Eco.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * ECO Global Survival Protocol Class
  23. *
  24. * @author Austin Bischoff <[email protected]>
  25. */
  26. class Eco extends Http
  27. {
  28. /**
  29. * Packets to send
  30. *
  31. * @var array
  32. */
  33. protected $packets = [
  34. self::PACKET_STATUS => "GET /frontpage HTTP/1.0\r\nAccept: */*\r\n\r\n",
  35. ];
  36. /**
  37. * Http protocol is SSL
  38. *
  39. * @var string
  40. */
  41. protected $transport = self::TRANSPORT_TCP;
  42. /**
  43. * The protocol being used
  44. *
  45. * @var string
  46. */
  47. protected $protocol = 'eco';
  48. /**
  49. * String name of this protocol class
  50. *
  51. * @var string
  52. */
  53. protected $name = 'eco';
  54. /**
  55. * Longer string name of this protocol class
  56. *
  57. * @var string
  58. */
  59. protected $name_long = "ECO Global Survival";
  60. /**
  61. * query_port = client_port + 1
  62. *
  63. * @type int
  64. */
  65. protected $port_diff = 1;
  66. /**
  67. * Normalize some items
  68. *
  69. * @var array
  70. */
  71. protected $normalize = [
  72. // General
  73. 'general' => [
  74. // target => source
  75. 'dedicated' => 'dedicated',
  76. 'hostname' => 'description',
  77. 'maxplayers' => 'totalplayers',
  78. 'numplayers' => 'onlineplayers',
  79. 'password' => 'haspassword',
  80. ],
  81. ];
  82. /**
  83. * Process the response
  84. *
  85. * @return array
  86. * @throws Exception
  87. */
  88. public function processResponse()
  89. {
  90. if (empty($this->packets_response)) {
  91. return [];
  92. }
  93. // Implode and rip out the JSON
  94. preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
  95. // Return should be JSON, let's validate
  96. if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
  97. throw new Exception("JSON response from Eco server is invalid.");
  98. }
  99. $result = new Result();
  100. // Server is always dedicated
  101. $result->add('dedicated', 1);
  102. foreach ($json->Info as $info => $setting) {
  103. $result->add(strtolower($info), $setting);
  104. }
  105. return $result->fetch();
  106. }
  107. }