Example #1
0
 public static function mm_update()
 {
     // Get the RSS feed from Morning Mail
     $xml = simplexml_load_file('http://morningmail.rpi.edu/rss');
     // Begin the transaction
     Database::beginTransaction();
     $count = 0;
     foreach ($xml->channel->item as $item) {
         // Check for duplicates (no DB-agnostic way
         // to ignore duplicate errors)
         if (self::find($item->link)) {
             continue;
         }
         // Parse data and construct Article objects,
         // save them to the DB
         $date = date_create($item->pubDate);
         $a = new Article($item->title, strip_tags($item->description), $date->format('Y-m-d H:i:s'), $item->link);
         // Increment row count
         $count++;
         if (!$a->save()) {
             Database::rollBack();
             return false;
         }
     }
     // Commit transaction
     Database::commit();
     return $count;
 }
 public function evolve($buildingId)
 {
     //check building
     $result = $this->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 = $this->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        ");
     $resources->execute([$this->user->getId(), $buildingId]);
     $resourcesData = $resources->fetch();
     if ($this->getUser()->getFood() < $resourcesData['food'] || $this->getUser()->getGold() < $resourcesData['gold']) {
         throw new \Exception("No resources");
     }
     //max level
     $maxLevel = $this->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 = $this->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 = $this->db->prepare("\n            UPDATE\n              users\n            SET\n              gold = gold - ?, food = food - ?\n            WHERE id = ?\n        ");
     $resourceUpdate->execute([$resourcesData['gold'], $resourcesData['food'], $this->getUser()->getId()]);
     if ($resourceUpdate->rowCount() > 0) {
         $levelUpdate = $this->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, $this->getUser()->getId(), $buildingId]);
         if ($levelUpdate->rowCount() > 0) {
             $this->db->commit();
             return true;
         } else {
             $this->db->rollBack();
             throw new \Exception("Level up error");
         }
     } else {
         throw new \Exception("Resource update error");
     }
 }
Example #3
0
 public static function handleUncaughtException(\Exception $instance)
 {
     @ob_end_clean();
     if (Database::inTransaction()) {
         Database::rollBack();
     }
     if (!$instance instanceof Error) {
         $instance = new self($instance->getMessage(), intval($instance->getCode()), $instance, $instance->getTrace());
     }
     include Template::load('Misc/Error');
     exit;
 }
Example #4
0
 public static function save()
 {
     // Clear out current clusters, TRUNCATE is implicitly
     // commited so it ruins the transaction, thats why its
     // out here
     Database::query("TRUNCATE centers;");
     // Initiate a large transaction to the DB
     Database::beginTransaction();
     // Save every cluster to the DB
     foreach (self::$clusters as $cluster) {
         if (!$cluster->save()) {
             // Undo all changes to the DB
             Database::rollBack();
             return false;
         }
     }
     // Commit the entire transaction
     Database::commit();
     return true;
 }