forked from maradine/ps2bot
/
KillCollectorEnhanced.java
287 lines (222 loc) · 11.7 KB
/
KillCollectorEnhanced.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.Connection;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.parser.Parser;
import java.util.HashMap;
import java.util.LinkedList;
import java.io.IOException;
import java.util.Properties;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.BatchUpdateException;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.DriverManager;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
public class KillCollectorEnhanced {
public static HashMap<String, Integer> collectKills(int timeout, long interval, Properties props) throws IOException, SQLException {
Document doc = null;
org.jsoup.Connection jcon = null;
String soeapikey = props.getProperty("soeapikey");
String querystring;
Long startingTimestampNumber = ((System.currentTimeMillis()/1000L)-90L);
String startingTimestamp = startingTimestampNumber.toString();
// http://census.soe.com/s:Merl0n/xml/get/ps2:v2/event?type=KILL&c:limit=10&c:join=type:character^on:attacker_character_id^to:character_id^show:faction_id'battle_rank.value
// NEW KILL STRING
// http://census.soe.com/s:Merl0n/xml/get/ps2:v2/event?type=KILL&c:limit=10&c:join=type:character^on:attacker_character_id^to:character_id^show:faction_id'battle_rank.value&c:join=type:character^on:character_id^to:character_id^show:faction_id'battle_rank.value
if (soeapikey == null || soeapikey.equals("")) {
querystring = "http://census.soe.com/xml/get/ps2:v2/event?type=KILL&after="+startingTimestamp+"&c:limit=1000&c:join=type:character^on:attacker_character_id^to:character_id^show:faction_id'battle_rank.value";
} else {
querystring = "http://census.soe.com/s:"+soeapikey+"/xml/get/ps2:v2/event?type=KILL&after="+startingTimestamp+"&c:limit=1000&c:join=type:character^on:attacker_character_id^to:character_id^show:faction_id'battle_rank.value&c:join=type:character^on:character_id^to:character_id^show:faction_id'battle_rank.value";
}
System.out.println("running "+ querystring);
try {
jcon = Jsoup.connect(querystring);
jcon.timeout(timeout);
jcon.parser(Parser.xmlParser());
doc = jcon.get();
} catch (IOException ioe) {
System.out.println("***connection error to SOE***");
System.out.println(ioe);
throw ioe;
}
Elements killEvents = doc.select("event_list > event");
int killEventsSize = killEvents.size();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
System.out.println("SOMETHING BLEW THE FUCK UP WHILE LOADING THE DRIVER");
}
java.sql.Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String insertString = "insert into v2_kills (attacker_character_id, attacker_vehicle_id, attacker_weapon_id, character_id, is_critical, is_headshot, "+
"timestamp, world_id, zone_id, attacker_faction_id, attacker_br_value, faction_id, br_value) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
String logString = "insert into v2_api_pulls (timestamp, dupes, first_timestamp_pulled, last_timestamp_pulled, write_time, `interval`) values (?, ?, ?, ?, ?, ?)";
String dbServer = props.getProperty("db_server");
String database = props.getProperty("database");
String dbUsername = props.getProperty("db_username");
String dbPassword = props.getProperty("db_password");
int skipCount = 0;
int minTimestamp=2000000000;
int maxTimestamp=0;
long startTimeMillis = System.currentTimeMillis();
int duration = 0;
int[] updateCounts=new int[0];
try {
conn = DriverManager.getConnection("jdbc:mysql://"+dbServer+"/"+database+"?user="+dbUsername+"&password="+dbPassword);
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(insertString);
for (int i=0;i<killEventsSize;i++) {
// System.out.println(killEvents.get(i));
long attacker_character_id = Long.parseLong(killEvents.get(i).attr("attacker_character_id"));
pstmt.setLong(1,attacker_character_id);
int attacker_vehicle_id = Integer.parseInt(killEvents.get(i).attr("attacker_vehicle_id"));
pstmt.setInt(2,attacker_vehicle_id);
int attacker_weapon_id = Integer.parseInt(killEvents.get(i).attr("attacker_weapon_id"));
pstmt.setInt(3,attacker_weapon_id);
long character_id = Long.parseLong(killEvents.get(i).attr("character_id"));
pstmt.setLong(4,character_id);
int is_critical = Integer.parseInt(killEvents.get(i).attr("is_critical"));
pstmt.setInt(5,is_critical);
int is_headshot = Integer.parseInt(killEvents.get(i).attr("is_headshot"));
pstmt.setInt(6,is_headshot);
int timestamp = Integer.parseInt(killEvents.get(i).attr("timestamp"));
pstmt.setInt(7,timestamp);
int world_id = Integer.parseInt(killEvents.get(i).attr("world_id"));
pstmt.setInt(8,world_id);
int zone_id = Integer.parseInt(killEvents.get(i).attr("zone_id"));
pstmt.setInt(9,zone_id);
int attacker_faction_id=-1;
int attacker_br_value=-1;
int faction_id=-1;
int br_value=-1;
// for first case of 2 nodes
if (killEvents.get(i).childNodeSize()==2) {
attacker_faction_id = Integer.parseInt(killEvents.get(i).childNode(0).attr("faction_id"));
attacker_br_value = Integer.parseInt(killEvents.get(i).childNode(0).childNode(0).attr("value"));
faction_id = Integer.parseInt(killEvents.get(i).childNode(1).attr("faction_id"));
br_value = Integer.parseInt(killEvents.get(i).childNode(1).childNode(0).attr("value"));
}
String tag = "";
if (killEvents.get(i).childNodeSize()>0) {
tag = killEvents.get(i).childNode(0).nodeName();
}
// System.out.println(tag);
String test1 = "attacker_character_id_join_character";
String test2 = "character_id_join_character";
// for second case of 1 node and attacker is saved and destroyed not reporting br value
if (killEvents.get(i).childNodeSize()==1 && tag.equals(test1)) {
attacker_faction_id = Integer.parseInt(killEvents.get(i).childNode(0).attr("faction_id"));
attacker_br_value = Integer.parseInt(killEvents.get(i).childNode(0).childNode(0).attr("value"));
// System.out.println(test1);
}
// if (killEvents.get(i).childNodeSize()==1 && killEvents.get(i).childNode(0).hasAttr("character_id_join_character")==true) {
if (killEvents.get(i).childNodeSize()==1 && tag.equals(test2)) {
faction_id = Integer.parseInt(killEvents.get(i).childNode(0).attr("faction_id"));
br_value = Integer.parseInt(killEvents.get(i).childNode(0).childNode(0).attr("value"));
// System.out.println(test1);
}
pstmt.setInt(10,attacker_faction_id);
pstmt.setInt(11,attacker_br_value);
pstmt.setInt(12,faction_id);
pstmt.setInt(13,br_value);
/*
//values that might not exist
int attacker_faction_id=-1;
if (killEvents.get(i).childNodeSize()>0) {
attacker_faction_id = Integer.parseInt(killEvents.get(i).childNode(0).attr("faction_id"));
}
pstmt.setInt(10,attacker_faction_id);
int attacker_br_value=-1;
if (killEvents.get(i).childNodeSize()>0) {
if (killEvents.get(i).childNode(0).childNodeSize()>0) {
attacker_br_value = Integer.parseInt(killEvents.get(i).childNode(0).childNode(0).attr("value"));
}
}
pstmt.setInt(11,attacker_br_value);
int faction_id=-1;
if (killEvents.get(i).childNodeSize()>1) {
faction_id = Integer.parseInt(killEvents.get(i).childNode(1).attr("faction_id"));
}
pstmt.setInt(12,faction_id);
int br_value=-1;
if (killEvents.get(i).childNodeSize()>1) {
if (killEvents.get(i).childNode(1).childNodeSize()>0) {
br_value = Integer.parseInt(killEvents.get(i).childNode(1).childNode(0).attr("value"));
}
}
pstmt.setInt(13,br_value);
*/
pstmt.addBatch();
if (timestamp < minTimestamp) {
minTimestamp = timestamp;
}
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
}
}
updateCounts = pstmt.executeBatch();
} catch (BatchUpdateException ex) {
System.out.println("Batch Update Exception - dupes in load stream.");
updateCounts = ex.getUpdateCounts();
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
throw ex;
}
conn.commit();
for (int i=0; i<updateCounts.length; i++) {
if (updateCounts[i] == Statement.EXECUTE_FAILED) {
skipCount++;
}
}
long endTimeMillis = System.currentTimeMillis();
duration = (int)(endTimeMillis-startTimeMillis);
try {
pstmt = conn.prepareStatement(logString);
pstmt.setInt(1, Integer.parseInt(startingTimestamp));
pstmt.setInt(2, skipCount);
pstmt.setInt(3, minTimestamp);
pstmt.setInt(4, maxTimestamp);
pstmt.setInt(5, duration);
pstmt.setLong(6, interval);
pstmt.executeUpdate();
conn.commit();
} catch (BatchUpdateException ex) {
System.out.println("Batch Update Exception: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
throw ex;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
throw ex;
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException sqlEx) { } // ignore
pstmt = null;
}
}
HashMap<String, Integer> returner = new HashMap<String, Integer>();
returner.put("startingTimestamp", Integer.parseInt(startingTimestamp));
returner.put("skipCount", skipCount);
returner.put("minTimestamp", minTimestamp);
returner.put("maxTimestamp", maxTimestamp);
returner.put("duration", duration);
return returner;
}
}