ClarkeRubber

main.php

Dec 30th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.21 KB | None | 0 0
  1. <?php
  2.  
  3. include("commandLineTools.php");
  4. include("databaseTools.php");
  5.  
  6. $pgnFile = file_get_contents("../PGNs/Kasparov.pgn");
  7.  
  8. $pgnArray = explode("\n\r\n[", $pgnFile);
  9.  
  10. $headers = array();
  11.  
  12. foreach($pgnArray as $key => $value){
  13.     if($key > 0){
  14.         $pgn = "[".$value;
  15.     }else{
  16.         $pgn = $value;
  17.     }
  18.  
  19.     //move current PGN to a temp file for usage by pgn-extract
  20.  
  21.     $pgnFile = 'pgn.pgn';
  22.  
  23.     $handle = fopen($pgnFile, 'w') or die('Cannot open file:  '.$pgnFile);
  24.     fwrite($handle, $pgn);
  25.     fclose($handle);
  26.  
  27.     $fenMoves = extractFENPositions($pgnFile);
  28.     $pgnMoves = extractPGNMoves($pgn);
  29.  
  30.     $header = extractPGNHeader($pgn);
  31.     $header['PGN'] = implode(' ', $pgnMoves);
  32.  
  33.     //Change 'Site' to 'Location'
  34.  
  35.     if(isset($header['Site'])){
  36.         $header['Location'] = $header['Site'];
  37.         unset($header['Site']);
  38.     }
  39.  
  40.  
  41.     //change 'Result' to ternary value
  42.     if(isset($header['Result'])){
  43.         if($header['Result'] == "1-0"){
  44.             $header['Result'] = 2;
  45.         }elseif($header['Result'] == "0-1"){
  46.             $header['Result'] = 1;
  47.         }else{
  48.             $header['Result'] = 0;
  49.         }
  50.     }
  51.  
  52.     //Fix date
  53.     $tempDate = explode('.', $header['Date']);
  54.  
  55.     if($tempDate[0] == '????'){
  56.         $tempDate[0] = '2000';
  57.     }
  58.     if($tempDate[1] == '??'){
  59.         $tempDate[1] = '01';
  60.     }
  61.     if($tempDate[2] == '??'){
  62.         $tempDate[2] = '01';
  63.     }
  64.  
  65.     $header['Date'] = $tempDate[0].'.'.$tempDate[1].'.'.$tempDate[2];
  66.  
  67.     //Fix Round
  68.     $header['Round'] = intval($header['Round']);
  69.     if($header['Round'] == NULL){
  70.         $header['Round'] = 1;
  71.     }
  72.  
  73.     //echo $header['White'].' ('.$header['WhiteElo'].') VS '.$header['Black'].' ('.$header['BlackElo'].") \n";
  74.     //echo 'Date: '.$header['Date'].' Event: '.$header['Event'].' Location: '.$header['Location'].' Result: '.$header['Result']."\n\n";
  75.  
  76.     //Check if players exist
  77.     if($white = findPlayer( array('Name' => $header['White']) ) ){
  78.         //echo $header['White']." is already in the database! Updating Player\n";
  79.  
  80.         //Check if the game is newer or older that the first/last recorded games
  81.         $intLastGame = intval( str_replace('.', '', $white['LastGame']) );
  82.         $intFirstGame = intval( str_replace('.', '', $white['FirstGame']) );
  83.  
  84.         $intDate = intval( str_replace('.', '', $header['Date']) );
  85.  
  86.         if($intDate > $intLastGame){
  87.             //echo "This game is newer!\n";
  88.  
  89.             $white['LastGame'] = $header['Date'];
  90.             //If this game is newer, update the players ELO
  91.  
  92.             if(!empty($header['WhiteElo'])){
  93.                 //echo "Updating ELO!\n";
  94.  
  95.                 $white['ELO'] = $header['WhiteElo'];
  96.             }
  97.  
  98.         }elseif($intDate < $intFirstGame){
  99.             //echo "This game is old!\n";
  100.  
  101.             $white['FirstGame'] = $header['Date'];
  102.         }
  103.     }else{
  104.         //echo $header['White']."White Player Does Not Exist! Creating Player\n";
  105.         if(empty($header['BlackElo'])){
  106.             $header['BlackElo'] = 2000;
  107.         }
  108.  
  109.         $white = createPlayer( array('Name' => $header['White'],
  110.             'ELO' => $header['WhiteElo'],
  111.             'FirstGame' => $header['Date'],
  112.             'LastGame' => $header['Date'],
  113.             'GamesPlayed' => 0) );
  114.     }
  115.  
  116.     //Check if players exist
  117.     if($black = findPlayer( array('Name' => $header['Black']) ) ){
  118.         //echo $header['Black']." White is already in the database! Updating Player\n";
  119.  
  120.         //Check if the game is newer or older that the first/last recorded games
  121.         $intLastGame = intval( str_replace('.', '', $black['LastGame']) );
  122.         $intFirstGame = intval( str_replace('.', '', $black['FirstGame']) );
  123.  
  124.         $intDate = intval( str_replace('.', '', $header['Date']) );
  125.  
  126.         if($intDate > $intLastGame){
  127.             //echo "This game is newer!\n";
  128.  
  129.             $black['LastGame'] = $header['Date'];
  130.             //If this game is newer, update the players ELO
  131.  
  132.             if(!empty($header['BlackElo'])){
  133.                 //echo "Updating ELO!\n";
  134.  
  135.                 $black['ELO'] = $header['BlackElo'];
  136.             }
  137.  
  138.         }elseif($intDate < $intFirstGame){
  139.             //echo "This game is old!\n";
  140.  
  141.             $black['FirstGame'] = $header['Date'];
  142.         }
  143.     }else{
  144.         //echo $header['Black']."Black Player Does Not Exist! Creating Player\n";
  145.         if(empty($header['BlackElo'])){
  146.             $header['BlackElo'] = 2000;
  147.         }
  148.  
  149.         $black = createPlayer( array('Name' => $header['Black'],
  150.             'ELO' => $header['BlackElo'],
  151.             'FirstGame' => $header['Date'],
  152.             'LastGame' => $header['Date'],
  153.             'GamesPlayed' => 0) );
  154.     }
  155.  
  156.     //echo "Creating Game!\n";
  157.  
  158.     if( $game = findGame( array('White' => $white['id'],
  159.         'Black' => $black['id'],
  160.         'Result' => $header['Result'],
  161.         'PGN' => $header['PGN'],
  162.         'Date' => $header['Date']) ) ){
  163.         //Game already exists. Do nothing
  164.  
  165.         //echo "Game Already Exists!\n";
  166.     }else{
  167.         //Create Game!
  168.         $game = createGame( array( 'White' => $white['id'],
  169.         'Black' => $black['id'],
  170.         'Result' => $header['Result'],
  171.         //'Opening' => $header['Opening'], Add later
  172.         'Event' => $header['Event'],
  173.         'Location' => $header['Location'],
  174.         'Round' => $header['Round'],
  175.         'Date' => $header['Date'],
  176.         'PGN' => $header['PGN'] ) );
  177.  
  178.         //Update player game count!
  179.  
  180.         $white['GamesPlayed']++;
  181.         $black['GamesPlayed']++;
  182.  
  183.         updatePlayer($white);
  184.         updatePlayer($black);
  185.  
  186.  
  187.         //Create positions
  188.  
  189.         //echo "Creating Positions!\n";
  190.  
  191.         $parent['id'] = 1;
  192.  
  193.         foreach($pgnMoves as $key => $value){
  194.  
  195.             //just use the key
  196.             //echo $pgnMoves[$key] . ' = ' . $fenMoves[$key]. "\n";
  197.             $side = $key%2;
  198.             //$side = $key%1;
  199.  
  200.             //check if the position already exists
  201.  
  202.             if( $position = findPosition( array( 'FEN' => $fenMoves[$key],  'Side' => $side ) ) ){
  203.                 //position already exists
  204.                 //echo "Position Already Exists! Updating Position\n";
  205.  
  206.                 $position['TimesPlayed']++;
  207.  
  208.                 if($header['Result'] == 2){
  209.                     //white win
  210.                     $position['WhiteWinRate']++;
  211.                     updatePosition($position);
  212.  
  213.                 }elseif($header['Result'] == 1){
  214.                     //black win
  215.                     $position['BlackWinRate']++;
  216.                     updatePosition($position);
  217.  
  218.                 }else{
  219.                     //draw
  220.                     $position['DrawRate']++;
  221.                     updatePosition($position);
  222.  
  223.                 }
  224.  
  225.             } else {
  226.                 //create position
  227.                 if($header['Result'] == 2){
  228.                     //white win
  229.                     $position = createPosition( array( 'FEN' => $fenMoves[$key],  'Side' => $side , 'WhiteWinRate' => 1) );
  230.                 }elseif($header['Result'] == 1){
  231.                     //black win
  232.                     $position = createPosition( array( 'FEN' => $fenMoves[$key],  'Side' => $side , 'BlackWinRate' => 1) );
  233.                 }else{
  234.                     //draw
  235.                     $position = createPosition( array( 'FEN' => $fenMoves[$key],  'Side' => $side , 'DrawRate' => 1) );
  236.                 }
  237.                
  238.                 //echo "Position Created! ".$position['id']."\n";
  239.             }
  240.  
  241.             //link position to parent position and game
  242.  
  243.             //echo "Creating Game Link!\n";
  244.  
  245.             $gameRelation = createGameRelation( array('PlayedMove' => $pgnMoves[$key], 'GameID' => $game['id'], 'PositionID' => $position['id']) );
  246.  
  247.             //echo "Game Link Created! ". $gameRelation['id']."\n";
  248.  
  249.             //echo "Creating Position Relation!\n";
  250.  
  251.             if( $positionRelation = findPositionRelation( array('PlayedMove' => $pgnMoves[$key], 'ParentID' => $parent['id'], 'ChildID' => $position['id']) ) ){
  252.                 $positionRelation['TimesPlayed']++;
  253.                 $positionRelation = updatePositionRelation($positionRelation);
  254.                 //echo "Position Relation Already Exists!\n";
  255.             }else{
  256.                 $positionRelation = createPositionRelation( array('PlayedMove' => $pgnMoves[$key], 'ParentID' => $parent['id'], 'ChildID' => $position['id'], 'TimesPlayed' => 1) );
  257.             }
  258.  
  259.             $parent = $position;
  260.         }
  261.  
  262.  
  263.     }
  264.  
  265.     //echo "Creating Positions!\n";
  266.  
  267.     //echo $header['PGN']."\n";
  268.  
  269.    
  270. }
Advertisement
Add Comment
Please, Sign In to add comment