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