示例#1
0
 public function parse()
 {
     $games = Game::get();
     foreach ($games as $game) {
         if (preg_match('/Event "(.*?)"/', $game->pgn, $match)) {
             $game->event = $match[1];
         }
         if (preg_match('/Site "(.*?)"/', $game->pgn, $match)) {
             $game->site = $match[1];
         }
         if (preg_match('/[^a-zA-Z]Date "(.*?)"/', $game->pgn, $match)) {
             $game->gameDate = $match[1];
             if (preg_match('/([0-9]+)/', $game->gameDate, $match2)) {
                 $game->year = $match2[1];
             }
         }
         if (preg_match('/Round "(.*?)"/', $game->pgn, $match)) {
             $game->gameRound = $match[1];
         }
         if (preg_match('/White "(.*?)"/', $game->pgn, $match)) {
             $game->white = $match[1];
         }
         if (preg_match('/Black "(.*?)"/', $game->pgn, $match)) {
             $game->black = $match[1];
         }
         if (preg_match('/Result "(.*?)"/', $game->pgn, $match)) {
             $game->gameResult = $match[1];
         }
         if (preg_match('/ECO "(.*?)"/', $game->pgn, $match)) {
             $game->eco = $match[1];
         }
         if (preg_match('/WhiteElo "(.*?)"/', $game->pgn, $match)) {
             $game->whiteElo = " (" . $match[1] . ")";
         } else {
             $game->whiteElo = "";
         }
         if (preg_match('/BlackElo "(.*?)"/', $game->pgn, $match)) {
             $game->blackElo = " (" . $match[1] . ")";
         } else {
             $game->blackElo = "";
         }
         // Compose a title from players information
         $game->title = $game->white . $game->whiteElo . " - " . $game->black . $game->blackElo;
         // Begin with complete pgn of game
         $game->moves = $game->pgn;
         // Strip comments
         $game->moves = preg_replace('/(\\{.*?\\})/', '', $game->moves);
         // Strip pgn tags
         $game->moves = preg_replace('/\\[.*\\]/', '', $game->moves);
         // Convert multiple white space to a single blank char
         $game->moves = preg_replace("/\\s+/", ' ', $game->moves);
         // Remove alternative lines and variations
         $game->moves = self::parseMoves($game->moves);
         // Strip single & double quotes
         $game->moves = self::strip_slashes($game->moves);
         // Find eco code for string opening name and moves
         $res = DB::select(DB::raw("SELECT * FROM mcc_ecos WHERE eco LIKE '%{$game->eco}%' LIMIT 1"));
         $game->ecoCode = $res[0]->id;
         $game->save();
     }
     $games = Game::sortable()->paginate(env('GAME_PAGINATION_MAX'));
     return view('games.index')->with('games', $games);
 }