Example #1
0
function saveHistory()
{
    global $CFG_TABLE;
    global $board, $isPromoting, $history, $numMoves, $isInCheck, $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;
    }
    /* set destination row for pawn promotion */
    if ($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) {
        $targetRow = 0;
    } else {
        $targetRow = 7;
    }
    /* determine if move results in pawn promotion */
    if (($board[$_POST['fromRow']][$_POST['fromCol']] & COLOR_MASK) == PAWN && $_POST['toRow'] == $targetRow) {
        $isPromoting = true;
    } else {
        $isPromoting = false;
    }
    /* determine who's playing based on number of moves so far */
    if ($numMoves == -1 || $numMoves % 2 == 1) {
        $curColor = "white";
        $oppColor = "black";
    } else {
        $curColor = "black";
        $oppColor = "white";
    }
    /* add move to history */
    $numMoves++;
    $history[$numMoves]['gamedID'] = $_SESSION['gameID'];
    $history[$numMoves]['curPiece'] = getPieceName($board[$_POST['fromRow']][$_POST['fromCol']]);
    $history[$numMoves]['curColor'] = $curColor;
    $history[$numMoves]['fromRow'] = $_POST['fromRow'];
    $history[$numMoves]['fromCol'] = $_POST['fromCol'];
    $history[$numMoves]['toRow'] = $_POST['toRow'];
    $history[$numMoves]['toCol'] = $_POST['toCol'];
    $history[$numMoves]['promotedTo'] = null;
    if ($isInCheck) {
        $history[$numMoves]['isInCheck'] = 1;
    } else {
        $history[$numMoves]['isInCheck'] = 0;
    }
    if (DEBUG) {
        if ($history[$numMoves]['curPiece'] == '') {
            echo "WARNING!!!  missing piece at " . $_POST['fromRow'] . ", " . $_POST['fromCol'] . ": " . $board[$_POST['fromRow']][$_POST['fromCol']] . "<p>\n";
        }
    }
    if ($board[$_POST['toRow']][$_POST['toCol']] == 0) {
        $tmpQuery = "INSERT INTO " . $CFG_TABLE[history] . " (timeOfMove, gameID, curPiece, curColor, fromRow, fromCol, toRow, toCol, replaced, promotedTo, isInCheck) VALUES (Now(), " . $_SESSION['gameID'] . ", '" . getPieceName($board[$_POST['fromRow']][$_POST['fromCol']]) . "', '{$curColor}', " . $_POST['fromRow'] . ", " . $_POST['fromCol'] . ", " . $_POST['toRow'] . ", " . $_POST['toCol'] . ", null, null, " . $history[$numMoves]['isInCheck'] . ")";
        $history[$numMoves]['replaced'] = null;
        $tmpReplaced = "";
    } else {
        $tmpQuery = "INSERT INTO " . $CFG_TABLE[history] . " (timeOfMove, gameID, curPiece, curColor, fromRow, fromCol, toRow, toCol, replaced, promotedTo, isInCheck) VALUES (Now(), " . $_SESSION['gameID'] . ", '" . getPieceName($board[$_POST['fromRow']][$_POST['fromCol']]) . "', '{$curColor}', " . $_POST['fromRow'] . ", " . $_POST['fromCol'] . ", " . $_POST['toRow'] . ", " . $_POST['toCol'] . ", '" . getPieceName($board[$_POST['toRow']][$_POST['toCol']]) . "', null, " . $history[$numMoves]['isInCheck'] . ")";
        $history[$numMoves]['replaced'] = getPieceName($board[$_POST['toRow']][$_POST['toCol']]);
        $tmpReplaced = $history[$numMoves]['replaced'];
    }
    mysql_query($tmpQuery);
    /* if email notification is activated and move does not result in a pawn's promotion... */
    /* NOTE: moves resulting in pawn promotion are handled by savePromotion() above */
    if ($CFG_USEEMAILNOTIFICATION && !$isPromoting && !$_SESSION['isSharedPC']) {
        /* get opponent's player ID */
        if ($oppColor == 'white') {
            $tmpOpponentID = mysql_query("SELECT whitePlayer FROM " . $CFG_TABLE[games] . " WHERE gameID = " . $_SESSION['gameID']);
        } else {
            $tmpOpponentID = mysql_query("SELECT blackPlayer FROM " . $CFG_TABLE[games] . " WHERE gameID = " . $_SESSION['gameID']);
        }
        $opponentID = mysql_result($tmpOpponentID, 0);
        /* if opponent is using email notification... */
        $tmpOpponentEmail = mysql_query("SELECT value FROM " . $CFG_TABLE[preferences] . " WHERE playerID = " . $opponentID . " AND preference = 'emailNotification'");
        if (mysql_num_rows($tmpOpponentEmail) > 0) {
            $opponentEmail = mysql_result($tmpOpponentEmail, 0);
            if ($opponentEmail != '') {
                /* get opponent's nick */
                $tmpOpponentNick = mysql_query("SELECT nick FROM " . $CFG_TABLE[players] . " WHERE playerID = " . $_SESSION['playerID']);
                $opponentNick = mysql_result($tmpOpponentNick, 0);
                /* get opponent's prefered history type */
                $tmpOpponentHistory = mysql_query("SELECT value FROM " . $CFG_TABLE[preferences] . " WHERE playerID = " . $opponentID . " AND preference = 'history'");
                /* default to PGN */
                if (mysql_num_rows($tmpOpponentHistory) > 0) {
                    $opponentHistory = mysql_result($tmpOpponentHistory, 0);
                } else {
                    $opponentHistory = 'pgn';
                }
                /* notify opponent of move via email */
                if ($opponentHistory == 'pgn') {
                    webchessMail('move', $opponentEmail, moveToPGNString($history[$numMoves]['curColor'], $history[$numMoves]['curPiece'], $history[$numMoves]['fromRow'], $history[$numMoves]['fromCol'], $history[$numMoves]['toRow'], $history[$numMoves]['toCol'], $tmpReplaced, '', $isInCheck), $opponentNick, $_SESSION['gameID']);
                } else {
                    webchessMail('move', $opponentEmail, moveToVerbousString($history[$numMoves]['curColor'], $history[$numMoves]['curPiece'], $history[$numMoves]['fromRow'], $history[$numMoves]['fromCol'], $history[$numMoves]['toRow'], $history[$numMoves]['toCol'], $tmpReplaced, '', $isInCheck), $opponentNick, $_SESSION['gameID']);
                }
            }
        }
    }
}
    function saveHistory($post = "")
    {
        global $db,$db_prefix, $board, $isPromoting, $history, $numMoves, $is_in_check, $CFG_USEEMAILNOTIFICATION;

        if ($post != "")
            $_POST = $post;


        /* set destination row for pawn promotion */
        if ($board[$_POST['from_row']][$_POST['from_col']] & BLACK)
            $targetRow = 0;
        else
            $targetRow = 7;

        /* determine if move results in pawn promotion */
        if ((($board[$_POST['from_row']][$_POST['from_col']] & COLOR_MASK) == PAWN) && ($_POST['to_row'] == $targetRow))
            $isPromoting = true;
        else
            $isPromoting = false;

        /* determine who's playing based on number of moves so far */
        if (($numMoves == -1) || ($numMoves % 2 == 1))
        {
            $cur_color = "white";
            $oppColor = "black";
            $targetRow = 7;
        }
        else
        {
            $cur_color = "black";
            $oppColor = "white";
            $targetRow = 0;
        }

        /* add move to history */
        $numMoves++;
        $history[$numMoves]['game_id'] = $_SESSION['game_id'];
        $history[$numMoves]['cur_piece'] = getPieceName($board[$_POST['from_row']][$_POST['from_col']]);
        $history[$numMoves]['cur_color'] = $cur_color;
        $history[$numMoves]['from_row'] = $_POST['from_row'];
        $history[$numMoves]['from_col'] = $_POST['from_col'];
        $history[$numMoves]['to_row'] = $_POST['to_row'];
        $history[$numMoves]['to_col'] = $_POST['to_col'];
        $history[$numMoves]['promoted_to'] = null;

        if ($is_in_check)
            $history[$numMoves]['is_in_check'] = 1;
        else
            $history[$numMoves]['is_in_check'] = 0;

        /*
        if (DEBUG)
        {
            if ($history[$numMoves]['curPiece'] == '')
                echo ("WARNING!!!  missing piece at ".$_POST['from_row'].", ".$_POST['from_col'].": ".$board[$_POST['from_row']][$_POST['from_col']]."<p>\n");
        }
        */

        if ($board[$_POST['to_row']][$_POST['to_col']] == 0)
        {
            $tmpQuery = "INSERT INTO {$db_prefix}history (time_of_move, game_id, cur_piece, cur_color, from_row, from_col, to_row, to_col, replaced, promoted_to, is_in_check) VALUES (Now(), '$game_id', '".getPieceName($board[$_POST['from_row']][$_POST['from_col']])."', '$cur_color', ".$_POST['from_row'].", ".$_POST['from_col'].", ".$_POST['to_row'].", ".$_POST['to_col'].", null, null, ".$history[$numMoves]['is_in_check'].")";
            $history[$numMoves]['replaced'] = null;
            $tmpReplaced = "";
        }
        else
        {
            $tmpQuery = "INSERT INTO {$db_prefix}history (time_of_move, game_id, cur_piece, cur_color, from_row, from_col, to_row, to_col, replaced, promoted_to, is_in_check) VALUES (Now(), '$game_id', '".getPieceName($board[$_POST['from_row']][$_POST['from_col']])."', '$cur_color', ".$_POST['from_row'].", ".$_POST['from_col'].", ".$_POST['to_row'].", ".$_POST['to_col'].", '".getPieceName($board[$_POST['to_row']][$_POST['to_col']])."', null, ".$history[$numMoves]['is_in_check'].")";

            $history[$numMoves]['replaced'] = getPieceName($board[$_POST['to_row']][$_POST['to_col']]);
            $tmpReplaced = $history[$numMoves]['replaced'];
        }

       $res =  mysql_query($tmpQuery) or die(mysql_error());


        /* if email notification is activated and move does not result in a pawn's promotion... */
        /* NOTE: moves resulting in pawn promotion are handled by savePromotion() above */
        if ($CFG_USEEMAILNOTIFICATION && !$isPromoting)
        {
            /* get opponent's player ID */
            if ($oppColor == 'white')
                $tmpOpponentID = mysql_query("SELECT white_player FROM games WHERE game_id = ".$_SESSION['game_id']);
            else
                $tmpOpponentID = mysql_query("SELECT black_player FROM games WHERE game_id = ".$_SESSION['game_id']);

            $opponentID = mysql_result($tmpOpponentID, 0);

            /* 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 != '')
                {
                    /* get opponent's nick */
                    $tmpOpponentNick = mysql_query("SELECT firstName FROM {$db_prefix}players WHERE playerID = ".$_SESSION['playerID']);
                    $opponentNick = mysql_result($tmpOpponentNick, 0);

                    /* get opponent's prefered history type */
                    $tmpOpponentHistory = mysql_query("SELECT value FROM preferences WHERE playerID = ".$opponentID." AND preference = 'history'");

                    /* default to PGN */
                    if (mysql_num_rows($tmpOpponentHistory) > 0)
                        $opponentHistory = mysql_result($tmpOpponentHistory, 0);
                    else
                        $opponentHistory = 'pgn';

                    /* notify opponent of move via email */
                    if ($opponentHistory == 'pgn')
                        webchessMail('move', $opponentEmail, moveToPGNString($history[$numMoves]['cur_color'], $history[$numMoves]['curPiece'], $history[$numMoves]['from_row'], $history[$numMoves]['from_col'], $history[$numMoves]['to_row'], $history[$numMoves]['to_col'], $tmpReplaced, '', $is_in_check), $opponentNick);
                    else
                        webchessMail('move', $opponentEmail, moveToVerbousString($history[$numMoves]['cur_color'], $history[$numMoves]['curPiece'], $history[$numMoves]['from_row'], $history[$numMoves]['from_col'], $history[$numMoves]['to_row'], $history[$numMoves]['to_col'], $tmpReplaced, '', $is_in_check), $opponentNick);
                }
            }
        }
    }
Example #3
0
function writeVerbousHistory()
{
    global $history, $numMoves;
    for ($i = 0; $i <= $numMoves; $i++) {
        $tmpReplaced = "";
        if (!is_null($history[$i]['replaced'])) {
            $tmpReplaced = $history[$i]['replaced'];
        }
        $tmpPromotedTo = "";
        if (!is_null($history[$i]['promotedTo'])) {
            $tmpPromotedTo = $history[$i]['promotedTo'];
        }
        $tmpCheck = $history[$i]['isInCheck'] == 1;
        $moves[$i / 2][$i & 1] = moveToVerbousString($history[$i]['curColor'], $history[$i]['curPiece'], $history[$i]['fromRow'], $history[$i]['fromCol'], $history[$i]['toRow'], $history[$i]['toCol'], $tmpReplaced, $tmpPromotedTo, $tmpCheck);
    }
    return $moves;
}
Example #4
0
    function writeVerbousHistory()
    {
        global $history, $numMoves, $MSG_LANG;

        echo ("<table width='300' border='1'>\n");
        echo ("<tr><th bgcolor='beige' colspan='2'>".$MSG_LANG["history"]."</th></tr>\n");

        for ($i = $numMoves; $i >= 0; $i--)
        {
            if ($i % 2 == 1)
            {
                echo ("<tr bgcolor='black'>");
                echo ("<td width='20'><font color='white'>".($i + 1)."</font></td><td><font color='white'>");
            }
            else
            {
                echo ("<tr bgcolor='white'>");
                echo ("<td width='20'>".($i + 1)."</td><td><font color='black'>");
            }

            $tmpReplaced = "";
            if (!is_null($history[$i]['replaced']))
                $tmpReplaced = $history[$i]['replaced'];

            $tmpPromotedTo = "";
            if (!is_null($history[$i]['promotedTo']))
                $tmpPromotedTo = $history[$i]['promotedTo'];

            $tmpCheck = ($history[$i]['is_in_check'] == 1);

            echo(moveToVerbousString($history[$i]['cur_color'], $history[$i]['cur_piece'], $history[$i]['from_row'], $history[$i]['from_col'], $history[$i]['to_row'], $history[$i]['to_col'], $tmpReplaced, $tmpPromotedTo, $tmpCheck));

            echo ("</font></td></tr>\n");
        }

        echo ("<tr bgcolor='#BBBBBB'><td>0</td><td>".$MSG_LANG["newgame"]."</td></tr>\n");
        echo ("</table>\n");
    }