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.

Place.lib.php 6.3KB

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