/// <summary>
 /// Returns amount of all items of the specified type belonging to a particular player with given type
 /// </summary>
 /// <param name="profile"></param>
 /// <param name="gameID"></param>
 /// <param name="itemType">Item type</param>
 /// <returns></returns>
 public async Task <ulong> GetPlayerItemsAmount(Profile profile, GameID gameID, string itemType)
 {
     if (Providers.ContainsKey(gameID))
     {
         IGameItemProvider c = Providers[gameID];
         return(await c.GetPlayerItemsAmount(profile, itemType).ConfigureAwait(false));
     }
     else
     {
         ErrorCallbackProvider.ReportWarning($"Game [{gameID.Name}] could not be found. Have you registered it properly?");
     }
     return(await Task.FromResult <ulong>(0));
 }
        private async Task <SubscriptionResponse> ReceiveSubscriptionResponse(CancellationToken token)
        {
            if (IsConnected == false)
            {
                ErrorCallbackProvider.ReportError("Whisper connection error!");
                throw new WebException("Whisper connection error!");
            }

            while (true)
            {
                byte[] msgBytes = await WebSocketProvider.Receive(token);

                //we are skipping all messages that are not subscriptions
                if (msgBytes != null)
                {
                    string jsonMsg = Encoding.UTF8.GetString(msgBytes);
                    ErrorCallbackProvider.ReportInfo(jsonMsg);

                    JToken  message = null;
                    JObject json    = JObject.Parse(jsonMsg);
                    json.TryGetValue("error", out message);
                    if (message != null)
                    {
                        Error = message.ToString();
                        ErrorCallbackProvider.ReportError("Received error from Whisper service: " + Error);
                    }
                    else
                    {
                        json.TryGetValue("method", out JToken method);
                        if ((method != null) && (method.ToString() == "shh_subscription"))
                        {
                            JToken prms = null;
                            json.TryGetValue("params", out prms);
                            if (prms != null)
                            {
                                JObject jsonParams = prms.ToObject <JObject>();
                                jsonParams.TryGetValue("result", out message);
                                if (message != null)
                                {
                                    return(JsonConvert.DeserializeObject <SubscriptionResponse>(message.ToString()));
                                }
                            }
                        }
                        else
                        {
                            ErrorCallbackProvider.ReportWarning("Expected notification but got: " + jsonMsg);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Returns all Game Items owned by player's subaccount in particular game
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="gameID"></param>
        /// <param name="page">Page number</param>
        /// <param name="itemsPerPage">Number of items per page</param>
        /// <param name="itemType">Item type</param>
        /// <returns></returns>
        public async Task <GameItem[]> GetPlayerItems(Profile profile, GameID gameID, string itemType, ulong page, ulong itemsPerPage)
        {
            List <GameItem> items = new List <GameItem>();

            if (Providers.ContainsKey(gameID))
            {
                IGameItemProvider c = Providers[gameID];
                items.AddRange(await c.GetPlayerItems(profile, itemType, page, itemsPerPage).ConfigureAwait(false));
            }
            else
            {
                ErrorCallbackProvider.ReportWarning($"Game [{gameID.Name}] could not be found. Have you registered it properly?");
            }
            return(items.ToArray());
        }
        private async Task <T> ReceiveResponse <T>(int reqId, T defaultValue, CancellationToken ctoken)
        {
            if (IsConnected == false)
            {
                ErrorCallbackProvider.ReportError("Whisper connection error!");
                throw new WebException("Whisper connection error!");
            }

            //wait for requested response (or timeout)
            while (true)
            {
                byte[] msgBytes = await WebSocketProvider.Receive(ctoken);

                if (msgBytes != null)
                {
                    string jsonMsg = Encoding.UTF8.GetString(msgBytes);
                    ErrorCallbackProvider.ReportInfo(jsonMsg);

                    JObject json = JObject.Parse(jsonMsg);
                    json.TryGetValue("error", out JToken error);
                    if (error != null)
                    {
                        Error = error.ToString();
                        ErrorCallbackProvider.ReportError("Received error from Whisper service: " + Error);
                        return(defaultValue);
                    }
                    else
                    {
                        json.TryGetValue("result", out JToken result);
                        if (result != null)
                        {
                            json.TryGetValue("id", out JToken respId);
                            if (respId.Value <int>() == reqId)
                            {
                                if (result.Type == JTokenType.Array || result.Type == JTokenType.Object)
                                {
                                    return(JsonConvert.DeserializeObject <T>(result.ToString()));
                                }
                                return(result.Value <T>());
                            }
                            else
                            {
                                ErrorCallbackProvider.ReportError("Waiting for response with ID: " + reqId + " but got " + respId.Value <int>());
                            }
                        }
                        else
                        {
                            json.TryGetValue("method", out JToken method);
                            if ((method != null) && (method.ToString() == "shh_subscription"))
                            {
                                json.TryGetValue("params", out JToken prms);
                                if (prms != null)
                                {
                                    JObject jsonParams = prms.ToObject <JObject>();
                                    jsonParams.TryGetValue("result", out JToken message);
                                    if (message != null)
                                    {
                                        OnSubscriptionMessage?.Invoke(JsonConvert.DeserializeObject <SubscriptionResponse>(message.ToString()));
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    ErrorCallbackProvider.ReportWarning("Conection was closed!");
                    return(defaultValue);
                }
            }
        }