function reports_view()
{
    global $smarty, $sql;
    if (!empty($_REQUEST['combatreport_id'])) {
        $combatreport_id = abs((int) $_REQUEST['combatreport_id']);
    }
    $sql->select(array('combatreports', 'report'));
    $sql->where(array(array('combatreports', 'combatreport_id', $combatreport_id), array('combatreports', 'round_id', $_SESSION['round_id']), array('combatreports', 'kingdom_id', $_SESSION['kingdom_id'])));
    $sql->limit(1);
    $db_result = $sql->execute();
    if (!$db_result || mysql_num_rows($db_result) == 0) {
        error(__FILE__, __LINE__, 'INVALID_COMBATREPORT', 'Invalid combat report specified.');
    }
    $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
    $combatreport = unserialize($db_row['report']);
    $combatreport['header']['date'] = format_timestamp($combatreport['header']['date']);
    foreach ($combatreport['names'] as $category => $names) {
        if ($category == 'kingdoms' || $category == 'weapons') {
            foreach ($names as $id => $name) {
                $combatreport['names'][$category][$id] = strshort($name, 20, '<span title="' . $name . '">...</span>');
            }
        } else {
            foreach ($names as $type => $more_names) {
                foreach ($more_names as $id => $name) {
                    $combatreport['names'][$category][$type][$id] = strshort($name, 20, '<span title="' . $name . '">...</span>');
                }
            }
        }
    }
    // Format all the numbers
    foreach ($combatreport['details'] as $kingdom_id => $types) {
        foreach ($types as $type => $groups) {
            foreach ($groups as $group_id => $units) {
                foreach ($units as $unit_id => $weapons) {
                    foreach ($weapons as $weapon_id => $target) {
                        foreach ($target as $detail_id => $details) {
                            $combatreport['details'][$kingdom_id][$type][$group_id][$unit_id][$weapon_id][$detail_id]['hits'] = format_number($combatreport['details'][$kingdom_id][$type][$group_id][$unit_id][$weapon_id][$detail_id]['hits'], true);
                            $combatreport['details'][$kingdom_id][$type][$group_id][$unit_id][$weapon_id][$detail_id]['damage'] = format_number($combatreport['details'][$kingdom_id][$type][$group_id][$unit_id][$weapon_id][$detail_id]['damage'], true);
                            $combatreport['details'][$kingdom_id][$type][$group_id][$unit_id][$weapon_id][$detail_id]['kills'] = format_number($combatreport['details'][$kingdom_id][$type][$group_id][$unit_id][$weapon_id][$detail_id]['kills'], true);
                        }
                    }
                }
            }
        }
    }
    $smarty->assign('combatreport', $combatreport);
    $smarty->display('reports_view.tpl');
}
 function view()
 {
     $group = $this->data->group($this->group_type, $this->group_id);
     $group_permissions = permissions_check(constant('PERMISSION_' . strtoupper($this->group_type)), $this->group_id, 'military');
     // Viewing->Units
     // Permission: Group & Planet & Group Owner = Planet Owner
     // Viewing->Cargo
     // Permission: Group & Planet & Group Owner = Planet Owner
     // Viewing->Destination
     // Permission: Group
     // Viewing->Targets
     // Permission: Group
     $onplanet = false;
     if (in_array($this->group_view, array('units', 'cargo')) && ($this->group_type == 'army' && !empty($group['planet_id']) || $this->group_type == 'navy' && $group['x_current'] == $group['x_destination'] && $group['y_current'] == $group['y_destination'])) {
         $planet = $this->data->planet($group['planet_id']);
         // Do they have permission to touch anything on this planet?
         $planet_permissions = permissions_check(PERMISSION_PLANET, $group['planet_id'], 'military', false);
         if (!empty($planet)) {
             $onplanet = true;
             $this->smarty->assign('onplanet', true);
         }
     }
     if (empty($planet['planet_id'])) {
         $planet['planet_id'] = '';
     }
     // ##################################################
     // Viewing->Units
     if ($this->group_view == 'units') {
         $group_empty = $this->isGroupEmpty($group);
         $units = $group['units'];
         if ($onplanet && $planet_permissions['owner'] && !empty($planet['units'][$this->group_type])) {
             $units = $units + $planet['units'][$this->group_type];
         }
         $units = $this->data->blueprint($this->group_type, array_keys($units));
         $unitlist = array();
         foreach ($units as $unit_id => $unit) {
             $unitlist[$unit_id]['name'] = htmlentities($unit['name']);
             // Units on planet
             if ($planet_permissions['owner'] && !empty($planet['units'][$this->group_type][$unit_id])) {
                 $unitlist[$unit_id]['planet'] = format_number($planet['units'][$this->group_type][$unit_id]);
             }
             // Units in group
             if (!empty($group['units'][$unit_id])) {
                 $unitlist[$unit_id]['group'] = format_number($group['units'][$unit_id]);
             }
             // Unit size/cargo capacity
             if ($this->group_type == 'army') {
                 $unitlist[$unit_id]['size'] = format_number($unit['size'], true);
             } else {
                 $unitlist[$unit_id]['cargo'] = format_number($unit['cargo'], true);
             }
         }
         $this->smarty->assign('group_empty', $group_empty);
         $this->smarty->assign('units', $unitlist);
     } elseif ($this->group_view == 'cargo' && $this->group_type == 'navy') {
         if (empty($group['cargo']['food'])) {
             $group['cargo']['food'] = 0;
         }
         if (empty($group['cargo']['workers'])) {
             $group['cargo']['workers'] = 0;
         }
         if (empty($group['cargo']['energy'])) {
             $group['cargo']['energy'] = 0;
         }
         if (empty($group['cargo']['minerals'])) {
             $group['cargo']['minerals'] = array();
         }
         $resources['group'] = array('food' => $group['cargo']['food'], 'workers' => $group['cargo']['workers'], 'energy' => $group['cargo']['energy']);
         $mineralnames = array(0 => 'fe', 1 => 'o', 2 => 'si', 3 => 'mg', 4 => 'ni', 5 => 's', 6 => 'he', 7 => 'h');
         foreach ($group['cargo']['minerals'] as $key => $value) {
             $resources['group']['minerals'][$mineralnames[$key]] = $value;
         }
         // If on planet group owns
         if ($onplanet && $planet_permissions['grant'] && $planet['player_id'] == $group['player_id']) {
             $resources['planet'] = array('food' => format_number($planet['food'], true), 'workers' => format_number($planet['workers'], true), 'energy' => format_number($planet['energy'], true));
             foreach ($planet['minerals'] as $key => $value) {
                 $resources['planet']['minerals'][$mineralnames[$key]] = format_number($value, true);
             }
         }
         $this->sql->select(array(array('armygroups', 'armygroup_id'), array('armygroups', 'name'), array('armygroups', 'size'), array('armygroups', 'navygroup_id')));
         $this->sql->leftjoin(array('navygroups', 'navygroup_id', array('armygroups', 'navygroup_id')));
         $this->sql->where(array(array('armygroups', 'round_id', $_SESSION['round_id']), array('armygroups', 'kingdom_id', $_SESSION['kingdom_id'])));
         if ($onplanet) {
             $this->sql->leftjoin(array('planets', 'planet_id', array('armygroups', 'planet_id')));
             $this->sql->select(array('planets', 'planet_id'));
         }
         $db_query = $this->sql->generate();
         if ($onplanet) {
             $db_query .= " AND (`armygroups`.`planet_id` = '" . $planet['planet_id'] . "' OR `armygroups`.`navygroup_id` = '" . $group['navygroup_id'] . "')";
         } else {
             $db_query .= " AND `armygroups`.`navygroup_id` = '" . $group['navygroup_id'] . "'";
         }
         $db_result = mysql_query($db_query);
         $grouplist = array();
         while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
             $grouplist[$db_row['armygroup_id']] = array('group_id' => $db_row['armygroup_id'], 'name' => strshort($db_row['name'], 20), 'size' => format_number($db_row['size'], true));
             if (!empty($db_row['navygroup_id'])) {
                 $grouplist[$db_row['armygroup_id']]['location'] = 'group';
             } else {
                 $grouplist[$db_row['armygroup_id']]['location'] = 'planet';
             }
         }
         $this->smarty->assign('groups', $grouplist);
         $this->smarty->assign('resources', $resources);
     } elseif ($this->group_view == 'destination' && $this->group_type == 'navy') {
     } elseif ($this->group_view == 'targets') {
         $this->sql->select(array($this->group_type . 'blueprints', 'weapons'));
         $this->sql->where(array($this->group_type . 'blueprints', $this->group_type . 'blueprint_id', array_keys($group['units']), 'IN'));
         $db_result = $this->sql->execute();
         $weapons = array();
         while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
             $db_row['weapons'] = unserialize($db_row['weapons']);
             if (is_array($db_row['weapons'])) {
                 $weapons = $weapons + $db_row['weapons'];
             }
         }
         if (!empty($weapons)) {
             $this->sql->select(array(array('weaponblueprints', 'weaponblueprint_id', 'weapon_id'), array('weaponblueprints', 'name')));
             $this->sql->where(array('weaponblueprints', 'weaponblueprint_id', array_keys($weapons), 'IN'));
             $db_result = $this->sql->execute();
             $weapons = array();
             while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
                 $weapons[$db_row['weapon_id']] = array('weapon_id' => $db_row['weapon_id'], 'name' => $db_row['name']);
                 if (isset($group['targets'][$db_row['weapon_id']])) {
                     $weapons[$db_row['weapon_id']]['target_id'] = $group['targets'][$db_row['weapon_id']];
                 }
             }
             if (!empty($weapons)) {
                 $this->sql->select(array(array($this->group_type . 'concepts', $this->group_type . 'concept_id', 'target_id'), array($this->group_type . 'concepts', 'name')));
                 $this->sql->orderby(array($this->group_type . 'concepts', $this->group_type . 'concept_id', 'asc'));
                 $db_result = $this->sql->execute();
                 $targets = array();
                 while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
                     $targets[$db_row['target_id']] = array('target_id' => $db_row['target_id'], 'name' => $db_row['name']);
                 }
                 $this->smarty->assign('targets', $targets);
                 $this->smarty->assign('weapons', $weapons);
             }
         }
     }
     $this->smarty->display('groups_view.tpl');
 }
function planet_massmanage()
{
    global $smarty, $sql, $data;
    // get all planets owned by this player
    // picture, name, food storage & rate, worker storage & rate, energy storage & rate, mineral storage & rate,
    // [ ] The Past	  1.2K  +8   1.1K  +40   872  +48   0  0   Derrick   1h, 38m
    $available_planning = $available_cranes = 0;
    // Retrieve all of the player's planets
    $player = $data->player($_SESSION['player_id']);
    $planet_ids = $player['planets'];
    $planet_ids = array_keys($planet_ids);
    $planets = $data->planet($planet_ids);
    if (!empty($planets)) {
        $db_query = "\n\t\t\t\tSELECT \n\t\t\t\t\tt.`planet_id`, \n\t\t\t\t\tb.`name`, \n\t\t\t\t\tMIN(t.`completion`) as 'completion' \n\t\t\t\tFROM \n\t\t\t\t\t`tasks` t, \n\t\t\t\t\t`buildings` b \n\t\t\t\tWHERE \n\t\t\t\t\tt.`planet_id` IN ('" . implode("', '", array_keys($planets)) . "') AND \n\t\t\t\t\tb.`building_id` = t.`building_id` \n\t\t\t\tGROUP BY t.`planet_id` \n\t\t\t\tLIMIT " . count($planets);
        $db_result = $sql->query($db_query);
        while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
            $planets[$db_row['planet_id']]['construction'] = array('name' => strshort($db_row['name'], 15), 'completion' => format_time(timeparser($db_row['completion'] - microfloat())));
        }
        $db_query = "\n\t\t\t\tSELECT \n\t\t\t\t\tt.`planet_id`, \n\t\t\t\t\tc.`name`, \n\t\t\t\t\tMIN(t.`completion`) as 'completion' \n\t\t\t\tFROM \n\t\t\t\t\t`tasks` t, \n\t\t\t\t\t`concepts` c \n\t\t\t\tWHERE \n\t\t\t\t\tt.`planet_id` IN ('" . implode("', '", array_keys($planets)) . "') AND \n\t\t\t\t\tc.`concept_id` = t.`concept_id` \n\t\t\t\tGROUP BY t.`planet_id` \n\t\t\t\tLIMIT " . count($planets);
        $db_result = $sql->query($db_query);
        while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
            $planets[$db_row['planet_id']]['research'] = array('name' => strshort($db_row['name'], 15), 'completion' => format_time(timeparser($db_row['completion'] - microfloat())));
        }
    }
    // Sort planets by cranes and then score, desc.
    $sort_key = array('cranes' => array('busy_planets' => array(), 'idle_planets' => array()), 'score' => array('busy_planets' => array(), 'idle_planets' => array()));
    $idle_planets = $busy_planets = array();
    foreach ($planets as $planet_id => $planet) {
        if (!empty($planet['minerals'])) {
            $planet['minerals'] = array_sum($planet['minerals']);
        } else {
            $planet['minerals'] = 0;
        }
        $resource_deficiency = 0;
        foreach (array('food', 'workers', 'energy', 'minerals') as $resource) {
            if ($resource_deficiency != 0) {
                $planet[$resource . 'deficiency'] = $planet[$resource . 'rate'];
            }
            $planet[$resource . 'rate'] += $resource_deficiency;
            if ($planet[$resource . 'rate'] < 0 && $planet[$resource] < abs($planet[$resource . 'rate'])) {
                $resource_deficiency = $planet[$resource . 'rate'] + $planet[$resource];
            }
            $planet[$resource] = format_number($planet[$resource], true);
            $planet[$resource . 'rate'] = format_number($planet[$resource . 'rate'], true, true);
        }
        if ($planet['planning'] > $available_planning && $planet['cranes'] > 0) {
            $available_planning = $planet['planning'];
        }
        if ($planet['cranes'] > $available_cranes) {
            $available_cranes = $planet['cranes'];
        }
        $planets[$planet['planet_id']] = $planet;
        if ($planet['cranes'] == 0) {
            $busy_planets[$planet['planet_id']] =& $planets[$planet['planet_id']];
            $sort_key['cranes']['busy_planets'][$planet_id] = $planet['cranes'];
            $sort_key['score']['busy_planets'][$planet_id] = $planet['score'];
        } else {
            $idle_planets[$planet['planet_id']] =& $planets[$planet['planet_id']];
            $sort_key['cranes']['idle_planets'][$planet_id] = $planet['cranes'];
            $sort_key['score']['idle_planets'][$planet_id] = $planet['score'];
        }
    }
    array_multisort($sort_key['cranes']['busy_planets'], SORT_DESC, $sort_key['score']['busy_planets'], SORT_DESC, $busy_planets);
    array_multisort($sort_key['cranes']['idle_planets'], SORT_DESC, $sort_key['score']['idle_planets'], SORT_DESC, $idle_planets);
    $kingdom = $data->kingdom($_SESSION['kingdom_id']);
    $db_query = "SELECT `building_id`, `name` FROM `buildings` WHERE `building_id` IN ('" . implode("', '", array_keys($kingdom['buildings'])) . "') ORDER BY `name` ASC";
    $db_result = $sql->query($db_query);
    $buildings = array();
    while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
        $buildings[$db_row['building_id']] = $db_row['name'];
    }
    $smarty->assign('available_cranes', $available_cranes);
    $smarty->assign('available_planning', $available_planning);
    $smarty->assign('buildings', $buildings);
    $smarty->assign('planets', $planets);
    $smarty->assign('idle_planets', $idle_planets);
    $smarty->assign('idle_planet_count', count($idle_planets));
    $smarty->assign('busy_planets', $busy_planets);
    $smarty->display('planet_massmanage.tpl');
}
 function planet()
 {
     // If requesting a specific / relative planet
     if (isset($_REQUEST['planet_id'])) {
         $planet_id = $_REQUEST['planet_id'];
         // Let's duplicate our effort for sake of simplicity. We can always refactor later.
         if (in_array($planet_id, array('previous', 'next'))) {
             $planetList = 'own';
             $action = $planet_id;
             $player =& $this->data->player($_SESSION['player_id']);
             $planets = $player['planets'];
             $planet_current = $player['planet_current'];
             $planet_id = $this->get_adjacent_planet($planets, $planet_current, $action);
             $this->set_current_planet($player, $planet_id, $planetList);
         } else {
             if (in_array($planet_id, array('previous_permissions', 'next_permissions'))) {
                 $planetList = 'permission';
                 if ($planet_id == 'previous_permissions') {
                     $action = 'previous';
                 } else {
                     $action = 'next';
                 }
                 permissions_update_planets($_SESSION['player_id']);
                 $player =& $this->data->player($_SESSION['player_id']);
                 $planets = $player['planets_permissions'];
                 $planet_current = $player['planet_permission_current'];
                 if (empty($planets)) {
                     $this->smarty->append('status', 'You do not have permission to access any other planets');
                     $this->smarty->display('error.tpl');
                     exit;
                 }
                 $planet_id = $this->get_adjacent_planet($planets, $planet_current, $action);
                 $this->set_current_planet($player, $planet_id, $planetList);
             } else {
                 $planet_id = abs((int) $planet_id);
             }
         }
         $_SESSION['planet_id'] = $planet_id;
     } else {
         $player =& $this->data->player($_SESSION['player_id']);
         $planet_id = $player['planet_current'];
     }
     $this->data->updater->update(0, $planet_id);
     $permissions = permissions_check(PERMISSION_PLANET, $planet_id);
     $round = $this->data->round();
     $planet = $this->data->planet($planet_id);
     if (!empty($planet['player_id'])) {
         $player = $this->data->player($planet['player_id']);
         $planet['player_name'] = strshort($player['name'], 15);
         if ($player['npc'] == 1) {
             $planet['npc_player'] = true;
         }
     }
     if (!empty($planet['kingdom_id'])) {
         $kingdom = $this->data->kingdom($planet['kingdom_id']);
         $planet['kingdom_name'] = strshort($kingdom['name'], 15);
     }
     $planet['permissions'] = $permissions;
     if ($planet['player_id'] == $_SESSION['player_id']) {
         $_SESSION['planet_id'] = $planet['planet_id'];
     }
     $planet['score'] = format_number($planet['score']);
     $planet['score_peak'] = format_number($planet['score_peak']);
     if ($permissions['grant']) {
         if (!empty($planet['minerals'])) {
             $planet['minerals'] = array_sum($planet['minerals']);
         } else {
             $planet['minerals'] = 0;
         }
         $resource_deficiency = 0;
         foreach (array('food', 'workers', 'energy', 'minerals') as $resource) {
             if ($resource_deficiency != 0) {
                 $planet[$resource . 'deficiency'] = $planet[$resource . 'rate'];
             }
             $planet[$resource . 'rate'] += $resource_deficiency;
             if ($planet[$resource . 'rate'] < 0 && $planet[$resource] < abs($planet[$resource . 'rate'])) {
                 $resource_deficiency = $planet[$resource . 'rate'] + $planet[$resource];
             }
             $planet[$resource] = format_number($planet[$resource], true);
             $planet[$resource . 'rate'] = format_number($planet[$resource . 'rate'], true, true);
         }
         if ($permissions['build']) {
             $types[] = 1;
         }
         if ($permissions['research']) {
             $types[] = 2;
             $types[] = 3;
         }
         if ($permissions['commission']) {
             $types[] = 4;
         }
         unset($planet['researching'], $planet['building'], $planet['army'], $planet['navy']);
         // Get tasks currently running on the planet
         $db_query = "SELECT DISTINCT `task_id`, `attribute`, `number`, `type` FROM `tasks` WHERE `round_id` = '" . $_SESSION['round_id'] . "' AND `planet_id` = '" . $planet_id . "' AND `type` IN ('" . implode("', '", $types) . "') ORDER BY `completion` DESC";
         $db_result_tasks = mysql_query($db_query);
         while ($tasks = mysql_fetch_array($db_result_tasks, MYSQL_ASSOC)) {
             $this->sql->select(array('tasks', 'completion'));
             $this->sql->where(array(array('tasks', 'round_id', $_SESSION['round_id']), array('tasks', 'task_id', $tasks['task_id'])));
             $this->sql->limit(1);
             switch ($tasks['type']) {
                 case 1:
                     $this->sql->select(array('buildings', 'name'));
                     $this->sql->where(array('buildings', 'building_id', array('tasks', 'building_id')));
                     $db_result = $this->sql->execute();
                     $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
                     $planet['building']['name'] = strshort($db_row['name'], 15);
                     $planet['building']['time'] = format_time(timeparser($db_row['completion'] - microfloat()));
                     break;
                 case 2:
                     $this->sql->select(array('concepts', 'name'));
                     $this->sql->where(array('concepts', 'concept_id', array('tasks', 'concept_id')));
                     $db_result = $this->sql->execute();
                     $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
                     $planet['researching']['name'] = strshort($db_row['name'], 15);
                     $planet['researching']['time'] = format_time(timeparser($db_row['completion'] - microfloat()));
                     break;
                 case 3:
                     $designs = array('army', 'navy', 'weapon');
                     $this->sql->select(array($designs[$tasks['number']] . 'designs', 'name'));
                     $this->sql->where(array($designs[$tasks['number']] . 'designs', $designs[$tasks['number']] . 'design_id', array('tasks', 'design_id')));
                     $db_result = $this->sql->execute();
                     $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
                     $planet['researching']['name'] = strshort($db_row['name'], 15);
                     $planet['researching']['time'] = format_time(timeparser($db_row['completion'] - microfloat()));
                     break;
                 case 4:
                     $this->sql->select(array($tasks['attribute'] . 'blueprints', 'name'));
                     $this->sql->where(array($tasks['attribute'] . 'blueprints', $tasks['attribute'] . 'blueprint_id', array('tasks', 'unit_id')));
                     $db_result = $this->sql->execute();
                     $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
                     $planet[$tasks['attribute']]['name'] = strshort($db_row['name'], 15);
                     $planet[$tasks['attribute']]['time'] = format_time(timeparser($db_row['completion'] - microfloat()));
                     break;
             }
         }
         $planet['nextupdate'] = format_time($round['resourcetick'] - (microfloat() - $planet['lastupdated']));
     }
     $this->smarty->assign('planet', $planet);
     $this->smarty->display('info_planet.tpl');
 }