// ==UserScript==
// @name NPC tracker for GoC
// @namespace http://gatesofcamelot.com/
// @description Track NPC encoutner names
// @author Azander
// @include *.gatesofcamelot.com/*
// @exclude *gatesofcamelot.com/forum*
// @exclude *gatesofcamelot.com/itemguide*
// @version 1.0.0
// @grant GM_addStyle
// @grant GM_log
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @downloadURL http://users.netonecom.net/~azander/greasemonkey/GoC_NPC_tracker.user.js
// @updateURL http://users.netonecom.net/~azander/greasemonkey/meta/GoC_NPC_tracker.meta.js
// ==/UserScript==
//Handle Menu bar additions
GM_registerMenuCommand( "Reset NPC Statistics", clear_stats );
//-------------------------------------------------------------//
//Generic lookup fuction for scraping a site//
var xMult = XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE;
var xSing = XPathResult.FIRST_ORDERED_NODE_TYPE;
var xOrd = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE;
function xpath(query,mode) {
return document.evaluate(query, document, null, mode, null);
}
//------------------------------------------------------------//
GM_addStyle(".NPCWindow { position:absolute; display:none; top:75px; left:50px; z-index:50; width:620px; height:465px; overflow-y:auto; border:1px solid #FFCC00; background-color:black; }");
function clear_stats(dummy) {
count=GM_getValue('NPCcount',0);
for(i=0; i<=count; i++) {
GM_setValue('NPC'+i,'');
}
GM_setValue('NPCcount',0);
GM_setValue('LastHit','');
}
//DefineRegular Expressions
var re_encounter = new RegExp("npcencounter");
var re_space = new RegExp("travel");
var re_win = /(.*) is defeated/i;
var re_loss = /(.*) defeated you/i;
var re_fleet= /took \$(.*) in tax/i;
var re_cargo = /leave the \$(.*) silver/i;
var re_item = /You found a (.*)/i;
var re_itemid = /javascript:itemWindowFunction\(([0-9]+)\)/i;
var re_points = /You gained (.+) points/i;
//define variables
var itm = '';
var npcs = Array();
var npcs_short = Array();
var temp ='';
//Build NPC arrays
var lasthit=GM_getValue('LastHit','0,0,0'); //lastHit: idx,cargo,points
var count=parseInt(GM_getValue('NPCcount','0'));
for(i=0; i<=count; i++) {
data = GM_getValue('NPC'+i,'');
if(data != "") {
areas = data.split(',');
npcs[i]=areas;
temp=parseInt(areas[0].charCodeAt(0));
if (npcs_short[temp] == null){
npcs_short[temp]= Array('blank');
}
npcs_short[temp].push([areas[0],i]);
}
}
//Select the index id for an already seen NPC or 0 for new
function select_npc(npcs, count, query) {
if(count <1) { return 0; }
var first_letter_code = parseInt(query.charCodeAt(0));
var search_array = npcs_short[first_letter_code];
if (npcs_short[first_letter_code] == null ){
return 0;
}
var i = search_array.length;
while (i--) {//testing
if (search_array[i][0] == query){
return search_array[i][1];
}
}
return 0;
}
// Places commas in the appropriate places for numbers larger than 999
function addCommas(str) {
var integer, real = '';
var retstr;
var arrlength;
var numarr = str.split('.');
integer = numarr[0].toString();
if (numarr.length > 1) {
real = numarr[1].toString();
}
if (integer.length % 3 == 0) {
arrlength = integer.length / 3;
} else {
arrlength = parseInt(integer.length / 3 + 1);
}
var arr = new Array(arrlength)
var chrStart = integer.length - 3;
var chrEnd = integer.length;
if (chrStart < 0) {
chrStart = 0;
}
for (i = arrlength-1; i >= 0; i--) {
arr[i] = integer.slice(chrStart,chrEnd);
chrEnd = chrStart;
chrStart -= 3;
if (chrStart < 0) {
chrStart = 0;
}
}
retstr = arr.join();
if (real != '' && real != '00') {
retstr = retstr + '.' + real;
}
return retstr;
}
//define window type
if (typeof unsafeWindow != 'undefined') window = unsafeWindow;
//Deal with pages that have encounters
result = window.location.href.match(re_encounter);
if(result != null) {
td = xpath("//td[@class='contentcontent']",xOrd);
var ssID=td.snapshotLength-1;
// GM_log("ssID="+ssID);
if(td.snapshotItem(ssID) != null) {
// GM_log(td.snapshotItem(ssID).innerHTML);
var content = td.snapshotItem(ssID).innerHTML;
var contentw = content.match(re_win);
var laststatus = '';
if(contentw != null) {
// GM_log("contentw: "+contentw);
// GM_log("content: "+content);
idx=select_npc(npcs, count, contentw[1]);
// GM_log("idx: "+idx);
if(idx >0) {
// GM_log("idx: "+idx);
count--;
var name = npcs[idx][0];
var wins = parseInt(npcs[idx][1]);
var losses = parseInt(npcs[idx][2]);
var cargo = parseInt(npcs[idx][4]);
var taxed = parseInt(npcs[idx][5]);
var xp = parseInt(npcs[idx][6]);
var items = parseInt(npcs[idx][7]);
var points = parseInt(npcs[idx][8]);
} else {
var wins = 0, losses = 0, cargo = 0, taxed = 0, xp = 0;
var items = 0, points = 0, idx = count+1;
var name = contentw[1];
}
// GM_log("name: "+name);
var EXP=content.match("earned (.*) experience");
// GM_log("EXP: "+EXP);
xp = xp+parseInt(EXP[1]);
var CARGO=content.match(re_cargo);
// GM_log("CARGO: "+CARGO);
CARGO=CARGO[1].replace(",","");
cargo=cargo+parseInt(CARGO);
var FLEET=content.match(re_fleet);
// GM_log("FLEET: "+FLEET);
if(FLEET != null) {
FLEET=FLEET[1].replace(",","");
} else {
FLEET='0';
}
taxed=taxed+parseInt(FLEET);
var ITEM=content.match(re_itemid);
if(ITEM == null) {
ITEM='';
} else {
ITEM=ITEM[1];
items++;
}
var POINTS=content.match(re_points);
if(POINTS != null) {
POINTS=POINTS[1].replace(",","");
points=points+parseInt(POINTS);
} else {
POINTS=0;
}
wins++;
laststatus = 'w';
}
var contentl = content.match(re_loss);
if(contentl != null) {
CARGO=0;
POINTS=0;
idx=select_npc(npcs, count, contentl[1]);
if(idx >0) {
count--;
var name = npcs[idx][0];
var wins = parseInt(npcs[idx][1]);
var losses = parseInt(npcs[idx][2]);
var cargo = parseInt(npcs[idx][4]);
var taxed = parseInt(npcs[idx][5]);
var xp = parseInt(npcs[idx][6]);
var items = parseInt(npcs[idx][7]);
var points = parseInt(npcs[idx][8]);
} else {
var wins = 0, losses = 0, cargo = 0, taxed = 0, xp = 0;
var items = 0, points = 0, idx = count+1;
var name = contentl[1];
}
losses++;
laststatus="l";
}
if(laststatus != "") {
resultstring=name+','+wins+','+losses+','+laststatus+','+cargo+','+taxed+','+xp+','+items+','+points;
GM_setValue('NPC'+idx,resultstring);
GM_setValue('LastHit',idx+','+CARGO+','+POINTS);
count++;
GM_setValue('NPCcount',count);
}
}
}
//create empty statistical window
var results = document.createElement("div");
results.setAttribute('class',"NPCWindow");
results.setAttribute("id", "NPC_results");
document.body.appendChild(results);
//display popup window link
regex = new RegExp('/search.php');
anchors = document.getElementsByTagName('a');
var HG=1;
for (var i = 19; i < anchors.length; i++) {
// Start at anchor #20 for performance reasons to skip early anchors.
fuel_menu=document.getElementById('menu_item_box_supplies');
var divMenu = document.createElement('span');
divMenu.innerHTML = "NPC Results
";//for high graphics mode
fuel_menu.appendChild(divMenu);
break;
}
//show results
unsafeWindow.display_npcs = function() {
window.setTimeout(function() {
lastencounter=lasthit.split(","); //lastHit: idx,cargo,points
// GM_log(lastencounter);
var lastnpc=npcs[lastencounter[0]];
var lastcargo=parseInt(lastencounter[1]);
var lastpoints=parseInt(lastencounter[2]);
var lastitem=lastencounter[3];
if(lastnpc[3]!='l') {
laststatus='win';
} else {
laststatus='loss';
}
var output = '';
output += '
NPC Results';
// output += "
";
// output += '
';
output += '
[Close]';
output += '
';
output += '
';
output += '
';
output += 'Last encountered: | ';
output += '#'+lastencounter[0]+' ';
output += ''+lastnpc[0]+' |
';
output += 'Last status: | '+laststatus+' |
';
if(lastcargo>0) {
output += 'Last silver: | $'+lastcargo+' |
';
}
if(lastpoints>0) {
output += 'Last points: | '+lastpoints+' |
';
}
output += '
';
output += '
';
output += '
';
output += '';
output += '# | NPC Name | Wins | Losses | Last W/L | Silver | Tax | Exp | Itms | Pnts |
';
// output += '# | NPC Name | Wins | Losses | Last W/L | Cargo | Taxed | Exp | Items | Points |
';
// create a place to display the results once we have gotten them from storage
for(i=1; i<= count; i++) {
area=npcs[i];
output+='';
output+=''+i+' | ';
output+=''+area[0]+' | ';
output+=''+area[1]+' | ';
output+=''+area[2]+' | ';
output+=''+area[3]+' | ';
output+=''+area[4]+' | ';
output+=''+area[5]+' | ';
output+=''+area[6]+' | ';
output+=''+area[7]+' | ';
output+=''+area[8]+' | ';
output+='
';
}
output += '
';
output += '
';
output += '
';
output += 'NPC Results';
output += '
[Close]';
output += '
';
// output += '
';
var d = document.getElementById('NPC_results');
d.innerHTML = output;
d.style.display = "block";
d.style.width = "750px";
}, 0);
} //window.display.npcs