public function evolve($buildingId) { $query = "\n SELECT\n ubl.building_id as 'Id',\n ubl.level_id AS 'Level',\n bl.gold AS 'Gold',\n bl.food AS 'Food'\n FROM users_buildings_levels ubl\n JOIN buildings b\n ON b.id = ubl.building_id\n JOIN building_levels bl\n ON bl.building_id = ubl.building_id AND bl.level = ubl.level_id + 1\n WHERE ubl.user_id = ? AND ubl.building_id = ?\n "; $result = $this->database->prepare($query); $result->execute([$_SESSION['id'], $buildingId]); $building = $result->fetch(\PDO::FETCH_ASSOC); if ($this->user->getGold() < $building['Gold'] || $this->user->getFood() < $building['Food']) { throw new \Exception('Insufficient resource to evolve building'); } if ($building['Level'] == 3) { throw new \Exception('Building has reached maximum level and cannot be evolved'); } $resourceUpdate = "\n UPDATE users\n SET gold = ?, food = ?\n WHERE id = ?\n "; $result = $this->database->prepare($resourceUpdate); $result->execute([$this->user->getGold() - $building['Gold'], $this->user->getFood() - $building['Food'], $_SESSION['id']]); if ($result) { $buildingUpdate = "\n UPDATE users_buildings_levels\n SET level_id = ?\n WHERE user_id = ? AND building_id = ?\n "; $result = $this->database->prepare($buildingUpdate); $result->execute([$building['Level'] + 1, $_SESSION['id'], $buildingId]); if ($result) { return true; } throw new \Exception('Error occurred while upgrading building'); } throw new \Exception('Error occurred while upgrading building'); }
public function evolve($buildingId) { $buildingCost = $this->db->prepare("SELECT\n lvl.food,\n lvl.gold\n FROM\n userbuildings ub\n INNER JOIN\n buildinglvldefs lvl ON ub.level_id + 1 = lvl.id\n WHERE\n ub.id = ?"); $buildingCost->execute([$buildingId]); $buildingCost = $buildingCost->fetch(); if ($this->user->getGold() < $buildingCost['gold'] || $this->user->getFood() < $buildingCost['food']) { throw new \Exception("Not enough resources."); } if (!$this->lvlCompare($buildingId)) { throw new \Exception('Max level reached.'); } $this->user->setGold($this->user->getGold() - $buildingCost['gold']); $this->user->setFood($this->user->getFood() - $buildingCost['food']); $update = $this->db->prepare("UPDATE\n userbuildings\n SET\n level_id = level_id + 1\n WHERE\n user_id = ?\n AND\n id = ?"); $update->execute([$_SESSION["user_id"], $buildingId]); }
public function evolve($buildingId) { $result = $this->db->prepare("\n SELECT\n id,\n building_id,\n (level_id + 1) as level,\n (SELECT gold FROM building_levels WHERE level = user_buildings.level_id + 1 AND building_id = user_buildings.building_id) as gold,\n (SELECT food FROM building_levels WHERE level = user_buildings.level_id + 1 AND building_id = user_buildings.building_id) as food,\n (SELECT name FROM buildings WHERE id = user_buildings.building_id) as name\n FROM user_buildings\n WHERE user_id = ? AND building_id = ?\n "); $result->execute([$this->user->getId(), $buildingId]); $buildingData = $result->fetch(); if ($this->user->getGold() < $buildingData['gold'] || $this->user->getFood() < $buildingData['food']) { throw new \Exception('Not enough resources'); } if ($buildingData['gold'] == null) { throw new \Exception('Building max level reached'); } $resourceUpdate = $this->db->prepare("UPDATE users SET gold = ?, food = ? WHERE id = ?"); $resourceUpdate->execute([$this->user->getGold() - $buildingData['gold'], $this->user->getFood() - $buildingData['food'], $this->user->getId()]); if ($resourceUpdate->rowCount() > 0) { $levelUpdate = $this->db->prepare("UPDATE user_buildings SET level_id = ? WHERE user_id = ? AND building_id = ?"); $levelUpdate->execute([$buildingData['level'], $this->user->getId(), $buildingId]); if ($levelUpdate->rowCount() > 0) { return true; } throw new \Exception("Something went wrong"); } throw new \Exception("Something went wrong"); }