Esempio n. 1
0
        //Temporarily store group visibility information for LoadDeck. //bug (google) #20

        public void LoadDeck(IDeck deck, bool limited)
        {
            var def    = Program.GameEngine.Definition;
            int nCards = deck.CardCount();
            var ids    = new int[nCards];
            var keys   = new Guid[nCards];
            var cards  = new Card[nCards];
            var groups = new Play.Group[nCards];
            var sizes  = new string[nCards];
            var gtmps  = new List <GrpTmp>(); //for temp groups visibility
            int j      = 0;

            foreach (var section in deck.Sections)
            {
                { // Add cards to LoadedCards deck
                    if (!LoadedCards.Sections.Any(x => x.Name == section.Name))
                    {
                        // Add section
                        ((ObservableCollection <ObservableSection>)LoadedCards.Sections).Add(new ObservableSection()
                        {
                            Name   = section.Name,
                            Shared = section.Shared,
                            Cards  = new ObservableCollection <ObservableMultiCard>()
                        });
                    }

                    var loadedCardsSection = LoadedCards.Sections.Single(x => x.Name == section.Name);

                    foreach (var card in section.Cards)
                    {
                        var existingCard = loadedCardsSection.Cards.FirstOrDefault(x => x.Id == card.Id);
                        if (existingCard != null)
                        {
                            existingCard.Quantity += card.Quantity;
                        }
                        else
                        {
                            var newCard = new ObservableMultiCard(card);
                            loadedCardsSection.Cards.AddCard(newCard);
                        }
                    }
                }

                DeckSection sectionDef = null;
                sectionDef = section.Shared ? def.SharedDeckSections[section.Name] : def.DeckSections[section.Name];
                if (sectionDef == null)
                {
                    throw new InvalidFileFormatException("Invalid section '" + section.Name + "' in deck file.");
                }
                var        player = section.Shared ? Player.GlobalPlayer : Player.LocalPlayer;
                Play.Group group  = player.Groups.First(x => x.Name == sectionDef.Group.Name); //TODO: match directly to SectionDef Group instead of name matching

                //In order to make the clients know what the card is (if visibility is set so that they can see it),
                //we have to set the visibility to Nobody, and then after the cards are sent, set the visibility back
                //to what it was. //bug (google) #20
                var gt = new GrpTmp(group, group.Visibility, group.Viewers.ToList());
                if (!gtmps.Contains(gt))
                {
                    gtmps.Add(gt);
                    group.SetVisibility(false, false);
                }

                foreach (IMultiCard element in section.Cards)
                {
                    //DataNew.Entities.Card mod = Definition.GetCardById(element.Id);
                    for (int i = 0; i < element.Quantity; i++)
                    {
                        //for every card in the deck, generate a unique key for it, ID for it
                        var id   = element.GenerateCardId();
                        var card = new Card(player, id, new DataNew.Entities.Card(element), true, element.Size.Name);
                        //   var card = element.ToPlayCard(player);
                        ids[j]  = card.Id;
                        keys[j] = card.Type.Model.Id;
                        //keys[j] = card.GetEncryptedKey();
                        groups[j]  = group;
                        sizes[j]   = card.Size.Name;
                        cards[j++] = card;
                        group.AddAt(card, group.Count);

                        DeckStats.AddCard(card);
                    }

                    // Load images in the background
                    string pictureUri = element.GetPicture();
                    Dispatcher.CurrentDispatcher.BeginInvoke(
                        new Func <string, BitmapImage>(ImageUtils.CreateFrozenBitmap),
                        DispatcherPriority.Background, pictureUri);
                }
            }

            string sleeveString = null;

            if (deck.Sleeve != null)
            {
                try {
                    var loadSleeve = true;

                    if (!IsLocal)
                    {
                        var isSubscriber = SubscriptionModule.Get().IsSubscribed;

                        if (isSubscriber == null)
                        {
                            loadSleeve = false;

                            Log.Warn("Can't set deck sleeve, unable to determin if user is a subscriber.");

                            Program.GameMess.Warning($"Deck sleeve can not be loaded, subscriber status is unknown.");
                        }
                        else if (isSubscriber == false)
                        {
                            loadSleeve = false;

                            Log.Warn("Not authorized to use deck sleeve.");

                            Program.GameMess.Warning($"Deck sleeve can not be used, you're not a subscriber.");
                        }
                    }

                    if (loadSleeve)
                    {
                        Player.LocalPlayer.SetSleeve(deck.Sleeve);

                        sleeveString = Sleeve.ToString(deck.Sleeve);
                    }
                    else
                    {
                        Log.Info("Sleeve will not be loaded.");
                    }
                } catch (Exception ex) {
                    Log.Warn(ex.Message, ex);

                    Program.GameMess.Warning($"There was an error loading the decks sleeve.");
                }
            }

            Program.Client.Rpc.LoadDeck(ids, keys, groups, sizes, sleeveString ?? string.Empty, limited);
            //reset the visibility to what it was before pushing the deck to everybody. //bug (google) #20
            foreach (GrpTmp g in gtmps)
            {
                switch (g.Visibility)
                {
                case GroupVisibility.Everybody:
                    g.Group.SetVisibility(true, false);
                    break;

                case GroupVisibility.Nobody:
                    g.Group.SetVisibility(false, false);
                    break;

                default:
                    foreach (Player p in g.Viewers)
                    {
                        g.Group.AddViewer(p, false);
                    }
                    break;
                }
            }
            gtmps.Clear();
            gtmps.TrimExcess();
        }