Ejemplo n.º 1
0
        private static void EncodeNOfs(List <byte> bytes, List <CardCodeAndCount> nOfs)
        {
            foreach (CardCodeAndCount ccc in nOfs)
            {
                bytes.AddRange(VarintTranslator.GetVarint(ccc.Count));

                ParseCardCode(ccc.CardCode, out int setNumber, out string factionCode, out int cardNumber);
                int factionNumber = FactionCodeToIntIdentifier[factionCode];

                bytes.AddRange(VarintTranslator.GetVarint(setNumber));
                bytes.AddRange(VarintTranslator.GetVarint(factionNumber));
                bytes.AddRange(VarintTranslator.GetVarint(cardNumber));
            }
        }
Ejemplo n.º 2
0
        private static void EncodeGroupOf(List <byte> bytes, List <List <CardCodeAndCount> > groupOf)
        {
            bytes.AddRange(VarintTranslator.GetVarint(groupOf.Count));
            foreach (List <CardCodeAndCount> currentList in groupOf)
            {
                //how many cards in current group?
                bytes.AddRange(VarintTranslator.GetVarint(currentList.Count));

                //what is this group, as identified by a set and faction pair
                string currentCardCode = currentList[0].CardCode;
                ParseCardCode(currentCardCode, out int currentSetNumber, out string currentFactionCode, out int _);
                int currentFactionNumber = FactionCodeToIntIdentifier[currentFactionCode];
                bytes.AddRange(VarintTranslator.GetVarint(currentSetNumber));
                bytes.AddRange(VarintTranslator.GetVarint(currentFactionNumber));

                //what are the cards, as identified by the third section of card code only now, within this group?
                foreach (CardCodeAndCount cd in currentList)
                {
                    string code           = cd.CardCode;
                    int    sequenceNumber = int.Parse(code.Substring(4, 3));
                    bytes.AddRange(VarintTranslator.GetVarint(sequenceNumber));
                }
            }
        }
Ejemplo n.º 3
0
        public static List <CardCodeAndCount> GetDeckFromCode(string code)
        {
            List <CardCodeAndCount> result = new List <CardCodeAndCount>();

            byte[] bytes = null;
            try
            {
                bytes = Base32.Decode(code);
            }
            catch
            {
                throw new ArgumentException("Invalid deck code");
            }

            List <byte> byteList = bytes.ToList();

            //grab format and version
            int format  = bytes[0] >> 4;
            int version = bytes[0] & 0xF;

            byteList.RemoveAt(0);

            if (version > MAX_KNOWN_VERSION)
            {
                throw new ArgumentException("The provided code requires a higher version of this library; please update.");
            }

            for (int i = 3; i > 0; i--)
            {
                int numGroupOfs = VarintTranslator.PopVarint(byteList);

                for (int j = 0; j < numGroupOfs; j++)
                {
                    int numOfsInThisGroup = VarintTranslator.PopVarint(byteList);
                    int set     = VarintTranslator.PopVarint(byteList);
                    int faction = VarintTranslator.PopVarint(byteList);

                    for (int k = 0; k < numOfsInThisGroup; k++)
                    {
                        int card = VarintTranslator.PopVarint(byteList);

                        string setString     = set.ToString().PadLeft(2, '0');
                        string factionString = IntIdentifierToFactionCode[faction];
                        string cardString    = card.ToString().PadLeft(3, '0');

                        CardCodeAndCount newEntry = new CardCodeAndCount()
                        {
                            CardCode = setString + factionString + cardString, Count = i
                        };
                        result.Add(newEntry);
                    }
                }
            }

            //the remainder of the deck code is comprised of entries for cards with counts >= 4
            //this will only happen in Limited and special game modes.
            //the encoding is simply [count] [cardcode]
            while (byteList.Count > 0)
            {
                int fourPlusCount   = VarintTranslator.PopVarint(byteList);
                int fourPlusSet     = VarintTranslator.PopVarint(byteList);
                int fourPlusFaction = VarintTranslator.PopVarint(byteList);
                int fourPlusNumber  = VarintTranslator.PopVarint(byteList);

                string fourPlusSetString     = fourPlusSet.ToString().PadLeft(2, '0');
                string fourPlusFactionString = IntIdentifierToFactionCode[fourPlusFaction];
                string fourPlusNumberString  = fourPlusNumber.ToString().PadLeft(3, '0');

                CardCodeAndCount newEntry = new CardCodeAndCount()
                {
                    CardCode = fourPlusSetString + fourPlusFactionString + fourPlusNumberString, Count = fourPlusCount
                };
                result.Add(newEntry);
            }

            return(result);
        }