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.

211 lines
5.9KB

  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 Place {
  8. /**
  9. * @var bool If this place exists at all
  10. */
  11. private $placeexists = false;
  12. private $name = "";
  13. private $latitude = null;
  14. private $longitude = null;
  15. /**
  16. * @var bool If this place exists in the game DB
  17. */
  18. private $gameexists = false;
  19. private $teamid = null;
  20. private $ownerid = null;
  21. private $osmid = null;
  22. public $energy = null;
  23. private $data = "[]";
  24. private $artifacts = [];
  25. /**
  26. * @var int Last player activity at the place, as UNIX timestamp
  27. */
  28. private $lastactivity = 0;
  29. public function __construct(int $osmid) {
  30. global $database;
  31. $this->osmid = $osmid;
  32. $this->energy = new Energy(0, 100);
  33. if ($database->has("locations", ["osmid" => $osmid])) {
  34. $game = $database->get("locations", ["locationid", "teamid", "ownerid", "osmid", "currentlife", "maxlife", "data", "lastactivity"], ["osmid" => $osmid]);
  35. $this->teamid = $game["teamid"];
  36. $this->ownerid = $game["ownerid"];
  37. $this->energy->setMaxEnergy($game["maxlife"]);
  38. $this->energy->setEnergy($game["currentlife"]);
  39. $this->data = $game["data"];
  40. $this->lastactivity = strtotime($game["lastactivity"]);
  41. $artifacts = $database->select("artifacts", "artuuid", ["locationid" => $game["locationid"]]);
  42. foreach ($artifacts as $art) {
  43. $this->artifacts[] = new Artifact($art);
  44. }
  45. $this->gameexists = true;
  46. } else {
  47. $this->gameexists = false;
  48. }
  49. }
  50. /**
  51. * Fetch IRL location data from the mapping server
  52. * @global array $SETTINGS
  53. * @return boolean True if successful, false on failure
  54. */
  55. public function fetchIRLData(): bool {
  56. global $SETTINGS;
  57. try {
  58. $poiurl = $SETTINGS["poi"]["server"] . "?osmid=" . $this->osmid;
  59. $place = json_decode(file_get_contents($poiurl), true);
  60. if ($place["type"] == "Feature") {
  61. $this->name = $place["properties"]["name"];
  62. $this->longitude = $place["geometry"]["coordinates"][0];
  63. $this->latitude = $place["geometry"]["coordinates"][1];
  64. $this->placeexists = true;
  65. return true;
  66. } else {
  67. $this->placeexists = false;
  68. return false;
  69. }
  70. } catch (Exception $ex) {
  71. $this->placeexists = false;
  72. return false;
  73. }
  74. }
  75. /**
  76. * Check if this place is claimed by a player/team.
  77. * @return bool
  78. */
  79. public function isClaimed(): bool {
  80. if (empty($this->teamid)) {
  81. return false;
  82. }
  83. if (empty($this->ownerid)) {
  84. return false;
  85. }
  86. if ($this->energy->getEnergy() == 0) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * Add the specified amount of energy to the place.
  93. * @param int $diff
  94. */
  95. public function changeEnergy(int $diff) {
  96. if ($diff < 0) {
  97. }
  98. $this->energy->setEnergy($this->energy->getEnergy() + $diff);
  99. if ($this->energy->getEnergy() == 0) {
  100. $this->unclaim();
  101. }
  102. }
  103. public function getTeamID(): int {
  104. return $this->teamid;
  105. }
  106. /**
  107. * Set the last activity on this place to now.
  108. */
  109. public function updateLastActivity() {
  110. $this->lastactivity = time();
  111. }
  112. /**
  113. * Claim a place for the specified User.
  114. * @param \Player $owner
  115. */
  116. public function claim(\Player $owner) {
  117. $this->energy = new Energy(25, 100);
  118. $this->ownerid = $owner->getUID();
  119. $this->teamid = $owner->getTeamID();
  120. $this->updateLastActivity();
  121. }
  122. /**
  123. * Reset place to natural unclaimed state.
  124. */
  125. public function unclaim() {
  126. $this->energy = new Energy(0, 0);
  127. $this->ownerid = null;
  128. $this->teamid = null;
  129. $this->updateLastActivity();
  130. foreach ($this->artifacts as $artifact) {
  131. $artifact->delete();
  132. }
  133. }
  134. public function getArtifacts() {
  135. return $this->artifacts;
  136. }
  137. public function doAttack(int $damage) {
  138. $artifact = false;
  139. foreach ($this->artifacts as $art) {
  140. if (!$art->deleted()) {
  141. $artifact = $art;
  142. break;
  143. }
  144. }
  145. if ($artifact) {
  146. // Artifact takes damage for place
  147. $artifact->changeEnergy($damage * -1);
  148. } else {
  149. $this->changeEnergy($damage * -1);
  150. }
  151. }
  152. public function doDefend(int $amount) {
  153. $this->changeEnergy($amount);
  154. }
  155. public function save() {
  156. global $database;
  157. if ($this->gameexists) {
  158. $database->update("locations", [
  159. "teamid" => $this->teamid,
  160. "ownerid" => $this->ownerid,
  161. "currentlife" => $this->energy->getEnergy(),
  162. "maxlife" => $this->energy->getMaxEnergy(),
  163. "lastactivity" => date("Y-m-d H:i:s", $this->lastactivity)
  164. ], [
  165. "osmid" => $this->osmid
  166. ]
  167. );
  168. foreach ($this->artifacts as $artifact) {
  169. $artifact->save();
  170. }
  171. } else {
  172. $database->insert("locations", [
  173. "teamid" => $this->teamid,
  174. "ownerid" => $this->ownerid,
  175. "currentlife" => $this->energy->getEnergy(),
  176. "maxlife" => $this->energy->getMaxEnergy(),
  177. "lastactivity" => date("Y-m-d H:i:s", $this->lastactivity),
  178. "osmid" => $this->osmid
  179. ]);
  180. $this->gameexists = true;
  181. }
  182. }
  183. }