osmid = $osmid; $this->energy = new Energy(0, 100); if ($database->has("locations", ["osmid" => $osmid])) { $game = $database->get("locations", ["locationid", "teamid", "ownerid", "osmid", "currentlife", "maxlife", "data", "lastactivity"], ["osmid" => $osmid]); $this->locationid = $game["locationid"]; $this->teamid = $game["teamid"]; $this->ownerid = $game["ownerid"]; $this->energy->setMaxEnergy($game["maxlife"]); $this->energy->setEnergy($game["currentlife"]); $this->data = $game["data"]; $this->lastactivity = strtotime($game["lastactivity"]); $artifacts = $database->select("artifacts", "artuuid", ["locationid" => $game["locationid"]]); foreach ($artifacts as $art) { $this->artifacts[] = new Artifact($art); } $this->gameexists = true; } else { $this->gameexists = false; } } /** * Fetch IRL location data from the mapping server * @global array $SETTINGS * @return boolean True if successful, false on failure */ public function fetchIRLData(): bool { global $SETTINGS; try { $poiurl = $SETTINGS["poi"]["server"] . "?osmid=" . $this->osmid; $place = json_decode(file_get_contents($poiurl), true); if ($place["type"] == "Feature") { $this->name = $place["properties"]["name"]; $this->longitude = $place["geometry"]["coordinates"][0]; $this->latitude = $place["geometry"]["coordinates"][1]; $this->placeexists = true; return true; } else { $this->placeexists = false; return false; } } catch (Exception $ex) { $this->placeexists = false; return false; } } public function exists(): bool { return $this->gameexists; } /** * Check if this place is claimed by a player/team. * @return bool */ public function isClaimed(): bool { if (empty($this->teamid)) { return false; } if (empty($this->ownerid)) { return false; } if ($this->energy->getEnergy() == 0) { return false; } return true; } /** * Add the specified amount of energy to the place. * @param int $diff */ public function changeEnergy(int $diff) { if ($diff < 0) { } $this->energy->setEnergy($this->energy->getEnergy() + $diff); if ($this->energy->getEnergy() == 0) { $this->unclaim(); } } public function getLocationID(): int { return $this->locationid; } public function getTeamID(): int { return $this->teamid; } /** * Set the last activity on this place to now. */ public function updateLastActivity() { $this->lastactivity = time(); } /** * Claim a place for the specified User. * @param \Player $owner */ public function claim(\Player $owner) { $this->energy = new Energy(25, 100); $this->ownerid = $owner->getUID(); $this->teamid = $owner->getTeamID(); $this->updateLastActivity(); } /** * Reset place to natural unclaimed state. */ public function unclaim() { $this->energy = new Energy(0, 0); $this->ownerid = null; $this->teamid = null; $this->updateLastActivity(); foreach ($this->artifacts as $artifact) { $artifact->delete(); } } public function getArtifacts() { return $this->artifacts; } public function addArtifact(\Artifact $artifact) { $this->artifacts[] = $artifact; } public function doAttack(int $damage) { $artifact = false; foreach ($this->artifacts as $art) { if (!$art->deleted()) { $artifact = $art; break; } } if ($artifact) { // Artifact takes damage for place $artifact->changeEnergy($damage * -1); } else { $this->changeEnergy($damage * -1); } } public function doDefend(int $amount) { $this->changeEnergy($amount); } public function save() { global $database; if ($this->gameexists) { $database->update("locations", [ "teamid" => $this->teamid, "ownerid" => $this->ownerid, "currentlife" => $this->energy->getEnergy(), "maxlife" => $this->energy->getMaxEnergy(), "lastactivity" => date("Y-m-d H:i:s", $this->lastactivity) ], [ "osmid" => $this->osmid ] ); foreach ($this->artifacts as $artifact) { $artifact->save(); } } else { $database->insert("locations", [ "teamid" => $this->teamid, "ownerid" => $this->ownerid, "currentlife" => $this->energy->getEnergy(), "maxlife" => $this->energy->getMaxEnergy(), "lastactivity" => date("Y-m-d H:i:s", $this->lastactivity), "osmid" => $this->osmid ]); $this->gameexists = true; } } }