Gta5m.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\Buffer;
  20. use GameQ\Exception\Protocol as Exception;
  21. use GameQ\Protocol;
  22. use GameQ\Result;
  23. /**
  24. * GTA Five M Protocol Class
  25. *
  26. * Server base can be found at https://fivem.net/
  27. *
  28. * Based on code found at https://github.com/LiquidObsidian/fivereborn-query
  29. *
  30. * @author Austin Bischoff <[email protected]>
  31. */
  32. class Gta5m extends Protocol
  33. {
  34. /**
  35. * Array of packets we want to look up.
  36. * Each key should correspond to a defined method in this or a parent class
  37. *
  38. * @type array
  39. */
  40. protected $packets = [
  41. self::PACKET_STATUS => "\xFF\xFF\xFF\xFFgetinfo xxx",
  42. ];
  43. /**
  44. * Use the response flag to figure out what method to run
  45. *
  46. * @type array
  47. */
  48. protected $responses = [
  49. "\xFF\xFF\xFF\xFFinfoResponse" => "processStatus",
  50. ];
  51. /**
  52. * The query protocol used to make the call
  53. *
  54. * @type string
  55. */
  56. protected $protocol = 'gta5m';
  57. /**
  58. * String name of this protocol class
  59. *
  60. * @type string
  61. */
  62. protected $name = 'gta5m';
  63. /**
  64. * Longer string name of this protocol class
  65. *
  66. * @type string
  67. */
  68. protected $name_long = "GTA Five M";
  69. /**
  70. * Normalize settings for this protocol
  71. *
  72. * @type array
  73. */
  74. protected $normalize = [
  75. // General
  76. 'general' => [
  77. // target => source
  78. 'gametype' => 'gametype',
  79. 'hostname' => 'hostname',
  80. 'mapname' => 'mapname',
  81. 'maxplayers' => 'sv_maxclients',
  82. 'mod' => 'gamename',
  83. 'numplayers' => 'clients',
  84. 'password' => 'privateClients',
  85. ],
  86. ];
  87. /**
  88. * Process the response
  89. *
  90. * @return array
  91. * @throws \GameQ\Exception\Protocol
  92. */
  93. public function processResponse()
  94. {
  95. // In case it comes back as multiple packets (it shouldn't)
  96. $buffer = new Buffer(implode('', $this->packets_response));
  97. // Figure out what packet response this is for
  98. $response_type = $buffer->readString(PHP_EOL);
  99. // Figure out which packet response this is
  100. if (empty($response_type) || !array_key_exists($response_type, $this->responses)) {
  101. throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid");
  102. }
  103. // Offload the call
  104. $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]);
  105. return $results;
  106. }
  107. /*
  108. * Internal methods
  109. */
  110. /**
  111. * Handle processing the status response
  112. *
  113. * @param Buffer $buffer
  114. *
  115. * @return array
  116. */
  117. protected function processStatus(Buffer $buffer)
  118. {
  119. // Set the result to a new result instance
  120. $result = new Result();
  121. // Lets peek and see if the data starts with a \
  122. if ($buffer->lookAhead(1) == '\\') {
  123. // Burn the first one
  124. $buffer->skip(1);
  125. }
  126. // Explode the data
  127. $data = explode('\\', $buffer->getBuffer());
  128. // No longer needed
  129. unset($buffer);
  130. $itemCount = count($data);
  131. // Now lets loop the array
  132. for ($x = 0; $x < $itemCount; $x += 2) {
  133. // Set some local vars
  134. $key = $data[$x];
  135. $val = $data[$x + 1];
  136. if (in_array($key, ['challenge'])) {
  137. continue; // skip
  138. }
  139. // Regular variable so just add the value.
  140. $result->add($key, $val);
  141. }
  142. /*var_dump($data);
  143. var_dump($result->fetch());
  144. exit;*/
  145. return $result->fetch();
  146. }
  147. }