diff --git a/api/actions/placeact.php b/api/actions/placeact.php index 72db971..c2f88ee 100644 --- a/api/actions/placeact.php +++ b/api/actions/placeact.php @@ -16,7 +16,7 @@ if ($place->isClaimed()) { if ($place->energy->getEnergy() == $place->energy->getMaxEnergy()) { sendJsonResp($Strings->get("Nothing happened.", false)); } - $place->changeEnergy(10); + $place->doDefend(10); $player->changeEnergy(-5); $player->addExp(); $player->stats->updateStat(PlayerStats::DEFENDS, 1); @@ -25,7 +25,7 @@ if ($place->isClaimed()) { sendJsonResp($Strings->get("Defending...", false)); } else { // Different teams, player is attacking - $place->changeEnergy(-10); + $place->doAttack(10); $player->changeEnergy(-5); $player->addExp(); $player->stats->updateStat(PlayerStats::ATTACKS, 1); diff --git a/lib/Artifact.lib.php b/lib/Artifact.lib.php new file mode 100644 index 0000000..1a7a227 --- /dev/null +++ b/lib/Artifact.lib.php @@ -0,0 +1,76 @@ +insert("artifacts", ["currentlife" => $energy->getEnergy(), "maxlife" => $energy->getMaxEnergy(), "locationid" => $locationid, "accountid" => $ownerid, "itemid" => $itemid]); + return (new Artifact($database->id())); + } + + public function __construct($uuid) { + global $database; + $data = $database->get("artifacts", ["artuuid", "currentlife", "maxlife", "locationid"], ["artuuid" => $uuid]); + + $this->uuid = $data["artuuid"]; + $this->energy = new Energy($data["currentlife"], $data["maxlife"]); + $this->locationid = $data["locationid"]; + } + + public function changeEnergy(int $diff) { + $this->energy->setEnergy($this->energy->getEnergy() + $diff); + } + + public function getEnergy(): \Energy { + return $this->energy; + } + + /** + * Check if the artifact will be deleted on save. + * @return bool + */ + public function deleted(): bool { + if ($this->energy->getEnergy() == 0 || $this->delete) { + return true; + } + return false; + } + + /** + * Mark for deletion on save. + */ + public function delete() { + $this->delete = true; + } + + public function save() { + global $database; + + if ($this->deleted()) { + $database->delete("artifacts", ["artuuid" => $this->uuid]); + } else { + $database->update("artifacts", ["currentlife" => $this->energy->getEnergy(), "maxlife" => $this->energy->getMaxEnergy()], ["artuuid" => $this->uuid]); + } + } + +} diff --git a/lib/Place.lib.php b/lib/Place.lib.php index 24dab32..d95363f 100644 --- a/lib/Place.lib.php +++ b/lib/Place.lib.php @@ -25,6 +25,7 @@ class Place { private $osmid = null; public $energy = null; private $data = "[]"; + private $artifacts = []; /** * @var int Last player activity at the place, as UNIX timestamp @@ -46,6 +47,12 @@ class Place { $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; @@ -102,6 +109,9 @@ class 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(); @@ -130,11 +140,41 @@ class Place { $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() { @@ -150,6 +190,10 @@ class Place { "osmid" => $this->osmid ] ); + + foreach ($this->artifacts as $artifact) { + $artifact->save(); + } } else { $database->insert("locations", [ "teamid" => $this->teamid,