public void ImportGames(string[] pgnFiles) { RaiseStatus($"Beginning import of {pgnFiles.Length} PGN files at: {DateTime.Now}\n"); var fileCount = 0; var averages = new List <int>(); RaiseStatusShowingFileImportHeader(); pgnFiles.ToList().ForEach(file => { fileCount++; try { RaiseStatus($"({fileCount:G3}/{pgnFiles.Length:G3}) {Path.GetFileName(file)}".PadRight(50)); var pgnGames = PgnGame.ReadAllGamesFromFile(file).ToArray(); var importGames = pgnGames .Select(_mapper.Map <PgnImport>) .Distinct(PgnImport.PgnDeDupeQueueComparer) // NOTE: Ensures no duplicates games within the file .ToList(); RaiseStatus($"{importGames.Count}".PadLeft(7)); var sw = Stopwatch.StartNew(); var createdCount = _pgnRepository.QueuePgnGamesForValidation(importGames); sw.Stop(); RaiseStatus($"{createdCount}".PadLeft(7) + $"{pgnGames.Length - createdCount}".PadLeft(7)); var createdPerSec = 0; if (sw.ElapsedMilliseconds > 0) { createdPerSec = createdCount * (1000 / sw.Elapsed.Milliseconds); averages.Add(createdPerSec); } RaiseStatus($"{sw.Elapsed.Milliseconds}ms".PadLeft(7)); RaiseStatus($"{createdPerSec}".PadLeft(7)); RaiseStatus("\n"); } catch (Exception e) { RaiseStatus($"ERROR: {e.Message}\n"); if (e is SqlException) { throw; } } }); if (averages.Any()) { RaiseStatus($"\n{pgnFiles.Length} total files processed, {averages.Average()} games created per second.\n"); } }
private PgnImport MapToPgnDeDupeQueue(PgnGame pgnGame) { var game = new PgnImport { Event = pgnGame.Event, Black = pgnGame.Black, Site = pgnGame.Site, White = pgnGame.White, Round = pgnGame.Round, Date = pgnGame.Date.RevertDateToText(), Result = pgnGame.Result.RevertToText(), MoveList = NormaliseMoveText(pgnGame.MoveText) }; var usedTags = new List <string>(MandatoryTags); var pairs = pgnGame.TagPairs.ToDictionary(k => k.Name, v => v.Value); void ParseCommonTag(string tag, Action <string> valueSetter) { if (!pairs.ContainsKeyInsensitive(tag)) { return; } usedTags.Add(tag.ToLowerInvariant()); var ecoValue = pairs.ValueInsensitive(tag); if (!string.IsNullOrWhiteSpace(ecoValue)) { valueSetter(ecoValue); } } ParseCommonTag("ECO", v => game.Eco = v); ParseCommonTag("WhiteELO", v => game.WhiteElo = v); ParseCommonTag("BlackELO", v => game.BlackElo = v); var customTags = pgnGame.TagPairs .Where(t => !usedTags.Contains(t.Name.ToLowerInvariant())) .ToDictionary(k => k.Name, v => v.Value); game.CustomTagsJson = customTags.Any() ? JsonSerializer.Serialize(customTags) : null; return(game); }