Jessecar96 / SteamBot

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

How to create a trade offer #677

Closed hyt47 closed 9 years ago

hyt47 commented 9 years ago

TradeOfferUserHandler.cs

How to add an item using Dedindex and UniqueID

Dedindex = 5002 UniqueID= 5466784

offer.Items.AddMyItem(440, 0, 0); // AddMyItem(int appId, long contextId, long assetId, long amount = 1) appId = 440 = "Team Fortress 2"

contextId = ?? Defindex Maby ---- Is this correct ??? assetId = ?? UniqueID Maby ---- Is this correct ???

My Code:

    public override void OnMessage(string message, EChatEntryType type)
    {
        // Write message in program Console
        /////////////////////////////////////////////////////////////////
        Console.ForegroundColor = ConsoleColor.Gray;
        Console.WriteLine("chat msg " + OtherSID + ": " + message);
        Console.ResetColor();

        message = message.ToLower();

        //if (IsAdmin)
       // {
            //creating a new trade offer
            var offer = Bot.NewTradeOffer(OtherSID);

    // appId:
// 753 = "Steam"
// 440 = "Team Fortress 2"
// 620 = "Portal 2"
// 570 = "Dota 2"
// 238460 = "BattleBlock Theater"

            // AddMyItem(int appId, long contextId, long assetId, long amount = 1)
            //offer.Items.AddMyItem(0, 0, 0);
            offer.Items.AddMyItem(440, 5002, 0);
            if (offer.Items.NewVersion)
            {
                string newOfferId;
                if (offer.Send(out newOfferId))
                {
                    Log.Success("Trade offer sent : Offer ID " + newOfferId);
                    Console.WriteLine("Trade offer sent : Offer ID " + newOfferId);
                }
            }

            //creating a new trade offer with token
            var offerWithToken = Bot.NewTradeOffer(OtherSID);

            //offer.Items.AddMyItem(0, 0, 0);
            if (offerWithToken.Items.NewVersion)
            {
                string newOfferId;
                // "token" should be replaced with the actual token from the other user
                if (offerWithToken.SendWithToken(out newOfferId, "token"))
                {
                    Log.Success("Trade offer sent : Offer ID " + newOfferId);
                    Console.WriteLine("Trade offer sent : Offer ID " + newOfferId);
                }
            }
        //}
    }
scholtzm commented 9 years ago

Not an issue.

427 & #527

hyt47 commented 9 years ago

To Test variables i used: TradeOfferUserHandler.cs modified this script will create a trade offer adding all your refined metal if u are the Admin and it will write in console "contextId" and "assetId"

contextId = Item position in Backpack ? ---- Is this correct ??? assetId = UniqueID

Script:

using SteamKit2; using SteamTrade; using SteamTrade.TradeOffer; using System; using System.Collections.Generic; using TradeAsset = SteamTrade.TradeOffer.TradeOffer.TradeStatusUser.TradeAsset;

namespace SteamBot { public class TradeOfferUserHandler : UserHandler { public TradeOfferUserHandler(Bot bot, SteamID sid) : base(bot, sid) { }

    public override void OnNewTradeOffer(TradeOffer offer)
    {
        //receiving a trade offer 
        if (IsAdmin)
        {
            //parse inventories of bot and other partner
            //either with webapi or generic inventory
            //Bot.GetInventory();
            //Bot.GetOtherInventory(OtherSID);

            var myItems = offer.Items.GetMyItems();
            var theirItems = offer.Items.GetTheirItems();
            Log.Info("They want " + myItems.Count + " of my items.");
            Log.Info("And I will get " +  theirItems.Count + " of their items.");

            //do validation logic etc
            if (DummyValidation(myItems, theirItems))
            {
                string tradeid;
                if (offer.Accept(out tradeid))
                {
                    Log.Success("Accepted trade offer successfully : Trade ID: " + tradeid);
                }
            }
            else
            {
                // maybe we want different items or something

                //offer.Items.AddMyItem(0, 0, 0);
                //offer.Items.RemoveTheirItem(0, 0, 0);
                if (offer.Items.NewVersion)
                {
                    string newOfferId;
                    if (offer.CounterOffer(out newOfferId))
                    {
                        Log.Success("Counter offered successfully : New Offer ID: " + newOfferId);
                    }
                }
            }
        }
        else
        {
            //we don't know this user so we can decline
            if (offer.Decline())
            {
                Log.Info("Declined trade offer : " + offer.TradeOfferId + " from untrusted user " + OtherSID.ConvertToUInt64());
            }
        }
    }

    public override void OnMessage(string message, EChatEntryType type)
    {
        // Write message in program Console
        //
        Console.ForegroundColor = ConsoleColor.Gray;
        Console.WriteLine("chat msg " + OtherSID + ": " + message);
        Console.ResetColor();

        message = message.ToLower();

        if (IsAdmin)
        {
            //creating a new trade offer
            var offer = Bot.NewTradeOffer(OtherSID);

// appId:
// 753 = "Steam"
// 440 = "Team Fortress 2"
// 620 = "Portal 2"
// 570 = "Dota 2"
// 238460 = "BattleBlock Theater"

            // AddMyItem(int appId, long contextId, long assetId, long amount = 1)
            //offer.Items.AddMyItem(0, 0, 0);

            Bot.GetInventory();
            foreach (var item in Bot.MyInventory.Items)
            {

                if (item.Defindex == 5002)
                {
                    offer.Items.AddMyItem(440, item.ContextId, (long)item.Id);
                    Console.WriteLine("ContextId = " + item.ContextId);
                    Console.WriteLine("assetId  = " + (long)item.Id);

                }
            }

            if (offer.Items.NewVersion)
            {
                string newOfferId;
                if (offer.Send(out newOfferId))
                {
                    Log.Success("Trade offer sent : Offer ID " + newOfferId);
                    Console.WriteLine("Trade offer sent : Offer ID " + newOfferId);
                }
            }

            //creating a new trade offer with token
            var offerWithToken = Bot.NewTradeOffer(OtherSID);

            //offer.Items.AddMyItem(0, 0, 0);
            if (offerWithToken.Items.NewVersion)
            {
                string newOfferId;
                // "token" should be replaced with the actual token from the other user
                if (offerWithToken.SendWithToken(out newOfferId, "token"))
                {
                    Log.Success("Trade offer sent : Offer ID " + newOfferId);
                    Console.WriteLine("Trade offer sent : Offer ID " + newOfferId);
                }
            }
        }
    }

    public override bool OnGroupAdd() { return false; }

    public override bool OnFriendAdd() { return IsAdmin; }

    public override void OnFriendRemove() { }

    public override void OnLoginCompleted() { }

    public override bool OnTradeRequest() { return false; }

    public override void OnTradeError(string error) { }

    public override void OnTradeTimeout() { }

    public override void OnTradeSuccess() { }

    public override void OnTradeInit() { }

    public override void OnTradeAddItem(Schema.Item schemaItem, Inventory.Item inventoryItem) { }

    public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem) { }

    public override void OnTradeMessage(string message) { }

    public override void OnTradeReady(bool ready) { }

    public override void OnTradeAccept() { }

    private bool DummyValidation(List<TradeAsset> myAssets, List<TradeAsset> theirAssets)
    {
        //compare items etc
        //if (myAssets.Count == theirAssets.Count)
        //{
            return true;
        //}
        //return false;
    }
}

}

hyt47 commented 9 years ago

it works :)

scholtzm commented 9 years ago

Ugh. Did you even read #427 and #527?