Question Help with an inactivity timer

Discussion in 'Developer Support' started by Wargnat, Dec 30, 2015.

  1. Wargnat

    Joined:
    Dec 20, 2015
    Messages:
    6
    Likes Received:
    0
    Can someone help me make an in-activity timer, the idea being: the script bot stops if you do not gain xp in a desired skill within a specific time.

    -I do not know javascript/java
    -I have a feeling you would need to use a timer, but as you can see below I have no idea how to use timers
    -I dont know where i would place this bit of code.

    here is what i have so far:

    int cExp = 0;

    if(Skill.WOODCUTTING.getExperience() > cExp) { cExp = Skill.WOODCUTTING.getExperience(); } else {
    Timer idle = new Timer(5000);
    idle.start();
    int x = 1;
    if(idle.getElapsedTime() > x) {
    System.out.println("Idle for " +x + "; Stopping");
    stop();}
    }

    Just a simple bit of code with a lot of potential, especially within darkscape.

    Thanks!
     
  2. Derk

    Derk 12 year old normie

    Joined:
    Jan 8, 2015
    Messages:
    2,076
    Likes Received:
    760
    Code (Javascript):
    1. startExp = Skill.WOODCUTTING.getExperience();
    2. Execution.delay(60000); //1 minute/60000 milliseconds
    3. currentExp = Skill.WOODCUTTING.getExperience();
    4. if (startExp == currentExp)  {
    5.     this.stop();
    6. }
    --- Double Post Merged, Dec 30, 2015, Original Post Date: Dec 30, 2015 ---
    EDIT: you probably want to have a timer instead of a delay, since it delays the entire bot.
    --- Double Post Merged, Dec 30, 2015 ---
    Code (Javascript):
    1. Timer timer = new Timer(50000);
    2. startExp = Skill.WOODCUTTING.getExperience();
    3. timer.start();
    4. if (timer.getRemainingTime() == 0) {
    5.     currentExp = Skill.WOODCUTTING.getExperience();
    6.     if (startExp == currentExp) {
    7.         this.stop();
    8.         }
    9. }
     
    #2 Derk, Dec 30, 2015
    Last edited: Dec 30, 2015
  3. Wargnat

    Joined:
    Dec 20, 2015
    Messages:
    6
    Likes Received:
    0
  4. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    2,963
    Likes Received:
    874
    Even then, the remaining time will always be > 0 because you're creating and starting the timer just before you check it xD

    Code (Text):
    1. public class ExampleBot extends LoopingScript {
    2.     private Timer inactivityTimer = new Timer(60000);
    3.     private int lastExp;
    4.     private Skill skillToCheck = Skill.WOODCUTTING;
    5.  
    6.     public void onStart(String... args) {
    7.         inactivityTimer.start();
    8.         lastExp = skillToCheck.getExperience();
    9.     }
    10.  
    11.     public void onLoop() {
    12.         if (lastExp == skillToCheck.getExperience() && !inactivityTimer.isRunning()) { //No exp gained since last timer reset
    13.             stop();
    14.             return;
    15.         } else if (lastExp < skillToCheck.getExperience()) {
    16.             lastExp = skillToCheck.getExperience();
    17.             inactivityTimer.reset();
    18.         }
    19.         //Your code :D
    20.     }
    21. }
    Or if you're using a TaskScript, you could create a Task to handle it.
    Code (Text):
    1. public class InactivityStopper extends Task {
    2.     private Timer inactivityTimer = new Timer(60000);
    3.     private int lastExp;
    4.     private Skill skillToCheck = Skill.WOODCUTTING;
    5.  
    6.     public InactivityStopper() {
    7.         inactivityTimer.start();
    8.         lastExp = skillToCheck.getExperience();
    9.     }
    10.  
    11.     @Override
    12.     public boolean validate() {
    13.         if (lastExp == skillToCheck.getExperience() && !inactivityTimer.isRunning()) {
    14.             return true;
    15.         } else if (lastExp < skillToCheck.getExperience()) {
    16.             lastExp = skillToCheck.getExperience();
    17.             inactivityTimer.reset();
    18.         }
    19.         return false;
    20.     }
    21.  
    22.     @Override
    23.     public void execute() {
    24.         AbstractScript bot = Environment.getScript();
    25.         if (bot != null)
    26.             bot.stop();
    27.     }
    28. }
    Written off of the top of my head, without an IDE so there may be some mistakes xD

    You could track multiple Skills by using a Skill array or Collection instead of a single Skill, and setting lastExp to the sum of all of their exp.
     
  5. Derk

    Derk 12 year old normie

    Joined:
    Jan 8, 2015
    Messages:
    2,076
    Likes Received:
    760
    You are going to have to explain me why my code wouldn't work. On slack or here I don't mind. :p
     
  6. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    2,963
    Likes Received:
    874
    Copy/pasted it wouldn't work, because you're creating the timer, setting the cExp, starting the timer, and then checking the exp in the same code block/scope.
    Your timer start is followed straight away by your timer check, and for 50 seconds to pass between those 2 lines would take some pretty rare conditions :p
     
  7. Derk

    Derk 12 year old normie

    Joined:
    Jan 8, 2015
    Messages:
    2,076
    Likes Received:
    760
    Code (Javascript):
    1. timer.start();
    2. if (timer.getRemainingTime() == 0) {
    3.    //do stuff
    4. }
    According to my thought-train it starts the counter (from 60000 milliseconds), counts down to zero, if the remaining time on the timer reaches 0, execute if statement.

    I must be misunderstanding timers if it wouldn't work.
     
  8. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    2,963
    Likes Received:
    874
    Timers don't block execution of the thread. They either run concurrently or calculate the time remaining when the methods are called, as far as I can tell from the decompiled code. timer.start(); sets the "start time", as does reset(),
    That code is pretty much the same as
    Code (Text):
    1. long startTime = System.currentTimeMillis();
    2. long timerLength = 60000;
    3. if (System.currentTimeMillis() - startTime > timerLength) {
    4.     //do stuff
    5. }
     
    #8 SlashnHax, Jan 1, 2016
    Last edited: Jan 1, 2016
  9. Wargnat

    Joined:
    Dec 20, 2015
    Messages:
    6
    Likes Received:
    0
    I am pleased
     

Share This Page

Loading...