for ($i = 0; $i < db_num_rows($result); $i++) {
        $row = db_fetch_assoc($result);
        addnav("", "runmodule.php?module=staminasystem&op=editplayer&op2=edit&id=" . $row['acctid'] . "");
        output_notl("Player: " . $row['name'] . "<a href='runmodule.php?module=staminasystem&op=editplayer&op2=edit&id=" . $row['acctid'] . "'> (EDIT)</a> | <a href='runmodule.php?module=staminasystem&op=editplayer&op2=defaults&id=" . $row['acctid'] . "'> (SET TO DEFAULT)</a>", true);
        output("`n");
    }
    if (db_num_rows($result) == 0) {
        output("Nobody came up in the search!  Computer says no.");
    }
    addnav("Return");
    addnav("Search again", "runmodule.php?module=staminasystem&op=editplayer");
    addnav("Return", "runmodule.php?module=staminasystem&op=superuser");
}
if ($op2 == "edit") {
    $id = httpget('id');
    $actions = get_player_action_list($id);
    output("Here we have the editing screen.  Follow the format you see already, seperating parameters from their values with '=>' and using ';;' to denote a new line.  Leave the last line without the ';;' and everything should be fine.`n`n");
    rawoutput("<form action='runmodule.php?module=staminasystem&op=editplayer&op2=save&id=" . $id . "' method='POST'>");
    foreach ($actions as $action => $details) {
        rawoutput($action);
        rawoutput("<br /><textarea name='{$action}' cols='40' rows='12'>");
        $size = count($details);
        $i = 0;
        foreach ($details as $detail => $value) {
            $textboxoutput = $detail . "=>" . $value;
            $i++;
            if ($i != $size) {
                $textboxoutput .= ";;";
            }
            rawoutput($textboxoutput);
        }
$colorbackground = $colordarkgreen;
if ($greenpct == 0) {
    $colorgreen = $colordarkamber;
    $colorbackground = $colordarkamber;
}
if ($amberpct == 0) {
    $colorgreen = $colordarkred;
    $coloramber = $colordarkred;
    $colorbackground = $colordarkred;
}
$pctgrey = 100 - $greenwidth - $amberwidth - $redwidth;
rawoutput("<table style='border: solid 1px #000000' bgcolor='{$colorbackground}' cellpadding='0' cellspacing='0' width='100%' height='20'><tr><td width='{$redwidth}%' bgcolor='{$colorred}'></td><td width='{$amberwidth}%' bgcolor='{$coloramber}'></td><td width='{$greenwidth}%' bgcolor='{$colorgreen}'></td><td width='{$pctgrey}%'></td></tr></table>");
output("Total Stamina: %s / %s | Amber point: %s | Red point: %s", number_format($stamina), number_format($daystamina), number_format($amberpoint), number_format($redpoint));
output("`n`nHere is the nitty-gritty of your Stamina statistics.  The most important value is the total cost, over there on the right.  If there's anything in the Buff column, something's temporarily affecting the cost of performing that action (negative numbers are good!).  More details follow after the stats.`n`n");
rawoutput("<table width=100%><tr><td><b>Action</b></td><td><b>Experience</b></td><td><b>Natural Cost</b></td><td><b>Buff</b></td><td><b>Total</b></td></tr>");
$act = get_player_action_list();
foreach ($act as $key => $values) {
    $lvlinfo = stamina_level_up($key);
    $nextlvlexp = round($lvlinfo['nextlvlexp']);
    $nextlvlexpdisplay = number_format($nextlvlexp);
    $currentlvlexp = round($lvlinfo['currentlvlexp']);
    $currentlvlexpdisplay = number_format($currentlvlexp);
    $cost = $values['naturalcost'];
    $level = $values['lvl'];
    $exp = $values['exp'];
    $mincost = $values['mincost'];
    $costwithbuff = stamina_calculate_buffed_cost($key);
    $modifier = $costwithbuff - $cost;
    $bonus = "None";
    if ($modifier < 0) {
        $bonus = "`@" . number_format($modifier) . "`0";
function stamina_level_up($action, $userid = false)
{
    global $session;
    if ($userid === false) {
        $userid = $session['user']['acctid'];
    }
    $returninfo = array();
    $stop = 0;
    $actions = get_player_action_list($userid);
    if ($actions[$action]['lvl'] >= 100) {
        return false;
    }
    while ($stop == 0) {
        $actions = get_player_action_list($userid);
        $currentexp = $actions[$action]['exp'];
        $currentlvl = $actions[$action]['lvl'];
        $first = $actions[$action]['firstlvlexp'];
        $increment = $actions[$action]['expincrement'];
        $stop = 1;
        //Determine the next level's EXP requirements
        $addup = array();
        $addup[0] = $first;
        for ($i = 1; $i <= 100; $i++) {
            $addup[$i] = round($addup[$i - 1] * $increment);
        }
        $levels = array();
        $levels[0] = $first;
        for ($i = 1; $i <= 100; $i++) {
            $levels[$i] = $levels[$i - 1] + $addup[$i];
        }
        if ($currentlvl != 0) {
            $currentlvlexp = $levels[$currentlvl];
        } else {
            $currentlvlexp = 0;
        }
        $nextlvlexp = $levels[$currentlvl];
        $currentlvlexp = $levels[$currentlvl - 1];
        $returninfo['exp'] = $currentexp;
        $returninfo['lvl'] = $currentlvl;
        $returninfo['nextlvlexp'] = $nextlvlexp;
        $returninfo['currentlvlexp'] = $currentlvlexp;
        //Check if player's exp is more than level requirement, and level up if true
        if ($currentexp > $nextlvlexp && $actions[$action]['lvl'] <= 100) {
            $stop = 0;
            //level up
            $actions[$action]['lvl']++;
            //reduce costs
            $actions[$action]['naturalcost'] -= $actions[$action]['costreduction'];
            //write back array to modulepref
            set_module_pref("actions", serialize($actions), "staminasystem", $userid);
            //set "levelledup" to true, so that the module can output levelling up text
            $returninfo['levelledup'] = true;
            $returninfo['newlvl'] = $actions[$action]['lvl'];
        }
    }
    return $returninfo;
}