<?php require 'resourcewrapper.class.php'; $wrapper = new ResourceWrapper(); $wrapper->setMiddleGame('getMiddle', '1.0', true, true); $wrapper->rest(); // Variables $originalpath = 'Crafty.ini'; $inputpath = 'input.txt'; $outputpath = 'log.001'; // Get the middle moves according to the FEN. function getMiddle($fen) { $originalpath = 'Crafty.ini'; $inputpath = 'input.txt'; $outputpath = 'log.001'; // Step 1 : Create "input.txt" file // ---------------------------------------------- if (copy($originalpath, $inputpath)) { $inputfile = fopen($inputpath, "a"); // "a" = Start writing at the end of the file fwrite($inputfile, "\nsetboard " . $fen . " b \n"); fwrite($inputfile, "go\n"); // *Important* Write \n at the end to launch search fclose($inputfile); } else { echo "Failed to copy {$file}...\n"; } // Step 2 : Crafty process the move // ---------------------------------------------- exec('Crafty.bat');
<?php require 'resourcewrapper.class.php'; $wrapper = new ResourceWrapper(); $wrapper->setDatabaseOpenings('http://www.shredderchess.com/online/playshredder/fetch.php?action=book&fen=$url_fen$&la=en&bookcode=p40', '1.0', true, 'parserOpeningsToJSON', true); $wrapper->setDatabaseEndings('http://www.shredderchess.com/online/playshredder/fetch.php?action=egtb&fen=$url_fen$s', '1.0', false, 'parserEndingsToJSON', true); $wrapper->rest(); // Parse the text result from the distant website to get a JSON document. function parserOpeningsToJSON($text, $fen) { $lines = explode("\n", $text); $moves = array(); foreach ($lines as $line) { $infos = explode('#', $line); if (count($infos) == 14) { $move_exp = explode('...', $infos[0]); $move = "null"; if (count($move_exp) > 0) { $move = $move_exp[1]; } $percentage = $infos[7] / 1000; $wins = round($infos[11] / $infos[6], 3); $draws = round($infos[12] / $infos[6], 3); $looses = round($infos[13] / $infos[6], 3); $nb_play = (int) $infos[6]; if ($move != "null") { $moves[] = array('move' => $move, 'probatowin' => $wins, 'probatonull' => $draws, 'nb' => $nb_play); } } } return json_encode($moves);
public function rest() { // URL parser: $input = file_get_contents('php://input'); $chars = preg_split('/\\//', $_SERVER['REQUEST_URI'], -1, PREG_SPLIT_NO_EMPTY); if (count($chars) == 3) { if ($this->openings && $chars[1] == 'openings') { if ($chars[2] == 'version') { // Return the version number. echo $this->open_version; } else { // Get the FEN: // $ are replaced by /. $fen = rawurldecode($chars[2]); $fen = str_replace('$', '/', $fen); if ($this->open_function != null) { $result = call_user_func($this->open_function, $fen); } else { if ($this->open_post) { $result = $this->curlPost($this->open_url, $fen, $this->open_complete_fen); } else { $result = $this->curlGet($this->open_url, $fen, $this->open_complete_fen); } } // Display the result as a JSON document: header("Content-Type: application/json"); if ($this->open_parser == null) { echo $result; } else { echo call_user_func($this->open_parser, $result, $fen); } } } else { if ($this->endings && $chars[1] == 'endings') { if ($chars[2] == 'version') { // Return the version number. echo $this->end_version; } else { // Get the FEN: // $ are replaced by /. $fen = rawurldecode($chars[2]); $fen = str_replace('$', '/', $fen); if ($this->end_function != null) { $result = call_user_func($this->end_function, $fen); } else { if ($this->end_post) { $result = $this->curlPost($this->end_url, $fen, $this->end_complete_fen); } else { $result = $this->curlGet($this->end_url, $fen, $this->end_complete_fen); } } // Display the result as a JSON document: header("Content-Type: application/json"); if ($this->end_parser == null) { echo $result; } else { echo call_user_func($this->end_parser, $result, $fen); } } } else { ResourceWrapper::error404Redirection(); } } } elseif (count($chars) == 2) { if ($chars[1] == 'version') { // Return the version number. echo $this->middle_version; } else { if ($this->middle) { // Get the FEN: // $ are replaced by /. $fen = rawurldecode($chars[1]); $fen = str_replace('$', '/', $fen); if (!$this->middle_complete_fen) { //On enlève les deux derniers chiffres du fen preg_match("#^(.+)((\\s[0-9]+){2})\$#", $fen, $matches); if (count($matches) > 0) { $fen = $matches[1]; } } $result = call_user_func($this->middle_function, $fen); // Display the result as a JSON document: header("Content-Type: application/json"); echo $result; } else { ResourceWrapper::error404Redirection(); } } } else { ResourceWrapper::error404Redirection(); } }
<?php require 'resourcewrapper.class.php'; $wrapper = new ResourceWrapper(); $wrapper->setCustomOpenings('getOpenings', '1.0', true); $wrapper->setCustomEndings('getEndings', '1.0', true); $wrapper->rest(); // Open the connection to the local mysql database. function connectToDatabase() { try { $db = new PDO('mysql:host=localhost;dbname=chessgames', 'root', ''); } catch (PDOException $e) { exit('Error: ' . $e->getMessage()); } return $db; } // Get the openings moves according to the FEN. function getOpenings($fen) { $db = connectToDatabase(); try { $moves = $db->prepare("SELECT move, probatowin, probatonull, nb FROM openings WHERE fen LIKE ?"); $moves->execute(array($fen)); } catch (PDOException $e) { exit('Error: ' . $e->getMessage()); } $arrayMoves = $moves->fetchAll(); return json_encode($arrayMoves); } // Get the endings moves according to the FEN.
<?php require 'resourcewrapper.class.php'; $wrapper = new ResourceWrapper(); $wrapper->setDatabaseOpenings('http://chessok.com/onlineserv/opening/connection.php?timestamp=' . time(), '1.0', false, 'parserOpeningsToJSON', false); $wrapper->setDatabaseEndings('http://chessok.com/onlineserv/endbase/connection.php?timestamp=' . time(), '1.0', true, 'parserEndingsToJson', true); $wrapper->rest(); // Transforme le xml renvoye par chessok en JSon et l'affiche. function parserOpeningsToJSON($xmlstr, $fen) { // On regarde qui va jouer (blancs ou noirs). preg_match("/^[^ ]* ([bw]) .*\$/", $fen, $matches); if (count($matches) == 2) { $whiteToPlay = $matches[1] == 'w'; } $moves = new SimpleXMLElement($xmlstr); // On calcule toutes les stats necessaires pour le serveur central. $movesArray = array(); foreach ($moves->MoveList->Item as $item) { $nbWWhite = $item->WhiteWins; $nbWBlack = $item->BlackWins; $nbDraws = $item->Draws; $nb = $nbWWhite + $nbWBlack + $nbDraws; // Cast pour eviter de se retouver avec un SimpleXMLObject au lieu d'un string. $move = (string) $item->Move; if ($nb > 0) { if ($whiteToPlay) { $probaToWin = round($nbWWhite / $nb, 3); } else { $probaToWin = round($nbWBlack / $nb, 3); }