示例#1
0
        //private async void GetMarketValueAsync(string url, InventoryItem item)
        //{
        //    using (var client = new HttpClient())
        //    {
        //        string responseText;
        //        try
        //        {
        //            responseText = await client.GetStringAsync(url);
        //            MarketResponse marketObj = JsonConvert.DeserializeObject<MarketResponse>(responseText);
        //            ParseMarketResults(marketObj, item);
        //        }
        //        catch(Exception e)
        //        {
        //            if (e.Message.Contains("503"))
        //            {
        //            }
        //        }
        //    }
        //}

        private void ParseMarketResults(MarketResponse marketObj, InventoryItem item)
        {
            int total       = 0;
            int count       = 0;
            int onlineCount = 0;
            int onlineTotal = 0;
            int?min         = null;
            int?max         = null;

            foreach (Buy b in marketObj.response.buy)
            {
                count++;
                total += b.price;
                if ((min == null || b.price < min) && b.price > 0)
                {
                    min = b.price;
                }
                if ((max == null || b.price > max) && b.price > 0)
                {
                    max = b.price;
                }
                if (b.online_ingame || b.online_status)
                {
                    onlineCount++;
                    onlineTotal += b.price;
                }
            }

            item.avg_val    = (count > 0) ? total / count : 0;
            item.onlineAvg  = (onlineCount > 0) ? onlineTotal / onlineCount : 0;
            item.sale_count = count;
            item.min_val    = min.GetValueOrDefault();
            item.max_val    = max.GetValueOrDefault();
        }
示例#2
0
        private void GetMarketValue(string url, InventoryItem item)
        {
            WebRequest req = WebRequest.Create(url);

            req.ContentType = "application/json; charset=utf-8";
            WebResponse res = req.GetResponse();

            string jsonObj;

            using (Stream responseStream = res.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                jsonObj = reader.ReadToEnd();
            }
            MarketResponse marketObj = JsonConvert.DeserializeObject <MarketResponse>(jsonObj);

            ParseMarketResults(marketObj, item);
        }