Jump to content

A Simple Login Handler


Explv

Recommended Posts

3 hours ago, Juggles said:

How do you stop executing activities? 
Like I've found some in the middle of fishing just randomly logged out. It should have switched to login event but rather just stayed in the fishing loop doing nothing. I thought these kinds of things would automatically stop. 

Yep, same thoughts here.

I'm not using anything fancy really
 

int onLoop() {
	if (client.isLoggedIn()) 
		doTasks();
	else
		initLoginHandler();
}
	

Each webwalkevent/walkevent has a break condition to stop exuting if logged out

new Condition() {
			@Override
			public boolean evaluate() {
				if (!s.client.isLoggedIn())
					return true;
				return false;
			}
		};

if any of the tasks do a ton of things during one loop iteration, just check if the client is logged inside there. A horrible example but still:

// NO NO NO
public void catchFish() {
	while (!inventory.isFull)
		catchSomeFishies();
}

// YES YES YES
public void catchFish() {
	while(client.isLoggedIn() && !inventory.isFull)
		catchSomeFishies()'
}

Can write everything properly, but you get the gist of it

Edited by nosepicker
  • Like 1
Link to comment
Share on other sites

On 9/23/2017 at 2:43 AM, nosepicker said:

Yep, same thoughts here.

I'm not using anything fancy really
 


int onLoop() {
	if (client.isLoggedIn()) 
		doTasks();
	else
		initLoginHandler();
}
	

Each webwalkevent/walkevent has a break condition to stop exuting if logged out


new Condition() {
			@Override
			public boolean evaluate() {
				if (!s.client.isLoggedIn())
					return true;
				return false;
			}
		};

if any of the tasks do a ton of things during one loop iteration, just check if the client is logged inside there. A horrible example but still:


// NO NO NO
public void catchFish() {
	while (!inventory.isFull)
		catchSomeFishies();
}

// YES YES YES
public void catchFish() {
	while(client.isLoggedIn() && !inventory.isFull)
		catchSomeFishies()'
}

Can write everything properly, but you get the gist of it

Thanks I'll have a look into this later this week when I have time :)
Appreciate the help 

Link to comment
Share on other sites

On 6/9/2017 at 4:51 AM, Aap said:

Not sure if i do something wrong.

 

but when launching from CLI, i get this error.

ror executing event : testscript.LoginEvent@1538993
java.lang.NullPointerException
    at org.osbot.rs07.api.Widgets.getWidgetContainingText(bk:854)
    at testscript.LoginEvent.getLobbyButton(LoginEvent.java:133)
    at testscript.LoginEvent.execute(LoginEvent.java:30)
    at org.osbot.rs07.event.EventExecutor$2.run(ni:267)
    at org.osbot.rs07.event.EventExecutor.execute(ni:281)
    at org.osbot.rs07.script.MethodProvider.execute(kf:637)
    at testscript.LoginTest.onStart(LoginTest.java:18)
    at org.osbot.rs07.event.ScriptExecutor.IiIiIiiiiIiI(ti:266)
    at org.osbot.rs07.event.ScriptExecutor.start(ti:211)
    at org.osbot.Zb.run(ro:13)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

 

when i just stop the same script and start it from osbot script manager, it works fine(account details pre-filled in the script)

any idea of what i do wrong?

 

@Explv Would really appreciate a fix for that boss :D

Link to comment
Share on other sites

On 09/06/2017 at 2:51 AM, Aap said:

Not sure if i do something wrong.

 

but when launching from CLI, i get this error.

ror executing event : testscript.LoginEvent@1538993
java.lang.NullPointerException
    at org.osbot.rs07.api.Widgets.getWidgetContainingText(bk:854)
    at testscript.LoginEvent.getLobbyButton(LoginEvent.java:133)
    at testscript.LoginEvent.execute(LoginEvent.java:30)
    at org.osbot.rs07.event.EventExecutor$2.run(ni:267)
    at org.osbot.rs07.event.EventExecutor.execute(ni:281)
    at org.osbot.rs07.script.MethodProvider.execute(kf:637)
    at testscript.LoginTest.onStart(LoginTest.java:18)
    at org.osbot.rs07.event.ScriptExecutor.IiIiIiiiiIiI(ti:266)
    at org.osbot.rs07.event.ScriptExecutor.start(ti:211)
    at org.osbot.Zb.run(ro:13)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

 

when i just stop the same script and start it from osbot script manager, it works fine(account details pre-filled in the script)

any idea of what i do wrong?

 

 

33 minutes ago, progamerz said:

@Explv Would really appreciate a fix for that boss :D

 

Updated the code on the thread, should probably fix.

  • Like 1
Link to comment
Share on other sites

On 23/09/2017 at 7:43 AM, nosepicker said:

Yep, same thoughts here.

I'm not using anything fancy really
 


int onLoop() {
	if (client.isLoggedIn()) 
		doTasks();
	else
		initLoginHandler();
}
	

Each webwalkevent/walkevent has a break condition to stop exuting if logged out


new Condition() {
			@Override
			public boolean evaluate() {
				if (!s.client.isLoggedIn())
					return true;
				return false;
			}
		};

if any of the tasks do a ton of things during one loop iteration, just check if the client is logged inside there. A horrible example but still:


// NO NO NO
public void catchFish() {
	while (!inventory.isFull)
		catchSomeFishies();
}

// YES YES YES
public void catchFish() {
	while(client.isLoggedIn() && !inventory.isFull)
		catchSomeFishies()'
}

Can write everything properly, but you get the gist of it

 

22 hours ago, Juggles said:

Thanks I'll have a look into this later this week when I have time :)
Appreciate the help 

Not sure if there is a better way but you could have a login event always running in the background, it would do nothing if the player is logged in, but when the player logs out it would pause the script executor, log back in, and then resume the script executor.

Consider looking at the code in the original post, shouldn't be hard to modify.

Edited by Explv
Link to comment
Share on other sites

9 hours ago, Explv said:

 

Not sure if there is a better way but you could have a login event always running in the background, it would do nothing if the player is logged in, but when the player logs out it would pause the script executor, log back in, and then resume the script executor.

Consider looking at the code in the original post, shouldn't be hard to modify.

Great idea.
If I understood correctly, just run login event on a separate thread?

Link to comment
Share on other sites

5 hours ago, nosepicker said:

Great idea.
If I understood correctly, just run login event on a separate thread?

Yeah, you could either create your own separate thread that executes a LoginEvent when necessary, or you could modify the LoginEvent to be an asynchronous event that is always running (never calls setFinished())

Edited by Explv
Link to comment
Share on other sites

4 minutes ago, Juggles said:

Should we also remove setFailed? 

 

Probably. You would want to handle the cases where I call setFailed in some other way, e.g. if the account is banned then stop the script. If the script fails to enter input, just retry instead of failing.

7 minutes ago, Juggles said:

Should we also remove setFailed? 

 

I will write an example later today if I have time

Link to comment
Share on other sites

2 hours ago, Explv said:

Probably. You would want to handle the cases where I call setFailed in some other way, e.g. if the account is banned then stop the script. If the script fails to enter input, just retry instead of failing.

I will write an example later today if I have time

I still get the same problem after setAsync() :(

ERROR][Bot #1][09/27 04:20:48 PM]: Error executing event : LoginEvent@7a1b4403
java.lang.NullPointerException
	at org.osbot.rs07.api.Widgets.getWidgetContainingText(uj:500)
	at LoginEvent.getLobbyButton(LoginEvent.java:134)
	at LoginEvent.execute(LoginEvent.java:31)
	at org.osbot.rs07.event.EventExecutor$2.run(zl:85)
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

My code:

if (getClient().isLoggedIn()) {
			
		} else {
			log("Logging in");
			LoginEvent loginEvent = new LoginEvent(userName, pass);
			execute(loginEvent);
		}

 

Link to comment
Share on other sites

30 minutes ago, progamerz said:

I still get the same problem after setAsync() :(


ERROR][Bot #1][09/27 04:20:48 PM]: Error executing event : LoginEvent@7a1b4403
java.lang.NullPointerException
	at org.osbot.rs07.api.Widgets.getWidgetContainingText(uj:500)
	at LoginEvent.getLobbyButton(LoginEvent.java:134)
	at LoginEvent.execute(LoginEvent.java:31)
	at org.osbot.rs07.event.EventExecutor$2.run(zl:85)
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

My code:


if (getClient().isLoggedIn()) {
			
		} else {
			log("Logging in");
			LoginEvent loginEvent = new LoginEvent(userName, pass);
			execute(loginEvent);
		}

 

 

Did you update your LoginEvent to exactly what is in the thread? I added more than just setAsync

Link to comment
Share on other sites

1 minute ago, Explv said:

 

Did you update your LoginEvent to exactly what is in the thread? I added more than just setAsync

The new one works good, i only had to edit it and remove. Script wouldn't do anything if acc got banned or already logged in etc. After removing it, I got it to work 100% again. What;s the point of pausing it if you're already using norandoms? 

else if (!getBot().getScriptExecutor().isPaused()) {
            getBot().getScriptExecutor().pause();
        } 
Link to comment
Share on other sites

1 minute ago, Juggles said:

The new one works good, i only had to edit it and remove. Script wouldn't do anything if acc got banned or already logged in etc. After removing it, I got it to work 100% again. What;s the point of pausing it if you're already using norandoms? 


else if (!getBot().getScriptExecutor().isPaused()) {
            getBot().getScriptExecutor().pause();
        } 

If the event is async, it is non-blocking, therefore wouldn't onLoop() keep executing even while logged out if you don't pause the script executor?

I didn't really test this much.

Link to comment
Share on other sites

5 hours ago, Explv said:

If the event is async, it is non-blocking, therefore wouldn't onLoop() keep executing even while logged out if you don't pause the script executor?

I didn't really test this much.

Yes, this is my LoginEvent code:

Spoiler

import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.constants.ResponseCode;
import org.osbot.rs07.event.Event;
import org.osbot.rs07.input.mouse.RectangleDestination;
import org.osbot.rs07.listener.LoginResponseCodeListener;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;

public final class LoginEvent extends Event implements LoginResponseCodeListener {

    private final String username, password;

    public LoginEvent(final String username, final String password) {
        this.username = username;
        this.password = password;
        setAsync();
    }

    @ Override
    public final int execute() throws InterruptedException {
        if (!getBot().isLoaded()) {
            MethodProvider.sleep(1000);
        } else if (getClient().isLoggedIn() && getLobbyButton() == null) {
            getBot().getScriptExecutor().resume();
            setFinished();
        } else if (!getBot().getScriptExecutor().isPaused()) {
            getBot().getScriptExecutor().pause();
        } else if (getLobbyButton() != null) {
            clickLobbyButton();
        } else if (isOnWorldSelectorScreen()) {
            cancelWorldSelection();
        } else if (!isPasswordEmpty()) {
            clickCancelLoginButton();
        } else {
            login();
        }
        return random(100, 150);
    }

    private boolean isOnWorldSelectorScreen() {
        return getColorPicker().isColorAt(50, 50, Color.BLACK);
    }

    private void cancelWorldSelection() {
        if (getMouse().click(new RectangleDestination(getBot(), 712, 8, 42, 8))) {
            new ConditionalSleep(3000) {
                @ Override
                public boolean condition() throws InterruptedException {
                    return !isOnWorldSelectorScreen();
                }
            }.sleep();
        }
    }

    private boolean isPasswordEmpty() {
        return !getColorPicker().isColorAt(350, 274, Color.WHITE);
    }

    private boolean clickCancelLoginButton() {
        return getMouse().click(new RectangleDestination(getBot(), 398, 308, 126, 27));
    }

    private void login() {
        switch (getClient().getLoginUIState()) {
            case 0:
                clickExistingUsersButton();
                break;
            case 1:
                clickLoginButton();
                break;
            case 2:
                enterUserDetails();
                break;
            case 3:
                clickTryAgainButton();
                break;
        }
    }

    private void clickExistingUsersButton() {
        getMouse().click(new RectangleDestination(getBot(), 400, 280, 120, 20));
    }

    private void clickLoginButton() {
        getMouse().click(new RectangleDestination(getBot(), 240, 310, 120, 20));
    }

    private void enterUserDetails() {
        if (!getKeyboard().typeString(username)) {
            setFailed();
            return;
        }

        if (!getKeyboard().typeString(password)) {
            setFailed();
            return;
        }

        new ConditionalSleep(30_000) {
            @ Override
            public boolean condition() throws InterruptedException {
                return getLobbyButton() != null || getClient().getLoginUIState() == 3 || isDisabledMessageVisible();
            }
        }.sleep();

        if (!getClient().isLoggedIn()) {
            setFailed();
        }
    }

    private boolean clickTryAgainButton() {
        return getMouse().click(new RectangleDestination(getBot(), 318, 262, 130, 26));
    }

    private boolean isDisabledMessageVisible() {
        return getColorPicker().isColorAt(483, 205, Color.YELLOW);
    }

    private void clickLobbyButton() {
        if (getLobbyButton().interact()) {
            new ConditionalSleep(10_000) {
                @ Override
                public boolean condition() throws InterruptedException {
                    return getLobbyButton() == null;
                }
            }.sleep();
        }
    }

    private RS2Widget getLobbyButton() {
        return getWidgets().getWidgetContainingText("CLICK HERE TO PLAY");
    }

    @ Override
    public final void onResponseCode(final int responseCode) throws InterruptedException {
        if(ResponseCode.isDisabledError(responseCode)) {
            log("Login failed, account is disabled");
            setFailed();
            return;
        }

        if(ResponseCode.isConnectionError(responseCode)) {
            log("Connection error, attempts exceeded");
            setFailed();
            return;
        }
    }
}

 

Tho, if i pause script and re run, it will work well, is that maybe because it is running the script when using CLI while the client is still loading? :think:

 

Edited by progamerz
Link to comment
Share on other sites

5 minutes ago, progamerz said:

Yes, this is my LoginEvent code:


import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.constants.ResponseCode;
import org.osbot.rs07.event.Event;
import org.osbot.rs07.input.mouse.RectangleDestination;
import org.osbot.rs07.listener.LoginResponseCodeListener;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;

public final class LoginEvent extends Event implements LoginResponseCodeListener {

    private final String username, password;

    public LoginEvent(final String username, final String password) {
        this.username = username;
        this.password = password;
        setAsync();
    }

    @ Override
    public final int execute() throws InterruptedException {
        if (!getBot().isLoaded()) {
            MethodProvider.sleep(1000);
        } else if (getClient().isLoggedIn() && getLobbyButton() == null) {
            getBot().getScriptExecutor().resume();
            setFinished();
        } else if (!getBot().getScriptExecutor().isPaused()) {
            getBot().getScriptExecutor().pause();
        } else if (getLobbyButton() != null) {
            clickLobbyButton();
        } else if (isOnWorldSelectorScreen()) {
            cancelWorldSelection();
        } else if (!isPasswordEmpty()) {
            clickCancelLoginButton();
        } else {
            login();
        }
        return random(100, 150);
    }

    private boolean isOnWorldSelectorScreen() {
        return getColorPicker().isColorAt(50, 50, Color.BLACK);
    }

    private void cancelWorldSelection() {
        if (getMouse().click(new RectangleDestination(getBot(), 712, 8, 42, 8))) {
            new ConditionalSleep(3000) {
                @ Override
                public boolean condition() throws InterruptedException {
                    return !isOnWorldSelectorScreen();
                }
            }.sleep();
        }
    }

    private boolean isPasswordEmpty() {
        return !getColorPicker().isColorAt(350, 274, Color.WHITE);
    }

    private boolean clickCancelLoginButton() {
        return getMouse().click(new RectangleDestination(getBot(), 398, 308, 126, 27));
    }

    private void login() {
        switch (getClient().getLoginUIState()) {
            case 0:
                clickExistingUsersButton();
                break;
            case 1:
                clickLoginButton();
                break;
            case 2:
                enterUserDetails();
                break;
            case 3:
                clickTryAgainButton();
                break;
        }
    }

    private void clickExistingUsersButton() {
        getMouse().click(new RectangleDestination(getBot(), 400, 280, 120, 20));
    }

    private void clickLoginButton() {
        getMouse().click(new RectangleDestination(getBot(), 240, 310, 120, 20));
    }

    private void enterUserDetails() {
        if (!getKeyboard().typeString(username)) {
            setFailed();
            return;
        }

        if (!getKeyboard().typeString(password)) {
            setFailed();
            return;
        }

        new ConditionalSleep(30_000) {
            @ Override
            public boolean condition() throws InterruptedException {
                return getLobbyButton() != null || getClient().getLoginUIState() == 3 || isDisabledMessageVisible();
            }
        }.sleep();

        if (!getClient().isLoggedIn()) {
            setFailed();
        }
    }

    private boolean clickTryAgainButton() {
        return getMouse().click(new RectangleDestination(getBot(), 318, 262, 130, 26));
    }

    private boolean isDisabledMessageVisible() {
        return getColorPicker().isColorAt(483, 205, Color.YELLOW);
    }

    private void clickLobbyButton() {
        if (getLobbyButton().interact()) {
            new ConditionalSleep(10_000) {
                @ Override
                public boolean condition() throws InterruptedException {
                    return getLobbyButton() == null;
                }
            }.sleep();
        }
    }

    private RS2Widget getLobbyButton() {
        return getWidgets().getWidgetContainingText("CLICK HERE TO PLAY");
    }

    @ Override
    public final void onResponseCode(final int responseCode) throws InterruptedException {
        if(ResponseCode.isDisabledError(responseCode)) {
            log("Login failed, account is disabled");
            setFailed();
            return;
        }

        if(ResponseCode.isConnectionError(responseCode)) {
            log("Connection error, attempts exceeded");
            setFailed();
            return;
        }
    }
}

Tho, if i pause script and re run, it will work well, is that maybe because it is running the script when using CLI while the client is still loading? :think:

 

Delete.

else if (!getBot().getScriptExecutor().isPaused()) {
            getBot().getScriptExecutor().pause();
        } 

Same thing happened to me and now its running smooth without that 

Edited by Juggles
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...