if ($tmpOldGame['gameMessage'] == '') { //Abandoned game. User will lose points $p = mysql_query("SELECT * FROM history WHERE gameID='" . $tmpOldGame['gameID'] . "' ORDER BY timeOfMove DESC"); $row = mysql_fetch_array($p); if ($row['curColor'] != "") { if ($row['curColor'] == "white") { $playersColor = "black"; } else { if ($row['curColor'] == "black") { $playersColor = "white"; } } } else { $playersColor = "white"; } saveRanking($tmpOldGame[gameID], "resign", $playersColor); mysql_query("UPDATE games SET lastMove = NOW() WHERE gameID = " . $tmpOldGame[gameID]); //echo "Deleting old games Game: ".$tmpOldGame[gameID]."<BR>"; $log = "\"Deleting old games\" \"Game: {$tmpOldGame['gameID']}\""; } else { /* ... clear the history... */ mysql_query("DELETE FROM history WHERE gameID = " . $tmpOldGame['gameID']); /* ... and the board... */ mysql_query("DELETE FROM pieces WHERE gameID = " . $tmpOldGame['gameID']); /* ... and the messages... */ mysql_query("DELETE FROM messages WHERE gameID = " . $tmpOldGame['gameID']); /* ... and the chat... */ mysql_query("DELETE FROM chat WHERE gameID = " . $tmpOldGame['gameID']); /* ... and finally the game itself from the database */ mysql_query("DELETE FROM games WHERE gameID = " . $tmpOldGame['gameID']); $log = "\"Deleting refused games\" \"Game: {$tmpOldGame['gameID']}\"";
/* find out if it's the current player's turn */ if (( (($numMoves == -1) || (($numMoves % 2) == 1)) && ($playersColor == "white")) || ((($numMoves % 2) == 0) && ($playersColor == "black"))) $isPlayersTurn = true; else $isPlayersTurn = false; if ($white == $_SESSION['playerID']) $opponent = $black; else $opponent = $white; if (!isBoardDisabled() && !$isCheckMate && $timeLimit > 0) if (tempoEsgotado($mycolor2)){ saveRanking($_SESSION['gameID'],"resign",$mycolor2,1); updateTimestamp(); // Update the opponent time if ($mycolor2 == "white") mysql_query("UPDATE games set timeWhite=$timeLimit*60 WHERE gameID=".$_SESSION['gameID']); else mysql_query("UPDATE games set timeBlack=$timeLimit*60 WHERE gameID=".$_SESSION['gameID']); echo "<script> alert('".$MSG_LANG["theflaghasfallen"]." $mycolor2 $MSG_LANG[lost]'); window.location='chess.php'; </script>\n"; exit; }
function delete_old_games($game_prune, $db) { /* cleanup dead games */ /* determine threshold for oldest game permitted */ $targetDate = date("Y-m-d H:i:s", mktime(date('H'), date('i'), 0, date('m'), date('d') - $game_prune, date('Y'))); /* find out which games are older */ $sql = $db->Prepare("SELECT * FROM games WHERE lastMove < ? AND (gameMessage='inviteDeclined' OR gameMessage='playerInvited' OR gameMessage='')"); $prune_query = $db->Execute($sql, array($target_date)); db_op_result($prune_query, __LINE__, __FILE__); while (!$prune_query->EOF) { $old_game = $prune_query->fields; if ($old_game['gameMessage'] == '') { //Abandoned game. User will lose points $sql = $db->Prepare("SELECT MAX( timeOfMove ) AS move_time, curColor FROM history WHERE gameID = ? GROUP BY gameID"); $sql_array = array($old_game['gameID']); $query = $db->Execute($sql, $sql_array); db_op_result($query, __LINE__, __FILE__); $row = $query->fields; if ($row['curColor'] != "") { if ($row['curColor'] == "white") { $playersColor = "black"; } else { if ($row['curColor'] == "black") { $playersColor = "white"; } } } else { $playersColor = "white"; } saveRanking($old_game['gameID'], "resign", $playersColor, 1); $sql = "UPDATE games SET lastMove = NOW() WHERE gameID = ?"; $res = $db->Execute($sql, $sql_array); db_op_result($res, __LINE__, __FILE__); adminlog(GAME_PRUNE, "Deleting Game ID " . $old_game['gameID'] . " from Database"); } else { $sql = array(); $sql[1] = $db->Prepare("DELETE FROM history WHERE gameID = ?"); $sql[2] = $db->Prepare("DELETE FROM pieces WHERE gameID = ?"); $sql[3] = $db->Prepare("DELETE FROM messages WHERE gameID = ?"); $sql[4] = $db->Prepare("DELETE FROM chat WHERE gameID = ?"); $sql[5] = $db->Prepare("DELETE FROM notes WHERE gameID = ?"); $sql[6] = $db->Prepare("DELETE FROM games WHERE gameID = ?"); foreach ($sql as $string) { $res = $db->Execute($string, $sql_array); db_op_result($res, __LINE__, __FILE__); adminlog(GAME_PRUNE, "Deleting Game ID " . $old_game['gameID'] . " from Database"); } } $prune_query->MoveNext(); } }
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; } }
//BROKEN POINT - below, it's all broke. SESSION data is f****d up //TODO: - I dont fully understand exactly what this code does, but appears it could use some optimization //perhaps a function- ########## RESEARCH MARKER START########### var_dump($_SESSION); //SHEESH! where the hell is gameID and this ["isSharedPC"]=> bool(false) ["playerID"]=> int(-1) ["gameID"]=> int(0) } shit coming from?? //if (!isBoardDisabled() && !$isCheckMate && $timeLimit > 0) //{ //there's no else, this if ends at the bottom of script //this way we can allow kibbitzing, but not posting. in this way we can control what is loaded //and for certain games or player preferences, allow kibbitz may lock them out .. if ($white != $_SESSION['player_id'] && $black != $_SESSION['player_id']) { die($MSG_LANG["youdonthavepermission"]); } if (tempoEsgotado($mycolor2)) { //here we have to figure out teh time limit shit a little better- when the clock starts and stops.. $do_rankings = saveRanking($game_id, "resign", $mycolor2, 1); $update_stamp = updateTimestamp(); $time_limmit = $timeLimit * 60; // Update the opponent time if ($mycolor2 == "white") { $sql = $db->Prepare("UPDATE games set timeWhite=? WHERE game_id=?"); $query = $db->Execute($sql, array($time_limit, $game_id)); db_op_result($query, __LINE__, __FILE__); } else { $sql = $db->Prepare("UPDATE games set timeBlack=? WHERE game_id=?"); $query = $db->Execute($sql, array($time_limit, $game_id)); db_op_result($query, __LINE__, __FILE__); } $smarty->assign('js_checkmate_alert', "<script>alert('" . $MSG_LANG['theflaghasfallen'] . " " . $mycolor2 . $MSG_LANG['lost'] . "'); window.location='chess.php'; </script>"); //we switch the display based on that. good reason to exit and halt execution here I guess exit;
if ($white != $_SESSION['playerID'] && $black != $_SESSION['playerID']) { die("Você não tem permissão."); } if ($playersColor == "white") { $opponentColor = "black"; } else { $opponentColor = "white"; } if (!$playersColor || !$action) { displayError("Fatal Error at apply.php", "index.php"); } if ($CFG_LOG_PATH = !"") { $log = "\"Apply bot game\" \"Game: {$_SESSION['gameID']}\" \"Winner: {$playersColor}\""; grava_log("webchess.log", $log, $CFG_LOG_PATH); } saveRanking($_SESSION['gameID'], $action, $playersColor); updateTimestamp(); ?> <form name="existingGames" action="chess.php" method="post"> <input type='hidden' name='rdoShare' value='no'> <input type="hidden" name="gameID" value=""> <input type="hidden" name="sharePC" value="no"> </form> <script> document.existingGames.gameID.value=<?php echo $_SESSION['gameID']; ?> ; document.existingGames.submit(); </script>