Example #1
0
        $query = "\n\t\t\tINSERT INTO " . T_CHAT . "\n\t\t\t\t(c_game_id, c_player_id, c_time, c_message, c_private)\n\t\t\tVALUES\n\t\t\t\t('{$_SESSION['game_id']}', '{$_SESSION['player_id']}', NOW( ), '{$_POST['txtChatbox']}', '{$private}')\n\t\t";
        $mysql->query($query, __LINE__, __FILE__);
    }
    // refresh the page to avoid double posts
    if (!DEBUG) {
        header('Location: chess.php');
    }
}
//*/
//******************************************************************************
//  send wake up email
//******************************************************************************
$wake_up_sent = false;
if (isset($_POST['wakeID']) && $_SESSION['game_id'] == $_POST['wakeID']) {
    call("webchessMail('wakeup',{$_SESSION['opponent']['p_email']},'',{$_SESSION['username']},{$_SESSION['game_id']})");
    $wake_up_sent = webchessMail('wakeup', $_SESSION['opponent']['p_email'], '', $_SESSION['username'], $_SESSION['game_id']);
}
//*/
//******************************************************************************
//  load game from database for display
//******************************************************************************
// get FEN array
$query = "\n\tSELECT h_fen\n\tFROM " . T_HISTORY . "\n\tWHERE h_game_id = '{$_SESSION['game_id']}'\n\tORDER BY h_time\n";
$FENarray = $mysql->fetch_value_array($query, __LINE__, __FILE__);
$num_moves = count($FENarray) - 1;
// remove one for initpos
loadGame();
// sets up board using last entry in ".T_HISTORY." table (chessdb.inc.php)
// convert the current FEN array to an array of standard moves
FENtomoves();
// (chessutils.inc.php)
        if (is_numeric($_POST['txtReload'])) {
            if (intval($_POST['txtReload']) >= $CFG_MINAUTORELOAD) {
                $_SESSION['pref_autoreload'] = intval($_POST['txtReload']);
            } else {
                $_SESSION['pref_autoreload'] = $CFG_MINAUTORELOAD;
            }
        } else {
            $_SESSION['pref_autoreload'] = $CFG_MINAUTORELOAD;
        }
        if ($CFG_USEEMAILNOTIFICATION) {
            $_SESSION['pref_emailnotification'] = $_POST['txtEmailNotification'];
        }
        break;
    case 'TestEmail':
        if ($CFG_USEEMAILNOTIFICATION) {
            webchessMail('test', $_SESSION['pref_emailnotification'], '', '');
        }
        break;
}
/* check session status */
require_once 'sessioncheck.php';
/* set default playing mode to different PCs (as opposed to both players sharing a PC) */
$_SESSION['isSharedPC'] = false;
/* Language selection */
require_once "languages/" . $_SESSION['pref_language'] . "/strings.inc.php";
?>

<html>
<head>
	<title>WebChess</title>
	<script type="text/javascript">
Example #3
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;
    }
}
            $opponentID = mysql_result($tmpOpponentID, 0);
            if ($opponentID == $_SESSION['playerID']) {
                $tmpOpponentID = mysql_query("SELECT blackPlayer FROM games WHERE gameID = " . $_POST['gameID']);
                $opponentID = mysql_result($tmpOpponentID, 0);
            }
            $tmpQuery = "DELETE FROM games WHERE gameID = " . $_POST['gameID'];
            mysql_query($tmpQuery);
            /* if email notification is activated... */
            if ($CFG_USEEMAILNOTIFICATION) {
                /* if opponent is using email notification... */
                $tmpOpponentEmail = mysql_query("SELECT value FROM preferences WHERE playerID = " . $opponentID . " AND preference = 'emailNotification'");
                if (mysql_num_rows($tmpOpponentEmail) > 0) {
                    $opponentEmail = mysql_result($tmpOpponentEmail, 0);
                    if ($opponentEmail != '') {
                        /* notify opponent of invitation via email */
                        webchessMail('withdrawal', $opponentEmail, '', $_SESSION['nick']);
                    }
                }
            }
        }
        break;
}
?>
<html>
<head>
    <title>Webmaster - Join / Create Game</title>
<LINK rel="stylesheet" href="themes/<?php 
echo $_SESSION["pref_colortheme"];
?>
/styles.css" type="text/css">
<script>
Example #5
0
         $mysql->query($query, __LINE__, __FILE__);
     }
 }
 // reset passwords before deleting as well
 if (isset($_POST['resetpass'])) {
     foreach ($_POST['resetpass'] as $user) {
         // reset the password to change!me
         $pass = password_make('change!me');
         $query = "\n\t\t\t\t\t\tUPDATE " . T_PLAYER . "\n\t\t\t\t\t\tSET p_password = '******'\n\t\t\t\t\t\tWHERE p_id = '{$user}'\n\t\t\t\t\t\tLIMIT 1\n\t\t\t\t\t";
         $mysql->query($query, __LINE__, __FILE__);
         // get the users email address
         $query = "\n\t\t\t\t\t\tSELECT p_email\n\t\t\t\t\t\tFROM " . T_PLAYER . "\n\t\t\t\t\t\tWHERE p_id = '{$user}'\n\t\t\t\t\t";
         $email = $mysql->fetch_value($query, __LINE__, __FILE__);
         // email the user and let them know their password has been changed
         call("webchessMail('passupdate',{$email},'','','')");
         webchessMail('passupdate', $email, '', '', '');
     }
 }
 $i = 0;
 if (isset($_POST['delete'])) {
     foreach ($_POST['delete'] as $user) {
         $query = "\n\t\t\t\t\t\tSELECT p_username\n\t\t\t\t\t\tFROM " . T_PLAYER . "\n\t\t\t\t\t\tWHERE p_id = '{$user}'\n\t\t\t\t\t";
         $name = $mysql->fetch_value($query, __LINE__, __FILE__);
         // protect the root admin, just in case
         if ($CFG_ROOT_ADMIN != $name) {
             // find all the games that user was playing
             $query = "\n\t\t\t\t\t\t\tSELECT g_id\n\t\t\t\t\t\t\tFROM " . T_GAME . "\n\t\t\t\t\t\t\tWHERE g_black_player_id = '{$user}'\n\t\t\t\t\t\t\t\tOR g_white_player_id = '{$user}'\n\t\t\t\t\t\t";
             $list = $mysql->fetch_value_array($query, __LINE__, __FILE__);
             call('list');
             $list[] = 0;
             $games = implode(',', $list);
            } else {
                if ($row['curColor'] == "black") {
                    $playersColor = "white";
                }
            }
        } else {
            $playersColor = "white";
        }
        $p = mysql_query("SELECT whitePlayer,blackPlayer FROM games WHERE gameID='{$tmpOldGame['gameID']}'");
        $row = mysql_fetch_array($p);
        if ($playersColor == 'white') {
            $player_id = $row[0];
        } else {
            $player_id = $row[1];
        }
        //send reminder email
        $tmpEmail = mysql_query("SELECT email FROM players WHERE playerID='{$player_id}' ");
        $Email = mysql_result($tmpEmail, 0);
        if ($Email != '') {
            $game = $tmpOldGame['gameID'];
            webchessMail('reminder', $Email, '', '', $game);
            //set we have sent reminder to 1
            $tmpQuery = "UPDATE games SET reminder = '1' WHERE gameID = " . $tmpOldGame[gameID];
            mysql_query($tmpQuery);
        }
    }
}
// END OF SEND EMAIL REMINDER FOR GAMES NEARLY LOST ON TIME
?>

    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;
        }

    }
                }
                $tmpQuery .= "$_POST[timelimit], $white, $black, 'playerInvited', '".$tmpColor."', NOW(), NOW(),".getRating($white).",".getRating($black).",".getRatingMonth($white).",".getRatingMonth($black).",'".$_POST['oficial']."',".getPV($black).",".getPV($white).",'".$_POST['thematic']."')";
                mysql_query($tmpQuery);

                /* if email notification is activated... */
                if ($CFG_USEEMAILNOTIFICATION)
                {
                    /* if opponent is using email notification... */
                    $tmpOpponentEmail = mysql_query("SELECT value FROM preferences WHERE playerID = ".$_POST['opponent']." AND preference = 'emailNotification'");
                    if (mysql_num_rows($tmpOpponentEmail) > 0)
                    {
                        $opponentEmail = mysql_result($tmpOpponentEmail, 0);
                        if ($opponentEmail != '')
                        {
                            /* notify opponent of invitation via email */
                            webchessMail('invitation', $opponentEmail, '', $_SESSION['firstName']);
                        }
                    }
                }
            }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta name="Keywords" content="chess,ajedrez,échecs,echecs,scacchi,schach,check,check mate,jaque,jaque mate,queenalice,queen alice,queen,alice,play,game,games,turn based,correspondence,correspondence chess,online chess,play chess online">

        <title>Challenge</title>
Example #9
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;
    }
}