/// <summary> /// Creates a deck from a deck file /// </summary> /// <param name="pathToFile">Path to the deck file</param> /// <returns>The deck based on the deck file</returns> /// <remarks> /// A deck file follows the following format: /// [count], [card name] /// with each card on a separate line /// </remarks> public static Deck FromDeckFile(string pathToFile) { var deck = new Deck(); using (var stream = new StreamReader(pathToFile)) { while (stream.Peek() >= 0) { var line = stream.ReadLine(); if (line == null) continue; // lines follow the format of [count], [card name] var splitLine = line.Split(','); if (splitLine.Count() != 2) { throw new InvalidDataException(string.Format("Expected format of line is \"[count], [card name]\". Got {0} instead", line)); } var count = int.Parse(splitLine[0]); var cardName = splitLine[1]; var cleanedCardName = Regex.Replace(cardName, @"[\W]", "", RegexOptions.None); for (int i = 0; i < count; i++) { var assembly = Assembly.Load("HearthAnalyzer.Core"); var cardType = assembly.GetTypes().FirstOrDefault(t => String.Equals(t.Name, cleanedCardName, StringComparison.InvariantCultureIgnoreCase)); if (cardType == null) { throw new InvalidDataException(string.Format("Failed to find card type with name: {0}", cleanedCardName)); } var createCardMethod = typeof (HearthEntityFactory).GetMethod("CreateCard"); var createCardTypeMethod = createCardMethod.MakeGenericMethod(new[] {cardType}); dynamic card = createCardTypeMethod.Invoke(null, null); deck.AddCard(card); } } } if (deck.Cards.Count() > Constants.MAX_CARDS_IN_DECK) { throw new InvalidDataException("There are too many cards in this deck!"); } if (deck.Cards.Count() < Constants.MAX_CARDS_IN_DECK) { throw new InvalidDataException("There are too few cards in this deck!"); } return deck; }
public void FromValidDeckFile() { var zooLockDeckFile = Path.Combine(DeckTestDataPath, "ZooLock.txt"); var actualDeck = Deck.FromDeckFile(zooLockDeckFile); HearthEntityFactory.Reset(); var expectedDeck = new Deck(new List<BaseCard>() { HearthEntityFactory.CreateCard<Soulfire>(), HearthEntityFactory.CreateCard<Soulfire>(), HearthEntityFactory.CreateCard<AbusiveSergeant>(), HearthEntityFactory.CreateCard<AbusiveSergeant>(), HearthEntityFactory.CreateCard<ArgentSquire>(), HearthEntityFactory.CreateCard<ArgentSquire>(), HearthEntityFactory.CreateCard<ElvenArcher>(), HearthEntityFactory.CreateCard<FlameImp>(), HearthEntityFactory.CreateCard<FlameImp>(), HearthEntityFactory.CreateCard<Voidwalker>(), HearthEntityFactory.CreateCard<Voidwalker>(), HearthEntityFactory.CreateCard<DireWolfAlpha>(), HearthEntityFactory.CreateCard<DireWolfAlpha>(), HearthEntityFactory.CreateCard<KnifeJuggler>(), HearthEntityFactory.CreateCard<KnifeJuggler>(), HearthEntityFactory.CreateCard<LorewalkerCho>(), HearthEntityFactory.CreateCard<BloodKnight>(), HearthEntityFactory.CreateCard<HarvestGolem>(), HearthEntityFactory.CreateCard<HarvestGolem>(), HearthEntityFactory.CreateCard<ScarletCrusader>(), HearthEntityFactory.CreateCard<ScarletCrusader>(), HearthEntityFactory.CreateCard<ShatteredSunCleric>(), HearthEntityFactory.CreateCard<ShatteredSunCleric>(), HearthEntityFactory.CreateCard<DarkIronDwarf>(), HearthEntityFactory.CreateCard<DarkIronDwarf>(), HearthEntityFactory.CreateCard<DefenderofArgus>(), HearthEntityFactory.CreateCard<DefenderofArgus>(), HearthEntityFactory.CreateCard<Doomguard>(), HearthEntityFactory.CreateCard<Doomguard>(), HearthEntityFactory.CreateCard<ArgentCommander>() }); Assert.IsTrue(actualDeck.Cards.SequenceEqual(expectedDeck.Cards), "Verify the generated deck is the same."); }