Created
September 7, 2014 22:49
-
-
Save Strikeskids/ee303431421db0f2949f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sk.bot; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.util.AbstractCollection; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import org.powerbot.script.Condition; | |
import org.powerbot.script.Script.State; | |
import org.powerbot.script.rt6.Actor; | |
import org.powerbot.script.rt6.ClientContext; | |
import org.powerbot.script.rt6.Constants; | |
import org.powerbot.script.rt6.Item; | |
import org.powerbot.script.rt6.Npc; | |
public class DataLogger implements Runnable { | |
private ClientContext ctx; | |
private ResourceWatcher fishing; | |
private static final long PULSE_TIME = 60000; | |
public DataLogger(ClientContext ctx) { | |
this.ctx = ctx; | |
@SuppressWarnings("unchecked") | |
List<Runnable> stops = ctx.controller.script().getExecQueue(State.STOP); | |
stops.add(new Runnable() { | |
@Override | |
public void run() { | |
fishing.finish(); | |
} | |
}); | |
@SuppressWarnings("unchecked") | |
List<Runnable> starts = ctx.controller.script().getExecQueue(State.START); | |
starts.add(new Runnable() { | |
@Override | |
public void run() { | |
fishing = new ResourceWatcher(Constants.SKILLS_FISHING); | |
Thread t = new Thread(DataLogger.this); | |
t.setDaemon(true); | |
t.start(); | |
} | |
}); | |
} | |
@Override | |
public void run() { | |
while (!ctx.controller.isStopping()) { | |
Actor interact = ctx.players.local().interacting(); | |
boolean validNpc = interact.valid() && interact instanceof Npc; | |
fishing.loop(validNpc && interact.name().equals("Fishing spot")); | |
Condition.sleep(700); | |
} | |
System.out.println("Stop logging"); | |
} | |
private static final String BASE = "http://strikeskids.com:3347/rsbot/skill_log?"; | |
private static final int[] MAGIC_NUMBERS = { 364571, 4111 }; | |
class ResourceWatcher { | |
private boolean started; | |
private ItemGroup startPack; | |
private long startTime; | |
private long lastPulse; | |
private int level = -1; | |
private int skill; | |
private long totalTime; | |
private ItemGroup gained = new ItemGroup(); | |
private DataConnection conn; | |
public ResourceWatcher(int skill) { | |
this.skill = skill; | |
this.conn = new DataConnection(); | |
this.lastPulse = System.currentTimeMillis(); | |
conn.set("skill", skill); | |
} | |
private ItemGroup getItems() { | |
ItemGroup ret = new ItemGroup(); | |
ctx.backpack.select().addTo(ret); | |
ctx.equipment.select().addTo(ret); | |
return ret; | |
} | |
private int level() { | |
return ctx.skills.level(skill); | |
} | |
public void loop(boolean gathering) { | |
if (gathering ^ started) { | |
if (gathering) { | |
startTime = System.currentTimeMillis(); | |
startPack = getItems(); | |
if (level == -1) | |
level = level(); | |
} else { | |
update(); | |
} | |
started = gathering; | |
} | |
if (started && level != level()) { | |
finish(); | |
} else if (System.currentTimeMillis() - lastPulse > PULSE_TIME) { | |
pulse(); | |
} | |
} | |
private void pulse() { | |
update(); | |
if (totalTime > 0) { | |
StringBuilder resources = new StringBuilder(); | |
StringBuilder counts = new StringBuilder(); | |
for (Item it : gained) { | |
if (it.stackSize() > 0) { | |
resources.append(it.id()); | |
resources.append(","); | |
counts.append(it.stackSize()); | |
counts.append(","); | |
} | |
} | |
if (resources.length() > 0) { | |
conn.set("resource", resources.toString()); | |
conn.set("count", counts.toString()); | |
} else { | |
conn.set("resource", ""); | |
conn.set("count", ""); | |
} | |
conn.set("level", level); | |
conn.set("duration", totalTime); | |
conn.pulse(); | |
} | |
lastPulse = System.currentTimeMillis(); | |
} | |
public void reset() { | |
started = false; | |
totalTime = 0; | |
gained.clear(); | |
conn.clear(); | |
level = -1; | |
} | |
public void update() { | |
if (started) { | |
ItemGroup items = getItems(); | |
items.subtract(startPack); | |
gained.add(items); | |
items.add(startPack); | |
startPack = items; | |
long time = System.currentTimeMillis(); | |
totalTime += time - startTime; | |
startTime = time; | |
} | |
} | |
public void finish() { | |
pulse(); | |
reset(); | |
} | |
} | |
class DataConnection { | |
private int id = -1; | |
private Map<String, Object> query; | |
public DataConnection() { | |
query = new LinkedHashMap<String, Object>(); | |
clear(); | |
} | |
public void pulseAndClear() { | |
pulse(); | |
clear(); | |
} | |
public void clear() { | |
query.clear(); | |
} | |
public void set(String key, Object param) { | |
query.put(key, param); | |
} | |
private void updateQuery() { | |
query.put("id", id); | |
String topic = (String) ctx.controller.script().getProperties().get("topic"); | |
query.put("token", Integer.parseInt(topic)); | |
updateCheck(); | |
} | |
private void updateCheck() { | |
query.remove("check"); | |
int check = 0; | |
for (Object obj : query.values()) { | |
if (obj instanceof Number) { | |
check = (check + ((Number) obj).intValue()) % MAGIC_NUMBERS[0]; | |
} | |
} | |
check = (check * MAGIC_NUMBERS[1]) % MAGIC_NUMBERS[0]; | |
query.put("check", check); | |
} | |
public void pulse() { | |
String url = BASE + makeQuery(); | |
String response = ctx.controller.script().downloadString(url); | |
if (response != null) { | |
try { | |
id = Integer.parseInt(response); | |
} catch (NumberFormatException ignored) { | |
} | |
} | |
} | |
private String makeQuery() { | |
updateQuery(); | |
StringBuilder builder = new StringBuilder(); | |
for (Entry<String, Object> entry : query.entrySet()) { | |
try { | |
builder.append(URLEncoder.encode(String.valueOf(entry.getKey()), "UTF-8")); | |
builder.append("="); | |
builder.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8")); | |
builder.append("&"); | |
} catch (UnsupportedEncodingException ignored) { | |
} | |
} | |
return query.size() > 0 ? builder.substring(0, builder.length() - 1) : ""; | |
} | |
} | |
class ItemGroup extends AbstractCollection<Item> implements Collection<Item> { | |
protected Map<Integer, Integer> itemMap; | |
public ItemGroup() { | |
this.itemMap = new HashMap<Integer, Integer>(); | |
} | |
public void clear() { | |
itemMap.clear(); | |
} | |
public int get(int id) { | |
Integer value = itemMap.get(id); | |
if (value == null) | |
return 0; | |
else | |
return value; | |
} | |
protected boolean set(int id, int stack) { | |
itemMap.put(id, stack); | |
return true; | |
} | |
public boolean subtract(ItemGroup group) { | |
boolean success = true; | |
for (Entry<Integer, Integer> entry : group.itemMap.entrySet()) { | |
success &= !add(entry.getKey(), -entry.getValue()); | |
} | |
return success; | |
} | |
public boolean add(int id, int stack) { | |
return set(id, get(id) + stack); | |
} | |
public boolean add(Item item) { | |
return add(item.id(), item.stackSize()); | |
} | |
public void add(Item... items) { | |
for (Item it : items) { | |
add(it); | |
} | |
} | |
public boolean add(ItemGroup group) { | |
boolean success = true; | |
for (Entry<Integer, Integer> item : group.itemMap.entrySet()) { | |
success &= add(item.getKey(), item.getValue()); | |
} | |
return success; | |
} | |
@Override | |
public Iterator<Item> iterator() { | |
final Iterator<Entry<Integer, Integer>> mapIterator = itemMap.entrySet().iterator(); | |
return new Iterator<Item>() { | |
@Override | |
public boolean hasNext() { | |
return mapIterator.hasNext(); | |
} | |
@Override | |
public Item next() { | |
Entry<Integer, Integer> entry = mapIterator.next(); | |
return new Item(ctx, entry.getKey(), entry.getValue(), null); | |
} | |
@Override | |
public void remove() { | |
mapIterator.remove(); | |
} | |
}; | |
} | |
@Override | |
public int size() { | |
return itemMap.size(); | |
} | |
@Override | |
public String toString() { | |
return "ItemGroup " + itemMap.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment