Пример #1
0
        private static string Serialize(Deck deck)
        {
            if (deck == null)
            {
                throw  new InvalidDeckException("Deck can not be null");
            }
            if (deck.HeroDbfId == 0)
            {
                throw new InvalidDeckException("HeroDbfId can not be zero");
            }
            if (deck.GetHero()?.Type != CardType.HERO)
            {
                throw new InvalidDeckException("HeroDbfId does not represent a valid hero");
            }
            if (deck.Format == FormatType.FT_UNKNOWN)
            {
                throw new InvalidDeckException("Format can not be FT_UNKNOWN");
            }

            using (var ms = new MemoryStream())
            {
                void Write(int value)
                {
                    if (value == 0)
                    {
                        ms.WriteByte(0);
                    }
                    else
                    {
                        var bytes = VarInt.GetBytes((ulong)value);
                        ms.Write(bytes, 0, bytes.Length);
                    }
                }

                ms.WriteByte(0);
                Write(1);
                Write((int)deck.Format);
                Write(1);
                Write(deck.HeroDbfId);

                var cards      = deck.CardDbfIds.OrderBy(x => x.Key).ToList();
                var singleCopy = cards.Where(x => x.Value == 1).ToList();
                var doubleCopy = cards.Where(x => x.Value == 2).ToList();
                var nCopy      = cards.Where(x => x.Value > 2).ToList();

                Write(singleCopy.Count);
                foreach (var card in singleCopy)
                {
                    Write(card.Key);
                }

                Write(doubleCopy.Count);
                foreach (var card in doubleCopy)
                {
                    Write(card.Key);
                }

                Write(nCopy.Count);
                foreach (var card in nCopy)
                {
                    Write(card.Key);
                    Write(card.Value);
                }

                return(Convert.ToBase64String(ms.ToArray()));
            }
        }
Пример #2
0
        private static Deck DeserializeDeckString(string deckString)
        {
            var deck = new Deck();

            byte[] bytes;
            try
            {
                bytes = Convert.FromBase64String(deckString);
            }
            catch (Exception e)
            {
                throw new ArgumentException("Input is not a valid deck string.", e);
            }
            var offset = 0;

            ulong Read()
            {
                if (offset > bytes.Length)
                {
                    throw new ArgumentException("Input is not a valid deck string.");
                }
                var value = VarInt.ReadNext(bytes.Skip(offset).ToArray(), out var length);

                offset += length;
                return(value);
            }

            //Zero byte
            offset++;

            //Version - always 1
            Read();

            deck.Format = (FormatType)Read();

            //Num Heroes - always 1
            Read();

            deck.HeroDbfId = (int)Read();

            void AddCard(int?dbfId = null, int count = 1)
            {
                dbfId = dbfId ?? (int)Read();
                deck.CardDbfIds[dbfId.Value] = count;
            }

            var numSingleCards = (int)Read();

            for (var i = 0; i < numSingleCards; i++)
            {
                AddCard();
            }

            var numDoubleCards = (int)Read();

            for (var i = 0; i < numDoubleCards; i++)
            {
                AddCard(count: 2);
            }

            var numMultiCards = (int)Read();

            for (var i = 0; i < numMultiCards; i++)
            {
                var dbfId = (int)Read();
                var count = (int)Read();
                AddCard(dbfId, count);
            }
            return(deck);
        }