Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

211 linhas
5.9 KiB
PHP

<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
class Place {
/**
* @var bool If this place exists at all
*/
private $placeexists = false;
private $name = "";
private $latitude = null;
private $longitude = null;
/**
* @var bool If this place exists in the game DB
*/
private $gameexists = false;
private $teamid = null;
private $ownerid = null;
private $osmid = null;
public $energy = null;
private $data = "[]";
private $artifacts = [];
/**
* @var int Last player activity at the place, as UNIX timestamp
*/
private $lastactivity = 0;
public function __construct(int $osmid) {
global $database;
$this->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->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;
}
}
/**
* 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 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 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;
}
}
}