public static async Task <List <ChessGame> > FetchGameRecordsForUser(string username, DirectoryInfo cacheDir) { Helpers.ResetDisplayCounter(); ConcurrentBag <ChessGame> PgnList = new ConcurrentBag <ChessGame>(); ArchivedGamesList monthlyArchive = await GetPlayerMonthlyArchive(username).ConfigureAwait(false); Parallel.ForEach(monthlyArchive.Archives, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (dataForMonth) => { string[] urlSplit = dataForMonth.Split('/'); Task <PlayerArchivedGames> t2 = GetAllPlayerMonthlyGames(cacheDir, username, int.Parse(urlSplit[7], CultureInfo.InvariantCulture), int.Parse(urlSplit[8], CultureInfo.InvariantCulture)); t2.Wait(); try { foreach (ArchiveGame game in t2.Result.Games) { if (game.Rules == GameVariant.Chess) { Helpers.ProcessedDisplay("."); PgnList.Add(new ChessGame() { Source = "ChessDotCom", Text = game.Pgn, IsRatedGame = game.IsRated, Rules = GameVariant.Chess.ToString(), TimeControl = game.TimeControl, TimeClass = game.TimeClass.ToString(), WhiteRating = game.IsRated ? game.White.Rating : 0, BlackRating = game.IsRated ? game.Black.Rating : 0, GameAttributes = GameHeader.GetHeaderAttributesFromPgn(game.Pgn) }); } else { Helpers.ProcessedDisplay("X"); } } } catch { Helpers.ProcessedDisplay("E"); } }); return(PgnList.ToList()); }
public static GameHeader GetHeaderAttributesFromPgn(string gameText) { GameHeader pgnHeader = new GameHeader(); foreach (string attribute in Regex.Split(gameText, @"^\[(.*?)\]", RegexOptions.Multiline)) { string[] nameVal = attribute.TrimStart('[').TrimEnd(']').Split(' ', 2); if (nameVal.Length == 2) { if (Enum.TryParse <SupportedAttribute>(nameVal[0], out SupportedAttribute attrib)) { pgnHeader.Attributes.Add( attrib.ToString(), nameVal[1].TrimStart('\"').TrimEnd('\"')); } } } return(pgnHeader); }