Beispiel #1
0
    function process_move($ConfigFile, $player_id, $gid, $move)
    {
        if ($gid == "" || $move == "") {
            return "F" . $this->zero_pad($player_id, 8) . $move;
        }
        // TODO, do timeout check for this game.
        $move_stat = "F";
        $next_color = "b";
        $other_player;
        if ($this->check_move($move)) {
            //include config file
            include $ConfigFile;
            // connect to mysql and open database
            $db_my = mysql_connect($conf['database_host'], $conf['database_login'], $conf['database_pass']) or die("Couldn't connect to the database.");
            @mysql_select_db($conf['database_name']) or die("Unable to select database");
            $sti = "SELECT w_player_id, b_player_id, next_move, w_time_used, b_time_used, start_time FROM game WHERE game_id='" . $gid . "'";
            $stireturn = mysql_query($sti) or die(mysql_error());
            $stinum = mysql_numrows($stireturn);
            if ($stinum != 0) {
                if ($player_id == mysql_result($stireturn, 0, "w_player_id")) {
                    $next_color = "b";
                    $other_player = mysql_result($stireturn, 0, "b_player_id");
                } else {
                    $next_color = "w";
                    $other_player = mysql_result($stireturn, 0, "w_player_id");
                }
                $w_time_used = (int) mysql_result($stireturn, 0, 'w_time_used');
                $b_time_used = (int) mysql_result($stireturn, 0, 'b_time_used');
                $start_time = (int) mysql_result($stireturn, 0, 'start_time');
                //castling
                if ($move2 = checkCastling($move, $gid, $ConfigFile)) {
                    $st = "INSERT INTO move_history(game_id,player_id,move,time) VALUES('" . $gid . "'," . $player_id . ",'" . $move2 . "'," . time() . ")";
                    mysql_query($st) or die(mysql_error());
                } elseif ($move2 = checkPromotion($move)) {
                    $st = "INSERT INTO move_history(game_id,player_id,move,time) VALUES('" . $gid . "'," . $player_id . ",'" . $move2 . "'," . time() . ")";
                    mysql_query($st) or die(mysql_error());
                } elseif ($move2 = checkEnpassent($move)) {
                    $st = "INSERT INTO move_history(game_id,player_id,move,time) VALUES('" . $gid . "'," . $player_id . ",'" . $move2 . "'," . time() . ")";
                    mysql_query($st) or die(mysql_error());
                } else {
                    $st = "INSERT INTO move_history(game_id,player_id,move,time) VALUES('" . $gid . "'," . $player_id . ",'" . $move . "'," . time() . ")";
                    mysql_query($st) or die(mysql_error());
                }
                // Get the game timing mode in use, along with any time controls
                //$query = "SELECT * FROM cfm_game_options WHERE o_gameid='" . $gid . "'";
                $query = <<<qq
SELECT cfm_game_options.*, timed_games.moves1, timed_games.time1, timed_games.moves2, timed_games.time2
FROM cfm_game_options
LEFT JOIN timed_games ON cfm_game_options.o_gameid = timed_games.id
WHERE o_gameid = '{$gid}'
qq;
                $return = mysql_query($query) or die(mysql_error());
                $num = mysql_numrows($return);
                $timing_mode = (int) mysql_result($return, 0, "time_mode");
                $m1 = (int) @mysql_result($return, $i, 'moves1');
                $m2 = (int) @mysql_result($return, $i, 'moves2');
                $t1 = (int) @mysql_result($return, $i, 'time1') * 60;
                $t2 = (int) @mysql_result($return, $i, 'time2') * 60;
                //$timing_type = mysql_result($return, 0, "o_timetype");
                $game_update = array();
                if ($timing_mode == 1) {
                    $now = time();
                    //$timetype = substr(trim(strtolower($timing_type)), 2);
                    // If time controls are used, get the number of moves to work out which time control
                    // applies. If a time control has been reached, remove the required time to the player's
                    // 'used' time.
                    if ($m1) {
                        $query = "SELECT count(*) as `count` FROM move_history WHERE game_id = '{$gid}' AND player_id = {$player_id}";
                        $return = mysql_query($query) or die(mysql_error());
                        $move_cnt = mysql_result($return, 0, 'count');
                        if ($move_cnt == $m1) {
                            if ($next_color == 'b') {
                                $w_time_used -= $t1;
                            } elseif ($next_color == 'w') {
                                $b_time_used -= $t1;
                            }
                        } elseif ($move_cnt > $m1) {
                            if (($move_cnt - $m1) % $m2 == 0) {
                                if ($next_color == 'b') {
                                    $w_time_used -= $t2;
                                } elseif ($next_color == 'w') {
                                    $b_time_used -= $t2;
                                }
                            }
                        }
                    }
                    // Get the 2nd last move's time. Subtract the move's time from the current time to work
                    // out how long it took the player to make this move. The first move made doesn't
                    // attract any time usage.
                    $query = "SELECT `time` FROM move_history WHERE game_id = '{$gid}' ORDER BY `time` DESC LIMIT 1,1";
                    $return = mysql_query($query) or die(mysql_error());
                    $num = mysql_numrows($return);
                    if ($num != 0) {
                        $last_move_time = (int) trim(mysql_result($return, 0, "time"));
                    } else {
                        $last_move_time = $now;
                        $game_update[] = "start_time = {$now}";
                    }
                    $diff = $now - $last_move_time;
                    if ($next_color == 'w') {
                        $game_update[] = "b_time_used=" . ($b_time_used + $diff);
                    } else {
                        $game_update[] = "w_time_used=" . ($w_time_used + $diff);
                    }
                }
                $game_update[] = "next_move='{$next_color}'";
                $game_update = implode(', ', $game_update);
                $st = "UPDATE game SET {$game_update} WHERE game_id='" . $gid . "'";
                //echo "run $st";
                //exit();
                mysql_query($st) or die(mysql_error());
                $st = "INSERT INTO message_queue(player_id, message, posted) VALUES(" . $other_player . ",'" . $this->add_header("M", $move_stat . $this->zero_pad($player_id, 8) . $gid . $move, "0") . "'," . time() . ")";
                mysql_query($st) or die(mysql_error());
                //////////////////////////////////////////////
                //Instantiate theCR3DCQuery Class
                $oR3DCQuery = new CR3DCQuery($this->ChessCFGFileLocation);
                $isblack = $oR3DCQuery->IsPlayerBlack($this->ChessCFGFileLocation, $gid, $other_player);
                $isrealtime = $oR3DCQuery->IsRequestRealTime($this->ChessCFGFileLocation, $gid, $isblack);
                if ($oR3DCQuery->MoveNotification($other_player) == true && $isrealtime != "IDS_REAL_TIME") {
                    $requestorname = $oR3DCQuery->GetUserIDByPlayerID($this->ChessCFGFileLocation, $player_id);
                    $otherguysname = $oR3DCQuery->GetUserIDByPlayerID($this->ChessCFGFileLocation, $other_player);
                    $otheremail = $oR3DCQuery->GetEmailByPlayerID($this->ChessCFGFileLocation, $other_player);
                    $subject = $this->GetStringFromStringTable("IDS_CR3DCQUERY_EMAIL_TVST_17", $ConfigFile);
                    $aTags1 = array("['otherguysname']", "['requestorname']", "['gid']", "['move']", "['siteurl']", "['sitename']");
                    $aReplaceTags1 = array($otherguysname, $requestorname, $gid, $move, $this->TrimRSlash($conf['site_url']), $conf['site_name']);
                    $bodyp1 = str_replace($aTags1, $aReplaceTags1, $this->GetStringFromStringTable("IDS_CR3DCQUERY_EMAIL_TVST_18", $ConfigFile));
                    $this->SendEmail($otheremail, $conf['registration_email'], $conf['site_name'], $subject, $bodyp1);
                }
                unset($oR3DCQuery);
                //////////////////////////////////////////////
                //////////////////////////////////////////////
                //Check if the king was killed
                //////////////////////////////////////////////
                $FEN = $this->request_FEN($this->ChessCFGFileLocation, $gid);
                $Moves = "";
                $RestOfSentence = "";
                list($Moves, $RestOfSentence) = preg_split("/ /", $FEN);
                $nwhitek = strpos($Moves, 'k');
                if ($nwhitek === false) {
                    $st = "UPDATE game SET status='C', completion_status='B' WHERE game_id='" . $gid . "'";
                    mysql_query($st) or die(mysql_error());
                }
                $nblackk = strpos($Moves, 'K');
                if ($nblackk === false) {
                    $st = "UPDATE game SET status='C', completion_status='W' WHERE game_id='" . $gid . "'";
                    mysql_query($st) or die(mysql_error());
                }
                //////////////////////////////////////////////
                $move_stat = "S";
            }
        }
        return $move_stat . $this->zero_pad($player_id, 8) . $gid . $move;
    }