public static BackpackTF FetchSchema() { var url = "http://backpack.tf/api/IGetPrices/v2/?format=json¤cy=metal"; string cachefile = "tf_pricelist.cache"; string result = ""; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); } catch { response = null; } DateTime SchemaLastRequested = response.LastModified; TimeSpan difference = DateTime.Now - System.IO.File.GetCreationTime(cachefile); if (!System.IO.File.Exists(cachefile) || ((difference.TotalMinutes > 5) && response != null)) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { result = sr.ReadToEnd(); //Close and clean up the StreamReader sr.Close(); } File.WriteAllText(cachefile, result); System.IO.File.SetCreationTime(cachefile, SchemaLastRequested); } else { TextReader reader = new StreamReader(cachefile); result = reader.ReadToEnd(); reader.Close(); } response.Close(); BackpackTF schemaResult = JsonConvert.DeserializeObject <BackpackTF>(result); UpdateBasePrices(schemaResult); return(schemaResult ?? null); }
static void UpdateBasePrices(BackpackTF schemaResult) { KeyPrice = schemaResult.Response.Prices[5021][6][0].Value; BillPrice = schemaResult.Response.Prices[126][6][0].Value; BudPrice = schemaResult.Response.Prices[143][6][0].Value; }
void LoadBP() { ListBackpack.Clear(); bot.GetOtherInventory(SID); Inventory.Item[] inventory = bot.OtherInventory.Items; if (inventory == null) { bot.main.Invoke((Action)(() => { list_inventory.EmptyListMsg = "Could not retrieve backpack contents. Backpack is likely private."; BrightIdeasSoftware.TextOverlay textOverlay = this.list_inventory.EmptyListMsgOverlay as BrightIdeasSoftware.TextOverlay; })); return; } bot.main.Invoke((Action)(() => { list_inventory.View = View.Tile; list_inventory.TileSize = new Size(250, 64); ListView_SetSpacing(list_inventory, 70, 10); })); BackpackTF.CurrentSchema = BackpackTF.FetchSchema(); foreach (Inventory.Item item in inventory) { bool isGift = false; bool isUnusual = false; var currentItem = Trade.CurrentSchema.GetItem(item.Defindex); try { for (int count = 0; count < item.Attributes.Length; count++) { if (item.Attributes[count].Defindex == 229) { Console.WriteLine("Item: " + currentItem.ItemName); Console.WriteLine(item.Attributes[count].FloatValue); Console.WriteLine(item.Attributes[count].Value); } } } catch { } string name = ""; string price = null; var type = Convert.ToInt32(item.Quality.ToString()); if (QualityToName(type) != "Unique") { name += QualityToName(type) + " "; } name += currentItem.ItemName; if (QualityToName(type) == "Unusual") { isUnusual = true; try { for (int count = 0; count < item.Attributes.Length; count++) { if (item.Attributes[count].Defindex == 134) { name += " (Effect: " + EffectToName(item.Attributes[count].FloatValue) + ")"; price = Util.GetPrice(item.Defindex, type, item, false, (int)item.Attributes[count].FloatValue); } } } catch { } } if (currentItem.CraftMaterialType == "supply_crate") { for (int count = 0; count < item.Attributes.Length; count++) { name += " #" + (item.Attributes[count].FloatValue); } } name += " (Level " + item.Level + ")"; try { int size = item.Attributes.Length; for (int count = 0; count < size; count++) { if (item.Attributes[count].Defindex == 261) { string paint = PaintToName(item.Attributes[count].FloatValue); name += " (Painted: " + paint + ")"; } if (item.Attributes[count].Defindex == 186) { isGift = true; name += " (Gifted)"; } } } catch { // Item has no attributes... or something. } if (currentItem.Name == "Wrapped Gift") { isGift = true; // Untested! try { var containedItem = Trade.CurrentSchema.GetItem(item.ContainedItem.Defindex); var containedName = GetItemName(containedItem, item.ContainedItem); price = Util.GetPrice(item.ContainedItem.Defindex, Convert.ToInt32(item.ContainedItem.Quality.ToString()), item, true); name += " (Contains: " + containedName + ")"; } catch (Exception ex) { Bot.Print(ex); // Guess this doesn't work :P. } } if (item.IsNotCraftable) { name += " (Uncraftable)"; } if (item.IsNotTradeable) { name += " (Untradeable)"; } if (!isGift && !isUnusual) { price = Util.GetPrice(currentItem.Defindex, type, item); ListBackpack.Add(name, item.Defindex, currentItem.ImageURL, price); } else { ListBackpack.Add(name, item.Defindex, currentItem.ImageURL, price); } list_inventory.SetObjects(ListBackpack.Get()); } }