Class ProgressWatchdog

java.lang.Object
privbot.mechanics.ProgressWatchdog

public final class ProgressWatchdog extends Object
Notices a bot that has stopped making progress. You tell it when something useful happened (progress(long)); it says whether too long has passed since (stalled(long)).

What it proves. stalled is true only when at least stallMs of counted time has passed with no progress call. Time spent held (a pause, a deliberate wait, a forced-move hold) is not counted, so a bot that was told to stand still is not "stuck". It proves silence, not cause: what to do about it is yours.

Unreadable. It reads no client; every time comes from you. Before the first call it counts as zero idle time (never stalled), because a watch that has seen nothing cannot say how long nothing has happened. Pass a monotonic clock (System.currentTimeMillis() is fine); a clock that goes backwards counts as zero.

Thread. Pure and not thread safe: call it from the one thread that runs your tick.

Lifted from AutoSlayer's stuck recovery (30 s without progress).

  • Constructor Summary

    Constructors
    Constructor
    Description
    ProgressWatchdog(long stallMs)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    held(boolean holding, long nowMs)
    Starts or ends a hold.
    long
    idleMs(long nowMs)
     
    void
    progress(long nowMs)
    Something useful happened at nowMs: the idle clock restarts.
    void
    reset(long nowMs)
    Starts over at nowMs: no idle time and no hold.
    boolean
    stalled(long nowMs)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ProgressWatchdog

      public ProgressWatchdog(long stallMs)
      Parameters:
      stallMs - counted idle time after which stalled(long) is true (AutoSlayer used 30000)
  • Method Details

    • progress

      public void progress(long nowMs)
      Something useful happened at nowMs: the idle clock restarts.
    • held

      public void held(boolean holding, long nowMs)
      Starts or ends a hold. Held time is not counted as idle. Repeating the current state changes nothing.
      Parameters:
      holding - true while the bot is deliberately waiting
      nowMs - the current time
    • idleMs

      public long idleMs(long nowMs)
      Returns:
      counted idle milliseconds since the last progress, never negative; 0 before any call
    • stalled

      public boolean stalled(long nowMs)
      Returns:
      whether counted idle time has reached the limit
    • reset

      public void reset(long nowMs)
      Starts over at nowMs: no idle time and no hold.