HTML and CSS.*/ $dbfile = './.htches'; $table = 'games'; $starting_board = "OTSEGSTO\n". "AAAAAAAA\n". "________\n". "________\n". "________\n". "________\n". "pppppppp\n". "rnbqkbnr"; $action = false; $lastServerMove = time(); $game_id = 0; $formaction = './'; if(isset($_REQUEST['game_id'])) $game_id = $_REQUEST['game_id']; $db = sqlite_open($dbfile, 0666, $sqliteerror); if(!$db) { die("Could not open/create db file. Error was $sqliteerror"); } else { if(isset($_REQUEST['id'])) { $game_id = intval($_REQUEST['id']); // prevent SQL injection } } if(isset($_REQUEST['action'])) { $action = $_REQUEST['action']; } /* * $move is of the form "p@b5-d5" or "P@e2xb@d3" */ function getUpdatedBoard($oldBoard, $move) { $eachChar = "//"; $offset = 0; $board = array( 'a' => array(''), 'b' => array(''), 'c' => array(''), 'd' => array(''), 'e' => array(''), 'f' => array(''), 'g' => array(''), 'h' => array('') ); foreach($board as $letter => $row) { for($i = 0; $i < 8; $i++) { if($oldBoard[$offset++] == "\n") $offset++; $board[$letter][] = $oldBoard[$offset]; // add to end of row } } $piece = $move[0]; $info = preg_split("/[x\-@]/", $move); // length 3 for move, 4 for capture $movingPiece = $info[0]; $oldPosition = preg_split($eachChar, $info[1], -1, PREG_SPLIT_NO_EMPTY); $newPosition = preg_split($eachChar, $info[ count($info) - 1 ], -1, PREG_SPLIT_NO_EMPTY); // "b" "5" $board[ $oldPosition[0] ][ $oldPosition[1] ] = '_'; $board[ $newPosition[0] ][ $newPosition[1] ] = $movingPiece; foreach($board as $letter => $row) { $board[$letter] = implode("", $row); } $board = implode("\n", $board); return $board; } session_start(); if(isset($_SESSION['username'])) { $user = $_SESSION['username']; } else if(!isset($_POST['username'])) { ?> Log In to Che§

"; echo "\nWhite's name is $user

"; $game_id = sqlite_last_insert_rowid($db); if(!empty($blackname)) echo "\nBlack's name is $blackname

"; else "\nBlack's name was unspecified -- anyone will be able to join the game.

"; echo "\nGame id is $game_id

"; echo "



"; echo '
'; } else if($action == 'move') { $move = $_REQUEST['move'] or die("no move supplied"); $row = sqlite_array_query($db, "select * from $table where id = $game_id", SQLITE_ASSOC); $row = $row[0]; // first row returned... $isWhite = $row['white'] == $user; $isBlack = $row['black'] == $user; $userColor = ($isWhite) ? 'white' : 'black'; if( $isWhite || $isBlack ) { $suffix = ($isWhite) ? "\t" : "\n"; $append = $move . $suffix; $moves = $row['moves']; $lastChar = substr($moves, -1); if( empty($moves) // first move || ($lastChar == "\n" && $isWhite) // or || ($lastChar == "\t" && $isBlack)) { // last move by other player... $moves .= $append; $moves = str_replace("\n", "\\\n", $moves); // newline makes string split across multiple lines // must be "continued" with a preceding backslash $newBoard = getUpdatedBoard($row['board'], $move); $timestamp = time(); sqlite_exec($db, "update $table set moves = moves || '$append' , " . "board = '$newBoard' , " . "lastMoveTime = $timestamp ". "where id = $game_id"); if(preg_match('/(xk|x0)/', $move)) { // checkmate/captured king sqlite_exec($db, "update $table set status = 'finished', winner = $userColor where id = $game_id"); } else { echo "var moves = '$moves'; "; "stringBoard = '$newBoard'; ". "server.init(); ". 'setActivePlayer("'; echo ($isWhite) ? 'black' : 'white'; echo '"); '. 'board = boardStringToArray(stringBoard); setPiecesFromArray(board);'; } } else { die("alert('Not your turn, silly!');"); } } else { die("alert('Observers can't make moves, silly!');"); } } else if($action == 'play') { $row = sqlite_array_query($db, "select * from $table where id = $game_id", SQLITE_ASSOC); $row = $row[0]; // first row returned... $isWhite = $row['white'] == $user; $isBlack = $row['black'] == $user; $activePlayer = ( empty($row['moves']) || substr($row['moves'], -1) == "\n" ) ? 'white' : 'black'; ?> Che§<?php echo " -- Game $game_id, $row[white] vs. $row[black]"; ?>
It is currently White's turn. "; } ?>
" style="background-color: #daa502;">
h g f e d c b a
8 8
7 7
6 6
5 5
4 4
3 3
2 2
1 1
h g f e d c b a
"> 15 seconds till update

'.time(); echo ""; while (sqlite_has_more($result)) { $row = sqlite_fetch_array($result, SQLITE_ASSOC); $status = $row['status']; if($status == 'inprogress' ||($status == 'open' && $type != 'mine') ||($status == 'closed' && $type == 'mine')) $status = "".$status.""; $whitestyle = ($row['white'] == $user) ? ' style="font-weight: bold;"' : ''; $blackstyle = ($row['black'] == $user) ? ' style="font-weight: bold;"' : ''; echo " $row[white] $row[black] "; } echo "
id white black last move date status winner
$row[id] ". date('Y-n-j h:i:sa' , $row['lastMoveTime']) ." $status $row[winner]
"; } } else { if(isset($_GET['needupdate'])) { $lastClientMove = intval($_GET['needupdate']); $row = sqlite_array_query($db, "SELECT * FROM $table WHERE id = $game_id"); if(!empty($row)) { $row = $row[0]; $lastServerMove = intval($row['lastMoveTime']); } else { die( "/* No timestamp existed for the selected game (id=$game_id), ". "most likely because that game was deleted or never created. */"); } $needsUpdate = (($lastServerMove > $lastClientMove) ? true : false); if(!$needsUpdate) { echo '/* no update */'; return; } else { if($row['winner'] != 'none') { echo "server.updater.stop(); alert('Game Over. Winner is {$row['winner']}. Thanks for playing...');"; } // empty means white's turn; ending with newline means white's turn. $activePlayer = ( empty($row['moves']) || substr($row['moves'], -1) == "\n" ) ? 'white' : 'black'; preg_match('/[@\-](..)\s$/', $row['moves'], $matchInfo); $lastMovedPiece = $matchInfo[1]; $response = "server.updater.lastKnownUpdate = $lastServerMove; " . "stringBoard = '{$row['board']}'; " . "board = boardStringToArray(stringBoard); " . "setPiecesFromArray(board); " . "setActivePlayer('$activePlayer'); " . "removeAttributesFromElements(['lastMovedPiece'], 'td'); " . "document.getElementById('$lastMovedPiece').setAttribute('lastMovedPiece', 'true');"; echo $response; } exit; } else { ?> Che§
You are

List Games


Create New Game


(only enter if you know the exact name your opponent logs in with. Otherwise, leave blank.