public function create($params)
 {
     $userId = self::getUserIdByToken($params['token']);
     $x = $params['x'];
     $y = $params['y'];
     if (!$userId) {
         return Utils::formatErrorMessage(ERROR_BAD_TOKEN, "Bad token");
     }
     if (!self::isValidLanternPosition($x, $y)) {
         return Utils::formatErrorMessage(ERROR_BAD_LANTERN_POSITION, "Bad lantern position");
     }
     $sql = "SELECT id FROM lantern_users WHERE x = '" . $x . "' AND y ='" . $y . "' AND user_id ='" . $userId . "'";
     $result = $GLOBALS['app']['db']->fetchAll($sql);
     if ($result) {
         return Utils::formatErrorMessage(ERROR_LANTERN_ALREADY_EXISTS, "The lantern you try to create already exists");
     }
     $settings = file_get_contents(__DIR__ . "./../../../assets/json/buildingsSettings.json", FILE_USE_INCLUDE_PATH);
     $settings = json_decode($settings, true);
     $lanternsSettings = $settings['lanterns'];
     $resourceId = ResourcesController::getResourceIdByName($params['hard'] === 'true' ? 'spice' : $lanternsSettings['resource_price']);
     $count = $params['hard'] === 'true' ? $lanternsSettings['hard_price'] : $lanternsSettings['price'];
     $number = count(self::getLanterns($userId));
     $count *= 1 + $number;
     if (!ResourcesController::spendResource($resourceId, $count, $userId)) {
         return Utils::formatErrorMessage(ERROR_NO_MONEY, "Pas assez de resources pour cette item");
     }
     $date = date("Y-m-d H:i:s");
     $request = $GLOBALS['app']['db']->prepare('INSERT INTO lantern_users VALUES (NULL, ?, ?, ?, ?, ?)');
     $request->execute(array($userId, $date, $date, $x, $y));
     return ResourcesController::getResource($userId);
 }
 public function collect($params)
 {
     $userId = self::getUserIdByToken($params['token']);
     if (!$userId) {
         return Utils::formatErrorMessage(ERROR_BAD_TOKEN, "Bad Token");
     }
     $sql = "SELECT id\n\t\t\t\tFROM users\n\t\t\t\tWHERE username = '******'author_name'] . "'";
     $result = $GLOBALS['app']['db']->fetchAll($sql);
     echo $result;
     if (count($result) == 0) {
         return Utils::formatErrorMessage(ERROR_BAD_USERNAME, "Bad username");
     }
     $author_id = $result[0]['id'];
     $gifts = file_get_contents(__DIR__ . "./../../../assets/json/giftsSettings.json", FILE_USE_INCLUDE_PATH);
     $gifts = json_decode($gifts, true);
     $sql = "UPDATE gift_users\n\t\t\t\tSET is_collected = true\n\t\t\t\tWHERE user_id = " . $userId . "\n                    AND author_users_id = " . $author_id . "\n\t\t\t\t\tAND name = '" . $params['name'] . "'\n\t\t\t\t\tAND is_collected = false\n\t\t\t\t\tLIMIT 1";
     $result = $GLOBALS['app']['db']->exec($sql);
     if ($result == 0) {
         return Utils::formatErrorMessage(ERROR_NO_GIFT, "No gifts");
     }
     if (!array_key_exists($params['name'], $gifts)) {
         return Utils::formatErrorMessage(ERROR_BAD_GIFTS, "Bad gift name");
     }
     $gifts = $gifts[$params['name']];
     $resourceId = ResourcesController::getResourceIdByName($gifts['resource_name']);
     ResourcesController::addResource($resourceId, $gifts['resource_amount'], $userId);
     return ResourcesController::getResource($userId);
 }
 /**
  *Enlever les elses
  */
 public function createBuilding($params)
 {
     $userId = self::getUserIdByToken(addslashes($params['token']));
     if (!$userId) {
         return Utils::formatErrorMessage(ERROR_BAD_TOKEN, "Bad token");
     }
     if (!self::isValidPosition(addslashes($params['x']), addslashes($params['y']), $userId)) {
         return Utils::formatErrorMessage(ERROR_BAD_POSITION, "Cette position est prise ou pas à portée d'une lanterne");
     }
     if (!BuildingModel::isBuildingExist($params['name'])) {
         return Utils::formatErrorMessage(ERROR_BUILDING_NAME_INCORRECT, "Le nom du building est incorrect");
     }
     if (!self::buyBuilding($params['name'], $userId)) {
         return Utils::formatErrorMessage(ERROR_NO_MONEY, "Not enough gold");
     }
     $buildingID = BuildingModel::getBuildingTypeIdByName($params['name']);
     //echo($result);
     //$result = 0;
     switch ($params['name']) {
         case 'rocket_factory':
             $date = date("Y-m-d H:i:s");
             $lvl = 1;
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_rocket_factory VALUES (NULL, ?, ?, ?)');
             $request->execute(array($userId, $lvl, $date));
             break;
         case 'temple':
             $pinataReady = date("Y-m-d H:i:s");
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_temple VALUES (NULL, ?, ?)');
             $request->execute(array($userId, $pinataReady));
             break;
         case 'bar':
             $date = date("Y-m-d H:i:s");
             $lvl = 1;
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_bar VALUES (NULL, ?, ?, ?)');
             $request->execute(array($userId, $lvl, $date));
             break;
         case 'brothel':
             if (!BuildingModel::getBuildings($buildingID, $userId)) {
                 return Utils::formatErrorMessage(ERROR_BUILDING_NAME_INCORRECT, "Building unique déja construit");
             }
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_brothel VALUES (NULL, ?)');
             $request->execute(array($userId));
             break;
         case 'main_square':
             if (!BuildingModel::getBuildings($buildingID, $userId)) {
                 return Utils::formatErrorMessage(ERROR_BUILDING_NAME_INCORRECT, "Building unique déja construit");
             }
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_main_square VALUES (NULL, ?)');
             $request->execute(array($userId));
             break;
         case 'pyrotechnician':
             $date = date("Y-m-d H:i:s");
             $lvl = 1;
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_pyrotechnician VALUES (NULL, ?, ?, ?)');
             $request->execute(array($userId, $lvl, $date));
             break;
         case 'city_hall':
             if (!BuildingModel::getBuildings($buildingID, $userId)) {
                 return Utils::formatErrorMessage(ERROR_BUILDING_NAME_INCORRECT, "Building unique déja construit");
             }
             $date = date("Y-m-d H:i:s");
             $lvl = 1;
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_city_hall VALUES (NULL, ?, ?, ?)');
             $request->execute(array($userId, $lvl, $date));
             break;
         case 'church':
             if (!BuildingModel::getBuildings($buildingID, $userId)) {
                 return Utils::formatErrorMessage(ERROR_BUILDING_NAME_INCORRECT, "Building unique déja construit");
             }
             $date = date("Y-m-d H:i:s");
             $lvl = 1;
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_church VALUES (NULL, ?, ?, ?)');
             $request->execute(array($userId, $lvl, $date));
             break;
         case 'cantina':
             $date = date("Y-m-d H:i:s");
             $lvl = 1;
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_cantina VALUES (NULL, ?, ?, ?)');
             $request->execute(array($userId, $lvl, $date));
             break;
         case 'gift_shop':
             if (!BuildingModel::getBuildings($buildingID, $userId)) {
                 return Utils::formatErrorMessage(ERROR_BUILDING_NAME_INCORRECT, "Building unique déja construit");
             }
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_gift_shop VALUES (NULL, ?)');
             $request->execute(array($userId));
             break;
         default:
             $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_' . $params['name'] . ' VALUES (NULL, ?)');
             $request->execute(array($userId));
             break;
     }
     $building_id = $GLOBALS['app']['db']->lastInsertId();
     $building_type_id = BuildingModel::getBuildingTypeIdByName($params['name']);
     $date = date("Y-m-d H:i:s");
     $color = "A";
     //Les couleurs sont d'une lettres, en uppercase
     $constructTime = BuildingModel::getConstructTime($params['name']);
     $construct_end = date("Y-m-d H:i:s", strtotime("+" . $constructTime . " hour", strtotime($date)));
     $request = $GLOBALS['app']['db']->prepare('INSERT INTO building_users VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
     $request->execute(array($userId, $date, $date, $building_type_id, $building_id, addslashes($params['x']), addslashes($params['y']), $construct_end, $color));
     return ResourcesController::getResource($userId);
 }
 public function paradeUpdate($params)
 {
     $userId = self::getUserIdByToken(addslashes($params['token']));
     if (!$userId) {
         return Utils::formatErrorMessage(ERROR_BAD_TOKEN, "Bad token");
     }
     $params['hardPurchase'] = $params['hardPurchase'] == 'true';
     $paradeSettings = file_get_contents(__DIR__ . "./../../../assets/json/paradeSettings.json", FILE_USE_INCLUDE_PATH);
     $paradeSettings = json_decode($paradeSettings, true);
     $lanterns = LanternsController::getLanterns($userId);
     $maxBonus = $paradeSettings['generatedBonusCountPerLanterns'] * count($lanterns);
     if ($params['bonusHarvested'] > $maxBonus) {
         return Utils::formatErrorMessage(ERROR_BONUS_SENT_TOO_BIG, "Bonus sent is too big");
     }
     $offeringResourceId = ResourcesController::getResourceIdByName("offering");
     if (!$params['hardPurchase']) {
         $sql = "SELECT last_parade_at FROM users WHERE id = '" . $userId . "'";
         $result = $GLOBALS['app']['db']->fetchAll($sql);
         $lastParadeAt = new DateTime($result[0]['last_parade_at']);
         $now = new DateTime();
         // Verification de si la parade n'a pas eu lieu aujourd'hui
         if ($lastParadeAt->format('d') != $now->format('d') || $now->getTimestamp() - $lastParadeAt->getTimestamp() >= 86400) {
             $sql = "UPDATE users\n\t\t\t\t\t\tSET last_parade_at = NOW()\n\t\t\t\t\t\tWHERE id = '" . $userId . "'";
             $result = $GLOBALS['app']['db']->exec($sql);
             $goldGained = $this->convertBonusToGold($params['bonusHarvested'], $paradeSettings, $userId);
             ResourcesController::addResource($offeringResourceId, $goldGained, $userId);
         } else {
             return Utils::formatErrorMessage(ERROR_PARADE_ALREADY_LAUNCH, "Parade already launched today");
         }
     } else {
         $resourceId = ResourcesController::getResourceIdByName("spice");
         if (ResourcesController::spendResource($resourceId, $paradeSettings['paradeHardPrice'], $userId)) {
             $goldGained = $this->convertBonusToGold($params['bonusHarvested'], $paradeSettings, $userId);
             ResourcesController::addResource($offeringResourceId, $goldGained, $userId);
         } else {
             return Utils::formatErrorMessage(ERROR_NO_MONEY, "Not enought spice");
         }
     }
     return ResourcesController::getResource($userId);
 }