Ship.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 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 General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU 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\Result;
  21. /**
  22. * Class Ship
  23. *
  24. * @package GameQ\Protocols
  25. *
  26. * @author Nikolay Ipanyuk <[email protected]>
  27. * @author Austin Bischoff <[email protected]>
  28. */
  29. class Ship extends Source
  30. {
  31. /**
  32. * String name of this protocol class
  33. *
  34. * @type string
  35. */
  36. protected $name = 'ship';
  37. /**
  38. * Longer string name of this protocol class
  39. *
  40. * @type string
  41. */
  42. protected $name_long = "The Ship";
  43. /**
  44. * Specific player parse for The Ship
  45. *
  46. * Player response has unknown data after the last real player
  47. *
  48. * @param \GameQ\Buffer $buffer
  49. *
  50. * @return array
  51. */
  52. protected function processPlayers(Buffer $buffer)
  53. {
  54. // Set the result to a new result instance
  55. $result = new Result();
  56. // We need to read the number of players because this response has other data at the end usually
  57. $num_players = $buffer->readInt8();
  58. // Player count
  59. $result->add('num_players', $num_players);
  60. // No players, no work
  61. if ($num_players == 0) {
  62. return $result->fetch();
  63. }
  64. // Players list
  65. for ($player = 0; $player < $num_players; $player++) {
  66. $result->addPlayer('id', $buffer->readInt8());
  67. $result->addPlayer('name', $buffer->readString());
  68. $result->addPlayer('score', $buffer->readInt32Signed());
  69. $result->addPlayer('time', $buffer->readFloat32());
  70. }
  71. // Extra data
  72. if ($buffer->getLength() > 0) {
  73. for ($player = 0; $player < $num_players; $player++) {
  74. $result->addPlayer('deaths', $buffer->readInt32Signed());
  75. $result->addPlayer('money', $buffer->readInt32Signed());
  76. }
  77. }
  78. unset($buffer);
  79. return $result->fetch();
  80. }
  81. }