예제 #1
0
 public function profile()
 {
     if (!$this->isLogged()) {
         header('Location: login');
     }
     $userModel = new User();
     $userInfo = $userModel->getInfo($_SESSION['id']);
     $userViewModel = new \SoftUni\ViewModels\User($userInfo['username'], $userInfo['password'], $userInfo['id']);
     if (isset($_POST['edit'])) {
         if ($_POST['password'] != $_POST['confirm'] || empty($_POST['password'])) {
             $userViewModel->error = 1;
             return new View($userViewModel);
         }
         if ($userModel->edit($_POST['username'], $_POST['password'], $_SESSION['id'])) {
             $userViewModel->success = 1;
             $userViewModel->setUsername($_POST['username']);
             $userViewModel->setPass($_POST['password']);
             return new View($userViewModel);
         }
         $userViewModel->error = 1;
         return new View($userViewModel);
     }
     $userViewModel->pageTitle = 'Profile';
     return new View($userViewModel);
 }
예제 #2
0
 public function editUser(User $user)
 {
     $db = Database::getInstance('app');
     $result = $db->prepare("UPDATE users SET password = ?, username = ? WHERE id = ?");
     $result->execute([$user->getPass(), $user->getUsername(), $user->getId()]);
     return $result->rowCount() > 0;
 }
예제 #3
0
 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()) {
         header('Location: ../login');
         exit;
     }
     $buildingModel = new Building($this->databaseInstance);
     $viewModel = new UserBuildingsInformation();
     if (isset($_GET['id']) && is_numeric($_GET['id'])) {
         try {
             $buildingModel->evolve($_GET['id']);
         } catch (\Exception $exception) {
             $viewModel->error = $exception->getMessage();
         }
         header('Location: buildings');
         exit;
     }
     $userModel = new User($this->databaseInstance);
     $buildingData = $buildingModel->all($_SESSION['id']);
     $userData = $userModel->getInfo($_SESSION['id']);
     $viewModel->setBuildingsInfo($buildingData);
     $viewModel->setUserInfo($userData['username'], $userData['id'], $userData['gold'], $userData['food']);
     return new View('Users/buildings', $viewModel);
 }
 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);
 }
예제 #6
0
 private static function insert(User $model)
 {
     $db = Database::getInstance('app');
     $query = "INSERT INTO users (username,password,gold,food,grass) VALUES (:username, :password, :gold, :food, :grass);";
     $result = $db->prepare($query);
     $result->execute([':username' => $model->getUsername(), ':password' => $model->getPassword(), ':gold' => $model->getGold(), ':food' => $model->getFood(), ':grass' => $model->getGrass()]);
     $model->setId($db->lastId());
 }