Jump to content

Trading API unpredictable


Recommended Posts

Posted

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! :)

Posted
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
Posted
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

 

Posted
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 😅

 

  • 2 weeks later...
Posted (edited)

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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