function military_overview()
{
    global $data, $smarty, $sql;
    $incomingfleets = array();
    $combatlocations = array();
    $declarations = array();
    $microtime = microfloat();
    $sql->select(array(array('planets', 'planet_id'), array('planets', 'name', 'planetname'), array('navygroups', 'navygroup_id'), array('navygroups', 'name', 'navygroupname'), array('tasks', 'kingdom_id'), array('tasks', 'completion', 'time')));
    $sql->where(array(array('navygroups', 'round_id', $_SESSION['round_id']), array('navygroups', 'navygroup_id', array('tasks', 'group_id')), array('planets', 'round_id', $_SESSION['round_id']), array('planets', 'planet_id', array('tasks', 'planet_id')), array('tasks', 'round_id', $_SESSION['round_id']), array('tasks', 'type', 5), array('tasks', 'kingdom_id', array('tasks', 'target_kingdom_id'), '<>')));
    $db_query = $sql->generate();
    $db_query .= " AND (`tasks`.`kingdom_id` = '" . $_SESSION['kingdom_id'] . "' OR `tasks`.`target_kingdom_id` = '" . $_SESSION['kingdom_id'] . "') ORDER BY `planets`.`planet_id` ASC, `tasks`.`completion` ASC";
    $db_result = mysql_query($db_query);
    if (mysql_num_rows($db_result) > 0) {
        while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
            if ($db_row['kingdom_id'] == $_SESSION['kingdom_id']) {
                $db_row['direction'] = 'Outgoing';
            } else {
                $db_row['direction'] = 'Incoming';
            }
            unset($db_row['kingdom_id'], $db_row['target_kingdom_id']);
            $db_row['time'] = format_time(timeparser($db_row['time'] - $microtime));
            $incomingfleets[] = $db_row;
        }
    }
    $typearray = array('army', 'navy');
    foreach ($typearray as $type) {
        $sql->property('DISTINCT');
        $sql->select(array(array('planets', 'planet_id'), array('planets', 'name', 'planetname'), array('combat', 'completion')));
        $sql->where(array(array($type . 'groups', 'kingdom_id', array('planets', 'kingdom_id'), '!='), array($type . 'groups', 'planet_id', array('planets', 'planet_id')), array($type . 'groups', 'units', 'a:0:{}', '<>'), array('combat', 'planet_id', array('planets', 'planet_id'))));
        if ($type == 'navy') {
            $sql->where(array(array('navygroups', 'x_current', array('navygroups', 'x_destination')), array('navygroups', 'y_current', array('navygroups', 'y_destination'))));
        }
        $db_query = $sql->generate();
        $db_query .= " AND (`" . $type . "groups`.`kingdom_id` = '" . $_SESSION['kingdom_id'] . "' OR `planets`.`kingdom_id` = '" . $_SESSION['kingdom_id'] . "') ORDER BY `combat`.`completion` ASC";
        $db_result = mysql_query($db_query);
        if (mysql_num_rows($db_result) > 0) {
            $location_restarts = array();
            while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
                $db_row['time'] = format_time(timeparser($db_row['completion'] - $microtime));
                $combatlocations[$db_row['planet_id']] = $db_row;
                // If the update hasn't finished after 30 seconds, it has died.
                if ($db_row['completion'] < $microtime + 30) {
                    $location_restarts[] = $db_row['planet_id'];
                }
            }
            // Restart any dead combats.
            if (!empty($location_restarts)) {
                $db_query = "UPDATE `combat` SET `beingupdated` = '0' WHERE `planet_id` IN ('" . implode("', '", $location_restarts) . "')";
                $db_result = mysql_query($db_query);
            }
        }
    }
    $sort_function = create_function('$a,$b', 'if ($a[\'completion\'] == $b[\'completion\']) return 0; return ($a[\'completion\'] < $b[\'completion\']) ? -1 : 1;');
    usort($combatlocations, $sort_function);
    // Declarations
    military_declarations();
    $smarty->assign('incomingfleets', $incomingfleets);
    $smarty->assign('combatlocations', $combatlocations);
    $smarty->display('military_overview.tpl');
}
function status_kingdom()
{
    global $smarty, $sql, $data;
    if (!empty($_REQUEST['kingdom_id'])) {
        $kingdom_id = (int) $_REQUEST['kingdom_id'];
    } else {
        $kingdom_id = $_SESSION['kingdom_id'];
    }
    $data->updater->update($kingdom_id);
    $kingdom = $data->kingdom($kingdom_id);
    if (empty($kingdom)) {
        error(__FILE__, __LINE__, 'DATA', 'Invalid kingdom selected.');
    }
    if ($kingdom['kingdom_id'] == $_SESSION['kingdom_id']) {
        $display['member'] = true;
    }
    $display['kingdom_id'] = $kingdom['kingdom_id'];
    $display['name'] = $kingdom['name'];
    $display['image'] = $kingdom['image'];
    $display['planet_count'] = count($kingdom['planets']);
    $display['description'] = nl2br(htmlspecialchars($kingdom['description']));
    $display['score'] = format_number($kingdom['score']);
    if ($kingdom['score_peak'] > $kingdom['score']) {
        $display['score_peak'] = format_number($kingdom['score_peak']);
    }
    if ($kingdom['kingdom_id'] == $_SESSION['kingdom_id']) {
        $player = $data->player($_SESSION['player_id']);
        // News
        /*
        	Mergers
        	War / Peace / Alliance Declarations
        */
        if ($player['rank'] >= 80) {
            // Propositions
            /*
            	Show last six proposals by status and then time
            	Show 'More' at bottom of list.
            	
            	Title	 Y  M  N 
            	xxxxxxxx  1  0  2 
            	xxxxxxxx  2  1  0 
            	xxxxxxxx  0  3  1 
            	xxxxxxxx  Rejected
            	xxxxxxxx   Expired
            	xxxxxxxx	Passed
            		More	   
            */
            $sql->select(array(array('propositions', 'proposition_id'), array('propositions', 'title'), array('propositions', 'status'), array('propositions', 'for'), array('propositions', 'against'), array('propositions', 'neutral')));
            $sql->where(array(array('propositions', 'round_id', $_SESSION['round_id']), array('propositions', 'kingdom_id', $_SESSION['kingdom_id']), array('propositions', 'status', 0)));
            $sql->orderby(array(array('propositions', 'expires', 'DESC')));
            $sql->limit(6);
            $db_query = $sql->generate(false);
            $db_result = mysql_query($db_query);
            $count = 0;
            $propositions = array();
            while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
                $count++;
                $propositions[] = $db_row;
            }
            $sql->where(array('propositions', 'status', 0), false);
            $sql->where(array('propositions', 'status', 0, '>'));
            $sql->limit(6 - $count);
            $db_query = $sql->generate();
            $db_result = mysql_query($db_query);
            while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
                $propositions[] = $db_row;
            }
            $display['propositions'] = $propositions;
        }
    } else {
        military_declarations($kingdom['kingdom_id']);
    }
    if (!empty($kingdom['members'])) {
        $members = $data->player(array_keys($kingdom['members']));
        $member_ranks = array(RANK_PRISONER => 'Prisoner', RANK_GOVERNOR => 'Governor', RANK_STEWARD => 'Steward', RANK_COMMANDER => 'Commander', RANK_SENATOR => 'Senator', RANK_EMPEROR => 'Emperor');
        foreach ($members as $player_id => $player) {
            $display['members'][$player_id] = array('name' => $player['name'], 'player_id' => $player_id, 'rank' => $member_ranks[$player['rank']], 'score' => format_number($player['score'], true), 'on' => microfloat() - $player['lastactive'] < 900 ? true : false);
        }
    }
    $smarty->assign('kingdom', $display);
    $smarty->display('status_kingdom.tpl');
}