// Instance CTOR public Dominion(string name, string text, DominionSet set, List <DominionCardType> types, int coinCost, bool costsAPotion = false) { Name = name; Text = text; Set = set; Types = types; CoinCost = coinCost; CostsAPotion = costsAPotion; }
public void ParseCSVFileToCardList() { // Create object used to process .csv file. TextFieldParser parser = new TextFieldParser(@"../../../DominionCardList.csv"); // Ensure fields enclosed by double-quotes are treated verbatim. // Commas may appear within card text, which will mess up a straight .Split() on commas. parser.HasFieldsEnclosedInQuotes = true; parser.SetDelimiters(","); // Skip the first line, which contains column headers / titles. parser.ReadLine(); // Read the entire file. while (!parser.EndOfData) { // Each line contains data for a single card. // Read a line and break it into separate pieces of info. string[] cardInfo = parser.ReadFields(); // Column number - Description // 0 - Name // 1 - Set number // 2 - Set // 3 - Type // 4 - Cost (ex. $3, $2P) // 5 - QRankCat (ex. Cost 3, Potion) // 6 - 2012QRank1 // 7 - 2012QRank2 // 8 - 2013QRank // 9 - 2014QRank // 10 - Text // Card text contains extra delimiters to emulate the actual text on the physical cards. // Replace delimiters to get "pure" text. cardInfo[10] = cardInfo[10].Replace("\\n", ". ").Replace("\\d", ". "); List <DominionCardType> types = new List <DominionCardType>(); // Individual card types separated by a hyphen. string[] cardTypes = cardInfo[3].Split('-'); foreach (string type in cardTypes) { try { // Attempt to parse a card type from the string and add it to the list of types that define the card. types.Add((DominionCardType)Enum.Parse(typeof(DominionCardType), type, ignoreCase: true)); } catch (Exception ex) { Console.WriteLine("Error parsing card type for [" + cardInfo[0] + "] : " + ex.Message); } } DominionSet set = DominionSet.Base; try { // Using slightly modified terminology from source data. cardInfo[2] = cardInfo[2].Replace("Base Cards", "Supply"); cardInfo[2] = cardInfo[2].Replace("Dark Ages", "DarkAges"); cardInfo[2] = cardInfo[2].Replace("Dominion", "Base"); set = (DominionSet)Enum.Parse(typeof(DominionSet), cardInfo[2], ignoreCase: true); } catch (Exception ex) { Console.WriteLine("Error parsing card set for [" + cardInfo[0] + "] : " + ex.Message); } // Extract the coin cost from one of the fields. int coinCost = 0; int.TryParse(new string(cardInfo[4].Where(c => char.IsDigit(c)).ToArray()), out coinCost); // Determine if the card costs a potion to buy. bool costsAPotion = cardInfo[5].Equals("Potion", StringComparison.CurrentCultureIgnoreCase); _cardList.Add(new Dominion(cardInfo[0], // Card name cardInfo[10], // Processed card text set, // Processed card set types, // Processed card types coinCost, // Extracted coin cost costsAPotion)); // Does it cost a potion to buy? } }