Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include("commandLineTools.php");
- include("databaseTools.php");
- $pgnFile = file_get_contents("../PGNs/Kasparov.pgn");
- $pgnArray = explode("\n\r\n[", $pgnFile);
- $headers = array();
- foreach($pgnArray as $key => $value){
- if($key > 0){
- $pgn = "[".$value;
- }else{
- $pgn = $value;
- }
- //move current PGN to a temp file for usage by pgn-extract
- $pgnFile = 'pgn.pgn';
- $handle = fopen($pgnFile, 'w') or die('Cannot open file: '.$pgnFile);
- fwrite($handle, $pgn);
- fclose($handle);
- $fenMoves = extractFENPositions($pgnFile);
- $pgnMoves = extractPGNMoves($pgn);
- $header = extractPGNHeader($pgn);
- $header['PGN'] = implode(' ', $pgnMoves);
- //Change 'Site' to 'Location'
- if(isset($header['Site'])){
- $header['Location'] = $header['Site'];
- unset($header['Site']);
- }
- //change 'Result' to ternary value
- if(isset($header['Result'])){
- if($header['Result'] == "1-0"){
- $header['Result'] = 2;
- }elseif($header['Result'] == "0-1"){
- $header['Result'] = 1;
- }else{
- $header['Result'] = 0;
- }
- }
- //Fix date
- $tempDate = explode('.', $header['Date']);
- if($tempDate[0] == '????'){
- $tempDate[0] = '2000';
- }
- if($tempDate[1] == '??'){
- $tempDate[1] = '01';
- }
- if($tempDate[2] == '??'){
- $tempDate[2] = '01';
- }
- $header['Date'] = $tempDate[0].'.'.$tempDate[1].'.'.$tempDate[2];
- //Fix Round
- $header['Round'] = intval($header['Round']);
- if($header['Round'] == NULL){
- $header['Round'] = 1;
- }
- //echo $header['White'].' ('.$header['WhiteElo'].') VS '.$header['Black'].' ('.$header['BlackElo'].") \n";
- //echo 'Date: '.$header['Date'].' Event: '.$header['Event'].' Location: '.$header['Location'].' Result: '.$header['Result']."\n\n";
- //Check if players exist
- if($white = findPlayer( array('Name' => $header['White']) ) ){
- //echo $header['White']." is already in the database! Updating Player\n";
- //Check if the game is newer or older that the first/last recorded games
- $intLastGame = intval( str_replace('.', '', $white['LastGame']) );
- $intFirstGame = intval( str_replace('.', '', $white['FirstGame']) );
- $intDate = intval( str_replace('.', '', $header['Date']) );
- if($intDate > $intLastGame){
- //echo "This game is newer!\n";
- $white['LastGame'] = $header['Date'];
- //If this game is newer, update the players ELO
- if(!empty($header['WhiteElo'])){
- //echo "Updating ELO!\n";
- $white['ELO'] = $header['WhiteElo'];
- }
- }elseif($intDate < $intFirstGame){
- //echo "This game is old!\n";
- $white['FirstGame'] = $header['Date'];
- }
- }else{
- //echo $header['White']."White Player Does Not Exist! Creating Player\n";
- if(empty($header['BlackElo'])){
- $header['BlackElo'] = 2000;
- }
- $white = createPlayer( array('Name' => $header['White'],
- 'ELO' => $header['WhiteElo'],
- 'FirstGame' => $header['Date'],
- 'LastGame' => $header['Date'],
- 'GamesPlayed' => 0) );
- }
- //Check if players exist
- if($black = findPlayer( array('Name' => $header['Black']) ) ){
- //echo $header['Black']." White is already in the database! Updating Player\n";
- //Check if the game is newer or older that the first/last recorded games
- $intLastGame = intval( str_replace('.', '', $black['LastGame']) );
- $intFirstGame = intval( str_replace('.', '', $black['FirstGame']) );
- $intDate = intval( str_replace('.', '', $header['Date']) );
- if($intDate > $intLastGame){
- //echo "This game is newer!\n";
- $black['LastGame'] = $header['Date'];
- //If this game is newer, update the players ELO
- if(!empty($header['BlackElo'])){
- //echo "Updating ELO!\n";
- $black['ELO'] = $header['BlackElo'];
- }
- }elseif($intDate < $intFirstGame){
- //echo "This game is old!\n";
- $black['FirstGame'] = $header['Date'];
- }
- }else{
- //echo $header['Black']."Black Player Does Not Exist! Creating Player\n";
- if(empty($header['BlackElo'])){
- $header['BlackElo'] = 2000;
- }
- $black = createPlayer( array('Name' => $header['Black'],
- 'ELO' => $header['BlackElo'],
- 'FirstGame' => $header['Date'],
- 'LastGame' => $header['Date'],
- 'GamesPlayed' => 0) );
- }
- //echo "Creating Game!\n";
- if( $game = findGame( array('White' => $white['id'],
- 'Black' => $black['id'],
- 'Result' => $header['Result'],
- 'PGN' => $header['PGN'],
- 'Date' => $header['Date']) ) ){
- //Game already exists. Do nothing
- //echo "Game Already Exists!\n";
- }else{
- //Create Game!
- $game = createGame( array( 'White' => $white['id'],
- 'Black' => $black['id'],
- 'Result' => $header['Result'],
- //'Opening' => $header['Opening'], Add later
- 'Event' => $header['Event'],
- 'Location' => $header['Location'],
- 'Round' => $header['Round'],
- 'Date' => $header['Date'],
- 'PGN' => $header['PGN'] ) );
- //Update player game count!
- $white['GamesPlayed']++;
- $black['GamesPlayed']++;
- updatePlayer($white);
- updatePlayer($black);
- //Create positions
- //echo "Creating Positions!\n";
- $parent['id'] = 1;
- foreach($pgnMoves as $key => $value){
- //just use the key
- //echo $pgnMoves[$key] . ' = ' . $fenMoves[$key]. "\n";
- $side = $key%2;
- //$side = $key%1;
- //check if the position already exists
- if( $position = findPosition( array( 'FEN' => $fenMoves[$key], 'Side' => $side ) ) ){
- //position already exists
- //echo "Position Already Exists! Updating Position\n";
- $position['TimesPlayed']++;
- if($header['Result'] == 2){
- //white win
- $position['WhiteWinRate']++;
- updatePosition($position);
- }elseif($header['Result'] == 1){
- //black win
- $position['BlackWinRate']++;
- updatePosition($position);
- }else{
- //draw
- $position['DrawRate']++;
- updatePosition($position);
- }
- } else {
- //create position
- if($header['Result'] == 2){
- //white win
- $position = createPosition( array( 'FEN' => $fenMoves[$key], 'Side' => $side , 'WhiteWinRate' => 1) );
- }elseif($header['Result'] == 1){
- //black win
- $position = createPosition( array( 'FEN' => $fenMoves[$key], 'Side' => $side , 'BlackWinRate' => 1) );
- }else{
- //draw
- $position = createPosition( array( 'FEN' => $fenMoves[$key], 'Side' => $side , 'DrawRate' => 1) );
- }
- //echo "Position Created! ".$position['id']."\n";
- }
- //link position to parent position and game
- //echo "Creating Game Link!\n";
- $gameRelation = createGameRelation( array('PlayedMove' => $pgnMoves[$key], 'GameID' => $game['id'], 'PositionID' => $position['id']) );
- //echo "Game Link Created! ". $gameRelation['id']."\n";
- //echo "Creating Position Relation!\n";
- if( $positionRelation = findPositionRelation( array('PlayedMove' => $pgnMoves[$key], 'ParentID' => $parent['id'], 'ChildID' => $position['id']) ) ){
- $positionRelation['TimesPlayed']++;
- $positionRelation = updatePositionRelation($positionRelation);
- //echo "Position Relation Already Exists!\n";
- }else{
- $positionRelation = createPositionRelation( array('PlayedMove' => $pgnMoves[$key], 'ParentID' => $parent['id'], 'ChildID' => $position['id'], 'TimesPlayed' => 1) );
- }
- $parent = $position;
- }
- }
- //echo "Creating Positions!\n";
- //echo $header['PGN']."\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment