ClarkeRubber

commandLineTools.php

Dec 30th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.11 KB | None | 0 0
  1. <?php
  2.  
  3. function getEval($fenPosition, $movetime, $uciEnginePath = "/Volumes/Extra\ Stuff/Dev/ChessDatabase/stockfish-3-mac/Mac/stockfish-3-32"){
  4.     $descriptorspec = array(
  5.         0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  6.         1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
  7.         2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
  8.     );
  9.  
  10.     $cwd = '/tmp';
  11.     $env = array('some_option' => 'aeiou');
  12.  
  13.     $process = proc_open("$uciEnginePath", $descriptorspec, $pipes, $cwd, $env);
  14.  
  15.     if (is_resource($process)) {
  16.  
  17.         fwrite($pipes[0], "uci\n");
  18.         fwrite($pipes[0], "ucinewgame\n");
  19.         fwrite($pipes[0], "isready\n");
  20.         fwrite($pipes[0], "position fen $fenPosition\n");
  21.         fwrite($pipes[0], "go movetime $movetime\n");
  22.         usleep(1000 * $movetime);
  23.         fclose($pipes[0]);
  24.  
  25.         preg_match_all("/cp (-?[0-9]*?)(?= )/", stream_get_contents($pipes[1]), $matches);
  26.  
  27.         preg_match("/-?[0-9]+/", end($matches[0]), $output);
  28.  
  29.         fclose($pipes[1]);
  30.     }
  31.     return $output[0];
  32. }
  33.  
  34. function getEvalBulk($fenPositions, $movetime, $uciEnginePath = "/Volumes/Extra\ Stuff/Dev/ChessDatabase/stockfish-3-mac/Mac/stockfish-3-32"){
  35.     foreach ($fenPositions as $key => $fenPosition) {
  36.         $descriptorspec = array(
  37.             0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  38.             1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
  39.             2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
  40.         );
  41.  
  42.         $cwd = '/tmp';
  43.         $env = array('some_option' => 'aeiou');
  44.  
  45.         $process = proc_open("$uciEnginePath", $descriptorspec, $pipes, $cwd, $env);
  46.  
  47.         if (is_resource($process)) {
  48.  
  49.             fwrite($pipes[0], "uci\n");
  50.             fwrite($pipes[0], "ucinewgame\n");
  51.             fwrite($pipes[0], "isready\n");
  52.             fwrite($pipes[0], "position fen $fenPosition\n");
  53.             fwrite($pipes[0], "go movetime $movetime\n");
  54.             usleep(1000 * $movetime);
  55.             fclose($pipes[0]);
  56.  
  57.             preg_match_all("/cp (-?[0-9]*?)(?= )/", stream_get_contents($pipes[1]), $matches);
  58.  
  59.             //print_r(end($matches[0]));
  60.  
  61.             preg_match("/-?[0-9]+/", end($matches[0]), $output);
  62.  
  63.             //print_r($output);
  64.  
  65.             $eval[$key] = $output[0];
  66.  
  67.             fclose($pipes[1]);
  68.         }
  69.     }
  70.     return $eval;
  71. }
  72.  
  73. function extractFENPositions($pgnFile, $stripCounter = TRUE, $pgnExtractPath = "./../pgn-extract/pgn-extract"){
  74.     $fen = shell_exec("$pgnExtractPath --fencomments -s ".$pgnFile);
  75.  
  76.     preg_match_all("/\{(\n|\r|.)*?\}/", $fen, $fenTemp);
  77.  
  78.     $matches = array("/\n/", "/({ )|( })/");
  79.     $replaces = array(" ", "");
  80.  
  81.     $output = array();
  82.  
  83.     foreach($fenTemp[0] as $value){
  84.        
  85.         $value = preg_replace($matches, $replaces, $value);
  86.  
  87.         if($stripCounter == TRUE){
  88.             $value = explode(" ", $value);
  89.             array_pop($value);
  90.             array_pop($value);
  91.             array_pop($value);
  92.             $value = implode(" ", $value);
  93.         }
  94.        
  95.        
  96.         $output[] = $value;
  97.     }
  98.  
  99.     return $output;
  100. }
  101.  
  102. function extractPGNHeader($pgn){
  103.     //$pgn = file_get_contents($pgnFile);
  104.  
  105.     preg_match_all("/\[.*?\]/", $pgn, $matches);
  106.  
  107.     $output = array();
  108.  
  109.     foreach ($matches[0] as $key => $value) {
  110.         preg_match("/(?<=\[).*?(?= )/", $value, $title);
  111.         preg_match("/(?<=\").*?(?=\")/", $value, $data);
  112.  
  113.         $output[$title[0]] = $data[0];
  114.     }
  115.  
  116.     return $output;
  117. }
  118.  
  119. function extractPGNMoves($pgn){
  120.     //$pgn = file_get_contents($pgnFile);
  121.  
  122.     $match = array("/\[.*?\]/", "/\n|\r/", "/\. /", "/\(.*?\)/", "/\{.*\}/");
  123.     $replace = array("", " ", ".", "", "");
  124.  
  125.     $pgn = explode(" ", trim(preg_replace($match, $replace, $pgn)));
  126.  
  127.     array_pop($pgn);
  128.  
  129.     foreach ($pgn as $key => $value) {
  130.         if(empty($value)){
  131.             unset($pgn[$key]);
  132.         }
  133.     }
  134.  
  135.     $pgn = array_values($pgn);
  136.  
  137.     return $pgn;
  138. }
  139.  
  140. $pgnExtractPath = "./../pgn-extract/pgn-extract";
  141. $uciEnginePath = "/Volumes/Extra\ Stuff/Dev/ChessDatabase/stockfish-3-mac/Mac/stockfish-3-32"; //absolute path to uci engine
  142. $movetime = 500; //thousandths of a second
  143.  
  144. /*
  145. $pgnFile = "test.pgn";
  146. $pgnHeader = extractPGNHeader($pgnFile);
  147. $pgnMoves = extractPGNMoves($pgnFile);
  148. $fenPositions = extractFENPositions($pgnFile, $pgnExtractPath, TRUE);
  149. $evaluations = getEvalBulk($fenPositions, $movetime, $uciEnginePath);
  150. */
Advertisement
Add Comment
Please, Sign In to add comment