Example #1
0
 static function load_chess_game($game_id)
 {
     ChessHelper::$game_id = $game_id;
     ChessHelper::$CB = new ChessBoard2();
     ChessHelper::$CBU = new ChessBoardUtilities();
     ChessHelper::$last_move_time = NULL;
     $moves = array();
     try {
         // TODO: set starting FEN
         $dbh = CSession::$dbh;
         $stmt = $dbh->prepare("SELECT `move`, `time` FROM `move_history` WHERE `game_id` = ? ORDER BY time ASC");
         $stmt->bind_param('s', $game_id);
         if ($stmt->execute()) {
             $results = ChessHelper::get_results($stmt);
             foreach ($results as $result) {
                 $moves[] = $result['move'];
                 ChessHelper::$last_move_time = $result['time'];
             }
         } else {
             echo "<ERROR>Database Error</ERROR>\n";
             return false;
         }
         //echo "<pre>"; var_dump($moves); echo '</pre>';
         $fen = ChessHelper::get_custom_fen($game_id);
         if ($fen) {
             ChessHelper::$CB->SetupBoardWithFEN($fen);
         }
     } catch (mysqli_sql_exception $e) {
         echo "<ERROR>Database Connection Error</ERROR>\n";
         return false;
     }
     //$CBU = new ChessBoardUtilities();
     foreach ($moves as $move) {
         $promo = '';
         // Castle moves are stored with the side that made the move.
         if ($move == 'O-O w') {
             $from = 'e1';
             $to = 'g1';
         } else {
             if ($move == 'O-O-O w') {
                 $from = 'e1';
                 $to = 'c1';
             } else {
                 if ($move == 'O-O b') {
                     $from = 'e8';
                     $to = 'g8';
                 } else {
                     if ($move == 'O-O-O b') {
                         $from = 'e8';
                         $to = 'c8';
                     } else {
                         // En passant moves are stored as "from,to tile_num". Don't care about the tile_num part.
                         $move = substr($move, 0, 6);
                         // Get possible promotion piece.
                         if (strlen($move) == 6) {
                             if ($move[5] == 'Q') {
                                 $promo = PIECE_TYPE::QUEEN;
                             } else {
                                 if ($move[5] == 'B') {
                                     $promo = PIECE_TYPE::BISHOP;
                                 } else {
                                     if ($move[5] == 'N') {
                                         $promo = PIECE_TYPE::KNIGHT;
                                     } else {
                                         if ($move[5] == 'R') {
                                             $promo = PIECE_TYPE::ROOK;
                                         }
                                     }
                                 }
                             }
                         }
                         // Now only want 5 chars to get from and to tiles.
                         $move = substr($move, 0, 5);
                         list($from, $to) = explode(',', $move);
                     }
                 }
             }
         }
         $from = ChessHelper::$CBU->ConvertAlgebraicNotationTileToInteger($from);
         $to = ChessHelper::$CBU->ConvertAlgebraicNotationTileToInteger($to);
         //echo "$from -> $to<br/>";
         ChessHelper::$CB->MakeMove($from, $to, $promo, $moveType, true, true);
         //echo "<br/>move type: $moveType";
         //echo "<br/>Board status is: " . $CB->GetGameBoardStatus();
         // ChessHelper::$CB->debug_bitboards();
     }
     return true;
 }