/// <summary>
        /// Retrive match reward from Nakama server.
        /// </summary>
        public async Task <int> GetMatchRewardAsync(string matchId)
        {
            Client   client  = NakamaSessionManager.Instance.Client;
            ISession session = NakamaSessionManager.Instance.Session;

            // Maximum number of attempts to receive match result
            // Sometimes host tries to receive match message before it is fully stored on the server
            int maxRetries = 10;

            // The message containing last match id we send to server in order to receive required match info
            Dictionary <string, string> payload = new Dictionary <string, string> {
                { "match_id", matchId }
            };
            string payloadJson = JsonWriter.ToJson(payload);

            while (maxRetries-- > 0)
            {
                try
                {
                    // Calling an rpc method which returns our reward
                    IApiRpc response = await client.RpcAsync(session, "last_match_reward", payloadJson);

                    Dictionary <string, int> changeset = response.Payload.FromJson <Dictionary <string, int> >();
                    return(changeset["gold"]);
                }
                catch (Exception)
                {
                    Debug.Log("Couldn't retrieve match reward, retrying");
                    await Task.Delay(500);
                }
            }
            Debug.LogError("Couldn't retrieve match reward; network error");
            return(-1);
        }
Exemple #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);
        }
        /// <summary>
        /// Search for users matching name requirements
        /// </summary>
        /// <param name="text"></param>
        private async void SearchUsers(string text)
        {
            //dont search when text is empty
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            //client and session shortcuts
            Client   client  = NakamaSessionManager.Instance.Client;
            ISession session = NakamaSessionManager.Instance.Session;

            //nakama rpc method parameter
            string payload = text;
            //rpc method id from server
            string rpcid = "search_username";

            try
            {
                //creating search task - sending request for running "search_username" method on server with text parameter
                Task <IApiRpc> searchTask = client.RpcAsync(session, rpcid, payload);
                //adding this search task to list
                _actualTasks.Add(searchTask);
                //awaiting for server returning value
                IApiRpc searchResult = await searchTask;
                Debug.Log(searchResult.Payload);
                //unpacking results to SearchResult struct object
                SearchResult result = JsonUtility.FromJson <SearchResult>(searchResult.Payload);

                //checking if its last currently running search task
                if (_actualTasks.Count == 1)
                {
                    DeleteAllTips();
                    //creating tip for first 5 matched usernames
                    for (int i = 0; i < 5 && i < result.usernames.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(result.usernames[i]))
                        {
                            CreateTip(result.usernames[i]);
                        }
                    }
                }
                //removing task from actual tasks list
                _actualTasks.Remove(searchTask);
            }
            catch (Exception e)
            {
                Debug.LogError("Could not search users (" + e.Message + ")");
            }
        }
        /// <summary>
        /// Adds free gems to user's wallet.
        /// </summary>
        /// <remarks>
        /// This method is created for demo purpose only.
        /// </remarks>
        private async void HandleAddFreeGems()
        {
            try
            {
                IApiRpc newGems = await _connection.Client.RpcAsync(_connection.Session, "add_user_gems");

                _connection.Account = await _connection.Client.GetAccountAsync(_connection.Session);
            }
            catch (ApiResponseException e)
            {
                Debug.LogError("Error adding user gems: " + e.Message);
            }

            UpdateGemsUI();
        }
        /// <summary>
        /// Search for users matching name requirements
        /// </summary>
        /// <param name="text"></param>
        private async void SearchUsers(string text)
        {
            //dont search when text is empty
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            var searchRequest = new Dictionary <string, string>()
            {
                { "username", text }
            };

            //nakama rpc method parameter
            string payload = searchRequest.ToJson();
            //rpc method id from server
            string rpcid = "search_username";

            Task <IApiRpc> searchTask;

            try
            {
                //creating search task - sending request for running "search_username" method on server with text parameter
                searchTask = _connection.Client.RpcAsync(_connection.Session, rpcid, payload);
            }
            catch (Exception e)
            {
                Debug.LogError("Could not search users (" + e.Message + ")");
                return;
            }

            //awaiting for server returning value
            IApiRpc searchResult = await searchTask;

            //unpacking results to SearchResult struct object
            SearchResult[] usernames = searchResult.Payload.FromJson <SearchResult[]>();

            DeleteAllTips();
            //creating tip for first 5 matched usernames
            for (int i = 0; i < 5 && i < usernames.Length; i++)
            {
                if (!string.IsNullOrEmpty(usernames[i].username))
                {
                    CreateTip(usernames[i].username);
                }
            }
        }
Exemple #6
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);
        }
        /// <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 HandleClearCards()
        {
            try
            {
                IApiRpc response = await _connection.Client.RpcAsync(_connection.Session, "reset_card_collection");

                _cardCollection = response.Payload.FromJson <CardCollection>();
                _cardInfoSidePanel.SetCardCollection(_cardCollection);
                _cardInfoSidePanel.EnableUpgradeButton(HasGemsForUpgrade());
            }
            catch (ApiResponseException e)
            {
                Debug.LogWarning("Error clearing cards: " + e.Message);
                return;
            }

            _lastSelectedSlot = null;
            UpdateDeckCardsUI(_cardCollection.GetDeckList());
            UpdateStoredCardsUI(_cardCollection.GetStoredList());
        }
        public static async Task <ConfigurationOperationResponse> ReadAsync()
        {
            Client   client  = NakamaSessionManager.Instance.Client;
            ISession session = NakamaSessionManager.Instance.Session;

            var payload = "{\"PokemonName\": \"dragonite\"}";

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

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

            return(response);

            /*
             * var pokemonInfo = await Client.RpcAsync(Session, rpcid, payload);
             */

            //IApiRpc responsePayload = await client.RpcAsync(session, "rc", payload);
            //CardOperationResponse response = Nakama.TinyJson.JsonParser.FromJson<CardOperationResponse>(responsePayload.Payload);

            //Task<IApiRpc> responsePayload = client.RpcAsync(session, rpcid, payload);
            //CardOperationResponse response = Nakama.TinyJson.JsonParser.FromJson<CardOperationResponse>(responsePayload.Payload);

            /*
             *
             * 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, "merge_cards", payload);
             * CardOperationResponse response = Nakama.TinyJson.JsonParser.FromJson<CardOperationResponse>(responsePayload.Payload);
             *
             * return response;
             */
        }
        /// <summary>
        /// Add a random card to the owned card list.
        /// </summary>
        private async void HandleBuyRandomCard()
        {
            try
            {
                IApiRpc response = await _connection.Client.RpcAsync(_connection.Session, "add_random_card");

                var    card   = response.Payload.FromJson <Dictionary <string, CardData> >();
                string cardId = card.Keys.First();
                _cardCollection.AddStored(new Card(cardId, card[cardId]));
                _connection.Account = await _connection.Client.GetAccountAsync(_connection.Session);

                _lastSelectedSlot = null;
                UpdateGemsUI();
                UpdateDeckCardsUI(_cardCollection.GetDeckList());
                UpdateStoredCardsUI(_cardCollection.GetStoredList());
                _cardInfoSidePanel.EnableUpgradeButton(HasGemsForUpgrade());
            }
            catch (Exception e)
            {
                Debug.LogWarning("Couldn't handle buy random card: " + e.Message);
                return;
            }
        }
Exemple #10
0
    public async Task <string> RpcCall(string rpcName, string payload = null, bool unauthorized = false)
    {
        string rpcPayload;

        try
        {
            IApiRpc apiRpc = null;
            if (unauthorized)
            {
                apiRpc = await _client.RpcAsync(HttpKey, rpcName, payload);
            }
            else
            {
                apiRpc = await _client.RpcAsync(_session, rpcName, payload);
            }
            rpcPayload = apiRpc.Payload;
        }
        catch (Exception)
        {
            return(null);
        }

        return(rpcPayload);
    }