Jump to content

Trading API unpredictable


ry4n

Recommended Posts

Could anyone please explain to me why the following code is so incredibly un-predictable? 

        if (m.getTrade().isCurrentlyTrading()) {
            m.log("Currently in trade with: " + m.getTrade().getOtherPlayer());

            if (m.getTrade().getTheirOffers() != null && !m.getTrade().getTheirOffers().isEmpty()) {
                m.getTrade().acceptTrade();
                return Utils.randNum(1000, 2000);
            }

            if (m.getTrade().isSecondInterfaceOpen()) {
                m.getTrade().acceptTrade();
                return Utils.randNum(1000, 2000);
            }

            return Utils.randNum(250, 500);

        }

        if (!m.getTrade().isCurrentlyTrading()) {
            if (m.getTrade().getLastRequestingPlayer() != null && m.getTrade().getLastRequestingPlayer().exists()) {
                m.log("Interacting with: " + m.getTrade().getLastRequestingPlayer().getName());
                if (m.getTrade().getLastRequestingPlayer().interact("Trade with")) {
                    Sleep.sleepUntil(() -> m.getTrade().isCurrentlyTrading(), 5000, 500);
                    return Utils.randNum(1000, 2000);
                }
            } else {
                delay = Utils.randNum(1000, 2000);
                m.log("Not currently in trade, checking again in: " + delay + "ms");
                return delay;
            }
        }

The m in this case is the MethodProvider. 

All i'm trying to do is run an account that sits there & just accepts trade requests, however it is incredibly un-reliable & un-predictable. Sometimes it works & accepts the trade, other times it will just close the trade interface or will spam trade the requesting player. 

As a side note, is there any difference in using trade or getTrade() 

Thanks! :)

Link to comment
Share on other sites

10 minutes ago, ry4n said:

Could anyone please explain to me why the following code is so incredibly un-predictable? 


        if (m.getTrade().isCurrentlyTrading()) {
            m.log("Currently in trade with: " + m.getTrade().getOtherPlayer());
            if (m.getTrade().getTheirOffers() != null && !m.getTrade().getTheirOffers().isEmpty()) {
                if (m.getTrade().acceptTrade()) {
                	return Utils.randNum(1000, 2000); // conditional sleep
                } else {
                	// handle failure
                }
            } else if (m.getTrade().isSecondInterfaceOpen()) {
                if (m.getTrade().acceptTrade()) {
                	return Utils.randNum(1000, 2000); // conditional sleep
                } else {
                	// handle failure
                }
            } else {
                // handle invalid case, conditional sleep/throw exception/stop script
            }
        } else {
            if (m.getTrade().getLastRequestingPlayer() != null && m.getTrade().getLastRequestingPlayer().exists()) {
                m.log("Interacting with: " + m.getTrade().getLastRequestingPlayer().getName());
                if (m.getTrade().getLastRequestingPlayer().interact("Trade with")) {
                    Sleep.sleepUntil(() -> m.getTrade().isCurrentlyTrading(), 5000, 500);
                } else { 
                	// handle failure
                }
            } else {
            	// conditional sleep
            }
        }
		return Utils.randNum(1000, 2000);

The m in this case is the MethodProvider. 

All i'm trying to do is run an account that sits there & just accepts trade requests, however it is incredibly un-reliable & un-predictable. Sometimes it works & accepts the trade, other times it will just close the trade interface or will spam trade the requesting player. 

As a side note, is there any difference in using trade or getTrade() 

Thanks! :)

Can’t go into much detail as I edited that on my phone. There is no difference between the two, one is the field and the other is the getter for the same field.

  • Like 1
Link to comment
Share on other sites

6 minutes ago, Token said:

Can’t go into much detail as I edited that on my phone. There is no difference between the two, one is the field and the other is the getter for the same field.

Thanks! Will give that a go now

@Token Quick question, would there be any reason conditional sleeps are being ignored if they are in a separate class & then called within a method of that class in onLoop

 

Link to comment
Share on other sites

48 minutes ago, Czar said:

Trade is a bit weird it works 50/50 for some reason it’s not replicated easily enough to post a solid bug report for, but it should be fixed if there was a way to reproduce the bug

Ah right that makes so much more sense, it was driving me crazy haha, I was running the exact same script in under the exact same conditions and getting differing results 😅

 

Link to comment
Share on other sites

  • 2 weeks later...

I wrote up some code real quick that should give you a basic trade system to where the bot will accept a incoming trade from a player and will follow through with the trade. Haven't tested it much but it should work. It wont stick items in the trade menu though, but I could add that if you need it. It uses the onMessage() method. Hope this helps you out! 😁

First, the main class.

Spoiler

import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.ui.Message;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

@ScriptManifest(name = "Trader", author = "BravoTaco", version = 1.0, info = "", logo = "")
public class Trader extends Script {

    Player tradingPlayer;

    TradeHandler tradeHandler;

    @Override
    public void onStart() {
        tradeHandler = new TradeHandler(this);
    }


    @Override
    public void onExit() {

    }

    @Override
    public int onLoop() {

        if (tradeHandler.canTrade(tradingPlayer)) {
            if (tradeHandler.tradeWithPlayer(tradingPlayer)) {
                if (tradeHandler.inFirstTradeScreen()) {
                    if (tradeHandler.clickAcceptOnFirstScreen()) {
                        if (tradeHandler.waitForSecondScreenOrCancel()) {
                            if (tradeHandler.inSecondTradeScreen()) {
                                if (tradeHandler.clickAcceptOnSecondScreen()) {
                                    log("Trade Completed!");
                                }
                            }
                        }
                    }
                }
            }
        }

        tradingPlayer = null;

        return random(1300, 2300);

    }

    @Override
    public void onPaint(Graphics2D g) {

    }

    @Override
    public void onMessage(Message e) throws InterruptedException {
        if (e.getMessage().contains("wishes to trade with you")) {
            String message = e.getMessage();
            String name = message.substring(0, message.indexOf(' '));
            log(name);
            if (tradingPlayer == null) {
                tradingPlayer = players.closest(name);
            }
        }
    }

}

 

Now the TradeHandler Class.

Spoiler

import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.utility.ConditionalSleep;

public class TradeHandler {

    private Script script;
    private RS2Widget firstAcceptButton;
    private RS2Widget secondAcceptButton;

    public TradeHandler(Script script) {
        this.script = script;
    }

    public boolean inFirstTradeScreen() {
        return script.getWidgets().getWidgetContainingText("Accept") != null;
    }

    public boolean clickAcceptOnFirstScreen() {
        if (script.getWidgets().getWidgetContainingText("Accept") != null) {
            firstAcceptButton = script.getWidgets().getWidgetContainingText("Accept");
            if (firstAcceptButton.interact("Accept")) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    public boolean inSecondTradeScreen() {
        return script.getWidgets().getWidgetContainingText("Are you sure you want to make this trade?") != null;
    }

    public boolean clickAcceptOnSecondScreen() {
        secondAcceptButton = script.getWidgets().get(334, 13);
        if (secondAcceptButton != null) {
            if (secondAcceptButton.interact("Accept")) {
                new ConditionalSleep(100000, 100) {
                    @Override
                    public boolean condition() throws InterruptedException {
                        return !secondAcceptButton.isVisible();
                    }
                }.sleep();
            } else {
                return false;
            }
        }
        return true;
    }

    public boolean waitForSecondScreenOrCancel() {
        new ConditionalSleep(100000, 100) {
            @Override
            public boolean condition() throws InterruptedException {
                return !firstAcceptButton.isVisible();
            }
        }.sleep();
        return true;
    }

    public boolean canTrade(Player player) {
        return player != null;
    }

    public boolean tradeWithPlayer(Player player) {
        if (player.interact("Trade with")) {
            new ConditionalSleep(15000, 100) {
                @Override
                public boolean condition() throws InterruptedException {
                    return script.getWidgets().getWidgetContainingText("Accept") != null;
                }
            }.sleep();
        } else {
            return false;
        }
        return true;
    }
}

 

 

Edited by BravoTaco
Forgot to add a return false for one of the checks
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...