//public void ImportDeckFromText(GameItem gameItem, string text, MappingManager mappingManager) //{ // DeckItem deck = new DeckItem(gameItem.Code); // deck.Category = "Imported"; // deck.Name = "???"; // ImportMwsDeck(deck, text, mappingManager); // SaveDeck(deck); //} public DeckImportStatistics ImportDecksFromFiles(GameInfoItem gameItem, string[] fileNames, CodesMapService mappingManager) { DeckImportStatistics statistics = new DeckImportStatistics(); foreach(string fileName in fileNames) { Dictionary<string, string> discardedLines = new Dictionary<string, string>(); if(string.Compare(Path.GetExtension(fileName), DECK_FILE_EXTENSION) != 0) { DeckItem deck = new DeckItem(gameItem.Code); deck.Category = "Imported"; deck.Name = Path.GetFileNameWithoutExtension(fileName); ImportMwsDeck(gameItem, deck, File.ReadAllText(fileName), mappingManager, discardedLines); SaveDeck(deck); } else { File.Copy(fileName, Path.Combine(SettingsManager.Settings.DeckPath, Path.GetFileName(fileName)), true); } statistics.ImportResults.Add(new DeckImportResult() { FileName = fileName, Successfull = discardedLines.Count == 0, DiscardedLines = discardedLines }); } return statistics; }
void ImportMwsDeck(GameInfoItem gameItem, DeckItem deck, string text, CodesMapService mappingManager, Dictionary<string, string> discardedLines) { ServicesProvider engServicesProvider = servicesProvider; if(engServicesProvider.GameLanguageCode != "eng") { engServicesProvider = new ServicesProvider("eng", "eng"); engServicesProvider.LoadGame(gameItem); } discardedLines.Clear(); foreach(string line in text.Split(new string[] {Environment.NewLine}, StringSplitOptions.None). Select(e => e.Trim()).Where(e => e.Length > 0 && !e.StartsWith("//"))) { string[] lineParts = line.Split(' '); if(lineParts.Length < 3) continue; try { var cardList = deck.MainCards; int partIndex = 0; if(lineParts[0] == "SB:") { partIndex++; cardList = deck.SideboardCards; } int amount; if(int.TryParse(lineParts[partIndex++], out amount)) { string mwsSetCode = string.Empty; try { mwsSetCode = lineParts[partIndex++].Replace("[", string.Empty).Replace("]", string.Empty); } catch { throw new Exception("Cannot read set code on this line: " + line); } string cardName = string.Empty; try { cardName = string.Concat( lineParts.Skip(partIndex).Where(e => e != "(1)" && e != "(2)" && e != "(3)" && e != "(4)").Select( e => string.Concat(e, " ")).ToArray()).TrimEnd(); cardName = cardName.Split(new string[] { "--" }, StringSplitOptions.None).First(); } catch { throw new Exception("Cannot read card name on this line: " + line); } string internalSetCode = mappingManager.GetCode(mwsSetCode); if(string.IsNullOrEmpty(internalSetCode)) throw new Exception("Set code unknown: " + mwsSetCode); CardSearchParams p = new CardSearchParams(); p.Sets.Add(internalSetCode); p.Name = cardName; p.Language = "ENG"; p.NameExact = true; CardItem cardItem = engServicesProvider.CardsService.SelectCards(p).FirstOrDefault(); if(cardItem == null) throw new Exception("Card name unknown: " + cardName); for(int i = 0; i < amount; i++) cardList.Add(cardItem.Clone()); } else { throw new Exception("Cannot read card amount on this line: " + line); } } catch(Exception ex) { discardedLines.Add(line, ex.Message); } } }