// METHODS public override List <CardCodeAndCount> GetActiveDeck() { List <CardCodeAndCount> deck = new List <CardCodeAndCount>(); if (!_isActive) { return(deck); } try { _client ??= new HttpClient(); Task <HttpResponseMessage> resp = _client.GetAsync("http://127.0.0.1:21337/static-decklist"); if (resp.Result.IsSuccessStatusCode) { string respBody = resp.Result.Content.ReadAsStringAsync().Result; DeckResponse deckResponse = JsonConvert.DeserializeObject <DeckResponse>(respBody); if (deckResponse != null && deckResponse.DeckCode != string.Empty) { deck = LoRDeckEncoder.GetDeckFromCode(deckResponse.DeckCode); } } } catch (AggregateException) { //TODO: do something about that lol. } catch (ArgumentException) { //TODO: do something about that lol. } return(deck); }
public void MtTargonSet() { List <CardCodeAndCount> deck = new List <CardCodeAndCount>(); deck.Add(new CardCodeAndCount() { CardCode = "01DE002", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "03MT003", Count = 2 }); deck.Add(new CardCodeAndCount() { CardCode = "03MT010", Count = 3 }); deck.Add(new CardCodeAndCount() { CardCode = "02BW004", Count = 5 }); string code = LoRDeckEncoder.GetCodeFromDeck(deck); List <CardCodeAndCount> decoded = LoRDeckEncoder.GetDeckFromCode(code); Assert.True(VerifyRehydration(deck, decoded)); }
private void LoadLor(string line) { if (string.IsNullOrEmpty(line)) { return; } if (line.StartsWith("#")) { if (line.StartsWith("###")) { Name = line.Substring(3).Trim(); } return; } List <CardCodeAndCount> cardCodeAndCounts = LoRDeckEncoder.GetDeckFromCode(line); foreach (CardCodeAndCount cardCount in cardCodeAndCounts) { if (!((UnityCardGame)SourceGame).Cards.TryGetValue(cardCount.CardCode, out UnityCard card)) { continue; } for (var i = 0; i < cardCount.Count; i++) { _cards.Add(card); } } }
public static IEnumerable <CardData> GetCardsFromCode(this string deckCode, ILogger logger = null) { logger ??= new FileLogger(); var cards = new List <CardData>(); // Gather all card data to map to current deck. try { var allCardData = new List <CardData>(); var cardDataFilePaths = Directory.GetFiles(@$ "{Directory.GetCurrentDirectory()}\wwwroot\assets", "set*-en_us.json", SearchOption.AllDirectories); foreach (var cardDataFilePath in cardDataFilePaths) { var cardData = JsonConvert.DeserializeObject <IEnumerable <CardData> >(File.ReadAllText(cardDataFilePath)); allCardData.AddRange(cardData); } var cardCodeAndCounts = LoRDeckEncoder.GetDeckFromCode(deckCode); foreach (var cardCodeAndCount in cardCodeAndCounts) { int.TryParse(cardCodeAndCount.CardCode.Substring(0, 2), out int cardSet); var card = allCardData.FirstOrDefault(c => c.CardCode == cardCodeAndCount.CardCode); if (card != null) { cards.Add(new CardData { CardCode = cardCodeAndCount.CardCode, Count = cardCodeAndCount.Count, Name = card.Name, Description = card.Description, LevelupDescription = card.LevelupDescription, Region = card.Region, RegionRef = card.RegionRef, Cost = card.Cost, Attack = card.Attack, Health = card.Health, Type = card.Type, FlavorText = card.FlavorText, Keywords = card.Keywords, SpellSpeed = card.SpellSpeed, Subtype = card.Subtype, Subtypes = card.Subtypes, Supertype = card.Supertype, Rarity = card.Rarity, Collectible = card.Collectible }); } } } catch (Exception ex) { logger.Error($"Error occurred getting card data from assets: {ex.Message}"); } return(cards.OrderBy(c => c.Cost)); }
public void GarbageDecoding() { var badEncodingNotBase32 = "I'm no card code!"; var badEncoding32 = "ABCDEFG"; var badEncodingEmpty = ""; Assert.ThrowsException <ArgumentException>(() => LoRDeckEncoder.GetDeckFromCode(badEncodingNotBase32)); Assert.ThrowsException <ArgumentException>(() => LoRDeckEncoder.GetDeckFromCode(badEncoding32)); Assert.ThrowsException <ArgumentException>(() => LoRDeckEncoder.GetDeckFromCode(badEncodingEmpty)); }
public void SmallDeck() { List <CardCodeAndCount> deck = new List <CardCodeAndCount>(); deck.Add(new CardCodeAndCount() { CardCode = "01DE002", Count = 1 }); string code = LoRDeckEncoder.GetCodeFromDeck(deck); List <CardCodeAndCount> decoded = LoRDeckEncoder.GetDeckFromCode(code); Assert.True(VerifyRehydration(deck, decoded)); }
public void SmallDeck() { var deck = new List <CardCodeAndCount> { new CardCodeAndCount { CardCode = "01DE002", Count = 1 } }; string code = LoRDeckEncoder.GetCodeFromDeck(deck); var decoded = LoRDeckEncoder.GetDeckFromCode(code); Assert.IsTrue(VerifyRehydration(deck, decoded)); }
public void GarbageDecoding() { string badEncodingNotBase32 = "I'm no card code!"; string badEncoding32 = "ABCDEFG"; string badEncodingEmpty = ""; bool failed = false; try { List <CardCodeAndCount> deck = LoRDeckEncoder.GetDeckFromCode(badEncodingNotBase32); } catch (ArgumentException) { failed = true; } catch (Exception e) { Assert.True(false, $"Expected to throw an ArgumentException, but it threw {e}."); } Assert.True(failed, "Expected to throw an ArgumentException, but it succeeded."); failed = false; try { List <CardCodeAndCount> deck = LoRDeckEncoder.GetDeckFromCode(badEncoding32); } catch (ArgumentException) { failed = true; } catch (Exception e) { Assert.True(false, $"Expected to throw an ArgumentException, but it threw {e}."); } Assert.True(failed, "Expected to throw an ArgumentException, but it succeeded."); failed = false; try { List <CardCodeAndCount> deck = LoRDeckEncoder.GetDeckFromCode(badEncodingEmpty); } catch { failed = true; } Assert.True(failed, "Expected to throw an ArgumentException, but it succeeded."); }
private static void TryDecodeCollection() { List <CardCodeAndCount> cards = LoRDeckEncoder.GetDeckFromCode( "CEKQCAYDCIBAGBQGBYBAGBARCIBAGAAGBQEQGAQBAIBQQCIKBMIRECQCAIAQEAYEAUDAOCAJBIFAEBABAIBQIBIGA4EASCQKAIA" + "ACAQDAQCQMBYIBEFAUAQFAEBAGBAFAYDQQCIKBIBACAICAMCAKBQHBAEQUCQDAUBAGBAFAYDQUDANBYFAEAYBAIBQIBIGA4EASC" + "QKAMAQEBAFAYDQQCQLCEJDAAYJAMDAODAOB4IBCEYUCULRUGY4DUQSGJBGE4USYLZQGM3DQOR6H5DUQSKLJRIVEVCVKZLVQWK4L" + "ZQGENABAEAQGBAFAYDQQCIKBMGA2DQQCEJBGFAVCYLRQGI2DMOR4IBBEIRSIJJGE4UCSKRLFUXC6MBRGIZTINJWG44DSNIBAAAQ" + "EAYEAYDQSCQLBQGQ4DYRCIJRIFIWC4MBSGQ3DQOR4HZAEERCGJBFEYTSQKJKFMWC2LRPGAYTEMZUGU3DOOBVAECACAQDAQCQMBY" + "IBEFAYDIOCAIREEYUCULRQGI2DMOB2HQ7EERCGJBGE4UCUKZMFUXC6MBRGIZTINJWG44DSOR3GUAQEAICAMCAKBQHBAEQUCYMBU" + "HA6EARCIJRIFIWC4MBUGY4DUPB6IBBEQSSMJZIFEVCWLBNFYXTAMJSGQ2TMNZYHE2QCAYCAMCAKBQHBAEQUCYMBUHA6EARCMKBK" + "FQXDAMRUGY5DYPSAIJCEMSCKJRHFAUSUKZMFUXC6MBRGIZTINJWG44DKAIFAEBQIBIGA4EQUCYMBYHRAEQTCQKRMFYZDINRYHI6" + "D4QCCIRDEQSSMJZIFEVCWLBNFYXTAMJSGM2DKNRXHA4TUPQCAYAQEAYEAUDAOCAJBIFQYDIOB4IBCEQTCQKRMFYYDENBWHA5DYP" + "SAIJCEMSCKJRHFAUSUKZMFUXC6MBRGIZTKNRXHA4TUOZ4HU7D6AQBAMBBGAYDBEBBSPACAEBQGEIBAEAR6"); cards.ForEach(c => Console.WriteLine($"{c.CardCode}: {c.Count}")); }
public void BadVersion() { // make sure that a deck with an invalid version fails List <CardCodeAndCount> deck = new List <CardCodeAndCount>(); deck.Add(new CardCodeAndCount() { CardCode = "01DE002", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE003", Count = 2 }); deck.Add(new CardCodeAndCount() { CardCode = "02DE003", Count = 3 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE004", Count = 5 }); List <byte> bytesFromDeck = Base32.Decode(LoRDeckEncoder.GetCodeFromDeck(deck)).ToList(); List <byte> result = new List <byte>(); byte[] formatAndVersion = new byte[] { 88 }; // arbitrary invalid format/version result.AddRange(formatAndVersion); bytesFromDeck.RemoveAt(0); // remove the actual format/version result.Concat(bytesFromDeck); // replace with invalid one try { string badVersionDeckCode = Base32.Encode(result.ToArray()); List <CardCodeAndCount> deckBad = LoRDeckEncoder.GetDeckFromCode(badVersionDeckCode); } catch (ArgumentException e) { string expectedErrorMessage = "The provided code requires a higher version of this library; please update."; Console.WriteLine(e.Message); Assert.Equal(expectedErrorMessage, e.Message); } }
public static bool GetDeckFromCode(out List <CardCodeAndCount> deck, string deckCode) { deck = new List <CardCodeAndCount>(); try { deck = LoRDeckEncoder.GetDeckFromCode(deckCode); } catch (ArgumentException) { return(false); } catch { return(false); } return(true); }
public void GarbageDecoding() { string badEncodingNotBase32 = "I'm no card code!"; string badEncoding32 = "ABCDEFG"; string badEncodingEmpty = ""; try { List <CardCodeAndCount> deck = LoRDeckEncoder.GetDeckFromCode(badEncodingNotBase32); Assert.Fail(); } catch (ArgumentException) { } catch { Assert.Fail(); } try { List <CardCodeAndCount> deck = LoRDeckEncoder.GetDeckFromCode(badEncoding32); Assert.Fail(); } catch (ArgumentException) { } catch { Assert.Fail(); } try { List <CardCodeAndCount> deck = LoRDeckEncoder.GetDeckFromCode(badEncodingEmpty); Assert.Fail(); } catch { } }
public void EncodeDecodeRecommendedDecks() { List <string> codes = new List <string>(); List <List <CardCodeAndCount> > decks = new List <List <CardCodeAndCount> >(); //Load the test data from file. string line; using (StreamReader myReader = new StreamReader(GetTestDataFilePath())) { while ((line = myReader.ReadLine()) != null) { codes.Add(line); List <CardCodeAndCount> newDeck = new List <CardCodeAndCount>(); while (!string.IsNullOrEmpty(line = myReader.ReadLine())) { string[] parts = line.Split(new char[] { ':' }); newDeck.Add(new CardCodeAndCount() { Count = int.Parse(parts[0]), CardCode = parts[1] }); } decks.Add(newDeck); } } //Encode each test deck and ensure it's equal to the correct string. //Then decode and ensure the deck is unchanged. for (int i = 0; i < decks.Count; i++) { string encoded = LoRDeckEncoder.GetCodeFromDeck(decks[i]); Assert.Equal(codes[i], encoded); List <CardCodeAndCount> decoded = LoRDeckEncoder.GetDeckFromCode(encoded); Assert.True(VerifyRehydration(decks[i], decoded)); } }
public void WorstCaseLength() { List <CardCodeAndCount> deck = new List <CardCodeAndCount>(); deck.Add(new CardCodeAndCount() { CardCode = "01DE002", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE003", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE004", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE005", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE006", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE007", Count = 5 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE008", Count = 6 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE009", Count = 7 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE010", Count = 8 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE011", Count = 9 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE012", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE013", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE014", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE015", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE016", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE017", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE018", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE019", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE020", Count = 4 }); deck.Add(new CardCodeAndCount() { CardCode = "01DE021", Count = 4 }); string code = LoRDeckEncoder.GetCodeFromDeck(deck); List <CardCodeAndCount> decoded = LoRDeckEncoder.GetDeckFromCode(code); Assert.True(VerifyRehydration(deck, decoded)); }
public void LargeDeck() { var deck = new List <CardCodeAndCount> { new CardCodeAndCount { CardCode = "01DE002", Count = 3 }, new CardCodeAndCount { CardCode = "01DE003", Count = 3 }, new CardCodeAndCount { CardCode = "01DE004", Count = 3 }, new CardCodeAndCount { CardCode = "01DE005", Count = 3 }, new CardCodeAndCount { CardCode = "01DE006", Count = 3 }, new CardCodeAndCount { CardCode = "01DE007", Count = 3 }, new CardCodeAndCount { CardCode = "01DE008", Count = 3 }, new CardCodeAndCount { CardCode = "01DE009", Count = 3 }, new CardCodeAndCount { CardCode = "01DE010", Count = 3 }, new CardCodeAndCount { CardCode = "01DE011", Count = 3 }, new CardCodeAndCount { CardCode = "01DE012", Count = 3 }, new CardCodeAndCount { CardCode = "01DE013", Count = 3 }, new CardCodeAndCount { CardCode = "01DE014", Count = 3 }, new CardCodeAndCount { CardCode = "01DE015", Count = 3 }, new CardCodeAndCount { CardCode = "01DE016", Count = 3 }, new CardCodeAndCount { CardCode = "01DE017", Count = 3 }, new CardCodeAndCount { CardCode = "01DE018", Count = 3 }, new CardCodeAndCount { CardCode = "01DE019", Count = 3 }, new CardCodeAndCount { CardCode = "01DE020", Count = 3 }, new CardCodeAndCount { CardCode = "01DE021", Count = 3 } }; string code = LoRDeckEncoder.GetCodeFromDeck(deck); var decoded = LoRDeckEncoder.GetDeckFromCode(code); Assert.IsTrue(VerifyRehydration(deck, decoded)); }
public void ArgumentExceptionOnFutureVersion() { const string singleCardDeckWithVersion5 = "CUAAAAIBAUAAC"; Assert.Throws <ArgumentException>(() => LoRDeckEncoder.GetDeckFromCode(singleCardDeckWithVersion5)); }