Пример #1
0
        /// <summary>
        /// Replaces a card from the list of unused cards with a card from the deck.
        /// Sends the request to the server.
        /// </summary>
        private async Task <bool> ReplaceCardAsync(Card removedCard, Card usedCard)
        {
            // Perform card swap on server
            CardOperationResponse canReplace = await DeckBuildingManager.SwapAsync(removedCard, usedCard);

            if (canReplace.response == false)
            {
                Debug.LogWarning("Couldn't swap cards: " + canReplace.message);
                return(false);
            }

            // Get indices of supplied cards
            int usedIndex    = _deck.unusedCards.IndexOf(usedCard);
            int removedIndex = _deck.usedCards.IndexOf(removedCard);

            // Replace cards
            _deck.usedCards.RemoveAt(removedIndex);
            _deck.unusedCards.RemoveAt(usedIndex);
            _deck.usedCards.Insert(removedIndex, usedCard);
            _deck.unusedCards.Insert(usedIndex, removedCard);

            // Store changes locally
            usedCard.isUsed    = true;
            removedCard.isUsed = false;

            // Update UI
            RefreshUsedCards(_deck.usedCards);
            RefreshUnusedCards(_deck.unusedCards);

            // Change selected card's background color
            CardSlotUI slot = _deckCardsDisplays.Find(x => x.Card == usedCard);

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Debug method which forces server to remove cards owned by the user.
        /// </summary>
        public static async Task <CardOperationResponse> DebugClearDeckAsync()
        {
            Client   client  = NakamaSessionManager.Instance.Client;
            ISession session = NakamaSessionManager.Instance.Session;

            IApiRpc responsePayload = await client.RpcAsync(session, "debug_clear_deck");

            CardOperationResponse response = Nakama.TinyJson.JsonParser.FromJson <CardOperationResponse>(responsePayload.Payload);

            return(response);
        }
Пример #3
0
        /// <summary>
        /// Adds free gems to user's wallet.
        /// </summary>
        /// <remarks>
        /// This method is created for demo purpose only.
        /// </remarks>
        private async void AddFreeGems()
        {
            CardOperationResponse response = await DeckBuildingManager.DebugAddGemsAsync();

            if (response.response == false)
            {
                Debug.Log("Couldn't add gems: " + response.message);
                return;
            }

            await UpdateFundsCounterAsync();
        }
Пример #4
0
        /// <summary>
        /// Upgrades a card to the next level and removes the second.
        /// </summary>
        private async void OnMerge(Card upgraded, Card removed)
        {
            // Perform card upgrade on server
            CardOperationResponse canMerge = await DeckBuildingManager.MergeAsync(upgraded, removed);

            if (canMerge.response == false)
            {
                Debug.LogWarning("Couldn't merge cards: " + canMerge.message);
                return;
            }

            _deck.unusedCards.Remove(removed);
            // Create a new card based on the upgraded card
            Card card = new Card();

            card.cardType = upgraded.cardType;
            card.level    = upgraded.level + 1;
            card.isUsed   = upgraded.isUsed;

            // Replace the upgraded card with newly created copy
            if (upgraded.isUsed == true)
            {
                int index = _deck.usedCards.IndexOf(upgraded);
                _deck.usedCards.RemoveAt(index);
                _deck.usedCards.Insert(index, card);
            }
            else
            {
                _deck.unusedCards.Remove(upgraded);
                _deck.unusedCards.Add(card);
            }

            // Update UI
            await UpdateFundsCounterAsync();

            RefreshUsedCards(_deck.usedCards);
            RefreshUnusedCards(_deck.unusedCards);

            CardSlotUI slot = null;

            if (card.isUsed == true)
            {
                slot = _deckCardsDisplays.Find(x => x.Card.IsCopy(card));
            }
            else
            {
                slot = _ownedCardsDisplays.Find(x => x.Card.IsCopy(card));
            }
            OnCardSelected(slot);
        }
Пример #5
0
        /// <summary>
        /// Checks on the server whether user is allowed to replace the two supplied cards,
        /// then performs the swap.
        /// </summary>
        public static async Task <CardOperationResponse> SwapAsync(Card card1, Card card2)
        {
            Client   client  = NakamaSessionManager.Instance.Client;
            ISession session = NakamaSessionManager.Instance.Session;

            Dictionary <string, Dictionary <string, string> > cardPayload = new Dictionary <string, Dictionary <string, string> >
            {
                { "first", card1.Serialize() },
                { "second", card2.Serialize() }
            };
            string payload = Nakama.TinyJson.JsonWriter.ToJson(cardPayload);

            IApiRpc responsePayload = await client.RpcAsync(session, "swap_cards", payload);

            CardOperationResponse response = Nakama.TinyJson.JsonParser.FromJson <CardOperationResponse>(responsePayload.Payload);

            return(response);
        }
Пример #6
0
        /// <summary>
        /// Debug method used to add a random card to the owned card list.
        /// </summary>
        private async void GetRandomCard()
        {
            CardOperationResponse response = await DeckBuildingManager.DebugAddRandomCardAsync();

            if (response.response == false)
            {
                Debug.Log("Couldn't receive random card: " + response.message);
                return;
            }

            Deck deck = await _deckStorage.LoadDataAsync("deck");

            if (deck != null)
            {
                await UpdateFundsCounterAsync();

                _deck.usedCards   = deck.usedCards;
                _deck.unusedCards = deck.unusedCards;
                RefreshUsedCards(_deck.usedCards);
                RefreshUnusedCards(_deck.unusedCards);
            }
        }
Пример #7
0
        /// <summary>
        /// Generates a new deck based on <see cref="_defaultDeck"/>.
        /// Sends the deck to Nakama server and then stores it locally.
        /// </summary>
        private async void ClearCards()
        {
            CardOperationResponse response = await DeckBuildingManager.DebugClearDeckAsync();

            if (response.response == false)
            {
                Debug.Log("Couldn't clear deck: " + response.message);
                return;
            }

            Deck deck = GenerateDefaultDeck(_defaultDeck);

            deck.deckName = _deck.deckName;

            bool good = await _deckStorage.StoreDataAsync(deck);

            if (good == true)
            {
                _deck.usedCards   = deck.usedCards;
                _deck.unusedCards = deck.unusedCards;
                RefreshUsedCards(_deck.usedCards);
                RefreshUnusedCards(_deck.unusedCards);
            }
        }