function mobile_get_game_update_on_state_change($xsid) { //$session = base64_decode($xsid); //list($uniq, $player_id) = preg_split("/\|/", $session); if ($_GET['gameid'] != "") { $game_id = $_GET['gameid']; try { $dbh = CSession::$dbh; $side_to_move = $_GET['side_to_move']; $get_game_over = (bool) $_GET['get_game_over']; $get_new_move = (bool) $_GET['get_new_move']; $with_full_update = (bool) $_GET['with_full_update']; $require_full_update = FALSE; $new_move = FALSE; // Work out whose turn it is. $player_w = -1; $player_b = -1; $next_move = ''; $stmt = $dbh->prepare("SELECT `w_player_id`,`b_player_id`,`next_move` FROM `game` WHERE `game_id` = ?"); $stmt->bind_param('s', $game_id); if ($stmt->execute()) { $stmt->bind_result($player_w, $player_b, $next_move); $result = $stmt->fetch(); if ($result) { if ($next_move == NULL) { $next_move = 'w'; } // Game creation does not initially set a next move value. Assume white to move as custom game setup isn't yet implemented. } $stmt->close(); } else { echo "<ERROR>Database Error</ERROR>\n"; return false; } if ($get_new_move) { //echo "next $next_move , side $side_to_move"; if ($next_move != $side_to_move) { echo "<NEW_MOVE>true</NEW_MOVE>\n"; $new_move = TRUE; $require_full_update = TRUE; } else { echo "<NEW_MOVE>false</NEW_MOVE>\n"; } } if ($get_game_over) { // See if the game is over (because the opponent resigned, there was a draw or a player won). $game_result = 0; $stmt = $dbh->prepare("SELECT `completion_status` FROM `game` WHERE `game_id` = ?"); $stmt->bind_param('s', $game_id); if ($stmt->execute()) { $stmt->bind_result($status); $result = $stmt->fetch(); if ($result) { if ($status == "W") { $game_result = 1; } elseif ($status == "B") { $game_result = 2; } elseif ($status == "D") { $game_result = 3; } } $stmt->close(); } else { echo "<ERROR>Database Error</ERROR>\n"; return false; } if ($game_result != 0) { echo "<GAME_OVER>true</GAME_OVER>\n"; $require_full_update = TRUE; } else { echo "<GAME_OVER>false</GAME_OVER>\n"; } } if ($require_full_update && $with_full_update) { // There might be cases where we only want to know if a move was made or the game is over without the details of the game state. mobile_get_full_game_update(); } if ($new_move) { $move = ChessHelper::get_last_move(); echo "<MOVE_SAN>" . $move['SAN'] . "</MOVE_SAN>\n"; echo "<MOVE_FROM>" . $move['from'] . "</MOVE_FROM>\n"; echo "<MOVE_TO>" . $move['to'] . "</MOVE_TO>\n"; } // Return the draw status. $session = base64_decode($xsid); list($uniq, $player_id) = preg_split("/\\|/", $session); $oR3DCQuery = new CR3DCQuery($Root_Path . "bin/config.php"); $isblack = $oR3DCQuery->IsPlayerBlack($Root_Path . "bin/config.php", $game_id, $player_id); $isdraw = $oR3DCQuery->IsRequestDraw($Root_Path . "bin/config.php", $game_id, $isblack); echo "<DRAWCODE>"; echo $isdraw; echo "</DRAWCODE>\n"; } catch (mysqli_sql_exception $e) { echo "<ERROR>Database Connection Error</ERROR>\n"; return false; } } else { echo "<ERROR>IDS_GAME_ID_INVALID</ERROR>\n"; } }
} else { $image = "<img src='./avatars/noimage.jpg'>"; } $userid = $oR3DCQuery->GetUserIDByPlayerID($config, $b_player_id); $wins = 0; $loss = 0; $draws = 0; $oR3DCQuery->GetPlayerStatusrRefByPlayerID($config, $b_player_id, $wins, $loss, $draws); if ($oR3DCQuery->ELOIsActive()) { $points = $oR3DCQuery->ELOGetRating($b_player_id); } else { $points = $oR3DCQuery->GetPointValue($wins, $loss, $draws); } $blackdiv = "\n\t<div id=\"blackclock\"></div>\n\t<div class=\"avatar\">{$image}</div>\n\t<div class=\"userid\">{$userid}</div>\n\t<div class=\"points\">{$points}</div>\n\t"; //check the current player's color. set the location of player information accordingly. i.e if the player is black, black player's information is diplayed at the bottom and vice versa if ($oR3DCQuery->IsPlayerBlack($config, $gid, $_SESSION['id'])) { $player = 'black'; $tdiv = $whitediv; $bdiv = $blackdiv; $imgc = 'b'; } else { $player = 'white'; $bdiv = $whitediv; $tdiv = $blackdiv; $imgc = 'w'; } //get the current FEN data $fen = explode(" ", $oR3DCQuery->GetActualFEN($_SESSION['sid'], $gid)); //set the turn if ($fen[1] == 'w') { $turn = 'white';
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; }
if ($oR3DCQuery->ELOIsActive()) { $oR3DCQuery->ELOCreateRatings(); } $oR3DCQuery->MangeGameTimeOuts(); $oR3DCQuery->Close(); unset($oR3DCQuery); return $bValid; } if (isSessionIDValid($config, $xsid)) { $session = base64_decode($xsid); list($uniq, $player_id) = preg_split("/\\|/", $session); if ($_GET['gameid'] != "") { //Instantiate theCR3DCQuery Class $oR3DCQuery = new CR3DCQuery($config); $oR3DCQuery->GetNewMoveForMobile($_GET['gameid']); $isblack = $oR3DCQuery->IsPlayerBlack($config, $_GET['gameid'], $player_id); $isdraw = $oR3DCQuery->IsRequestDraw($config, $_GET['gameid'], $isblack); echo "<RESPONSE>\n"; echo "<DRAWCODE>" . $isdraw . "</DRAWCODE>\n"; echo "</RESPONSE>\n"; $initiator = ""; $w_player_id = ""; $b_player_id = ""; $status = ""; $completion_status = ""; $start_time = ""; $next_move = ""; $oR3DCQuery->GetGameInfoByRef($config, $_GET['gameid'], $initiator, $w_player_id, $b_player_id, $status, $completion_status, $start_time, $next_move); echo "<RESPONSE>\n"; echo "<STATUS>" . $completion_status . "</STATUS>\n"; echo "</RESPONSE>\n";
} } else { clearTimeout(timer) window.location.reload(true) } } window.onload=starttime <?php //////////// //Instantiate theCR3DCQuery Class $oR3DCQuery = new CR3DCQuery($config); $isblack = $oR3DCQuery->IsPlayerBlack($config, $GID, $_SESSION['id']); $isdraw = $oR3DCQuery->IsRequestDraw($config, $GID, $isblack); $initiator = ""; $w_player_id = ""; $b_player_id = ""; $status = ""; $completion_status = ""; $start_time = ""; $next_move = ""; $oR3DCQuery->GetGameInfoByRef($config, $GID, $initiator, $w_player_id, $b_player_id, $status, $completion_status, $start_time, $next_move); $oR3DCQuery->ManageRealTimeGame($config, $GID); $isrealtime = $oR3DCQuery->IsRequestRealTime($config, $GID, $isblack); if ($isrealtime == "IDS_REAL_TIME") { //Refresh the game board if ($_SESSION['RefreashGameOnlyOnce'] == "") { echo "parent.frames['chessboard'].location='./chess_game2.php?gameid=" . $GID . "';";
$oR3DCQuery->CurrentGameMovePiece($config, $gid, $_SESSION['sid'], $_SESSION['id'], $movestr); $initiator = ""; $w_player_id = ""; $b_player_id = ""; $status = ""; $completion_status = ""; $start_time = ""; $next_move = ""; $oR3DCQuery->GetGameInfoByRef($config, $gid, $initiator, $w_player_id, $b_player_id, $status, $completion_status, $start_time, $next_move); $bUpdateT = true; //checkmate if (get_GameState() == 1) { //if($w_player_id == $_SESSION['id']){ if ($next_move == 'w') { $oR3DCQuery->UpdateGameStatus($config, $gid, "C", "B"); $isblack = $oR3DCQuery->IsPlayerBlack($config, $gid, $_SESSION['id'], true); if ($isblack) { $oR3DCQuery->v2AddTournamentGameQueue($w_player_id, $type, $tid, $gid); } else { $oR3DCQuery->v2AddTournamentGameQueue($b_player_id, $type, $tid, $gid); } $bmove_error = false; } else { $oR3DCQuery->UpdateGameStatus($config, $gid, "C", "W"); $isblack = $oR3DCQuery->IsPlayerBlack($config, $gid, $_SESSION['id'], true); if ($isblack) { $oR3DCQuery->v2AddTournamentGameQueue($w_player_id, $type, $tid, $gid); } else { $oR3DCQuery->v2AddTournamentGameQueue($b_player_id, $type, $tid, $gid); } $bmove_error = false;