示例#1
0
        public static Boolean SetAutoBuy(ItemInSQL itemInSql, string apiKey, SteamTrade.SteamWeb steamWeb)
        {
            if (itemInSql.CanBuy == 1)
            {
                String deserializedResponse;
                string lang = "en"; //needed for some requests
                //TODO I'm inserting a new AutoBuyPrice, but can't change.
                //calculating new price for ItemInSQL
                int currentMaxAutoBuyPrice = GetMaxAutoBuyPriceForItem(itemInSql, lang, apiKey, steamWeb); //100
                int newPrice;
                if (currentMaxAutoBuyPrice < itemInSql.MinPriceBuy)
                {
                    newPrice = itemInSql.MinPriceBuy;
                }
                else if (currentMaxAutoBuyPrice < itemInSql.MaxPriceBuy)
                {
                    newPrice = currentMaxAutoBuyPrice + 1;
                }
                else
                {
                    newPrice = itemInSql.MaxPriceBuy;
                }
                //trying to update price for ItemInSQL
                string updateLink = string.Format("https://csgo.tm/api/UpdateOrder/{0}/{1}/{2}/?key={3}",
                    itemInSql.GetItemClass(), itemInSql.GetItemInstance(), newPrice, apiKey);

                string updateJsonResponse = steamWeb.Fetch(updateLink, "GET", null, false, null);
                deserializedResponse = JsonConvert.DeserializeObject(updateJsonResponse).ToString();
                //if couldn't find the order in base - create a new one.
                if (deserializedResponse.ToString().Contains("Данная заявка на покупку не найдена"))
                {
                    string insertLink = string.Format("https://csgo.tm/api/InsertOrder/{0}/{1}/{2}/{3}/?key={4}",
                        itemInSql.GetItemClass(), itemInSql.GetItemInstance(), newPrice, itemInSql.Hash, apiKey);
                    string insertJsonResponse = steamWeb.Fetch(insertLink, "GET", null, false, null);
                    deserializedResponse = JsonConvert.DeserializeObject(insertJsonResponse).ToString();
                    if (deserializedResponse.Contains("true"))
                    {
                        Console.WriteLine("A new autobuy order for ItemInSQL " + itemInSql.ItemName +
                                          " was created successfully!" +
                                          " Price: " + (double) newPrice/100 + " руб.");
                        return true;
                    }
                    else if (deserializedResponse.Contains("Неверно задана цена покупки"))
                    {
                        Console.WriteLine("Wrong price for " +itemInSql.ItemName + "! Check the prices for buying in database!");
                        return false;
                    }
                    Console.WriteLine("For item: " + itemInSql.ItemName);
                    Console.WriteLine(deserializedResponse);
                }
                else if (deserializedResponse.ToString().Contains("недостаточно средств на счету"))
                {
                    Console.WriteLine("Autobuy false. For " + itemInSql.ItemName +
                                      " - not enough funds in wallet! Пополните кошелек!");
                    return false;
                }
                else if (deserializedResponse.ToString().Contains("true"))
                {
                    Console.WriteLine("Autobuy price for " + itemInSql.ItemName + " was updated successfully! New price: " +
                                      (double) newPrice/100 + " руб.");
                    return true;
                }
                //TODO
                Console.WriteLine("We should never reach here." +
                                  " A new error ");
            }
            return false;
        }