import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Methods;
import java.awt.Graphics;
import java.awt.Color;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Comparator;
import java.util.Collections;
import java.util.Enumeration;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class BreakHandler extends Random implements PaintListener {
private final String confFile = "Settings/breaks.txt";
private long startTime = System.currentTimeMillis();
private ArrayList<Break> breaks = new ArrayList<Break>();
private Iterator it;
private Break curBreak;
private boolean reset;
private boolean setConfigs = true;
private long curTime = System.currentTimeMillis();
private long tookBreakAt = 0;
long breakLength = 0;
public String getName() {
return "Break Handler";
}
public String getAuthor() { //ported to RSBot by ngovil21
return "regex, sean, pd";
}
public double getVersion() {
return 1;
}
public void onRepaint(Graphics render) {
if (tookBreakAt>0 && System.currentTimeMillis() - tookBreakAt < breakLength) {
render.setColor(Color.yellow);
long time = breakLength - (System.currentTimeMillis() - tookBreakAt);
render.drawString("Break Ends in " + cTime(time),420,15);
return;
}
if(tookBreakAt>0) tookBreakAt = 0;
if (!reset && !checkForActiveScripts()) {
reset = true;
log("No Scripts Running. Reset...");
}
}
private boolean checkForActiveScripts() {
try {
return !Bot.getScriptHandler().getRunningScripts().isEmpty();
} catch(Exception e) { }
return false;
}
public void getConfig() {
Configuration conf = new Configuration(confFile);
for (Enumeration e = conf.propertyNames(); e.hasMoreElements();) {
String prop = (String) e.nextElement();
String val = conf.get(prop);
String breakVal = val.substring(0, val.indexOf(',')).trim();
String lengthVal = val.substring(val.indexOf(',') + 1).trim();
long breakAtMin, breakAtMax, lengthMin, lengthMax;
try {
if (breakVal.indexOf('|') != -1) {
breakAtMin = Long.parseLong(
breakVal.substring(0, breakVal.indexOf('|')).trim());
breakAtMax = Long.parseLong(
breakVal.substring(breakVal.indexOf('|') + 1).trim());
} else {
breakAtMax = Long.parseLong(breakVal);
breakAtMin = breakAtMax - breakAtMax / 4;
}
if (lengthVal.indexOf('|') != -1) {
lengthMin = Long.parseLong(
lengthVal.substring(0, lengthVal.indexOf('|')).trim());
lengthMax = Long.parseLong(
lengthVal.substring(lengthVal.indexOf('|') + 1).trim());
} else {
lengthMax = Long.parseLong(lengthVal);
lengthMin = lengthMax / 2;
}
} catch (Exception ex) {
log("Exception loading BreakHandler config (" + prop + " = " + val + ")");
ex.printStackTrace();
continue;
}
// convert to ms
breakAtMin *= 60000;
breakAtMax *= 60000;
lengthMin *= 60000;
lengthMax *= 60000;
Break b = new Break(breakAtMin, breakAtMax, lengthMin, lengthMax);
breaks.add(b);
}
Collections.sort(breaks,
new Comparator<Break>() {
public int compare(Break b1, Break b2) {
return (int) (b1.getBreakAtMin() - b2.getBreakAtMin());
}
});
it = breaks.iterator();
}
public boolean activateCondition() {
if (setConfigs) {
getConfig();
startTime = System.currentTimeMillis();
setConfigs = false;
}
if (breaks.isEmpty())
return false;
if (reset) {
it = breaks.iterator();
startTime = System.currentTimeMillis();
reset = false;
}
if (curBreak == null)
curBreak = (Break) it.next();
curTime = System.currentTimeMillis();
if (curBreak.shouldBreak(startTime, curTime))
return true;
return false;
}
public int loop() {
if (curBreak == null)
return -1;
if (curBreak.getLengthMin() <= 0) {
log("After " + cTime(curTime - startTime) + ", shutting down.");
logout();
reset = true;
stopAllScripts();
return -1;
} else {
breakLength = curBreak.randLength();
log("After " + cTime(curTime - startTime)
+ ", taking break for " + cTime(breakLength));
if (getMyPlayer().isInCombat()) {
wait(random(5000,10000));
}
if (getMyPlayer().isInCombat()) {
log("Still In Combat, Waiting To Logout!");
return random(3000,5000);
}
log("Not in Combat, Logging out..");
logout();
curBreak = null;
if (!it.hasNext())
reset = true;
tookBreakAt = System.currentTimeMillis();
return (int)breakLength;
}
//return random(1000,2000);
}
private String cTime(long eTime) {
long hrs = eTime / 1000 / 3600;
eTime -= (hrs * 3600 * 1000);
long mins = eTime / 1000 / 60;
eTime -= (mins * 60 * 1000);
long secs = eTime / 1000;
return String.format("%1$02d:%2$02d:%3$02d", hrs, mins, secs);
}
public void waitNoException(long t) {
try {
Thread.sleep(t);
} catch (Exception e) {}
}
protected class Break {
private final long breakAtMin;
private final long breakAtMax;
private final long lengthMin;
private final long lengthMax;
private long randBreak=0;
Break(long breakAtMin, long breakAtMax, long lengthMin, long lengthMax) {
this.breakAtMin = breakAtMin;
this.breakAtMax = breakAtMax;
this.lengthMin = lengthMin;
this.lengthMax = lengthMax;
this.randBreak = randBreakAt();
}
private long randLong(long min, long max) {
return min + (long) (java.lang.Math.random() * (max - min));
}
public long getBreakAtMin() {
return breakAtMin;
}
public long getBreakAtMax() {
return breakAtMax;
}
public long getLengthMin() {
return lengthMin;
}
public long getLengthMax() {
return lengthMax;
}
public long randBreakAt() {
return randLong(breakAtMin, breakAtMax);
}
public long randLength() {
return randLong(lengthMin, lengthMax);
}
public boolean shouldBreak(long startTime, long curTime) {
if ((curTime - startTime) > randBreak) {
randBreak = randBreakAt();
return true;
} else
return false;
}
}
public class Configuration {
private File loadedFrom;
private Properties props;
private boolean xml;
/**
* Reads and parses the specified file; can parse as both
* XML and standard Properties syntax. If fileName ends with
* .xml, XML format is assumed.
*
* @param fileName The name of the file to parse
*/
public Configuration(String fileName) {
loadedFrom = new File(fileName);
if (fileName.toLowerCase().endsWith(".xml"))
xml = true;
props = new Properties();
if (loadedFrom.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(loadedFrom);
if (xml)
props.loadFromXML(fis);
else
props.load(new FileInputStream(loadedFrom));
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Saves the configuration
*/
public void save() {
save(loadedFrom, xml);
}
/**
* Saves the configuration to specified filename; if the
* filename ends with .xml, saves in XML format.
*
* @param fileName Filename to save to.
*/
public void save(String fileName) {
File f = new File(fileName);
save(f);
}
/**
* Saves the configuration to specified File; if the name of
* the file ends with .xml, saves in XML format.
*
* @param f File object to save to
*/
public void save(File f) {
save(f, f.getName().toLowerCase().endsWith(".xml"));
}
/**
* Saves the configuration to specified File
*
* @param f File object to save to
* @param xml Whether to save in XML format
*/
public void save(File f, boolean xml) {
FileOutputStream fos = null;
try {
if (f.exists())
f.delete();
f.createNewFile();
fos = new FileOutputStream(f);
if (xml)
props.storeToXML(fos, null);
else
props.store(fos, null);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Gets the value of a key
*
* @param key The key to retrieve
* @return The String value of the given key
*/
public String get(String key) {
String rv = props.getProperty(key);
return rv == null ? "" : rv;
}
/**
* Gets the value of a key, with a default value to fall back on
*
* @param key The key to retrieve
* @param defaultValue The default value to use if no such key was available
* @return The String value of the given key, or the default value
*/
public String get(String key, String defaultValue) {
return props.getProperty(key, defaultValue);
}
public int getInt(String key) {
String l = get(key);
if (!l.equalsIgnoreCase("")) {
return Integer.parseInt(l);
} else {
return -1;
}
}
/**
* Gets an Enumeration of available property names
*
* @return Enumeration object containing available properties
*/
public Enumeration propertyNames() {
return props.propertyNames();
}
/**
* Puts a key/value pair into the configuration
*
* @param key The key to assign a value to
* @param value The value to assign
* @return The previous value of the key, or null if it did not have one
*/
public Object put(String key, String value) {
return props.setProperty(key, value);
}
/**
* Returns the number of available properties
*
* @return The number of properties
*/
public int size() {
return props.size();
}
}
}