public function getBuildings() { $db = Database::getInstance(DatabaseConfig::DB_INSTANCE); $result = $db->prepare("\n SELECT b.id, b.name, bl.level,\n (SELECT gold FROM building_levels WHERE building_id = ub.building_id AND level = (SELECT level FROM building_levels WHERE id = ub.level_id) + 1) AS gold,\n (SELECT food FROM building_levels WHERE building_id = ub.building_id AND level = (SELECT level FROM building_levels WHERE id = ub.level_id) + 1) AS food\n FROM user_buildings as ub\n INNER JOIN buildings AS b ON b.id = ub.building_id\n INNER JOIN building_levels AS bl ON bl.id = ub.level_id\n WHERE ub.user_id = ?;\n "); $result->execute([Session::get('id')]); $data = $result->fetchAll(); return $data; }
public function evolve($buildingId) { $db = Database::getInstance(DatabaseConfig::DB_INSTANCE); //check building $result = $db->prepare("SELECT id FROM buildings WHERE id = ?"); $result->execute([$buildingId]); if ($result->rowCount() < 0) { throw new \Exception("Building with such id does not exists"); } //get resources $resources = $db->prepare("\n SELECT\n (SELECT gold FROM building_levels WHERE building_id = b.id AND level = (SELECT level FROM building_levels WHERE id = ub.level_id) + 1) AS gold,\n (SELECT food FROM building_levels WHERE building_id = b.id AND level = (SELECT level FROM building_levels WHERE id = ub.level_id) + 1) AS food\n FROM buildings as b\n INNER JOIN user_buildings AS ub ON ub.building_id = b.id\n INNER JOIN building_levels AS bl ON bl.id = ub.level_id\n WHERE ub.user_id = ? AND b.id = ?;\n "); $userModel = new User(); $userInfo = $userModel->getInfo(Session::get('id')); $userInfo = new UserInformation($userInfo['username'], $userInfo['id'], $userInfo['gold'], $userInfo['food']); $resources->execute([$userInfo->getId(), $buildingId]); $resourcesData = $resources->fetch(); if ($userInfo->getFood() < $resourcesData['food'] || $userInfo->getGold() < $resourcesData['gold']) { throw new \Exception("No resources"); } //max level $maxLevel = $db->prepare("\n SELECT\n MAX(bl.level) AS level\n FROM building_levels bl\n WHERE bl.building_id = ?\n "); $maxLevel->execute([$buildingId]); $maxLevelData = $maxLevel->fetch(); //current level $currentLevel = $db->prepare("\n SELECT\n bl.level\n FROM user_buildings ub\n JOIN building_levels bl ON bl.id = ub.level_id\n WHERE ub.building_id = ?\n "); $currentLevel->execute([$buildingId]); $currentLevelData = $currentLevel->fetch(); if ($maxLevelData['level'] < $currentLevelData['level']) { throw new \Exception("Max level reached"); } $this->db->beginTransaction(); $resourceUpdate = $db->prepare("\n UPDATE\n users\n SET\n gold = gold - ?, food = food - ?\n WHERE id = ?\n "); $resourceUpdate->execute([$resourcesData['gold'], $resourcesData['food'], $userInfo->getId()]); if ($resourceUpdate->rowCount() > 0) { $levelUpdate = $db->prepare("\n UPDATE\n user_buildings ub\n SET\n ub.level_id = (SELECT bl.id FROM building_levels bl WHERE level = ? AND bl.building_id = ub.building_id)\n WHERE ub.user_id = ? AND ub.building_id = ?\n "); $levelUpdate->execute([$currentLevelData['level'] + 1, $userInfo->getId(), $buildingId]); if ($levelUpdate->rowCount() > 0) { $db->commit(); return true; } else { $db->rollBack(); throw new \Exception("Level up error"); } } else { throw new \Exception("Resource update error"); } }
public function buildings() { if (!$this->isLogged()) { RouteService::redirect('users', 'login', true); } $viewModel = new BuildingsInformation(); $userModel = new User(); $buildings = $userModel->getBuildings(); $userInfo = $userModel->getInfo(Session::get('id')); $viewModel->user = new UserInformation($userInfo['username'], $userInfo['id'], $userInfo['gold'], $userInfo['food']); $viewModel->buildings = $buildings; return new View($viewModel); }
<?php require_once 'Autoloader.php'; \SoftUni\Autoloader::register(); \SoftUni\Helpers\Session::start(); $phpSelf = $_SERVER['PHP_SELF']; $index = basename($phpSelf); \SoftUni\Helpers\RouteService::init(str_replace($index, '', $phpSelf)); $requestParams = []; $controller = "users"; $action = "login"; if (isset($_GET['uri'])) { $requestParams = explode('/', $_GET['uri']); $controller = ucfirst(array_shift($requestParams)); $action = ucfirst(array_shift($requestParams)); } \SoftUni\Core\Database::setInstance(\SoftUni\Config\DatabaseConfig::DB_INSTANCE, \SoftUni\Config\DatabaseConfig::DB_DRIVER, \SoftUni\Config\DatabaseConfig::DB_USER, \SoftUni\Config\DatabaseConfig::DB_PASSWORD, \SoftUni\Config\DatabaseConfig::DB_NAME, \SoftUni\Config\DatabaseConfig::DB_HOST); $app = new \SoftUni\Application($controller, $action, $requestParams); $app->start();
protected function isLogged() { return Session::get('id') != null; }