Esempio n. 1
0
             $old_moves = $mysql->fetch_value($query, __LINE__, __FILE__);
             // if it's black's turn...
             if (0 == $old_moves % 2) {
                 $player1 = $old_game['g_white_player_id'];
                 // give white a win
                 $player2 = $old_game['g_black_player_id'];
                 // give black a loss
             } else {
                 $player1 = $old_game['g_black_player_id'];
                 // give black a win
                 $player2 = $old_game['g_white_player_id'];
                 // give white a loss
             }
             // require a file
             require_once './includes/chessdb.inc.php';
             adjust_stats($player1, $player2, 1, 0);
         }
         // ...clear the history...
         $query = "\n\t\t\t\t\tDELETE FROM " . T_HISTORY . "\n\t\t\t\t\tWHERE h_game_id = '{$old_game['g_id']}'\n\t\t\t\t";
         $mysql->query($query, __LINE__, __FILE__);
         // ...and the messages...
         $query = "\n\t\t\t\t\tDELETE FROM " . T_MESSAGE . "\n\t\t\t\t\tWHERE m_game_id = '{$old_game['g_id']}'\n\t\t\t\t";
         $mysql->query($query, __LINE__, __FILE__);
         // ...and the chat...
         $query = "\n\t\t\t\t\tDELETE FROM " . T_CHAT . "\n\t\t\t\t\tWHERE c_game_id = '{$old_game['g_id']}'\n\t\t\t\t";
         $mysql->query($query, __LINE__, __FILE__);
         // ...and finally the game itself from the database
         $query = "\n\t\t\t\t\tDELETE FROM " . T_GAME . "\n\t\t\t\t\tWHERE g_id = '{$old_game['g_id']}'\n\t\t\t\t";
         $mysql->query($query, __LINE__, __FILE__);
     }
 }
Esempio n. 2
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;
    }
}