Example #1
0
function processMessages()
{
    global $CFG_TABLE;
    global $isUndoRequested, $isDrawRequested, $isUndoing, $isGameOver, $isCheckMate, $playersColor, $numMoves, $statusMessage, $CFG_USEEMAILNOTIFICATION;
    /* old PHP versions don't have _POST, _GET and _SESSION as auto_globals */
    if (!minimum_version("4.1.0")) {
        global $_POST, $_GET, $_SESSION;
    }
    if (DEBUG) {
        echo "Entering processMessages()<br>\n";
    }
    $isUndoRequested = false;
    $isGameOver = false;
    /* find out which player (black or white) we are serving */
    /* NOTE: When playing in the same computer $playersColor is always the player who logged in first */
    if (DEBUG) {
        echo "SharedPC..." . $_SESSION['isSharedPC'] . "<br>\n";
    }
    if ($_SESSION['isSharedPC']) {
        // Only the player to move is active in this case
        if (($numMoves == -1 || $numMoves % 2 == 1) && $playersColor == "white" || $numMoves % 2 == 0 && $playersColor == "black") {
            $currentPlayer = $playersColor;
        } else {
            // The player who logged in later is to move
            if ($playersColor == "white") {
                $currentPlayer = "black";
            } else {
                $currentPlayer = "white";
            }
        }
    } else {
        // The players are on different computers
        $currentPlayer = $playersColor;
    }
    if ($currentPlayer == "white") {
        $opponentColor = "black";
    } else {
        $opponentColor = "white";
    }
    /* *********************************************** */
    /* queue user generated (ie: using forms) messages */
    /* *********************************************** */
    if (DEBUG) {
        echo "Processing user generated (ie: form) messages...<br>\n";
    }
    /* queue a request for an undo */
    if ($_POST['requestUndo'] == "yes") {
        /* if the two players are on the same system, execute undo immediately */
        /* NOTE: assumes the two players discussed it live before undoing */
        if ($_SESSION['isSharedPC']) {
            $isUndoing = true;
        } else {
            $tmpQuery = "INSERT INTO " . $CFG_TABLE[messages] . " (gameID, msgType, msgStatus, destination) VALUES (" . $_SESSION['gameID'] . ", 'undo', 'request', '" . $opponentColor . "')";
            mysql_query($tmpQuery);
            // ToDo: Mail an undo request notice to other player??
        }
        updateTimestamp();
    }
    /* queue a request for a draw */
    if ($_POST['requestDraw'] == "yes") {
        /* if the two players are on the same system, execute Draw immediately */
        /* NOTE: assumes the two players discussed it live before declaring the game a draw */
        if ($_SESSION['isSharedPC']) {
            $tmpQuery = "UPDATE " . $CFG_TABLE[games] . " SET gameMessage = 'draw', messageFrom = '" . $currentPlayer . "' WHERE gameID = " . $_SESSION['gameID'];
            mysql_query($tmpQuery);
        } else {
            $tmpQuery = "INSERT INTO " . $CFG_TABLE[messages] . " (gameID, msgType, msgStatus, destination) VALUES (" . $_SESSION['gameID'] . ", 'draw', 'request', '" . $opponentColor . "')";
            mysql_query($tmpQuery);
        }
        updateTimestamp();
    }
    /* response to a request for an undo */
    if (isset($_POST['undoResponse'])) {
        if ($_POST['isUndoResponseDone'] == 'yes') {
            if ($_POST['undoResponse'] == "yes") {
                $tmpStatus = "approved";
                $isUndoing = true;
            } else {
                $tmpStatus = "denied";
            }
            $tmpQuery = "UPDATE " . $CFG_TABLE[messages] . " SET msgStatus = '" . $tmpStatus . "', destination = '" . $opponentColor . "' WHERE gameID = " . $_SESSION['gameID'] . " AND msgType = 'undo' AND msgStatus = 'request' AND destination = '" . $currentPlayer . "'";
            mysql_query($tmpQuery);
            updateTimestamp();
        }
    }
    /* response to a request for a draw */
    if (isset($_POST['drawResponse'])) {
        if ($_POST['isDrawResponseDone'] == 'yes') {
            if ($_POST['drawResponse'] == "yes") {
                $tmpStatus = "approved";
                $tmpQuery = "UPDATE " . $CFG_TABLE[games] . " SET gameMessage = 'draw', messageFrom = '" . $currentPlayer . "' WHERE gameID = " . $_SESSION['gameID'];
                mysql_query($tmpQuery);
            } else {
                $tmpStatus = "denied";
            }
            $tmpQuery = "UPDATE " . $CFG_TABLE[messages] . " SET msgStatus = '" . $tmpStatus . "', destination = '" . $opponentColor . "' WHERE gameID = " . $_SESSION['gameID'] . " AND msgType = 'draw' AND msgStatus = 'request' AND destination = '" . $currentPlayer . "'";
            mysql_query($tmpQuery);
            updateTimestamp();
        }
    }
    /* resign the game */
    if ($_POST['resign'] == "yes") {
        $tmpQuery = "UPDATE " . $CFG_TABLE[games] . " SET gameMessage = 'playerResigned', messageFrom = '" . $currentPlayer . "' WHERE gameID = " . $_SESSION['gameID'];
        mysql_query($tmpQuery);
        updateTimestamp();
        /* if email notification is activated... */
        if ($CFG_USEEMAILNOTIFICATION && !$_SESSION['isSharedPC']) {
            /* get opponent's player ID */
            if ($currentPlayer == 'white') {
                $tmpOpponentID = mysql_query("SELECT blackPlayer FROM " . $CFG_TABLE[games] . " WHERE gameID = " . $_SESSION['gameID']);
            } else {
                $tmpOpponentID = mysql_query("SELECT whitePlayer FROM " . $CFG_TABLE[games] . " WHERE gameID = " . $_SESSION['gameID']);
            }
            $opponentID = mysql_result($tmpOpponentID, 0);
            $tmpOpponentEmail = mysql_query("SELECT value FROM " . $CFG_TABLE[preferences] . " WHERE playerID = " . $opponentID . " AND preference = 'emailNotification'");
            /* if opponent is using email notification... */
            if (mysql_num_rows($tmpOpponentEmail) > 0) {
                $opponentEmail = mysql_result($tmpOpponentEmail, 0);
                if ($opponentEmail != '') {
                    /* notify opponent of resignation via email */
                    webchessMail('resignation', $opponentEmail, '', $_SESSION['nick'], $_SESSION['gameID']);
                }
            }
        }
    }
    /* ******************************************* */
    /* process queued messages (ie: from database) */
    /* ******************************************* */
    $tmpQuery = "SELECT * FROM " . $CFG_TABLE[messages] . " WHERE gameID = " . $_SESSION['gameID'] . " AND destination = '" . $currentPlayer . "'";
    $tmpMessages = mysql_query($tmpQuery);
    while ($tmpMessage = mysql_fetch_array($tmpMessages, MYSQL_ASSOC)) {
        switch ($tmpMessage['msgType']) {
            case 'undo':
                switch ($tmpMessage['msgStatus']) {
                    case 'request':
                        $isUndoRequested = true;
                        break;
                    case 'approved':
                        $tmpQuery = "DELETE FROM " . $CFG_TABLE[messages] . " WHERE gameID = " . $_SESSION['gameID'] . " AND msgType = 'undo' AND msgStatus = 'approved' AND destination = '" . $currentPlayer . "'";
                        mysql_query($tmpQuery);
                        $statusMessage .= "Undo approved";
                        break;
                    case 'denied':
                        $isUndoing = false;
                        $tmpQuery = "DELETE FROM " . $CFG_TABLE[messages] . " WHERE gameID = " . $_SESSION['gameID'] . " AND msgType = 'undo' AND msgStatus = 'denied' AND destination = '" . $currentPlayer . "'";
                        mysql_query($tmpQuery);
                        $statusMessage .= "Undo denied";
                        break;
                }
                break;
            case 'draw':
                switch ($tmpMessage['msgStatus']) {
                    case 'request':
                        $isDrawRequested = true;
                        break;
                    case 'approved':
                        $tmpQuery = "DELETE FROM " . $CFG_TABLE[messages] . " WHERE gameID = " . $_SESSION['gameID'] . " AND msgType = 'draw' AND msgStatus = 'approved' AND destination = '" . $currentPlayer . "'";
                        mysql_query($tmpQuery);
                        $statusMessage .= "Draw approved";
                        break;
                    case 'denied':
                        $tmpQuery = "DELETE FROM " . $CFG_TABLE[messages] . " WHERE gameID = " . $_SESSION['gameID'] . " AND msgType = 'draw' AND msgStatus = 'denied' AND destination = '" . $currentPlayer . "'";
                        mysql_query($tmpQuery);
                        $statusMessage .= "Draw denied";
                        break;
                }
                break;
        }
    }
    /* requests pending */
    $tmpQuery = "SELECT * FROM " . $CFG_TABLE[messages] . " WHERE gameID = " . $_SESSION['gameID'] . " AND msgStatus = 'request' AND destination = '" . $opponentColor . "'";
    $tmpMessages = mysql_query($tmpQuery);
    while ($tmpMessage = mysql_fetch_array($tmpMessages, MYSQL_ASSOC)) {
        switch ($tmpMessage['msgType']) {
            case 'undo':
                $statusMessage .= "Your undo request is pending";
                break;
            case 'draw':
                $statusMessage .= "Your request for a draw is pending";
                break;
        }
    }
    /* game level status: draws, resignations and checkmate */
    /* if checkmate, update games table */
    if ($_POST['isCheckMate'] == 'true') {
        mysql_query("UPDATE " . $CFG_TABLE[games] . " SET gameMessage = 'checkMate', messageFrom = '" . $currentPlayer . "' WHERE gameID = " . $_SESSION['gameID']);
    }
    // ToDo: Mail checkmate notification to opponent
    $tmpQuery = "SELECT gameMessage, messageFrom FROM " . $CFG_TABLE[games] . " WHERE gameID = " . $_SESSION['gameID'];
    $tmpMessages = mysql_query($tmpQuery);
    $tmpMessage = mysql_fetch_array($tmpMessages, MYSQL_ASSOC);
    if ($tmpMessage['gameMessage'] == "draw") {
        $statusMessage .= "Game ended in a draw";
        $isGameOver = true;
    }
    if ($tmpMessage['gameMessage'] == "playerResigned") {
        $statusMessage .= $tmpMessage['messageFrom'] . " has resigned the game";
        $isGameOver = true;
    }
    if ($tmpMessage['gameMessage'] == "checkMate") {
        $statusMessage .= "Checkmate! " . $tmpMessage['messageFrom'] . " has won the game";
        $isGameOver = true;
        $isCheckMate = true;
    }
}
		/* find out if it's the current player's turn */
		if (( (($numMoves == -1) || (($numMoves % 2) == 1)) && ($playersColor == "white"))
			|| ((($numMoves % 2) == 0) && ($playersColor == "black")))
			$isPlayersTurn = true;
		else
			$isPlayersTurn = false;

		if ($white == $_SESSION['playerID'])
			$opponent = $black;
		else $opponent = $white;


		if (!isBoardDisabled() && !$isCheckMate && $timeLimit > 0)
			if (tempoEsgotado($mycolor2)){
			    saveRanking($_SESSION['gameID'],"resign",$mycolor2,1);
			    updateTimestamp();
			    
			    // Update the opponent time
				if ($mycolor2 == "white")
					mysql_query("UPDATE games set timeWhite=$timeLimit*60 WHERE gameID=".$_SESSION['gameID']);
			    else
					mysql_query("UPDATE games set timeBlack=$timeLimit*60 WHERE gameID=".$_SESSION['gameID']);
					
				echo "<script>
				alert('".$MSG_LANG["theflaghasfallen"]." $mycolor2 $MSG_LANG[lost]');
				window.location='chess.php';
				</script>\n";
				exit;
			}

	if ($isUndoing)
Example #3
0
//TODO: - I dont fully understand exactly what this code does, but appears it could use some optimization
//perhaps a function- ########## RESEARCH MARKER START###########
var_dump($_SESSION);
//SHEESH!  where the hell is gameID and this  ["isSharedPC"]=>  bool(false) ["playerID"]=>  int(-1) ["gameID"]=>  int(0) }  shit coming from??
//if (!isBoardDisabled() && !$isCheckMate && $timeLimit > 0)
//{
//there's no else, this if ends at the bottom of script
//this way we can allow kibbitzing, but not posting. in this way we can control what is loaded
//and for certain games or player preferences, allow kibbitz may lock them out ..
if ($white != $_SESSION['player_id'] && $black != $_SESSION['player_id']) {
    die($MSG_LANG["youdonthavepermission"]);
}
if (tempoEsgotado($mycolor2)) {
    //here we have to figure out teh time limit shit a little better- when the clock starts and stops..
    $do_rankings = saveRanking($game_id, "resign", $mycolor2, 1);
    $update_stamp = updateTimestamp();
    $time_limmit = $timeLimit * 60;
    // Update the opponent time
    if ($mycolor2 == "white") {
        $sql = $db->Prepare("UPDATE games set timeWhite=? WHERE game_id=?");
        $query = $db->Execute($sql, array($time_limit, $game_id));
        db_op_result($query, __LINE__, __FILE__);
    } else {
        $sql = $db->Prepare("UPDATE games set timeBlack=? WHERE game_id=?");
        $query = $db->Execute($sql, array($time_limit, $game_id));
        db_op_result($query, __LINE__, __FILE__);
    }
    $smarty->assign('js_checkmate_alert', "<script>alert('" . $MSG_LANG['theflaghasfallen'] . " " . $mycolor2 . $MSG_LANG['lost'] . "');  window.location='chess.php'; </script>");
    //we switch the display based on that. good reason to exit and halt execution here I guess
    exit;
}
    function processMessages()
    {
        global $game_id,$db,$db_prefix, $MSG_LANG, $CFG_MIN_ROUNDS, $isUndoRequested, $isDrawRequested, $isUndoing, $isGameOver, $isCheckMate, $playersColor, $statusMessage, $CFG_USEEMAILNOTIFICATION, $flagFall;

        $isUndoRequested = false;
        $isGameOver = false;

        if ($playersColor == "white")
            $opponentColor = "black";
        else
            $opponentColor = "white";



        /* queue a request for an undo */
        if(!empty($_POST['requestUndo']))
        {
            $requestUndo = $_POST['requestUndo'];
        }
        else
        {
            $requestUndo = "no";
        }
        if ($requestUndo == "yes")
        {
            /* if the two players are on the same system, execute undo immediately */
            /* NOTE: assumes the two players discussed it live before undoing */
            if ($_SESSION['isSharedPC'])
                $isUndoing = true;
            else
            {
                $tmpQuery = "INSERT INTO {$db_prefix}messages (game_id, msgType, msgStatus, destination) VALUES ('$game_id', 'undo', 'request', '".$opponentColor."')";
                mysql_query($tmpQuery);
            }

            //updateTimestamp();
        }
        if(!empty($_POST['requestDraw']))
        {
            $request_draw = $_POST['requestDraw'];
        }
        else
        {
            $request_draw = "no";
        }
        /* queue a request for a draw */
        if ($request_draw == "yes")
        {
            /* if the two players are on the same system, execute Draw immediately */
            /* NOTE: assumes the two players discussed it live before declaring the game a draw */
            if ($_SESSION['isSharedPC'])
            {
                saveRanking($_SESSION['game_id'],"draw",$playersColor);
            }
            else
            {
                $tmpQuery = "INSERT INTO {$db_prefix}messages (game_id, msgType, msgStatus, destination) VALUES ('$game_id', 'draw', 'request', '".$opponentColor."')";
                mysql_query($tmpQuery);
            }

            //updateTimestamp();
        }

        /* response to a request for an undo */
        if (isset($_POST['undoResponse']))
        {
            if ($_POST['isUndoResponseDone'] == 'yes')
            {
                if ($_POST['undoResponse'] == "yes")
                {
                    $tmpStatus = "approved";
                    $isUndoing = true;
                    updateTimestamp();
                }
                else
                    $tmpStatus = "denied";

                $tmpQuery = "UPDATE {$db_prefix}messages SET msgStatus = '".$tmpStatus."', destination = '".$opponentColor."' WHERE game_id = '$game_id' AND msgType = 'undo' AND msgStatus = 'request' AND destination = '".$playersColor."'";
                mysql_query($tmpQuery);
            }
        }

        /* response to a request for a draw */
        if (isset($_POST['drawResponse']))
        {
            if ($_POST['isDrawResponseDone'] == 'yes')
            {
                if ($_POST['drawResponse'] == "yes")
                {
                    saveRanking($_SESSION['game_id'],"draw",$playersColor);
                    $tmpStatus = "approved";
                    updateTimestamp();
                }
                else
                    $tmpStatus = "denied";

                $tmpQuery = "UPDATE {$db_prefix}messages SET msgStatus = '".$tmpStatus."', destination = '".$opponentColor."' WHERE game_id = '$game_id' AND msgType = 'draw' AND msgStatus = 'request' AND destination = '".$playersColor."'";
                mysql_query($tmpQuery);
            }
        }

        /* resign the game */
        if (!empty($_POST['resign']))
        {
            $resigned = $_POST['resign'];
        }
        else
        {
            $resigned = "no";
        }
        if ($resigned == "yes")
        {
            saveRanking($_SESSION['game_id'],"resign",$playersColor);
            updateTimestamp();

            /* if email notification is activated... */
            if ($CFG_USEEMAILNOTIFICATION)
            {
                /* get opponent's player ID */
                if ($playersColor == 'white')
                    $tmpOpponentID = mysql_query("SELECT black_player FROM {$db_prefix}games WHERE game_id = ".$_SESSION['game_id']);
                else
                    $tmpOpponentID = mysql_query("SELECT white_player FROM {$db_prefix}games WHERE game_id = ".$_SESSION['game_id']);

                $opponentID = mysql_result($tmpOpponentID, 0);

                $tmpOpponentEmail = mysql_query("SELECT value FROM {$db_prefix}player_preference WHERE playerID = ".$opponentID." AND preference = 'emailNotification'");

                /* if opponent is using email notification... */
                if (mysql_num_rows($tmpOpponentEmail) > 0)
                {
                    $opponentEmail = mysql_result($tmpOpponentEmail, 0);
                    if ($opponentEmail != '')
                    {
                        /* notify opponent of resignation via email */
                        webchessMail('resignation', $opponentEmail, '', $_SESSION['firstName']);
                    }
                }
            }
        }


        /* ******************************************* */
        /* process queued messages (ie: from database) */
        /* ******************************************* */
        $tmpQuery = "SELECT * FROM {$db_prefix}messages WHERE game_id = '$game_id' AND destination = '".$playersColor."'";
        $tmpMessages = mysql_query($tmpQuery);

        while($tmpMessage = mysql_fetch_array($tmpMessages, MYSQL_ASSOC))
        {
            switch($tmpMessage['msgType'])
            {
                case 'undo':
                    switch($tmpMessage['msgStatus'])
                    {
                        case 'request':
                            $isUndoRequested = true;
                            break;
                        case 'approved':
                            $tmpQuery = "DELETE FROM {$db_prefix}messages WHERE game_id = '$game_id' AND msgType = 'undo' AND msgStatus = 'approved' AND destination = '".$playersColor."'";
                            mysql_query($tmpQuery);
                            $statusMessage .= $MSG_LANG["undoapproved"].".<br>\n";
                            break;
                        case 'denied':
                            $isUndoing = false;
                            $tmpQuery = "DELETE FROM {$db_prefix}messages WHERE game_id = '$game_id' AND msgType = 'undo' AND msgStatus = 'denied' AND destination = '".$playersColor."'";
                            mysql_query($tmpQuery);
                            $statusMessage .= $MSG_LANG["undodenied"].".<br>\n";
                            break;
                    }
                    break;

                case 'draw':
                    switch($tmpMessage['msgStatus'])
                    {
                        case 'request':
                            $isDrawRequested = true;
                            break;
                        case 'approved':
                            $tmpQuery = "DELETE FROM {$db_prefix}messages WHERE game_id = '$game_id' AND msgType = 'draw' AND msgStatus = 'approved' AND destination = '".$playersColor."'";
                            mysql_query($tmpQuery);
                            $statusMessage .= $MSG_LANG["drawapproved"].".<br>\n";
                            break;
                        case 'denied':
                            $tmpQuery = "DELETE FROM {$db_prefix}messages WHERE game_id = '$game_id' AND msgType = 'draw' AND msgStatus = 'denied' AND destination = '".$playersColor."'";
                            mysql_query($tmpQuery);
                            $statusMessage .= $MSG_LANG["drawdenied"].".<br>\n";
                            break;
                    }
                    break;
            }
        }

        /* requests pending */
        $tmpQuery = "SELECT * FROM {$db_prefix}messages WHERE game_id = '$game_id' AND msgStatus = 'request' AND destination = '".$opponentColor."'";
        $tmpMessages = mysql_query($tmpQuery);

        while($tmpMessage = mysql_fetch_array($tmpMessages, MYSQL_ASSOC))
        {
            switch($tmpMessage['msgType'])
            {
                case 'undo':
                    $statusMessage .= $MSG_LANG["undopending"].".<br>\n";
                    break;
                case 'draw':
                    $statusMessage .= $MSG_LANG["drawpending"].".<br>\n";
                    break;
            }
        }

        /* game level status: draws, resignations and checkmate */
        /* if checkmate, update games table */
        if(!empty($_POST['isCheckMate']))
        {
            $checkmated = $_POST['isCheckMate'];
        }
        else
        {
            $checkmated = "no";
        }
        if ($checkmated == 'true')
        {
            saveRanking($_SESSION['game_id'],"checkmate",$playersColor);
        }


        $getbw["black"] = $MSG_LANG["black"];
        $getbw["white"] = $MSG_LANG["white"];
        $getbwO["black"] = $MSG_LANG["white"];
        $getbwO["white"] = $MSG_LANG["black"];


        $tmpQuery = "SELECT status, message_from FROM {$db_prefix}games WHERE game_id = '$game_id'";
        $tmpMessages = mysql_query($tmpQuery);
        $tmpMessage = mysql_fetch_array($tmpMessages, MYSQL_ASSOC);

        if ($tmpMessage['status'] == "draw")
        {
            $statusMessage .= $MSG_LANG["endindraw"].".<br>\n";
            $isGameOver = true;
        }

        else if (!$flagFall && $tmpMessage['status'] == "playerResigned")
        {
            $statusMessage .= $getbw[$tmpMessage['message_from']]." ".$MSG_LANG["resigned"].".<br>\n";
            $isGameOver = true;
        }
        else if ($tmpMessage['status'] == "checkMate")
        {

            $statusMessage .= $MSG_LANG["checkmate"]."! ".$getbw[$tmpMessage['message_from']]." ".$MSG_LANG["wonthegame"].".<br>\n";
            $isGameOver = true;
            $isCheckMate = true;
        }

        else if ($flagFall && $tmpMessage['status'] == "playerResigned")
        {
            $statusMessage = $MSG_LANG["theflaghasfallen"]."! ".$getbwO[$tmpMessage['message_from']]." ".$MSG_LANG["wonthegame"].".<br>\n";
            $isGameOver = true;
            $isCheckMate = false;
        }

    }
Example #5
0
function processMessages()
{
    global $mysql;
    global $isUndoRequested, $isDrawRequested, $undoing, $isGameOver, $isCheckMate;
    global $statusMessage, $CFG_USEEMAIL, $FENarray;
    global $colorArray;
    if (DEBUG) {
        echo "Entering processMessages( )<br />\n";
    }
    $num_moves = count($FENarray) - 1;
    $isUndoRequested = false;
    $isGameOver = false;
    // find out which player (black or white) we are serving
    if (DEBUG) {
        echo "SharedPC... {$_SESSION['shared']}<br />\n";
    }
    $FENitems = explode(' ', $FENarray[$num_moves]);
    $curTurn = $colorArray[$FENitems[1]];
    if ($_SESSION['shared']) {
        if ($curTurn == $_SESSION['player']['p_color']) {
            // if
            $currentPlayer = $_SESSION['player']['p_color'];
        } else {
            if ('white' == $_SESSION['player']['p_color']) {
                $currentPlayer = 'black';
            } else {
                $currentPlayer = 'white';
            }
        }
    } else {
        // The players are on different computers
        $currentPlayer = $_SESSION['player']['p_color'];
    }
    /* *********************************************** */
    /* queue user generated (ie: using forms) messages */
    /* *********************************************** */
    if (DEBUG) {
        echo "Processing user generated (ie: form) messages...<br>\n";
    }
    /* queue a request for an undo */
    if (isset($_POST['requestUndo']) && 'yes' == $_POST['requestUndo'] && 0 != $num_moves) {
        /* if the two players are on the same system, execute undo immediately */
        /* NOTE: assumes the two players discussed it live before undoing */
        if ($_SESSION['shared']) {
            $undoing = true;
        } else {
            $query = "\n\t\t\t\tINSERT INTO " . T_MESSAGE . "\n\t\t\t\t\t(m_game_id, m_type, m_status, m_destination)\n\t\t\t\tVALUES\n\t\t\t\t\t('{$_SESSION['game_id']}', 'undo', 'request', '{$_SESSION['opponent']['p_color']}')\n\t\t\t";
            $mysql->query($query, __LINE__, __FILE__);
            // ToDo: Mail an undo request notice to other player??
        }
        updateTimestamp();
    }
    /* queue a request for a draw */
    if (isset($_POST['requestDraw']) && 'yes' == $_POST['requestDraw']) {
        /* if the two players are on the same system, execute Draw immediately */
        /* NOTE: assumes the two players discussed it live before declaring the game a draw */
        if ($_SESSION['shared']) {
            $query = "\n\t\t\t\tUPDATE " . T_GAME . "\n\t\t\t\tSET g_game_message = 'Draw'\n\t\t\t\t\t, g_message_from = '{$currentPlayer}'\n\t\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t\t";
            $mysql->query($query, __LINE__, __FILE__);
            adjust_stats($_SESSION['white']['p_id'], $_SESSION['black']['p_id'], 0.5, 0.5);
        } else {
            $query = "\n\t\t\t\tINSERT INTO " . T_MESSAGE . "\n\t\t\t\t\t(m_game_id, m_type, m_status, m_destination)\n\t\t\t\tVALUES\n\t\t\t\t\t('{$_SESSION['game_id']}', 'draw', 'request', '{$_SESSION['opponent']['p_color']}')\n\t\t\t";
            $mysql->query($query, __LINE__, __FILE__);
        }
        updateTimestamp();
    }
    /* response to a request for an undo */
    if (isset($_POST['undoResponse'])) {
        if ('yes' == $_POST['isUndoResponseDone']) {
            if ('yes' == $_POST['undoResponse']) {
                $status = 'approved';
                $undoing = true;
            } else {
                $status = 'denied';
            }
            $query = "\n\t\t\t\tUPDATE " . T_MESSAGE . "\n\t\t\t\tSET m_status   = '{$status}'\n\t\t\t\t\t, m_destination = '{$_SESSION['opponent']['p_color']}'\n\t\t\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\t\t\t\tAND m_type = 'undo'\n\t\t\t\t\tAND m_status = 'request'\n\t\t\t\t\tAND m_destination = '{$currentPlayer}'\n\t\t\t";
            $mysql->query($query, __LINE__, __FILE__);
            updateTimestamp();
        }
    }
    /* response to a request for a draw */
    if (isset($_POST['drawResponse'])) {
        if ('yes' == $_POST['isDrawResponseDone']) {
            if ('yes' == $_POST['drawResponse']) {
                $query = "\n\t\t\t\t\tUPDATE " . T_GAME . "\n\t\t\t\t\tSET g_game_message = 'Draw'\n\t\t\t\t\t\t, g_message_from = '{$currentPlayer}'\n\t\t\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t\t\t";
                $mysql->query($query, __LINE__, __FILE__);
                $status = 'approved';
                adjust_stats($_SESSION['player']['p_id'], $_SESSION['opponent']['p_id'], 0.5, 0.5);
            } else {
                $status = 'denied';
            }
            $query = "\n\t\t\t\tUPDATE " . T_MESSAGE . "\n\t\t\t\tSET m_status   = '{$status}'\n\t\t\t\t\t, m_destination = '{$_SESSION['opponent']['p_color']}'\n\t\t\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\t\t\t\tAND m_type = 'draw'\n\t\t\t\t\tAND m_status = 'request'\n\t\t\t\t\tAND m_destination = '{$currentPlayer}'\n\t\t\t";
            $mysql->query($query, __LINE__, __FILE__);
            updateTimestamp();
        }
    }
    /* resign the game */
    if (isset($_POST['resign']) && 'yes' == $_POST['resign']) {
        $query = "\n\t\t\tUPDATE " . T_GAME . "\n\t\t\tSET g_game_message = 'Player Resigned'\n\t\t\t\t, g_message_from = '{$currentPlayer}'\n\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t";
        $mysql->query($query, __LINE__, __FILE__);
        updateTimestamp();
        adjust_stats($_SESSION['player']['p_id'], $_SESSION['opponent']['p_id'], 0, 1);
        /* if email notification is activated... */
        if ($CFG_USEEMAIL && !$_SESSION['shared']) {
            /* get opponent's player ID */
            if ('white' == $currentPlayer) {
                $query = "\n\t\t\t\t\tSELECT g_black_player_id\n\t\t\t\t\tFROM " . T_GAME . "\n\t\t\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t\t\t";
            } else {
                $query = "\n\t\t\t\t\tSELECT g_white_player_id\n\t\t\t\t\tFROM " . T_GAME . "\n\t\t\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t\t\t";
            }
            $opponentID = $mysql->fetch_value($query, __LINE__, __FILE__);
            $query = "\n\t\t\t\tSELECT p_email\n\t\t\t\tFROM " . T_PLAYER . "\n\t\t\t\tWHERE p_id = '{$opponentID}'\n\t\t\t";
            $opponentEmail = $mysql->fetch_value($query, __LINE__, __FILE__);
            /* if opponent is using email notification... */
            if (0 < $mysql->num_rows()) {
                if ('' != $opponentEmail) {
                    /* notify opponent of resignation via email */
                    call("webchessMail('resignation', {$opponentEmail}, '', {$_SESSION['username']}, {$_SESSION['game_id']})");
                    webchessMail('resignation', $opponentEmail, '', $_SESSION['username'], $_SESSION['game_id']);
                }
            }
        }
    }
    /* ******************************************* */
    /* process queued messages (ie: from database) */
    /* ******************************************* */
    $query = "\n\t\tSELECT *\n\t\tFROM " . T_MESSAGE . "\n\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\tAND m_destination = '{$currentPlayer}'\n\t";
    $result = $mysql->fetch_array($query, __LINE__, __FILE__);
    foreach ($result as $message) {
        switch ($message['m_type']) {
            case 'undo':
                switch ($message['m_status']) {
                    case 'request':
                        $isUndoRequested = true;
                        break;
                    case 'approved':
                        $query = "\n\t\t\t\t\t\t\tDELETE FROM " . T_MESSAGE . "\n\t\t\t\t\t\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\t\t\t\t\t\t\tAND m_type = 'undo'\n\t\t\t\t\t\t\t\tAND m_status = 'approved'\n\t\t\t\t\t\t\t\tAND m_destination = '{$currentPlayer}'\n\t\t\t\t\t\t";
                        $mysql->query($query, __LINE__, __FILE__);
                        $statusMessage .= "Undo approved";
                        break;
                    case 'denied':
                        $undoing = false;
                        $query = "\n\t\t\t\t\t\t\tDELETE FROM " . T_MESSAGE . "\n\t\t\t\t\t\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\t\t\t\t\t\t\tAND m_type = 'undo'\n\t\t\t\t\t\t\t\tAND m_status = 'denied'\n\t\t\t\t\t\t\t\tAND m_destination = '{$currentPlayer}'\n\t\t\t\t\t\t";
                        $mysql->query($query, __LINE__, __FILE__);
                        $statusMessage .= "Undo denied";
                        break;
                }
                break;
            case 'draw':
                switch ($message['m_status']) {
                    case 'request':
                        $isDrawRequested = true;
                        break;
                    case 'approved':
                        $query = "\n\t\t\t\t\t\t\tDELETE FROM " . T_MESSAGE . "\n\t\t\t\t\t\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\t\t\t\t\t\t\tAND m_type = 'draw'\n\t\t\t\t\t\t\t\tAND m_status = 'approved'\n\t\t\t\t\t\t\t\tAND m_destination = '{$currentPlayer}'\n\t\t\t\t\t\t";
                        $mysql->query($query, __LINE__, __FILE__);
                        $statusMessage .= "Draw approved";
                        break;
                    case 'denied':
                        $query = "\n\t\t\t\t\t\t\tDELETE FROM " . T_MESSAGE . "\n\t\t\t\t\t\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\t\t\t\t\t\t\tAND m_type = 'draw'\n\t\t\t\t\t\t\t\tAND m_status = 'approved'\n\t\t\t\t\t\t\t\tAND m_destination = '{$currentPlayer}'\n\t\t\t\t\t\t";
                        $mysql->query($query, __LINE__, __FILE__);
                        $statusMessage .= "Draw denied";
                        break;
                }
                break;
        }
    }
    /* requests pending */
    $query = "\n\t\tSELECT *\n\t\tFROM " . T_MESSAGE . "\n\t\tWHERE m_game_id = '{$_SESSION['game_id']}'\n\t\t\tAND m_status = 'request'\n\t\t\tAND m_destination = '{$_SESSION['opponent']['p_color']}'\n\t";
    $result = $mysql->fetch_array($query, __LINE__, __FILE__);
    foreach ($result as $message) {
        switch ($message['m_type']) {
            case 'undo':
                $statusMessage .= "Your undo request is pending";
                break;
            case 'draw':
                $statusMessage .= "Your request for a draw is pending";
                break;
        }
    }
    /* game level status: draws, resignations and checkmate */
    /* if checkmate, update games table */
    $msgFr = 'white' == $curTurn ? 'black' : 'white';
    $msgTo = 'white' == $curTurn ? 'white' : 'black';
    if (isset($movesArray[$num_moves]['check']) && 'mate' == $movesArray[$num_moves]['check']) {
        $query = "\n\t\t\tUPDATE " . T_GAME . "\n\t\t\tSET g_game_message = 'Checkmate'\n\t\t\t\t, g_message_from = '{$msgFr}'\n\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t";
        $mysql->query($query, __LINE__, __FILE__);
        adjust_stats($_SESSION['player']['p_id'], $_SESSION['opponent']['p_id'], 1, 0);
        // let the loser know the bad news
        call("webchessMail('checkmate', {$_SESSION[$msgTo]['p_email']}, '', {$_SESSION[$msgFr]['p_username']}, '')");
        webchessMail('checkmate', $_SESSION[$msgTo]['p_email'], '', $_SESSION[$msgFr]['p_username'], '');
    }
    $query = "\n\t\tSELECT g_game_message\n\t\t\t, g_message_from\n\t\tFROM " . T_GAME . "\n\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t";
    $message = $mysql->fetch_assoc($query, __LINE__, __FILE__);
    if ('Draw' == $message['g_game_message']) {
        $statusMessage .= "Game ended in a draw";
        $isGameOver = true;
    }
    if ('Player Resigned' == $message['g_game_message']) {
        $statusMessage .= $_SESSION[$message['g_message_from']]['p_username'] . " has resigned the game";
        $isGameOver = true;
    }
    if ('Checkmate' == $message['g_game_message']) {
        $statusMessage .= "Checkmate! {$_SESSION[$message['g_message_from']]['p_username']} has won the game";
        $isGameOver = true;
        $isCheckMate = true;
    }
}