Jessecar96 / SteamBot

Automated bot software for interacting with Steam Trade
http://scrap.tf
MIT License
1.34k stars 910 forks source link

Craft all weapons into metal and metal up to refined #341

Closed ausey00 closed 11 years ago

ausey00 commented 11 years ago

Hi all,

I'm trying to set-up an automated trading bot that runs through its friends list and initiates trades with everyone sequentially.

I'm having trouble writing a function that crafts all of the bot's current TF2 inventory. A related issue thread covers crafting metal, but I'm unsure how how to write a function that implicitly crafts all weapons to metal, and then up to refined metal. Can anyone help me out?

Many Thanks, Austin

iMagooo commented 11 years ago

Without having done this, here is the steps I would take in order to achieve your goal:

Do some research on how weapons are represented in the item schema. Read the item schema, read the crafting code and the item code to determine what member functions/variables are available to you, deciding what functions/variables you need use in order to achieve your goal.

Use what you have found to create a loop that crafts 2 weapons while >=2 of them exist in the backpack.

Remember, this is not a programming help desk. This is not a bug with the bot, nor is it really something that should be asked in an issue thread.

ausey00 commented 11 years ago

Hi iMagoo,

This isn't my first go-to to solve this problem, i have been reading the code but really feel it's too much for me. Which is my reasoning behind the post.

I can only apologise for this being frowned upon but I have nowhere else to look. If anyone can help me get started with some example code I'd really appreciate it.

Austin

ausey00 commented 11 years ago

Thanks FunkyLoveCow, I did give that a read but a sizeable amount of it is above me. I'll get reading...

ausey00 commented 11 years ago

So I understand what waylaidwanderer's code is doing, but to scale it up and craft all weapons for a certain class to generate scrap, I'll need to just loop through all items in my inventory matching defindexes for items that pertain to a specific class, then break and craft them?

waylaidwanderer commented 11 years ago

Here's my code snippet for auto-crafting weapons together. Combine that with the function @FunkyLoveCow linked you, and you should be able to do what you want.
This function is untested though... it should work in theory, so if someone can look it over or test it and fix it that'd be cool!

// put this in Bot.cs
// To use, call it from the userhandler: `Bot.AutoCraftAllWeps();`
public void AutoCraftAllWeps()
{
    GetInventory();
    List<ulong> toCraft = new List<ulong>();
    string[] TF2Class = { "Scout", "Soldier", "Pyro", "Demoman", "Heavy", "Engineer", "Medic", "Sniper", "Spy" };
    int iterate = 0;
    bool canCraft = true;
    while (canCraft)
    {
        if (iterate > 9) break;
        string currentClass = TF2Class[iterate];
        foreach (var item in MyInventory.Items)
        {
            var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
            if (schemaItem.CraftClass == "weapon")
            {
                if (!item.IsNotTradeable && !item.IsNotCraftable && string.Join(",",schemaItem.UsableByClasses).Contains(currentClass) && !toCraft.Contains(item.Id))
                    toCraft.Add(item.Id);
                if (toCraft.Count == 2)
                    break;
            }
        }
        if (toCraft.Count == 2)
        {
            if (CurrentGame != 440)
            {
                SetGamePlaying(440);
                System.Threading.Thread.Sleep(1000);
            }
            TF2GC.Crafting.CraftItems(this, toCraft.ToArray());
            System.Threading.Thread.Sleep(200);
        }
        toCraft.Clear();
        iterate++;
    }
    SetGamePlaying(0);
}
iMagooo commented 11 years ago
public class Item
        {
            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("defindex")]
            public ushort Defindex { get; set; }

            [JsonProperty("item_class")]
            public string ItemClass { get; set; }

            [JsonProperty("item_type_name")]
            public string ItemTypeName { get; set; }

            [JsonProperty("item_name")]
            public string ItemName { get; set; }

            [JsonProperty("craft_material_type")]
            public string CraftMaterialType { get; set; }

            [JsonProperty("used_by_classes")]
            public string[] UsableByClasses { get; set; }

            [JsonProperty("item_slot")]
            public string ItemSlot { get; set; }

            [JsonProperty("craft_class")]
            public string CraftClass { get; set; }

            [JsonProperty("item_quality")]
            public int ItemQuality { get; set; }
        }

As you can see, the Item class has many member variables, you should be able to utilize these to reach your goal. http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=YOUR-STEAM-API

You should be able to use those member variables to find 2+ items of Item.CraftMaterialType.Contains("weapon"), Item.UsableByClasses.Contains("whatever class") and Item.ItemQuality==6 (unique).

Thus, all pairs of same class, unique weapons get smelted.

As I said, research here is key, I just worked that all out by looking at the code and schema.

Again, this is not a programming help desk, you need to learn this stuff for yourself.

ORRRRR just hand it to him on plate @waylaidwanderer, he doesn't need to learn anything -.-

waylaidwanderer commented 11 years ago

Hehe. I tend to learn code by looking at other people's code and examples, so I thought it might be helpful if he could see the code for himself. With your explanation it should be a lot clearer.

Also let me reiterate that the code is untested, so if it doesn't work, don't ask me to fix it. Figure out how to fix it yourself.

iMagooo commented 11 years ago

Yeah good point, hopefully he can read through the code and understand what's going on now.

Without testing it, this line:

if (schemaItem.CraftClass == "weapon")

should become:

if (schemaItem.CraftClass == "weapon" && schemaItem.ItemQuality == 6)

So that strange/other quality weapons are not crafted, just unique ones.

waylaidwanderer commented 11 years ago

@iMagooo good point. My bots usually only have unique weapons so it didn't cross my mind to add that condition check.

iMagooo commented 11 years ago

Which, IMO, proves the necessity of a newer bot owner to understand what is happening in the code, they could have implemented your code and lost a heap of valuable stranges if they were stupid enough :\

@ausey00 Be sure to read through the code, understand what it's doing, identify any issues in it and test it on a controlled backpack before you let it run wild.

ausey00 commented 11 years ago

Oh, thanks @waylaidwanderer, That looks like it'll do what I need it to.

Although reading that code, it does seem that it will just pair off two weapons for a class, craft them, then move to the next class. ignoring if there are more than 2 crafts that can be made (>3weapons) per class? Is that right?

And thanks @iMagooo , I have been reading a lot over the past few days, and yes, I understand how to evaluate the schema and my inventory. My previous knowledge of Csharp was anything that is common between it and C. Syntax for extracting data from and manipulating classes and objects is what is tripping me up at the moment.

I can use waylaidwanderer's code to hand hold me through some tutorials on the net.

Thanks again

iMagooo commented 11 years ago

Yup, I think you are correct. Instead of breaking the foreach loop if toCraft.count==2, just fill toCraft with all possible item IDs and then use a loop to craft pairs from toCraft.

ausey00 commented 11 years ago

That's good, I'll fix that and try to get it working. Thanks again iMagoo.

ausey00 commented 11 years ago

Last thing, then I'll close this issue. Why is there a "," concatenated with "UsableByClasses" when tested against the TF2Class[iterate]?

waylaidwanderer commented 11 years ago

@ausey00 I just wanted to turn the array of classes into a string so that I could use the Contains function, so that's the fastest way I knew of.

ausey00 commented 11 years ago

Ah, makes sense now. Cheers!