· 8 years ago · Feb 18, 2018, 10:14 PM
1// ==UserScript==
2// @name Talibri - Pirion's Combat Stat Tracker
3// @namespace http://talibri.pirion.net/
4// @version 0.1
5// @description This is used to get a list of inventory items.
6// @author Kaine "Pirion" Adams
7// @match https://talibri.com/combat_zones/*
8// @include https://talibri.com/combat_zones/*
9// @grant none
10// ==/UserScript==
11
12var combat = {
13 data: null,
14 is_stopped: false,
15 actionDictionary: { skills: [
16 {name: "Stab",successful: true, text: "You lunged at the enemy stabbing them"},
17 {name: "Stab",successful: false, text: "You attempted to use Stab"},
18 {name: "Shock Strike",successful: true, text: "You channel lightning energy into your blade"},
19 {name: "Shock Strike",successful: false, text: "You attempted to use Shock Strike"},
20 {name: "Lightning Defense",successful: true, text: "Lightning courses through your body"},
21 {name: "Lightning Defense",successful: false, text: "You attempted to use Lightning Defense"},
22 {name: "Dash",successful: true, text: "You dash into the enemy's defenses"},
23 {name: "Bash",successful: true, text: "You bash the enemy"},
24 {name: "Bash",successful: false, text: "You attempted to use Bash"},
25 {name: "ShieldBash",successful: true, text: "You bashed the enemy with your shield"},
26 // {name: "ShieldBash",successful: false, text: "SB HAS 100% ACC"},
27 {name: "Shout",successful: true, text: "You shout at the enemy building your adrenaline"},
28 {name: "Fiery Strike",successful: true, text: "You cover your weapon in oil and light it ablaze before striking the enemy"},
29 {name: "Fiery Strike",successful: false, text: "You attempted to use Fiery Strike"},
30 {name: "Aimed Shot",successful: true, text: "You line up the shot and let your arrow fly"},
31 {name: "Aimed Shot",successful: false, text: "You attempted to use Aimed Shot"},
32 {name: "Rapid Shot",successful: true, text: "One of your arrows launched in quick succession"},
33 {name: "Rapid Shot",successful: false, text: "You attempted to use Rapid Shot"},
34 {name: "Wing Clip",successful: true, text: "You aim for the enemy's weapon"},
35 {name: "Wing Clip",successful: false, text: "You attempted to use Wing Clip"},
36 {name: "Ignite",successful: true, text: "You set your enemy ablaze"},
37 {name: "Ignite",successful: false, text: "You attempted to use Ignite"},
38 {name: "Freeze",successful: true, text: "You freeze your enemy"},
39 {name: "Freeze",successful: false, text: "You attempted to use Freeze"},
40 {name: "Electrify",successful: true, text: "You Electrify your enemy"},
41 {name: "Electrify",successful: false, text: "You attempted to use Electrify"},
42 {name: "Earth Eruption",successful: true, text: "The Earth Erupts under your enemy"},
43 {name: "Earth Eruption",successful: false, text: "You attempted to use Earth Eruption"},
44 {name: "Item Failed",successful: false, text: "You are out of"},
45 ]},
46 cookiePath: function() {
47 return "/";
48 },
49 cookieName: function() {
50 return "/scripts/pirion/combat/data";
51 },
52 cookieExpirationDays: function() {
53 return 7;
54 },
55 initialize: function() {
56 //add jquery.cookie.js:
57 $('head').append('<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>');
58 $('head').append('<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>');
59 $('head').append('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />');
60 $('head').append('<script type="text/javascript">var script = script || {};</script>');
61 window.setTimeout(combat.initalizeUserInterface, 2000);
62
63 combat.start();
64 },
65 initalizeUserInterface: function() {
66 var html = "";
67 html += '<div style="width: 80%; margin-left: auto; margin-right: auto;">';
68 html += '<div style="width: 80px; margin-left: auto; margin-right: auto;">';
69 html += '<a style="width: 80px;" onclick="javascript: script.reset=true;return false;" href="#">Reset Stats</a></div>';
70 html += '<table style="width: 80%; margin-left: auto; margin-right: auto;">';
71 html += '<tr><th>Actions</th><td id="combat_actions">0</td></tr>';
72 html += '<tr><th>Fights (W/L/F)</th><td id="fights">0</td></tr>';
73 html += '<tbody><tr><th>Accuracy</th><td id="combat_accuracy">0/0</td></tr>';
74 html += '<tr><th>Exp Gained</th><td id="combat_exp">0</td></tr>';
75 html += '<tr><th>Leol Gained</th><td id="combat_leol">0</td></tr>';
76 html += '<tr><th>Exp/Tick</th><td id="combat_exp_tick">0</td></tr>';
77 html += '<tr><th>Leol/Tick</th><td id="combat_leol_tick">0</td></tr>';
78 // html += '<tr><th>Exp/Hour</th><td id="combat_exp_hour">0.00</td></tr>';
79 // html += '<tr><th>Leol/Hour</th><td id="combat_leol_hour">0.00</td></tr>';
80 combat.actionDictionary.skills.map(skill => skill.name).filter((v, i, a) => a.indexOf(v) === i).map(skill => {
81 html += `<tr style="display: none;"><th>${skill} Accuracy</th><td id="combat_${skill.replace(" ", "_")}_accuracy">0/0</td></tr>`;
82 });
83 html += '</tbody></table></div>';
84
85 $('body').append('<div id="combatTrackerDialog" title="Combat Tracking">' + html + '</div>');
86 $("#combatTrackerDialog").dialog();
87 if(!combat.data) {
88 combat.data = combat.load();
89 //if we can't check the cookie,
90 //let's skip this round so we don't overwrite it.
91 if(!combat.data) {
92 return;
93 }
94 }
95 combat.updateUI();
96 },
97 stop: function() {
98 combat.is_stopped = true;
99 },
100 start: function() {
101 //add a listener for any ajax page completed
102 $(document).ajaxComplete(combat.listen);
103 combat.is_stopped = false;
104 },
105 destory: function() {
106 //why would you ever want to do this?
107 combat.stop();
108 $.removeCookie(combat.cookieName(), {path: combat.cookiePath()});
109 },
110 reset: function() {
111 //overwrite data, and save
112 script.reset = false;
113 combat.data = combat.new();
114 combat.save();
115 combat.updateUI();
116 },
117 new: function() {
118 //generate an empty json array
119 var empty_data = {
120 version: 2,
121 update: (new Date()),
122 since: (new Date()),
123 rounds: 0,
124 win: 0,
125 loss: 0,
126 flee: 0,
127 inCombat: false,
128 leol: 0,
129 actions: {},
130 items: {},
131 monsters: {},
132 stats: {},
133 affinities: {},
134 loot: {}
135 };
136 return empty_data;
137 },
138 load: function() {
139 //if cookie exists, load, otherwise get a new array.
140 var cookie = $.cookie(combat.cookieName());
141 if(cookie) {
142 var loaded_data = $.parseJSON(cookie);
143 return combat.migrate(loaded_data);
144 }
145 return combat.new();
146 },
147 migrate: function(migration_data) {
148 //if we load, we need to make sure to reset the cookie to a good state.
149 if(migration_data.version == 1) {
150 //bug caused leol to fail. this will set it to zero.
151 migration_data.version = 2;
152 migration_data.leol = 0;
153 }
154 //if we are in combat, lets count as a flee:
155 if(migration_data.inCombat == true) {
156 migration_data.inCombat = false;
157 migration_data.flee += 1;
158 }
159 return migration_data;
160 },
161 save: function() {
162 //create cookie that expires in 7 days:
163 $.cookie(combat.cookieName(), JSON.stringify(combat.data), { path: combat.cookiePath(), expires: combat.cookieExpirationDays() });
164 },
165 listen: function(event, xhr, settings) {
166 //if we've been asked to exit, let's unbind:
167 if(combat.is_stopped) {
168 $(e.currentTarget).unbind('ajaxComplete');
169 return;
170 }
171
172 //if ajax response comes from expected location:
173 if(settings.url.indexOf('adventure/continue') > -1) {
174 if(script && script.reset) {
175 combat.reset();
176 }
177 //if data has not been initialized, initialize it.
178 if(!combat.data) {
179 combat.data = combat.load();
180 //if we can't check the cookie,
181 //let's skip this round so we don't overwrite it.
182 if(!combat.data) {
183 return;
184 }
185 }
186
187 var result = combat.getResult(xhr.responseText);
188
189 combat.logBattleRound(result);
190 }
191 },
192 getResult: function(response) {
193 var result = {
194 player: {
195 slain: false
196 },
197 monster: {
198 name: "Unknown",
199 slain: false
200 },
201 leol: 0,
202 action: {
203 type: "Nothing",
204 name: "Unknown",
205 successful: false
206 },
207 stats: {},
208 affinities: {},
209 loot: {}
210 };
211
212 if(response.indexOf('You limped back to town on the verge of death') > -1) {
213 result.player.slain = true;
214 }
215
216 responseLines = response.split(";");
217
218 for(var i = 0; i < responseLines.length; i++) {
219 var item = responseLines[i].toLowerCase();
220 if(item.indexOf("$('button:contains(\"") > -1) {
221 var userItem = item.substr(item.indexOf("$('button:contains(\"")+20);
222 result.action.type = "Item";
223 result.action.name = userItem.substr(0,userItem.indexOf(" ("));
224 result.action.successful = true;
225
226 } else if(item.indexOf("$combat_round.append") > -1 &&
227 item.indexOf("you") > -1)
228 {
229 var combatText = item.split("\"")[1];
230
231 if(combatText.indexOf("experience.") > -1) {
232 var parts = combatText.split(" ");
233 result.affinities[parts[3]] = parseInt(parts[2]);
234 } else if(combatText.indexOf("leol.") > -1) {
235 var parts = combatText.split(" ");
236 result.leol = parseInt(parts[2]);
237 } else if(combatText.indexOf("experience,") > -1) {
238 var parts = combatText
239 .replace("you gained ","")
240 .split(", ");
241 for(var j = 0; j < parts.length; j++){
242 var experienceString = parts[j].split(" ");
243 result.stats[experienceString[1]] = parseInt(experienceString[0]);
244 }
245 result.leol = parseInt(parts[2]);
246 } else if(combatText.indexOf("you killed the ") > -1) {
247 result.monster.name = combatText.replace("you killed the ","").replace("! <br/>","");
248 result.monster.slain = true;
249 } else if(combatText.indexOf(". you now have ") > -1) {
250 var itemStringParts = combatText.substr(11, combatText.indexOf("(")-11).split(" ");
251 var itemString = "";
252 for(var k = 1; k < itemStringParts.length; k++){
253 itemString += (itemString == "" ? "" : " ") + itemStringParts[k];
254 }
255 result.loot[itemString] = parseInt(itemStringParts[0]);
256 } else {
257 if(result.action.name == "Unknown") {
258 for(var l = 0; l < combat.actionDictionary.skills.length; l++) {
259 if(combatText.startsWith(combat.actionDictionary.skills[l].text.toLowerCase())) {
260 result.action = combat.actionDictionary.skills[l];
261 result.action.type = "Skill";
262 }
263 }
264 }
265 }
266
267 }
268 }
269
270 return result;
271 },
272 logBattleRound: function(result){
273 //update the data array:
274 combat.data.update = new Date();
275 combat.data.rounds += 1;
276 combat.data.leol += result.leol;
277
278 if(result.player.slain) {
279 combat.data.loss += 1;
280 }
281
282 //create action stub if not exists:
283 if(result.action.type=="Skill") {
284 if(!combat.data.actions[result.action.name]) {
285 combat.data.actions[result.action.name] = {successful: 0, unsuccessful: 0};
286 }
287
288 //count action:
289 if(result.action.successful) {
290 combat.data.actions[result.action.name].successful += 1;
291 } else {
292 combat.data.actions[result.action.name].unsuccessful += 1;
293 }
294 } else if(result.action.type=="Item") {
295 if(!combat.data.items[result.action.name]) {
296 combat.data.items[result.action.name] = {count: 1};
297 } else {
298 combat.data.items[result.action.name].count += 1;
299 }
300 }
301 //if monster was slain, count:
302 if(result.monster.slain) {
303 if(!combat.data.monsters[result.monster.name]) {
304 combat.data.monsters[result.monster.name] = 1;
305 } else {
306 combat.data.monsters[result.monster.name] += 1;
307 }
308 combat.data.inCombat = false;
309 combat.data.win += 1;
310 } else {
311 combat.data.inCombat = true;
312 }
313
314 for(var stat in result.stats)
315 {
316 if(!combat.data.stats[stat]) {
317 combat.data.stats[stat] = result.stats[stat];
318 } else {
319 combat.data.stats[stat] += result.stats[stat];
320 }
321 }
322
323 for(var affinity in result.affinities)
324 {
325 if(!combat.data.affinities[affinity]) {
326 combat.data.affinities[affinity] = result.affinities[affinity];
327 } else {
328 combat.data.affinities[affinity] += result.affinities[affinity];
329 }
330 }
331
332 for(var item in result.loot)
333 {
334 if(!combat.data.loot[item]) {
335 combat.data.loot[item] = result.loot[item];
336 } else {
337 combat.data.loot[item] += result.loot[item];
338 }
339 }
340
341 //make changes to the cookie
342 combat.save();
343 //make an update to the visible UI
344 combat.updateUI();
345 },
346 fleeBehavior: function() {
347 combat.data.flee += 1;
348 },
349 updateUI: function() {
350 var fleeButton = $(".btn.btn-large.btn-danger").get(0);
351 if (fleeButton) {
352 fleeButton.addEventListener("click", combat.fleeBehavior, false);
353 }
354
355 var ui = $('#combatTrackerDialog')[0];
356 if(!ui) {
357 return;
358 }
359 var runtime = (new Date(combat.data.update) - new Date(combat.data.since))/(3600000);
360 var successRate = (combat.data.win - (combat.data.loss + combat.data.flee)) / (combat.data.win);
361 $("#fights")[0].innerText = `${combat.data.win} | ${combat.data.loss} | ${combat.data.flee} - ${Math.round(successRate * 100)}%`;
362 var success = 0;
363 var failure = 0;
364 for(var action in combat.data.actions) {
365 var cur_success = combat.data.actions[action].successful;
366 var cur_failure = combat.data.actions[action].unsuccessful;
367 success += cur_success;
368 failure += cur_failure;
369 if (cur_success+cur_failure) {
370 $(`#combat_${action.replace(" ", "_")}_accuracy`)[0].innerText = cur_success.toString() + ' of ' + (cur_success+cur_failure).toString() + ' ('+ (Math.round(cur_success/(cur_success+cur_failure)*10000)/100).toString() + '%)';
371 $(`#combat_${action.replace(" ", "_")}_accuracy`).parent().show();
372 } else {
373 $(`#combat_${action.replace(" ", "_")}_accuracy`).parent().hide();
374 }
375 }
376 $("#combat_accuracy")[0].innerText = success.toString() + ' of ' + (success+failure).toString() + ' ('+ (Math.round(success/(success+failure)*10000)/100).toString() + '%)';
377 $("#combat_actions")[0].innerText = combat.data.rounds.toString();
378 var exp_all = 0;
379 for(var stat in combat.data.stats) {
380 exp_all += combat.data.stats[stat];
381 }
382 $("#combat_exp")[0].innerText = exp_all.toString();
383 $("#combat_leol")[0].innerText = combat.data.leol.toString();
384 $("#combat_exp_tick")[0].innerText = (Math.round(exp_all / combat.data.rounds)).toString();
385 $("#combat_leol_tick")[0].innerText = (Math.round(combat.data.leol / combat.data.rounds)).toString();
386 // $("#combat_exp_hour")[0].innerText = (Math.round(exp_all/runtime*100)/100).toString();
387 // $("#combat_leol_hour")[0].innerText = (Math.round(combat.data.leol/runtime*100)/100).toString();
388 },
389 getString: function() {
390 return JSON.stringify(combat.data);
391 },
392 log: function() {
393 console.log(combat.getString());
394 }
395};
396
397//start the script:
398combat.initialize();