This repository has been archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPresenceEngine.java
167 lines (139 loc) · 4.62 KB
/
PresenceEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.util.LinkedList;
import java.util.HashMap;
import org.pircbotx.PircBotX;
import org.pircbotx.Colors;
import java.io.IOException;
public class PresenceEngine implements Runnable {
private HashMap<String,HashMap<String,Boolean>> allPresenceStates;
private HashMap<String,Boolean> presenceState;
private PircBotX bot;
private String channel;
private boolean onSwitch;
private long interval;
private long backoff;
private int timeout;
private LinkedList<String> outfitAliasList;
private String soeapikey;
private boolean squelch;
public PresenceEngine(PircBotX bot, String channel, String soeapikey) {
allPresenceStates = new HashMap<String, HashMap<String,Boolean>>();
presenceState = new HashMap<String,Boolean>();
this.bot = bot;
this.channel = channel;
this.soeapikey = soeapikey;
onSwitch = false;
squelch = true;
interval = 60000L;
backoff = 0L;
timeout = 10000;
outfitAliasList = new LinkedList<String>();
outfitAliasList.add("FKPK");
}
public void setInterval(long set) {
interval = set;
}
public void setTimeout(int set) {
timeout = set;
}
public int getTimeout() {
return timeout;
}
public void turnOn() {
onSwitch = true;
}
public void squelchOn() {
squelch = true;
}
public void squelchOff() {
squelch = false;
}
public LinkedList<String> getOutfits() {
return outfitAliasList;
}
public void addOutfit(String set){
this.outfitAliasList.add(set);
}
public void purgeOutfits() {
this.outfitAliasList = new LinkedList<String>();
this.allPresenceStates = new HashMap<String, HashMap<String, Boolean>>();
}
public void turnOff() {
onSwitch = false;
}
public void run() {
while (true) {
try {
Thread.sleep(interval+backoff);
if (onSwitch) {
for (String outfit : outfitAliasList) {
// go and get new data from source
HashMap<String,Boolean> newPresenceState = PresenceChecker.getPresence(outfit, timeout, soeapikey);
HashMap<String,Boolean> oldPresenceState = allPresenceStates.get(outfit);
if (oldPresenceState == null) {
oldPresenceState = new HashMap<String,Boolean>();
}
LinkedList<String> wentonline = new LinkedList<String>();
LinkedList<String> wentoffline = new LinkedList<String>();
// compare data to current list
for (String name : newPresenceState.keySet()){
//bot.sendMessage(channel, "Iterating over "+name);
Boolean oldstatus = oldPresenceState.get(name);
Boolean newstatus = newPresenceState.get(name);
if (oldstatus == null) {
if (newstatus == true) {
wentonline.add(name);
}
} else if (oldstatus == true) {
if (newstatus == false) {
wentoffline.add(name);
}
} else if (oldstatus == false) {
if (newstatus == true) {
wentonline.add(name);
}
}
}
//squelch output if the change is unrealistically large (SOE patch time)
int totalSize = newPresenceState.size();
int onlineDiff = wentonline.size();
int offlineDiff = wentoffline.size();
float percentChangeOnline = (float)onlineDiff / (float)totalSize;
float percentChangeOffline = (float)offlineDiff / (float)totalSize;
boolean squelchTrigger = false;
if (percentChangeOnline > .9 || percentChangeOffline > .9) {
squelchTrigger = true;
}
if (!squelchTrigger) {
for (String s : wentonline) {
bot.sendMessage(channel, "["+outfit+"]"+s+" is now "+Colors.GREEN+"online.");
}
for (String s : wentoffline) {
bot.sendMessage(channel, "["+outfit+"]"+s+" is now "+Colors.RED+"offline.");
}
allPresenceStates.put(outfit, newPresenceState);
}
backoff = 0L;
}
}
} catch (InterruptedException e) {
bot.sendMessage(channel, "Interval timer interrupted - resetting backoff and restarting clock.");
} catch (IOException e) {
if (backoff==0L) {
backoff = 300000L;
bot.sendMessage(channel, "SOE just choked over an update check - sorry! Backing off a bit.");
System.out.println("API Failure - backoff is now "+backoff);
} else if (backoff > 3600000L) {
bot.sendMessage(channel, "Enough API calls have failed that I'm shutting down the presence engine. Please contact my owner.");
System.out.println("API Failure");
System.out.println("Shutting down presence and resetting timeouts.");
this.turnOff();
backoff=0L;
} else {
backoff = backoff*2;
bot.sendMessage(channel, "SOE choked again. Backing off further.");
System.out.println("API Failure - backoff is now "+backoff);
}
}
}
}
}