// ==UserScript==
// @name Splat Tracker for GoC
// @namespace http://www.gatesofcamelot.com/
// @description Tracks splats based on event log
// @version 1.1.0
// @include http://www.gatesofcamelot.com/events.php*
// @include http://www.gatesofcamelot.com/sendpoints.php*
// @downloadURL http://users.netonecom.net/~azander/greasemonkey/GoC_splat_tracker.user.js
// @updateURL http://users.netonecom.net/~azander/greasemonkey/meta/GoC_splat_tracker.meta.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_log
// @grant GM_deleteValue
// ==/UserScript==
var PID = new RegExp("(gatesofcamelot)", 'i');
var loc = window.location.href;
if(!PID.test(loc)) {
exit;
}
function _setValue(whatToSet,setValue) {
if (typeof(localStorage) == 'undefined' ) {
alert('Your browser does not support HTML5 localStorage. Try upgrading.');
} else {
try {
localStorage.setItem(whatToSet, setValue); //saves to the database, �key�, �value�
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert('Quota exceeded!'); //data wasn�t successfully saved due to quota exceed so throw an error
}
}
}
}
function _getValue(whatToGet,defaultValue) {
var refVal = "";
if (typeof(localStorage) == 'undefined' ) {
alert('Your browser does not support HTML5 localStorage. Try upgrading.');
} else {
try {
retVal= localStorage.getItem(whatToGet);
} catch (e) {
alert('ERROR: '+e+'!'); //data wasn�t successfully saved due to quota exceed so throw an error
}
if(retVal == null) {
retVal=defaultValue;
}
}
return retVal;
}
function _deleteValue(whatToDelete) {
if (typeof(localStorage) == 'undefined' ) {
alert('Your browser does not support HTML5 localStorage. Try upgrading.');
} else {
try {
localStorage.removeItem(whatToDelete);
} catch (e) {
alert('Error: '+e); //data wasn�t successfully deleted
}
}
}
function _log(whatToLog) {
if (window.opera) {
opera.postError(whatToLog);
} else if (window.chrome) {
window.console.log(whatToLog);
} else {
GM_log(whatToLog);
}
}
var debug = false;
var windowcreated = false;
var lastrefresh = _getValue("lastrefresh", "t19700101000001");
var highestid = _getValue("HighestID", 1);
var lowestid = _getValue("LowestID", 0);
var timestring;
if (window.location.href.match(/events.php/)) {
processSplats();
}
else if (window.location.href.match(/sendpoints.php/)){
processPoints();
}
function processPoints() {
//You have successfully transferred 1 points to Soulsnatcher.
var point_re_match = /You have successfully transferred ([0-9]+) point/;
var point_re_data = /profiles.php\?id=([0-9]+)[^>]+>([^<]+)/;
var blocks = xpath("//tr/td[@class='contentcontent']");
// _log("In processPoints: " + blocks.snapshotLength + " blocks found");
for( var i = 0; i < blocks.snapshotLength; i++ ) {
var block = blocks.snapshotItem(i);
// _log("In processPoints block loop (" + i + ")");
var html = block.innerHTML.replace(/[\xA0\x20\r\n\s]+/mg, " ");
var point_result = html.match(point_re_match);
if (point_result != null) {
// _log("Found point: " + point_result[0] + " -- " + point_result[1]);
var points = parseInt(point_result[1]);
var profile_result = html.match(point_re_data);
if (profile_result != null) {
var playerid = profile_result[1];
// _log("Found profile: " + profile_result[0] + " -- " + profile_result[1]);
var playerhits = _getValue("Splat"+playerid, "0");
if(playerhits != "0") {
// _log("Found splats: " + playerhits);
var totalpoints = parseInt(_getValue("SplatPoint"+playerid, "0"));
totalpoints += points;
_setValue("SplatPoint"+playerid, totalpoints);
}
}
break;
}
}
}
function processSplats() {
var eventlist = xpath("//tr//td[@colspan='3']|//td[@class='textm']");
// _log(eventlist.snapshotLength);
var timestamp_re = /(January|February|March|April|May|June|July|August|September|October|November|December) ([0-9]{1,2}), (20[0-9]{2}) (1?[0-9]):([0-5][0-9]):([0-5][0-9])(am|pm)/;
var splat_re_match = /You gained ([0-9]+) experience/;
var splat_re_data = /profiles.php\?id=([0-9]+)[^>]+>([^<]+)/;
var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
var lasttype = 1; // not timestamp
var output = "", timestamp = "";
var lasthittimestamp = _getValue("LastStamp", "t19700101000001");
var newtimestamp = "t19700101000001";
var newsplats = 0;
for( var i = 1; i < eventlist.snapshotLength; i++ ) {
// _log(i+") Timestamp="+timestamp+"\r\nnewtimestamp="+newtimestamp+"\r\nlasthittimestamp="+lasthittimestamp+"\r\neventlist.snapshitItem("+i+")="+eventlist.snapshotItem(i).innerHTML);
// alert("lasttype:"+lasttype);
if(timestamp > newtimestamp) { newtimestamp = timestamp; }
if(lasttype == 0) {
// the last one was a timestamp, look for player name
// but first, was the last time stamp we took (last cycle) higher than our 'last recording was made' time?
// if so, process, if not we're done, since the other events are older than the one we've just done.
if(timestamp > lasthittimestamp) {
var thisevent = eventlist.snapshotItem(i);
var html = thisevent.innerHTML.replace(/[\xA0\x20\r\n\s]+/mg, " ");
// _log("html:"+html);
if(splat_re_match.test(html)) {
var result = html.match(splat_re_data);
if(result != null) {
var playerid = parseInt(result[1]);
var playername = result[2];
output += "player id " + playerid + "\n ";
output += "player name " + playername + "
";
}
if(playerid > highestid) {
highestid = playerid;
if(lowestid == 0) { lowestid = playerid; }
}
if(playerid < lowestid) {
lowestid = playerid;
}
var playerdetails = _getValue("Splat"+playerid, "0");
var playerhits = parseInt(playerdetails);
//playertimestamp = playerdetails.substr(String(playerhits).length + 1, 15);
if(lasthittimestamp < timestamp) {
playerhits++;
playerstring = playerhits + "," + playername;
_setValue("Splat"+playerid, playerstring);
output += playerstring + " ";
output += "event was successful
";
newsplats++;
}
}
// lasttype = 1;
} else {
// break;
// lasttype=0;
}
lasttype = 1;
} else {
// last one was not timestamp, look for timestamp
//output = ""; // clear result
var thisevent = eventlist.snapshotItem(i);
var html = thisevent.innerHTML;
// _log("Timestamp html:"+html);
html = html.replace(/\s+/g, " ");
var tresult = html.match(timestamp_re);
if(tresult != null) {
timestamp = "t" + tresult[3];
for ( var j = 0; j < 12; j++) {
if(months[j] == tresult[1]) {
var numstr = ("0" + String(j+1));
timestamp += numstr.substr(numstr.length - 2);
break;
}
}
numstr = ("0" + String(tresult[2]));
timestamp += numstr.substr(numstr.length - 2);
var hour = parseInt(tresult[4]);
if(hour == 12) {
// 12am = hour 0
// 12pm = hour 12 (don't change it)
if(tresult[7] == "am") {
hour = 0;
}
}
else if(tresult[7] == "pm") {
hour += 12;
}
numstr = ("0" + String(hour));
timestamp += numstr.substr(numstr.length - 2) + tresult[5] + tresult[6];
/*
tresult[1] = month
tresult[2] = day
tresult[3] = year
tresult[4] = 12-hour hour
tresult[5] = minute zero padded
tresult[6] = second zero padded
tresult[7] = am/pm
*/
output += "event at ";
output += timestamp + " (";
for ( var j = 1; j <= 6; j++) {
output += tresult[j] + " ";
}
output += ")
";
}
lasttype = 0;
}
}
_setValue("HighestID", highestid);
_setValue("LowestID", lowestid);
var newoutput = document.createElement("div");
newoutput.setAttribute("id", "splat_results");
newoutput.setAttribute('style', 'float:right; font-size:10pt');
var newhtml;
if (newsplats > 0) {
if (newsplats == 1) {
newhtml = "1 new splat";
} else {
newhtml = newsplats + " new splats";
}
} else {
newhtml = "No new splats";
}
if(lastrefresh > "t1971") {
timestring = lastrefresh.substr(1,4) + "-" + lastrefresh.substr(5,2) + "-" + lastrefresh.substr(7,2) + " "
+ lastrefresh.substr(9,2) + ":" + lastrefresh.substr(11,2) + ":" + lastrefresh.substr(13,2);
newhtml += ' See splats since ' + timestring + '';
} else {
newhtml += ' See all splats';
}
newoutput.innerHTML = newhtml;
var td = xpath("//td[@class='contenthead']");
for( i = 0; i < td.snapshotLength; i++) {
var item = td.snapshotItem(i);
if(item.innerHTML.match(/Event Log/)) {
item.appendChild(newoutput);
break;
}
}
if(newtimestamp > "t19700101000001") {
_setValue("LastStamp", newtimestamp);
}
if (debug) {
var results = document.createElement("div");
results.setAttribute("id", "splat_debug");
results.innerHTML = output;
var cc = xpath("//td[@class='contentcontent']");
if (cc.snapshotLength > 0) {
cc.snapshotItem(0).appendChild(results);
}
}
}
function xpath(query) {
return document.evaluate(query, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}
unsafeWindow.resetstats = function() {
window.setTimeout(function() {
var dothis = confirm("Are you sure you want to reset stats?");
if(dothis == true) {
var highestid = _getValue("HighestID", 1);
var lowestid = _getValue("LowestID", 0);
_setValue("HighestID", 1);
_setValue("LowestID", 0);
for (i = lowestid; i <= highestid; i++) {
var idrequest = "Splat" + i;
var details = _getValue(idrequest, "-1");
if(details != "-1") {
_deleteValue(idrequest);
}
idrequest = "SplatPoint" + i;
details = _getValue(idrequest, "-1");
if(details != "-1") {
_deleteValue(idrequest);
}
}
_setValue("lastrefresh", _getValue("LastStamp", "t19700101000001"));
var results = document.getElementById('results_window_body');
results.innerHTML = "Values have been reset!";
//alert("Values have been reset!");
}
}, 0);
}
unsafeWindow.displaysplatresults = function() {
window.setTimeout(function() {
if (windowcreated) {
document.getElementById('results_window').style.display = 'block';
} else {
var finaloutput = '
Player | Splats | Points |
---|---|---|
' + playername + ' | ' + splats + ' | ' + points + ' |