Game server and admin dashboard for TerranQuest.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Player.lib.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. class Player {
  8. public $user = null;
  9. private $exists = false;
  10. private $name = "";
  11. private $level = 1.0;
  12. public $energy = null;
  13. private $teamid = 0;
  14. private $credits = 500;
  15. private $lastping = 0;
  16. private $stealth = true;
  17. public $stats = null;
  18. private $badges = [];
  19. public function __construct(\User $user) {
  20. global $database;
  21. if (!$user->exists()) {
  22. throw new InvalidArgumentException("User must exist.");
  23. }
  24. $this->user = $user;
  25. $this->energy = new Energy(100, 100);
  26. $this->stats = new PlayerStats($user);
  27. if ($database->has("players", ["accountid" => $user->getUID()])) {
  28. $profile = $database->get("players", ["accountid (id)", "nickname (name)", "level", "energy", "maxenergy", "teamid", "lastping", "stealth"], ["accountid" => $user->getUID()]);
  29. $this->name = $profile["name"];
  30. $this->level = $profile["level"] * 1.0;
  31. $this->energy->setMaxEnergy($profile["maxenergy"]);
  32. $this->energy->setEnergy($profile["energy"]);
  33. $this->teamid = $profile["teamid"];
  34. $this->lastping = strtotime($profile["lastping"]);
  35. $this->stealth = $profile["stealth"] == true;
  36. $this->exists = true;
  37. $this->badges = $database->select("player_badges", ["[>]badges" => "badgeid"], ["name", "desc", "icon", "color", "background", "gotdate (date)"], ["accountid" => $user->getUID()]);
  38. } else {
  39. $this->exists = false;
  40. }
  41. }
  42. public function exists(): bool {
  43. return $this->exists;
  44. }
  45. public function getBadges(): array {
  46. return $this->badges;
  47. }
  48. public function toArray(): array {
  49. //"accountid (id)", "nickname (name)", "level", "energy", "maxenergy", "teamid"
  50. return [
  51. "id" => $this->getUID(),
  52. "name" => $this->name,
  53. "level" => floor($this->level),
  54. "energy" => $this->energy->getEnergy(),
  55. "maxenergy" => $this->energy->getMaxEnergy(),
  56. "teamid" => $this->getTeamID(),
  57. "badges" => $this->getBadges()
  58. ];
  59. }
  60. public function getUID(): int {
  61. return $this->user->getUID();
  62. }
  63. public function getLevel(): float {
  64. return $this->level;
  65. }
  66. public function getTeamID(): int {
  67. return $this->teamid;
  68. }
  69. public function setTeamID(int $id) {
  70. $this->teamid = $id;
  71. }
  72. public function getCredits(): int {
  73. return $this->credits;
  74. }
  75. /**
  76. * Add the specified amount of energy to the player.
  77. * @param int $diff
  78. */
  79. public function changeEnergy(int $diff) {
  80. $this->energy->setEnergy($this->energy->getEnergy() + $diff);
  81. }
  82. /**
  83. * Set the player's last ping time to now.
  84. */
  85. public function doPing() {
  86. $this->lastping = time();
  87. }
  88. /**
  89. * Add experience to the user's level for doing something.
  90. */
  91. public function addExp() {
  92. $exp = pow(pow(floor($this->level * 1.0) + 1, 2), -1.2);
  93. $this->level = $this->level + $exp;
  94. $this->recalculateStats();
  95. }
  96. public function recalculateStats() {
  97. $healthpercent = ($this->energy->getEnergy() * 1.0) / ($this->energy->getMaxEnergy() * 1.0);
  98. $maxenergy = $this->energy->getMaxEnergy();
  99. $this->energy->setMaxEnergy(floor($this->level) * 100);
  100. // If the max energy increased, adjust the current energy
  101. // to the same percentage as it was before
  102. if ($this->energy->getMaxEnergy() != $maxenergy) {
  103. $this->energy->setEnergy($this->energy->getMaxEnergy() * $healthpercent);
  104. }
  105. }
  106. public function createPlayer() {
  107. global $database;
  108. if ($this->exists) {
  109. throw new Exception("This player already exists.");
  110. }
  111. $database->insert("players", [
  112. "accountid" => $this->user->getUID(),
  113. "level" => $this->getLevel(),
  114. "energy" => $this->energy->getEnergy(),
  115. "maxenergy" => $this->energy->getMaxEnergy(),
  116. "credits" => $this->getCredits(),
  117. "lastping" => date("Y-m-d H:i:s", $this->lastping),
  118. "teamid" => $this->getTeamID(),
  119. "nickname" => $this->user->getName()
  120. ]);
  121. $this->stats->save();
  122. $this->exists = true;
  123. }
  124. public function save() {
  125. global $database;
  126. if (!$this->exists) {
  127. $this->createPlayer();
  128. return;
  129. }
  130. $database->update("players",
  131. [
  132. "level" => $this->getLevel(),
  133. "energy" => $this->energy->getEnergy(),
  134. "maxenergy" => $this->energy->getMaxEnergy(),
  135. "credits" => $this->getCredits(),
  136. "lastping" => date("Y-m-d H:i:s", $this->lastping),
  137. "teamid" => $this->getTeamID(),
  138. "nickname" => $this->user->getName()
  139. ],
  140. [
  141. "accountid" => $this->user->getUID()
  142. ]
  143. );
  144. $this->stats->save();
  145. }
  146. }