function Buildings()
 {
     data::initialize();
     $this->planet_id = current_planet();
     $this->building_id = isset($_REQUEST['building_id']) ? (int) $_REQUEST['building_id'] : NULL;
     permissions_check(PERMISSION_PLANET, $this->planet_id, array('build', 'commission'));
     $this->data->updater->update(0, $this->planet_id);
     $this->quiet = false;
 }
 function upgrade()
 {
     $planet_id = $this->planet_id;
     if (isset($_POST['mode']) && $_POST['mode'] == 'js') {
         $output_mode = 'javascript';
     } else {
         $output_mode = '';
     }
     permissions_check(PERMISSION_PLANET, $planet_id, 'research');
     $designs['main'] = array(0 => 'army', 1 => 'navy', 2 => 'weapon');
     foreach ($designs['main'] as $key => $value) {
         if (!empty($_POST[$value . 'design_id'])) {
             $design_id = (int) $_POST[$value . 'design_id'];
             $design_name = $value;
             $type = $key;
             break;
         }
     }
     $attribute = $_POST['attribute'];
     if (empty($design_name) || empty($attribute)) {
         $status[] = 'No upgrade selected.';
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     if ($attribute != mysql_real_escape_string($attribute)) {
         error(__FILE__, __LINE__, 'DATA_INVALID', 'Invalid attribute selected');
     }
     $db_query = "\n\t\t\t\tSELECT \n\t\t\t\t\t`time`, \n\t\t\t\t\t`workers`, \n\t\t\t\t\t`energy`, \n\t\t\t\t\t`minerals`, \n\t\t\t\t\t`mineralspread`, \n\t\t\t\t\t`techlevel_current`, \n\t\t\t\t\t`techlevel_max`, \n\t\t\t\t\t`" . $attribute . "_base`, \n\t\t\t\t\t`" . $attribute . "_max`, \n\t\t\t\t\t`" . $attribute . "_inc`, \n\t\t\t\t\t`" . $attribute . "_per`, \n\t\t\t\t\t`" . $attribute . "_size`, \n\t\t\t\t\t`" . $attribute . "_sizeinc`, \n\t\t\t\t\t`size_base`, \n\t\t\t\t\t`size_max` \n\t\t\t\tFROM \n\t\t\t\t\t`" . $design_name . "designs` \n\t\t\t\tWHERE \n\t\t\t\t\t`kingdom_id` = '" . $_SESSION['kingdom_id'] . "' AND \n\t\t\t\t\t`" . $design_name . "design_id` = '" . $design_id . "' \n\t\t\t\tLIMIT 1";
     $db_result = mysql_query($db_query);
     if (mysql_num_rows($db_result) == 0) {
         error(__FILE__, __LINE__, 'DATA_INVALID', 'Invalid design selected');
     }
     $design = mysql_fetch_array($db_result, MYSQL_ASSOC);
     $design['mineralspread'] = unserialize($design['mineralspread']);
     if ($design['techlevel_current'] >= $design['techlevel_max']) {
         $status[] = 'The tech level has been maxed out.';
     }
     if ($design[$attribute . '_base'] == $design[$attribute . '_max']) {
         $status[] = 'That attribute has been maxed out.';
     }
     if (!empty($status)) {
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     $db_query = "SELECT `planet_id` FROM `tasks` WHERE `kingdom_id` = '" . $_SESSION['kingdom_id'] . "' AND `type` IN ('2', '3') AND (`planet_id` = '" . $planet_id . "' OR `design_id` = '" . $design_id . "') LIMIT 2";
     $db_result = mysql_query($db_query);
     while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
         if ($db_row['planet_id'] == $planet_id) {
             $status[] = 'That planet is already researching something.';
         } else {
             $status[] = 'That design is already being researched elsewhere';
         }
     }
     if (!empty($status)) {
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     $db_query = "SELECT `player_id`, `workers`, `energy`, `minerals`, `researchbonus` FROM `planets` WHERE `planet_id` = '" . $planet_id . "' LIMIT 1";
     $db_result = mysql_query($db_query);
     $planet = mysql_fetch_array($db_result, MYSQL_ASSOC);
     $planet['minerals'] = unserialize($planet['minerals']);
     $resources = array('workers', 'energy');
     foreach ($resources as $value) {
         $planet[$value] -= $design[$value];
         if ($planet[$value] < 0) {
             $status[] = 'Not enough ' . $value . '.';
         }
     }
     if (!empty($design['mineralspread'])) {
         $mineralnames = array(0 => 'fe', 1 => 'o', 2 => 'si', 3 => 'mg', 4 => 'ni', 5 => 's', 6 => 'he', 7 => 'h');
         foreach ($design['mineralspread'] as $key => $value) {
             $mineral = $value / 100 * $design['minerals'];
             $planet['minerals'][$key] -= $mineral;
             if ($planet['minerals'][$key] < 0) {
                 $status[] = 'Not enough ' . $mineralnames[$key] . '.';
             }
         }
     }
     if (!empty($status)) {
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     $completion = $design['time'] * $_SESSION['round_speed'] * ((100 - $planet['researchbonus']) / 100);
     $warptime = request_variable('warptime');
     if (!is_null($warptime)) {
         data::initialize();
         $data_planet =& $this->data->planet($planet_id);
         if ($data_planet['warptime_research'] > $completion) {
             $data_planet['warptime_research'] -= $completion;
             $completion = 0;
         } else {
             $completion -= $data_planet['warptime_research'];
             $data_planet['warptime_research'] = 0;
         }
         $this->data->save();
     }
     $insert_design = array('round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'planet_id' => $planet_id, 'type' => 3, 'design_id' => $design_id, 'attribute' => $attribute, 'number' => $type, 'start' => microfloat(), 'completion' => microfloat() + $completion);
     $db_result = $this->sql->execute('tasks', $insert_design);
     $this->sql->set(array(array('planets', 'researching', 1), array('planets', 'workers', $planet['workers']), array('planets', 'energy', $planet['energy']), array('planets', 'minerals', serialize($planet['minerals']))));
     $this->sql->where(array('planets', 'planet_id', $planet_id));
     $this->sql->limit(1);
     $db_result = $this->sql->execute();
     if ($output_mode == 'javascript') {
         echo 'varUpgrading = true; varError = false;';
         exit;
     }
     $_SESSION['status'][] = 'Upgrade successfully started.';
     redirect('designs.php');
 }
 function IK_Smarty()
 {
     data::initialize();
 }
 function alerts()
 {
     data::initialize();
 }
 function Updater_Round()
 {
     data::initialize();
     $this->combat =& new Updater_Combat();
 }