From 4cea9d1098106ec8ca5c66f0b77af65cb15fc8c4 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 28 May 2019 00:26:15 -0600 Subject: [PATCH] Add ability to add artifacts to places in useitem --- api/actions/useitem.php | 41 ++++++++++++++++++++++++++--------------- lib/Place.lib.php | 14 ++++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/api/actions/useitem.php b/api/actions/useitem.php index f19f8a6..0afcc9f 100644 --- a/api/actions/useitem.php +++ b/api/actions/useitem.php @@ -19,6 +19,7 @@ $item = $database->get( "[>]itemclasses" => ["classid" => "classid"] ], [ + "items.itemid", "inventory.itemjson", "items.classid", "itemclasses.classname", @@ -39,38 +40,48 @@ if (empty($item['itemjson']) || $item['itemjson'] == "[]") { $itemjson = json_decode($item["itemjson"], true); } -$player = $database->get("players", ["energy", "maxenergy", "teamid"], ["accountid" => $user->getUID()]); +$player = new Player($user); switch ($item["classname"]) { case "healmagic": - if ($player["energy"] < $player["maxenergy"]) { - $newhp = $player["energy"] + $item["itemcode"]["amount"]; - if ($newhp > $player["maxenergy"]) { - $newhp = $player["maxenergy"]; - } - $diff = $newhp - $player["energy"]; - $database->update("players", ["energy" => $newhp], ["accountid" => $user->getUID()]); + if ($player->energy->getEnergy() < $player->energy->getMaxEnergy()) { + $diff = ($player->energy->getEnergy() + $item["itemcode"]["amount"]) < $player->energy->getMaxEnergy() ? $item["itemcode"]["amount"] : ($player->energy->getMaxEnergy() - $player->energy->getEnergy()); + + $player->changeEnergy($item["itemcode"]["amount"]); + if ($itemjson["uses"] <= 1) { $database->delete("inventory", ["AND" => ["itemuuid" => $itemuuid, "accountid" => $user->getUID()]]); } else if ($itemjson["uses"] > 1) { $itemjson["uses"] -= 1; $database->update("inventory", ["itemjson" => json_encode($itemjson)], ["itemuuid" => $itemuuid]); } + $player->save(); sendJsonResp($Strings->build("Restored {x} energy points.", ["x" => $diff], false)); } else { sendJsonResp($Strings->get("That would have no effect.", false)); } break; case "artifact": - if (empty($VARS["placeid"]) || !$database->has("locations", ["AND" => ["osmid" => $VARS["placeid"], "teamid" => $player["teamid"]]])) { + if (empty($VARS["placeid"])) { sendJsonResp($Strings->get("You can't use that right now.", false)); } - $place = $database->get("locations", ["locationid", "teamid", "ownerid", "currentlife", "maxlife"], ["locationid" => $VARS["placeid"]]); - $placelife = $place["currentlife"] + $item["itemcode"]["amount"]; - $placemax = $place["maxlife"] + $item["itemcode"]["amount"]; - $database->update("locations", ["currentlife" => $placelife, "maxlife" => $placemax], ["osmid" => $VARS["placeid"]]); - // TODO: give user some exp - sendJsonResp($Strings->get("Artifact activated.")); + + $place = new Place($VARS["placeid"]); + if ($place->isClaimed() && $place->getTeamID() == $player->getTeamID()) { + $place->addArtifact(Artifact::create($player->getUID(), $place->getLocationID(), new Energy($item["itemcode"]["amount"], $item["itemcode"]["amount"]), $item["itemid"])); + $place->save(); + $player->addExp(); + $player->save(); + if ($itemjson["uses"] <= 1) { + $database->delete("inventory", ["AND" => ["itemuuid" => $itemuuid, "accountid" => $user->getUID()]]); + } else if ($itemjson["uses"] > 1) { + $itemjson["uses"] -= 1; + $database->update("inventory", ["itemjson" => json_encode($itemjson)], ["itemuuid" => $itemuuid]); + } + sendJsonResp($Strings->get("Artifact activated.", false)); + } + + sendJsonResp($Strings->get("You can't use that right now.", false)); break; default: sendJsonResp($Strings->get("You can't use that right now.", false)); diff --git a/lib/Place.lib.php b/lib/Place.lib.php index d95363f..a4426e0 100644 --- a/lib/Place.lib.php +++ b/lib/Place.lib.php @@ -20,6 +20,7 @@ class Place { * @var bool If this place exists in the game DB */ private $gameexists = false; + private $locationid = null; private $teamid = null; private $ownerid = null; private $osmid = null; @@ -41,6 +42,7 @@ class Place { 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"]); @@ -87,6 +89,10 @@ class Place { } } + public function exists(): bool { + return $this->gameexists; + } + /** * Check if this place is claimed by a player/team. * @return bool @@ -118,6 +124,10 @@ class Place { } } + public function getLocationID(): int { + return $this->locationid; + } + public function getTeamID(): int { return $this->teamid; } @@ -157,6 +167,10 @@ class Place { return $this->artifacts; } + public function addArtifact(\Artifact $artifact) { + $this->artifacts[] = $artifact; + } + public function doAttack(int $damage) { $artifact = false; foreach ($this->artifacts as $art) {