2, 'path' => drupal_get_path('module', 'chess') . '/views', ); } /** * Implementation of hook_help(). */ function chess_help($section) { $api = variable_get('devel_api_url', 'api.drupal.org'); global $user; if($user) { //print_r($user); } else { echo "NO \$USER DEFINED!"; } //return '

'. t('This is a list of the variables and their values currently stored in variables table and the $conf array of your settings.php file. These variables are usually accessed with variable_get() and variable_set(). Variables that are too long can slow down your pages.', array('@variable-get-doc' => "http://$api/api/HEAD/function/variable_get", '@variable-set-doc' => 'http://$api/api/HEAD/function/variable_set')).'

'; } /** * Implementation of hook_menu(). */ function chess_menu() { $items = array(); $treename = 'chess'; $items[$treename] = array( 'title' => t("Chess"), 'page callback' => 'chess_main', 'access arguments' => array('access chess module'), 'type' => MENU_NORMAL_ITEM, ); ############################################################# # active games ############################################################# # $branch = 'continuegame'; $items["$treename/$branch"] = array( 'link_path' => "$treename/$branch", 'title' => t("active games"), 'description' => t("Active games."), 'type' => MENU_NORMAL_ITEM, 'page callback' => 'chess_continuegame', 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 0, ); ############################################################# # pending challenges ############################################################# # $branch = 'invitations'; $items["$treename/$branch"] = array( 'link_path' => "$treename/$branch", 'title' => t("pending challenges"), 'description' => t("Pending challenges"), 'type' => MENU_NORMAL_ITEM, 'page callback' => 'chess_invitations', 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 1, ); ############################################################# # game itself ############################################################# # $branch = 'game'; $items["$treename/$branch"] = array( 'link_path' => "$treename/$branch", 'title' => t("Webchess"), 'description' => t("Webchess game"), 'type' => MENU_CALLBACK, 'page callback' => 'chess_game', 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 1, ); ############################################################# # challenge a player ############################################################# # $branch = 'challenge'; $items["$treename/$branch"] = array( 'link_path' => "$treename/$branch", 'title' => t("Challenge others"), 'description' => t("Challeng others for a game"), 'type' => MENU_NORMAL_ITEM, 'page callback' => 'chess_challenge', 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 1, ); ############################################################# # game replays ############################################################# # $branch = 'viewgame'; $items["$treename/$branch"] = array( 'link_path' => "$treename/$branch", 'title' => t("Replay"), 'description' => t("View finished games"), 'type' => MENU_NORMAL_ITEM, 'page callback' => 'chess_replays', 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 1, ); ############################################################# # list all games ############################################################# # $branch = 'listgames'; $items["$treename/$branch"] = array( 'title' => 'List all games', 'page callback' => '_chess_listgames', 'access callback' => TRUE, 'type' => MENU_NORMAL_ITEM, ); ############################################################# # list all ongoing games ############################################################# # $branch = 'listgames/%/ongoing'; $items["$treename/$branch"] = array( 'title' => t("List ongoing games"), 'description' => t("List ongoing games"), 'type' => MENU_LOCAL_TASK , 'page callback' => 'chess_listgames_ongoing', 'page arguments' => array(2), 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 1, ); ############################################################# # list all ended games ############################################################# # $branch = 'listgames/%/ended'; $items["$treename/$branch"] = array( 'title' => t("List ended games"), 'description' => t("List ended games"), 'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM, 'page callback' => 'chess_listgames_ended', 'page arguments' => array(2), 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 2, ); ############################################################# # list all invite games ############################################################# # $branch = 'listgames/%/invites'; $items["$treename/$branch"] = array( 'title' => t("List invite games"), 'description' => t("List invite games"), 'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM, 'page callback' => 'chess_listgames_invites', 'page arguments' => array(2), 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 3, ); ############################################################# # openpgn download ############################################################# # $branch = 'openpgn'; $items["$treename/$branch"] = array( 'title' => t("Open pgn"), 'description' => t("download pgn file."), 'type' => MENU_CALLBACK, 'page callback' => 'chess_openpgn', 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 3, ); ############################################################# # write JS board data ############################################################# # $branch = 'writeJSboard'; $items["$treename/$branch"] = array( 'title' => t("writeJSboard"), 'description' => t("writeJSboard()"), 'type' => MENU_CALLBACK, 'page callback' => 'chess_writeJSboard', 'access arguments' => array('access chess module'), //'access' => true, 'weight' => 1, ); return $items; } function chess_main() { global $user; /* kj changes if(function_exists(chess_statistic_user)) return chess_statistic_user(); if(function_exists(chess_continuegame)) return chess_continuegame(); */ if(function_exists(control_panel)) return control_panel(); else return "MAIN!"; } /* * Implementation of hook_user('login') */ function chess_user($op, &$edit, &$user, $category = NULL) { switch ($op) { case 'login': global $user; if (!$user->webchess_history) { $array = array('webchess_history' => 'pgn', 'webchess_historylayout' => 'columns', 'webchess_theme' => 'plain', 'webchess_autoreload' => 300, ); user_save($user, $array); } break; case 'insert': $edit[webchess_history] = 'pgn'; $edit[webchess_historylayout] = 'columns'; $edit[webchess_theme] = 'plain'; $edit[webchess_autoreload] = '300'; break; } } function chess_writeJSboard() { global $CFG_TABLE, $CFG_MINAUTORELOAD, $user; /* debug flag */ define ("DEBUG", 0); if (isset($_POST['gameID'])) $_SESSION['gameID'] = $_POST['gameID']; if (!isset($_SESSION['gameID'])) { //drupal_goto("chess"); print 'NO SESSION->GAMEID\n'; } /* get White's name */ $tmpNick = db_query("SELECT name FROM " . $CFG_TABLE['players'] . ", " . $CFG_TABLE['games'] . " WHERE uid = whitePlayer AND gameID = " . $_SESSION['gameID']); $whiteNick = db_result($tmpNick, 0); /* get Black's name */ $tmpNick = db_query("SELECT name FROM " . $CFG_TABLE['players'] . ", " . $CFG_TABLE['games'] . " WHERE uid = blackPlayer AND gameID = " . $_SESSION['gameID']); $blackNick = db_result($tmpNick, 0); /* load game */ $isInCheck = ($_POST['isInCheck'] == 'true'); $isCheckMate = false; $isPromoting = false; $isUndoing = false; loadHistory(); loadGame(); processMessages(); global $numMoves, $board, $playersColor, $isGameOver, $isInCheck, $isPromoting, $isObserver, $isPlayersTurn, $history, $isUndoRequested, $isDrawRequested, $isUndoing, $isCheckMate; /* ADDED: $isPlayersTurn, $history, $isUndoRequested, $isDrawRequested; */ if ($isUndoing) { doUndo(); saveGame(); } elseif (($_POST['promotion'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != "")) { savePromotion(); $board[$_POST['toRow']][$_POST['toCol']] = $_POST['promotion'] | ($board[$_POST['toRow']][$_POST['toCol']] & BLACK); saveGame(); } elseif (($_POST['fromRow'] != "") && ($_POST['fromCol'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != "")) { /* ensure it's the current player moving */ /* NOTE: if not, this will currently ignore the command... */ /* perhaps the status should be instead? */ /* (Could be confusing to player if they double-click or something */ $tmpIsValid = true; if (($numMoves == -1) || ($numMoves % 2 == 1)) { /* White's move... ensure that piece being moved is white */ if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != 0) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0)) /* invalid move */ $tmpIsValid = false; } else { /* Black's move... ensure that piece being moved is black */ if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != BLACK) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0)) { /* invalid move */ $tmpIsValid = false; } } if ($tmpIsValid) { saveHistory(); doMove(); saveGame(); } } elseif($history[$numMoves]['curPiece'] == 'pawn' && $history[$numMoves]['promotedTo'] == null) { // Incomplete promotion? if($history[$numMoves]['toRow'] == 7 || $history[$numMoves]['toRow'] == 0) { $isPromoting = true; } } /* 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; } $js = ''; $js .= writeJSboard(); $autoRefresh = !$isPlayersTurn && !isBoardDisabled(); $js .= 'var autoreload = '; if(!$autoRefresh) $js .= '0'; else if ($_SESSION['pref_autoreload'] >= $CFG_MINAUTORELOAD) $js .= $_SESSION['pref_autoreload']; else $js .= $CFG_MINAUTORELOAD; $js .= ";\n"; $js .= writeJShistory(); $js .= drawboard(); $js .= 'var gameId = ' . $_SESSION['gameID'] . ";\n"; $js .= 'var players = "' . $whiteNick . ' - ' . $blackNick . "\";\n"; $js .= 'var playersColor = "' . $playersColor . "\";\n"; $js .= 'var isPromoting = "'.$isPromoting. "\";\n"; $js .= 'var isKingInCheck = "'.$isInCheck. "\";\n"; $js .= 'var isGameOver = "'.$isGameOver. "\";\n"; $js .= 'var historyLayout = "'; if(!$user->webchess_historylayout) $js .= 'columns' . "\";\n"; else $js .= $user->webchess_historylayout . "\";\n"; $js .= writeStatus(); $js .= writeHistory(); $js .= listCapturedPieces(); //print ""; } function chess_openpgn() { global $CFG_TABLE; /* debug flag */ define ("DEBUG", 0); $output_file = 'game'.$_SESSION['gameID'].'.pgn'; header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP 1.1 header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP 1.1 header('Pragma: no-cache'); // HTTP 1.0 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: '.gmdate('D, d M Y H:i:s') . 'GMT'); header('Content-Transfer-Encoding: none'); header('Content-Type: application/x-chess-pgn; name="'. $output_file . '"'); header('Content-Disposition: attachment; filename="' .$output_file . '"'); // header("Content-length: $content_len"); loadHistory(); ReturnGameInfo($_SESSION['gameID']); echo writePGN(); } function chess_listgames_ongoing($user_id = NULL){ db_set_active('webchess'); global $CFG_TABLE, $user; drupal_add_css(drupal_get_path('module', 'chess'). "/css/main.css"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/mainmenu.js", "chess"); if( NULL != $user_id && 0 != $user_id) { $userQuery = "AND ( whitePlayer = $user_id OR blackPlayer = $user_id )"; } $game_select = "wcg.gameID, history.moveCount, wp.name as whiteName, bp.name as blackName, if (MOD(history.moveCount, 2) = 0,'White' , 'Black' ) as nextMove, wcg.dateCreated, wcg.lastMove, wcg.whitePlayer, wcg.blackPlayer "; $game_join = " Inner Join {$CFG_TABLE['players']} as wp ON wp.uid = wcg .whitePlayer Inner Join {$CFG_TABLE['players']} as bp ON bp.uid = wcg .blackPlayer INNER JOIN ( SELECT wch.gameID, count(wch.gameID) AS moveCount FROM {$CFG_TABLE['history']} AS wch GROUP by wch.gameID ) AS history ON history.gameID = wcg .gameID "; $game_where = "WHERE gameMessage = '' "; //$game_order = "ORDER BY gameID DESC"; $sql = " SELECT {$game_select} FROM ".$CFG_TABLE['games']." AS wcg {$game_join} {$game_where} {$userQuery} {$game_order} "; $sql_count = " SELECT COUNT(wcg.gameID) FROM {$CFG_TABLE['games']} AS wcg {$game_join} {$game_where} {$userQuery} {$game_order} "; //echo "
 ${sql} 
"; //echo "
 ${sql_count} 
"; $num_per_page=50; // gameID moves white black Next Move date created last move $header = array( array('data' => t('gameID'), 'field' => 'gameID', 'sort' => 'desc'), array('data' => t('moves'), 'field' => 'moveCount'), array('data' => t('white'), 'field' => 'whiteName'), array('data' => t('black'), 'field' => 'blackName'), array('data' => t('Next move'), 'field' => 'nextMove'), array('data' => t('date created'), 'field' => 'dateCreated'), array('data' => t('last move'), 'field' => 'lastMove'), ); $tablesort = db_rewrite_sql(tablesort_sql($header)); $result = pager_query($sql . $tablesort, $num_per_page, 0, $sql_count); while ($items = db_fetch_object($result)) { $rows[] = array('data' => array( " {$items->gameID} ", t($items->moveCount), l(t($items->whiteName), "user/{$items->whitePlayer}/chess", array(), NULL, NULL, FALSE, TRUE), l(t($items->blackName), "user/{$items->blackPlayer}/chess", array(), NULL, NULL, FALSE, TRUE), t($items->nextMove), t($items->dateCreated), t($items->lastMove), ), ); } $output .= "

List of all ongoing games

"; $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, 50, 0); // Add form so we could open game ids $output .= '
TRUE)) .'" method="post">
'; db_set_active('default'); return $output; } function chess_listgames_ended($user_id = NULL) { db_set_active('webchess'); global $CFG_TABLE, $user; drupal_add_css(drupal_get_path('module', 'chess'). "/css/main.css"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/mainmenu.js", "chess"); if( NULL != $user_id && 0 != $user_id) { $userQuery = "AND ( whitePlayer = $user_id OR blackPlayer = $user_id )"; } $game_select = "wcg.gameID, history.moveCount, wp.name as whiteName, bp.name as blackName, wcg.gameMessage, wcg.dateCreated, wcg.lastMove, wcg.whitePlayer, wcg.blackPlayer, wcg.messageFrom "; $game_join = " Inner Join {$CFG_TABLE['players']} as wp ON wp.uid = wcg .whitePlayer Inner Join {$CFG_TABLE['players']} as bp ON bp.uid = wcg .blackPlayer INNER JOIN ( SELECT wch.gameID, count(wch.gameID) AS moveCount FROM {$CFG_TABLE['history']} AS wch GROUP by wch.gameID ) AS history ON history.gameID = wcg .gameID "; $game_where = "WHERE ( gameMessage <> 'playerInvited' AND gameMessage <> '' )"; //$game_order = "ORDER BY gameID DESC"; $sql = " SELECT {$game_select} FROM ".$CFG_TABLE['games']." AS wcg {$game_join} {$game_where} {$userQuery} {$game_order} "; $sql_count = " SELECT COUNT(wcg.gameID) FROM {$CFG_TABLE['games']} AS wcg {$game_join} {$game_where} {$userQuery} {$game_order} "; //echo "
 ${sql} 
"; //echo "
 ${sql_count} 
"; $num_per_page=50; // gameID moves white black gameMessage datecreated lastmove $header = array( array('data' => t('gameID'), 'field' => 'gameID', 'sort' => 'desc'), array('data' => t('moves'), 'field' => 'moveCount'), array('data' => t('white'), 'field' => 'whiteName'), array('data' => t('black'), 'field' => 'blackName'), array('data' => t('Result'), 'field' => 'gameMessage'), array('data' => t('date created'), 'field' => 'dateCreated'), array('data' => t('last move'), 'field' => 'lastMove'), ); $tablesort = tablesort_sql($header); $result = pager_query($sql . $tablesort, $num_per_page, 0, $sql_count); while ($items = db_fetch_object($result)) { switch ($items->gameMessage) { case 'playerResigned': $gameMessage = $items->messageFrom . " resigned"; break; case 'checkMate': $gameWinner = ($items->messageFrom == 'white' ? 'black' : 'white'); // kj changes - check mate is not certain lately so dont put message to say it is //$gameMessage = "checkmate by {$gameWinner}"; $gameMessage = "possible checkmate by {$gameWinner}. Post here if is and wasn't caught so ADMIN can fix."; break; case 'draw': $gameMessage = $items->gameMessage; break; } $rows[] = array('data' => array( " {$items->gameID} ", t($items->moveCount), l(t($items->whiteName), "user/{$items->whitePlayer}/chess", array(), NULL, NULL, FALSE, TRUE), l(t($items->blackName), "user/{$items->blackPlayer}/chess", array(), NULL, NULL, FALSE, TRUE), t($gameMessage), t($items->dateCreated), t($items->lastMove), ), ); $gameWinner=NULL; } $output .= "

List of all ended games

"; $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, 50, 0); // Add form so we could open game ids $output .= '
TRUE)) .'" method="post">
'; db_set_active('default'); return $output; } function chess_listgames_invites($user_id = NULL) { db_set_active('webchess'); global $CFG_TABLE, $user; drupal_add_css(drupal_get_path('module', 'chess'). "/css/main.css"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/mainmenu.js", "chess"); if( NULL != $user_id && 0 != $user_id) { $userQuery = "AND ( whitePlayer = $user_id OR blackPlayer = $user_id )"; } $game_select = "wp.name as whiteName, bp.name as blackName, wcg.messageFrom, wcg.dateCreated, wcg.whitePlayer, wcg.blackPlayer "; $game_join = " Inner Join {$CFG_TABLE['players']} as wp ON wp.uid = wcg .whitePlayer Inner Join {$CFG_TABLE['players']} as bp ON bp.uid = wcg .blackPlayer "; $game_where = "WHERE gameMessage = 'playerInvited'"; //$game_order = "ORDER dateCreated DESC"; $sql = " SELECT {$game_select} FROM ".$CFG_TABLE['games']." AS wcg {$game_join} {$game_where} {$userQuery} {$game_order} "; $sql_count = " SELECT COUNT(wcg.gameID) FROM {$CFG_TABLE['games']} AS wcg {$game_join} {$game_where} {$userQuery} {$game_order} "; //echo "
 ${sql} 
"; //echo "
 ${sql_count} 
"; $num_per_page=50; //white black Invited by date created lastmove // Should be : white black Invited by date of invite $header = array( array('data' => t('white'), 'field' => 'whiteName'), array('data' => t('black'), 'field' => 'blackName'), array('data' => t('Invited by'), 'field' => 'messageFrom'), array('data' => t('date created'), 'field' => 'dateCreated', 'sort' => 'desc'), ); $tablesort = tablesort_sql($header); $result = pager_query($sql . $tablesort, $num_per_page, 0, $sql_count); while ($items = db_fetch_object($result)) { $rows[] = array('data' => array( l(t($items->whiteName), "user/{$items->whitePlayer}/chess", array(), NULL, NULL, FALSE, TRUE), l(t($items->blackName), "user/{$items->blackPlayer}/chess", array(), NULL, NULL, FALSE, TRUE), t($items->messageFrom), t($items->dateCreated), ), ); } $output .= "

List of all invited games that haven't started yet

"; $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, 50, 0); // Add form so we could open game ids $output .= '
TRUE)) .'" method="post">
'; db_set_active('default'); return $output; } function chess_replays() { db_set_active('webchess'); global $CFG_TABLE, $CFG_EXPIREGAME, $user; drupal_add_css(drupal_get_path('module', 'chess'). "/css/main.css"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/tablesort.js", "chess"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/mainmenu.js", "chess"); $output = ''; $output .= '
TRUE)) .'" method="post">

'. t("View finished games") .'

'. t("Select a game to view").'
'; $tmpGames = db_query(" SELECT ".$CFG_TABLE['games'].".*, whitePlayer.name as whiteName, blackPlayer.name as blackName, history.moveCount FROM ".$CFG_TABLE['games']." Inner Join ".$CFG_TABLE['players']." as whitePlayer ON whitePlayer.uid = ".$CFG_TABLE['games'].".whitePlayer Inner Join ".$CFG_TABLE['players']." as blackPlayer ON blackPlayer.uid = ".$CFG_TABLE['games'].".blackPlayer INNER JOIN ( SELECT ".$CFG_TABLE['history'].".gameID, count(".$CFG_TABLE['history'].".gameID) AS moveCount FROM ".$CFG_TABLE['history']." GROUP BY ".$CFG_TABLE['history'].".gameID ) AS history ON history.gameID = ".$CFG_TABLE['games'].".gameID WHERE (gameMessage <> '' AND gameMessage <> 'playerInvited' AND gameMessage <> 'inviteDeclined') AND (whitePlayer = ".$user->uid." OR blackPlayer = ".$user->uid.") ORDER BY lastMove DESC" ); $rowNbr = 0; while($tmpGame = db_fetch_array($tmpGames)) { if($rowNbr %2 == 0) { $output .= ''; } else { $output .= ''; } $rowNbr++; $output .= ''; $output .= '\n"; } if(0 == $rowNbr && NULL == $obj) { $output .= "\n"; } $output .= '
'. t("Finished games").'
'. t("Rank").' '. t("Id").' '. t("White").' '. t("Black").' '. t("Mvs").' '. t("Result").' '. t("Start Date").' '. t("Last Move").'
'; $output .= "".$tmpGame['gameID'].""; /* get white's nick */ $output .= ''; $output .= $tmpGame['whiteName']; /* black's nick */ $output .= ""; $output .= $tmpGame['blackName']; /* Your Color */ if ($tmpGame['whitePlayer'] == $user->uid) { $tmpColor = "white"; } else { $tmpColor = "black"; } $output .= ''; $output .= floor($tmpGame['moveCount'] / 2); /* Status */ if (is_null($tmpGame['gameMessage'])) $output .= ""; else { if ($tmpGame['gameMessage'] == "draw") $output .= "½-½"; else if ($tmpGame['gameMessage'] == "stalemate") $output .= "½-½ (" . t("stalemate") . ")"; else if ($tmpGame['gameMessage'] == "playerResigned") $output .= "".t($tmpGame['messageFrom'])." " . t("resigned"); // kj changes - disable checkmate for now else if (($tmpGame['gameMessage'] == "checkMate") && ($tmpGame['messageFrom'] == $tmpColor)) $output .= "" . t("Checkmate, you won!"); else if ($tmpGame['gameMessage'] == "checkMate") $output .= "" . t("Checkmate, you lost"); // else $output .= ""; } /* Start Date */ $output .= "".substr($tmpGame['dateCreated'], 0, -3); /* Last Move */ $output .= "".substr($tmpGame['lastMove'], 0, -3)."
" . t("You do not currently have any games in progress") . "
'. t("WARNING!") .'
'. t("Finished games will be deleted WITHOUT NOTICE after") . " " . ($CFG_EXPIREGAME) . " " . t("days!") .'
'; db_set_active('default'); return $output; } function chess_challenge() { db_set_active('webchess'); global $CFG_TABLE, $user; drupal_add_css(drupal_get_path('module', 'chess'). "/css/main.css"); if (isset($_POST['ToDo']) && $_POST['ToDo'] == 'InvitePlayer') { $enemyNick = db_result(db_query("SELECT name FROM " . $CFG_TABLE['players'] . " WHERE uid = ".$_POST['opponent'])); if($enemyNick == NULL) { drupal_set_message("You invited a non-existing player.", "error"); } else { /* prevent multiple pending requests between two players with the same originator */ $tmpQuery = "SELECT gameID FROM " . $CFG_TABLE['games'] . " WHERE gameMessage = 'playerInvited'"; $tmpQuery .= " AND ((messageFrom = 'white' AND whitePlayer = ".$user->uid." AND blackPlayer = ".$_POST['opponent'].")"; $tmpQuery .= " OR (messageFrom = 'black' AND whitePlayer = ".$_POST['opponent']." AND blackPlayer = ".$user->uid."))"; $tmpExistingRequests = db_fetch_object(db_query($tmpQuery)); if ($tmpExistingRequests == NULL) { if ($_POST['color'] == 'random') $tmpColor = (mt_rand(0,1) == 1) ? "white" : "black"; else $tmpColor = $_POST['color']; $tmpQuery = "INSERT INTO " . $CFG_TABLE['games'] . " (whitePlayer, blackPlayer, gameMessage, messageFrom, dateCreated, lastMove) VALUES ("; if ($tmpColor == 'white') $tmpQuery .= $user->uid.", ".$_POST['opponent']; else $tmpQuery .= $_POST['opponent'].", ".$user->uid; $tmpQuery .= ", 'playerInvited', '".$tmpColor."', NOW(), NOW())"; db_query($tmpQuery); drupal_set_message("You just invited ".$enemyNick." for a game."); } else drupal_set_message("You already have a pending game/invitation with ".$enemyNick.".", "error"); } } $output = ''; $output .= '
TRUE)) .'" method="post">

'. "Issue a challenge" .'

'. "Select Opponent" .'
'. "Your Color" .'
'. "Random" .'
'. "White" .'
'. "Black" .'
'; db_set_active('default'); return $output; } function chess_game() { db_set_active('webchess'); header("Pragma: no-cache"); global $CFG_TABLE, $CFG_MINAUTORELOAD, $user; if (isset($_POST['gameID'])) $_SESSION['gameID'] = $_POST['gameID']; if (!isset($_SESSION['gameID'])) drupal_goto("chess"); $output = ''; fixOldPHPVersions(); /* check if loading game */ /* debug flag */ define ("DEBUG", 0); /* get White's name */ $tmpNick = db_query("SELECT name FROM " . $CFG_TABLE['players'] . ", " . $CFG_TABLE['games'] . " WHERE uid = whitePlayer AND gameID = " . $_SESSION['gameID']); $whiteNick = db_result($tmpNick, 0); /* get Black's name */ $tmpNick = db_query("SELECT name FROM " . $CFG_TABLE['players'] . ", " . $CFG_TABLE['games'] . " WHERE uid = blackPlayer AND gameID = " . $_SESSION['gameID']); $blackNick = db_result($tmpNick, 0); /* load game */ $isInCheck = (isset($_POST['isInCheck']) && $_POST['isInCheck'] == 'true'); $isCheckMate = false; $isPromoting = false; $isUndoing = false; loadHistory(); loadGame(); processMessages(); global $numMoves, $board, $playersColor, $isGameOver, $isInCheck, $isPromoting, $isObserver, $isPlayersTurn, $history, $isUndoRequested, $isDrawRequested, $isUndoing, $isCheckMate; /* ADDED: $isPlayersTurn, $history, $isUndoRequested, $isDrawRequested; */ if ($isUndoing) { doUndo(); saveGame(); } elseif ((isset($_POST['promotion']) && $_POST['promotion'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != "")) { savePromotion(); $board[$_POST['toRow']][$_POST['toCol']] = $_POST['promotion'] | ($board[$_POST['toRow']][$_POST['toCol']] & BLACK); saveGame(); } elseif ((isset($_POST['fromRow']) && $_POST['fromRow'] != "") && ($_POST['fromCol'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != "")) { /* ensure it's the current player moving */ /* NOTE: if not, this will currently ignore the command... */ /* perhaps the status should be instead? */ /* (Could be confusing to player if they double-click or something */ $tmpIsValid = true; if (($numMoves == -1) || ($numMoves % 2 == 1)) { /* White's move... ensure that piece being moved is white */ if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != 0) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0)) /* invalid move */ $tmpIsValid = false; } else { /* Black's move... ensure that piece being moved is black */ if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != BLACK) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0)) { /* invalid move */ $tmpIsValid = false; } } if ($tmpIsValid) { saveHistory(); doMove(); saveGame(); } } elseif($history[$numMoves]['curPiece'] == 'pawn' && $history[$numMoves]['promotedTo'] == null) { // Incomplete promotion? if($history[$numMoves]['toRow'] == 7 || $history[$numMoves]['toRow'] == 0) { $isPromoting = true; } } /* 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($user->name != $whiteNick && $user->name != $blackNick) { $isObserver = TRUE; //drupal_set_title("WebChess - {$playersColor}'s Move"); } else $isObserver = FALSE; $chess_gameurl = url('chess/game', array('absolute' => TRUE)); $chess_basepath = url(NULL, array('absolute' => TRUE)) . drupal_get_path('module', 'chess') . '/'; drupal_add_js( array('chess' => array('base_path' => $chess_basepath, 'game_url' => $chess_gameurl, ), ), 'setting'); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/chessutils.js", "chess", "footer"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/board.js", "chess", "footer"); $js = ''; $js .= writeJSboard(); $autoRefresh = !$isPlayersTurn && !isBoardDisabled(); $js .= 'var autoreload = '; if(!$autoRefresh) $js .= '0'; else if ($_SESSION['pref_autoreload'] >= $CFG_MINAUTORELOAD) $js .= $_SESSION['pref_autoreload']; else $js .= $CFG_MINAUTORELOAD; $js .= ";\n"; $js .= writeJShistory(); $js .= drawboard(); $js .= 'var gameId = ' . $_SESSION['gameID'] . ";\n"; $js .= 'var players = "' . $whiteNick . ' - ' . $blackNick . "\";\n"; $js .= 'var playersColor = "' . $playersColor . "\";\n"; $js .= 'var isPromoting = "'.$isPromoting. "\";\n"; $js .= 'var isKingInCheck = "'.$isInCheck. "\";\n"; $js .= 'var isGameOver = "'.$isGameOver. "\";\n"; $js .= 'var historyLayout = "'; if(!$user->webchess_historylayout) $js .= 'columns' . "\";\n"; else $js .= $user->webchess_historylayout . "\";\n"; $js .= writeStatus(); $js .= writeHistory(); $js .= listCapturedPieces(); drupal_add_css(drupal_get_path('module', 'chess'). "/css/chess.css"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/commands.js", "chess", "footer"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/validation.js", "chess", "footer"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/mainmenu.js", "chess", "footer"); if($isPlayersTurn || $isPromoting) drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/ischeckmate.js", "chess", "footer"); if(!isBoardDisabled() || !$isObserver) drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/squareclicked.js", "chess", "footer"); $output .= '
'; if ($isPromoting && (!$isPlayersTurn)) // Write promotion dialog only to the correct player $output .= writePromotion(); // we hide it upon page ready if ($isUndoRequested) $output .= writeUndoRequest(); if ($isDrawRequested) $output .= writeDrawRequest(); $output .= '
'. "" .'
 
 
When castling, just move the king (the rook will move automatically).
Captured pieces
'; db_set_active('default'); return $output; } function control_panel() { global $user; /* welcome board for user */ $output = ""; //$output=chess_continuegame(); $countOngoingSQL="select count(*) as totals from webchess_games where ((blackPlayer = ".$user->uid.") or (whitePlayer = ".$user->uid.")) and gameMessage = ''"; $countInvitesSQL="select count(*) as totals from webchess_games where ((blackPlayer = ".$user->uid.") or (whitePlayer = ".$user->uid.")) and gameMessage = 'playerInvited'"; $countEndedSQL="select count(*) as totals from webchess_games where ((blackPlayer = ".$user->uid.") or (whitePlayer = ".$user->uid.")) and gameMessage in ('draw','playerResigned','checkMate','ruled AutoWin')"; $countOngoing=db_result(db_query($countOngoingSQL)); $countInvites=db_result(db_query($countInvitesSQL)); $countEnded=db_result(db_query($countEndedSQL)); //$output=$output+chess_statistic_user(); $output = "

ADMIN MESSAGE 7/19/2009 11:41pm EST

There is a checkmate bug that we are investigating. For now checkmates are disabled since false positives are being thrown out on some games. So if you are on checkmate or gave one, let us know here so we can end the game with a checkmate manually for now.

Also, the Autowin/Forfeit is something we can undo if your case deserves it. Just state your case here and be glad to review and make the change.


OPTIONS

  • $countOngoing ONGOING games - make a move on my games

  • $countInvites INVITES - check game invites sent and received

  • uid/ended\">$countEnded ENDED games - list of games that have ended in resignation, checkmate or draw

  • My STATS - see my current statistics

  • for other options, see left side menu "; return $output; } function chess_continuegame() { db_set_active('webchess'); global $CFG_TABLE, $CFG_EXPIREGAME, $user; drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/mainmenu.js", "chess"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/tablesort.js", "chess"); $output = ''; $output .= '
    TRUE)) .'" method="post">

    Continue a game in progress

    '; $tmpGames = db_query("SELECT * FROM ". $CFG_TABLE['games'] ." WHERE gameMessage = '' AND (whitePlayer = ".$user->uid." OR blackPlayer = ".$user->uid.") ORDER BY dateCreated"); if ($tmpGames == NULL) $output .= "\n"; else { $rowNbr = 0; while($tmpGame = db_fetch_array($tmpGames)) { if($rowNbr %2 == 0) $output .= ''; else $output .= ''; $rowNbr++; $output .= ''; $output .= '\n"; } } $output .= '
    Games in Progress
    "Rank" Id White Black Moves Current Turn Start Date Last Move
    " . "You do not currently have any games in progress" . "
    '; $output .= "".$tmpGame['gameID'].""; /* get white's name */ $player = db_result(db_query("SELECT name FROM ". $CFG_TABLE['players'] ." WHERE uid = ".$tmpGame['whitePlayer'])); $output .= ''; $output .= l($player, url('user/'.$tmpGame['whitePlayer'].'/chess')); /* black's name */ $player = db_result(db_query("SELECT name FROM ". $CFG_TABLE['players'] ." WHERE uid = ".$tmpGame['blackPlayer'])); $output .= ""; $output .= l($player, url('user/'.$tmpGame['blackPlayer'].'/chess')); /* Your Color */ if ($tmpGame['whitePlayer'] == $user->uid) { $tmpColor = "white"; } else { $tmpColor = "black"; } /* get number of moves from history */ $numMoves = db_result(db_query("SELECT COUNT(gameID) FROM ". $CFG_TABLE['history'] ." WHERE gameID = ".$tmpGame['gameID'])); $output .= ''; $output .= floor($numMoves / 2); /* Current Turn */ /* based on number of moves, output current color's turn */ if (($numMoves % 2) == 0) $tmpCurMove = "white"; else $tmpCurMove = "black"; $output .= ""; if ($tmpCurMove == $tmpColor) $output .= ""; else $output .= "
    Opponent
    "; /* Start Date */ $output .= "
    ".substr($tmpGame['dateCreated'], 0, -3); /* Last Move */ $output .= "".substr($tmpGame['lastMove'], 0, -3)."
    WARNING!
    Games will expire WITHOUT NOTICE if a move isn\'t made after '. $CFG_EXPIREGAME . ' days!
    '; db_set_active('default'); return $output; } function chess_invitations() { db_set_active('webchess'); global $CFG_TABLE, $user; ###################################################################################### # Redirect page directly to active games if this contains an accept/decline post data. # ###################################################################################### if (isset($_POST['ToDo']) && $_POST['ToDo'] == 'ResponseToInvite') { if ($_POST['response'] == 'accepted') { /* update game data */ $tmpQuery = "UPDATE " . $CFG_TABLE['games'] . " SET gameMessage = '', messageFrom = '' WHERE gameID = ".$_POST['gameID']; db_query($tmpQuery); /* setup new board */ $_SESSION['gameID'] = $_POST['gameID']; createNewGame($_POST['gameID']); saveGame(); header("Location: ?q=chess/continuegame"); } else { $tmpQuery = "UPDATE " . $CFG_TABLE['games'] . " SET gameMessage = 'inviteDeclined', messageFrom = '".$_POST['messageFrom']."' WHERE gameID = ".$_POST['gameID']; db_query($tmpQuery); } } if (isset($_POST['ToDo']) && $_POST['ToDo'] == 'WithdrawRequest') { /* get opponent's player ID */ $tmpOpponentID = db_query("SELECT whitePlayer FROM " . $CFG_TABLE['games'] . " WHERE gameID = ".$_POST['gameID']); if ($tmpOpponentID != NULL) { $opponentID = db_result($tmpOpponentID, 0); if ($opponentID == $user->uid) { $tmpOpponentID = db_query("SELECT blackPlayer FROM " . $CFG_TABLE['games'] . " WHERE gameID = ".$_POST['gameID']); $opponentID = db_result($tmpOpponentID, 0); } $tmpQuery = "DELETE FROM " . $CFG_TABLE['games'] . " WHERE gameID = ".$_POST['gameID']; db_query($tmpQuery); } } drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/mainmenu.js", "chess"); drupal_add_js(drupal_get_path('module', 'chess'). "/javascript/tablesort.js", "chess"); //drupal_add_css(drupal_get_path('module', 'chess'). "/css/chess.css", "chess"); $output = ''; $output .= '
    TRUE)) .'" method="post">

    "Pending challenges"

    "'; $tmpQuery = "SELECT * FROM ". $CFG_TABLE['games'] ." WHERE gameMessage = 'playerInvited' AND ((whitePlayer = ".$user->uid." AND messageFrom = 'black') OR (blackPlayer = ".$user->uid." AND messageFrom = 'white')) ORDER BY dateCreated"; $tmpGames = db_query($tmpQuery); $rowNbr = 0; if ($tmpGames == NULL) $output .= "\n"; else { while($tmpGame = db_fetch_array($tmpGames)) { if($rowNbr %2 == 0) $output .= ''; else $output .= ''; $rowNbr++; $output .= ''; $output .= "\n"; } } $output .= '
    "Challenges from other players"
    "Rank" "Id" "White" "Black" "Issued" "Action"
    " . "You are not currently invited to any games" . "
    "; $output .= $tmpGame['gameID']; /* get white's name */ $player = db_result(db_query("SELECT name FROM ". $CFG_TABLE['players'] ." WHERE uid = ".$tmpGame['whitePlayer'])); $output .= ''; $output .= $player; /* black's name */ $player = db_result(db_query("SELECT name FROM ". $CFG_TABLE['players'] ." WHERE uid = ".$tmpGame['blackPlayer'])); $output .=''; $output .= $player; /* Date issued */ $output .= "".substr($tmpGame['dateCreated'], 0, -3); /* Response */ $output .= ""; $output .= ""; $output .= ""; $output .= "
    '; $tmpQuery = "SELECT * FROM ". $CFG_TABLE['games'] ." WHERE (gameMessage = 'playerInvited' AND ((whitePlayer = ".$user->uid." AND messageFrom = 'white') OR (blackPlayer = ".$user->uid." AND messageFrom = 'black'))"; /* OR game is marked inviteDeclined and the response is from the opponent */ $tmpQuery .= ") OR (gameMessage = 'inviteDeclined' AND ((whitePlayer = ".$user->uid." AND messageFrom = 'black') OR (blackPlayer = ".$user->uid." AND messageFrom = 'white'))) ORDER BY dateCreated"; $tmpGames = db_query($tmpQuery); $rowNbr = 0; if ($tmpGames == NULL) $output .= "\n"; else { while($tmpGame = db_fetch_array($tmpGames)) { if($rowNbr %2 == 0) $output .= ''; else $output .= ''; $rowNbr++; $output .= ''; $output .= "\n"; } } $output .= '
    "Pending challenges from you"
    "Rank" "Id" "White" "Black" "Status" "Issued" "Action"
    " . "You have no current unanswered invitations" . "
    "; $output .= $tmpGame['gameID']; /* get white's name */ $player = db_result(db_query("SELECT name FROM ". $CFG_TABLE['players'] ." WHERE uid = ".$tmpGame['whitePlayer'])); $output .= ''; $output .= $player; /* black's name */ $player = db_result(db_query("SELECT name FROM ". $CFG_TABLE['players'] ." WHERE uid = ".$tmpGame['blackPlayer'])); $output .= ""; $output .= $player; /* Status */ $output .= ""; if ($tmpGame['gameMessage'] == 'playerInvited') $output .= "Response pending"; else if ($tmpGame['gameMessage'] == 'inviteDeclined') $output .= "Invitation declined"; /* Date issued */ $output .= "".substr($tmpGame['dateCreated'], 0, -3); /* Withdraw Request */ $output .= ""; $output .= ""; $output .= "
    '; db_set_active('default'); return $output; } function _chess_listgames() { menu_set_active_item('chess/listgames/'. 0 .'/ongoing'); return menu_execute_active_handler(); }