preamble

<?php

// connect to mysql and select the database
include 'localsettings.php';

// workaround for bt settings change Feb 9 2004
ini_set(allow_url_fopen,1);

// check that we have a team and player
if ((!$_GET["P"]) || (!$_GET["T"])) {
    echo "Team or Player is not set<p>";
    echo "Start at <a href=\"index.php\">the main page</a><p>";
    exit(0);
}
$DefTeam = $_GET["T"];
$DefPlayer = $_GET["P"];

$LOGIN = $DefTeam;

// look up this team's password in the database
$sql = "SELECT * FROM MWGames, MWMoves WHERE GameName = Game AND TeamName = '$DefTeam'";
$result = mysql_query($sql, $db);
if ((!$result) || (mysql_num_rows($result)==0)) {
    // invalid team name
    $NoTeamMsg = "There is no team in the db by the name of $DefTeam<p>";
    $NoTeamMsg .= "Query was $sql<p>";
    $PASSWORD = "slsjf87sjdf823js3823"; // so the authentication will fail
} else {
    $myrow = mysql_fetch_array($result);
    $PASSWORD = $myrow["Password"];
    $PlayerData = $myrow["PlayerData"];
    $GameData = $myrow["GameData"];
}


// require entry of the team name and password
if ( (!isset($_SERVER['PHP_AUTH_USER'])) || ! (($_SERVER['PHP_AUTH_USER'] == $LOGIN) && ( $_SERVER['PHP_AUTH_PW'] == "$PASSWORD" )) ) {
    header("WWW-Authenticate: Basic realm=\"Enter password for $LOGIN\"");
    header("HTTP/1.0 401 Unauthorized");
    echo "Authorization Required...<p>";
    echo $NoTeamMsg; // in case we failed with a bad team name
    if ($LOGIN == "demo") { echo "Username=demo($LOGIN), Password=demo($PASSWORD)<BR>pw=" . $_SERVER['PHP_AUTH_PW'] . "<BR>"; }
    exit;
}


// Game settings - may change from time to time, but, hopefully, not every game
// Each new game must be manually added to the MWGames table of the database
$GameDataFields = explode("|", $GameData);
$BoardURL = "http://minutewar.gpsgames.org/$GameDataFields[0]/board.txt";
$BoardHTMURL = "http://minutewar.gpsgames.org/$GameDataFields[0]/board.htm";
$GameEngineURL = "http://minutewar.gpsgames.org/cgi-bin/$GameDataFields[0]/mw.pl";
$TimeBetweenCaptures = $GameDataFields[1]; //seconds
$DefaultOffsets = $GameDataFields[2];
$DrawType = $GameDataFields[3];
// should read the above two lines from board.html, but I'm too lazy right now :)

// Now that we know the password, we can retrieve the MoveData
$sql = "SELECT DECODE(MoveData, '".$_SERVER['PHP_AUTH_PW']."') as MD FROM MWMoves WHERE TeamName = '$DefTeam'";
$result = mysql_query($sql, $db);
if ((!$result) || (mysql_num_rows($result)==0)) {
    // select failed
    echo "Failed to retrieve move data from database<p>";
    echo "Query was $sql<p>";
} else {
    $myrow = mysql_fetch_array($result);
    $MoveData = stripslashes($myrow["MD"]);
}

$MoveDelimiter = "|EOM|"; // used to separate different move records

//Team colors
$CellColor["!New"] = "#000000";
$CellColor["ENew"] = "#FF0099";
$CellColor["NNew"] = "#33CC00";
$CellColor["SNew"] = "#FFFF00";
$CellColor["WNew"] = "#6699FF";
$CellColor["!Old"] = "#000000";
$CellColor["EOld"] = "#FF99CC";
$CellColor["NOld"] = "#99FF00";
$CellColor["SOld"] = "#FFFF99";
$CellColor["WOld"] = "#99CCFF";
$CellColor[""] = "White";
$CellColor["0"] = "White";

//Individual colors
$CellColor["1New"] = "#0066FF";
$CellColor["2New"] = "#FF0000";
$CellColor["3New"] = "#00FF00";
$CellColor["4New"] = "#FFFF00";
$CellColor["5New"] = "#00FFFF";
$CellColor["6New"] = "#FF00FF";
$CellColor["7New"] = "#CCCCFF";
$CellColor["8New"] = "#FF9999";
$CellColor["9New"] = "#99FF99";
$CellColor["10New"] = "#FFFFCC";
$CellColor["11New"] = "#CCFFFF";
$CellColor["12New"] = "#FF99FF";
$CellColor["13New"] = "#9999FF";
$CellColor["14New"] = "#993333";
$CellColor["15New"] = "#339933";
$CellColor["16New"] = "#CCCC66";
$CellColor["17New"] = "#339999";
$CellColor["18New"] = "#993399";
$CellColor["19New"] = "#CCCCCC";

$CellColor["1Old"] = "#99CCFF";
$CellColor["2Old"] = "#FF99CC";
$CellColor["3Old"] = "#99FF00";
$CellColor["4Old"] = "#FFFF99";
$CellColor["5Old"] = "#FFCC33";
$CellColor["6Old"] = "#CC9999";
$CellColor["7Old"] = "#CCCC66";
$CellColor["8Old"] = "#CCCCCC";

//load default offsets and locations
if ($PlayerData) {
    $PlayerList = explode("\n",$PlayerData);
    for ($i=0; $i<count($PlayerList)-1; $i++) {
        $PlayerFields = explode("|",$PlayerList[$i]);
        $Deflts["LatOff"][$PlayerFields[0]] = $PlayerFields[1];
        $Deflts["LonOff"][$PlayerFields[0]] = $PlayerFields[2];
        $Deflts["LatHem"][$PlayerFields[0]] = $PlayerFields[3];
        $Deflts["LonHem"][$PlayerFields[0]] = $PlayerFields[4];
        $Deflts["LatDeg"][$PlayerFields[0]] = $PlayerFields[5];
        $Deflts["LonDeg"][$PlayerFields[0]] = $PlayerFields[6];
        $Deflts["Email"][$PlayerFields[0]] = $PlayerFields[7];
        $Deflts["LatMin"][$PlayerFields[0]] = $PlayerFields[8];
        $Deflts["LonMin"][$PlayerFields[0]] = $PlayerFields[9];
    }
}

// simple function to apply offsets
function FindSquare($Player, $Lat, $Lon) {
    global $Deflts;
// 5/4/04 added '+60' in two places below to fix a bug with negative offsets    
    return ((floor($Lat)+$Deflts["LatOff"][$Player]+60) % 60)."x".((floor($Lon) + $Deflts["LonOff"][$Player]+60) % 60);
}
?>