KlyptoKlyptoTech.com / Dasanfall Addon

// ==UserScript==
// @name       Dasanfall Addon
// @namespace  
// @version    0.3
// @description  Adds the ability to track changes on the Planetside 2 Dasanfall leaderboards.  Information is stored in a localized Database.  Future changes may support a 3D UI and changed on information on a per player basis.
// @match      http://stats.dasanfall.com/ps2/item_leaderboard.php*
// @copyright  2014+, KlyptoTech
// @grant       none
// @downloadURL https://openuserjs.org/install/KlyptoKlyptoTech.com/Dasanfall_Addon.user.js
// @require http://yourjavascript.com/341211113161/ydn-db-dev.js
// ==/UserScript==


var main = function start() {
    //Code Injection
    
    
    //Get current time
    var currenttime = new Date().getTime();
    
    //Get current weapon ID
    function getURLParameter(name) {
        return decodeURI(
            (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
        );
    }
    var item = getURLParameter('query');
    var tbl = 'l'+item; 
    
    //Schema
    var itemleader = {
        name: tbl,
        indexes: [{
            name: 'n, tstamp',
			keyPath: ['n', 'tstamp']
        },
        {
			name: 'n',
			keyPath: 'n'
        },
        {
			name: 'tstamp',
			keyPath: 'tstamp'
        }]
    };
        
    schema = {
        stores: [itemleader]
    };
    //Initialize connection to local database
    db = new ydn.db.Storage('Dasanfall', schema);

    //Create Extra elements used later
    $('#leaderboard > thead > tr').append( '<th class="header today">CHG-K</th>' );
    $('#leaderboard > thead > tr').append( '<th class="header today">CHG-T</th>' );
    $('#leaderboard > thead > tr').append( '<th class="header today">PERIOD KPH</th>' );
    $('#leaderboard > thead > tr').append( '<th class="header yesterday">CHG-K</th>' );
    $('#leaderboard > thead > tr').append( '<th class="header yesterday">CHG-T</th>' );
    $('#leaderboard > thead > tr').append( '<th class="header yesterday">PERIOD KPH</th>' );
    
    var NoonToday = daystamp();
    var NoonYesterday = daystamp(-1);
    var NoonLastWeek = daystamp(-7);
    var Noon30 = daystamp(-30);
    var Noon60 = daystamp(-60);
    var Noon90 = daystamp(-90);
    var Noon180 = daystamp(-180);
    var Noon365 = daystamp(-365);
    var closeToday = null;
    var closeYesterday = null;
    var closeLastWeek = null;
    var closeToday = null;
    var closeToday = null;
    var closeToday = null;
    var closeToday = null;
    var DisableWrite = false;
    var NoData = true;
    
   var fetchts = db.from(tbl).select('tstamp').unique(true).list();
	fetchts.done(function(tslist) {
        if(tslist.length == 0){
            closeToday = currenttime;
            NoData = true;
        }
        else
        {
        	closeToday = getclose(tslist,NoonToday);
            closeYesterday = getclose(tslist,NoonYesterday);
            closeLastWeek = getclose(tslist,NoonLastWeek);

            NoData = false;
            
            
        }
        if(getclose([currenttime, closeToday],NoonToday) == currenttime){
            //Process New Records
			DisableWrite = false;	
        }
        else
        {
            DisableWrite = true;	
            console.log("No New Data was stored.");
        }
db = new ydn.db.Storage('Dasanfall', schema);
        $('#leaderboard > tbody > tr').each(function() {
        
                //Current Data
                var playername = this.childNodes[3].textContent;
                var killcount = $(this.childNodes[4]).attr("dv");
                var vehiclekillcount = $(this.childNodes[5]).attr("dv");
                var deaths = $(this.childNodes[7]).attr("dv");
                var acc = $(this.childNodes[11]).attr("dv");
                var hsr = $(this.childNodes[13]).attr("dv");
                var kph = $(this.childNodes[15]).attr("dv");
                var vkph = $(this.childNodes[17]).attr("dv");
                var spm = $(this.childNodes[19]).attr("dv");
                var timetotal = $(this.childNodes[20]).attr("dv"); //Stored in seconds
                var landedpk = $(this.childNodes[21]).attr("dv");
                var shotspk = $(this.childNodes[22]).attr("dv");
                
                //Object Prep
                var player = {
                    n: playername,
                    k: killcount, 
                    vk: vehiclekillcount, 
                    d: deaths, 
                    ac: acc,
                    hsr: hsr,
                    kph: kph,
                    vkph: vkph,
                    spm: spm,
                    t: timetotal,
                    lpk: landedpk,
                    spk: shotspk,
                    tstamp: currenttime
                }
               
           		//Write data to DB
                if(!DisableWrite){
                    var DBwrite = db.put(itemleader, player, [player.n, player.tstamp]);
                    DBwrite.done(function(key) {
                        //console.log('Wrote data for ' + key);
                    });
                    DBwrite.fail(function(e) {
                        throw e;
                    }); 
                }
             
        
                
                var tableline = $(this)
                
            	if(!NoData){
                    
                    key_range = ydn.db.KeyRange.only([playername, closeToday]);
                	db.values(new ydn.db.IndexValueIterator(tbl, 'n, tstamp', key_range)).done(
                        function(DataSet) {
                            DataSet = DataSet[0];
                            changekills = player.k - DataSet.k;
                            tableline.append( '<td dv="' + changekills + '">' + changekills + '</td>' );
                            changetime = player.t - DataSet.t;
                            tableline.append( '<td dv="' + changetime + '">' + changetime + '</td>' );
                            killperhour = changekills/(changetime/60/60);
                            tableline.append( '<td dv="' + killperhour + '">' + killperhour + '</td>' );
                      
                		}
					);
                    key_range = ydn.db.KeyRange.only([playername, closeYesterday]);
                	db.values(new ydn.db.IndexValueIterator(tbl, 'n, tstamp', key_range)).done(
                        function(DataSet) {
                            DataSet = DataSet[0];
                            changekills = player.k - DataSet.k;
                            tableline.append( '<td dv="' + changekills + '">' + changekills + '</td>' );
                            changetime = player.t - DataSet.t;
                            tableline.append( '<td dv="' + changetime + '">' + changetime + '</td>' );
                            killperhour = changekills/(changetime/60/60);
                            tableline.append( '<td dv="' + killperhour + '">' + killperhour + '</td>' );
                      
                		}
					);
                       
                }
                
        
                
                
                
             
        
            }); 
    
    
        })
    
    
    //SYSTEM FUNCTIONS
    function getclose(listof, closeto){
        return listof.reduce(function (prev, curr) {
            return (Math.abs(curr - closeto) < Math.abs(prev - closeto) ? curr : prev);
        });
    }
    
    function daystamp(x) {
		if(!x){
			x=0
		}
      	var d = new Date();
  		d.setHours(12);
  		d.setMinutes(0);
  		d.setSeconds(0);
  		d.setMilliseconds(0);
		d.setDate(d.getDate()+x)
  		return d.getTime();
	};
    
    //Dasanfall's table sort
    $('#leaderboard').tablesorter({ 
            headers: {
                1: { sorter: 'rangesort' },
                4: { sorter: 'rangesort' },
                3: { sorter: 'rangesort' },
                5: { sorter: 'rangesort' },
                6: { sorter: 'rangesort' },
                7: { sorter: 'rangesort' },
                8: { sorter: 'rangesort' },
                9: { sorter: 'rangesort' },
                10: { sorter: 'rangesort' },
                11: { sorter: 'rangesort' },
                12: { sorter: 'rangesort' },
                13: { sorter: 'rangesort' },
                14: { sorter: 'rangesort' },
                15: { sorter: 'rangesort' },
                16: { sorter: 'rangesort' },
                17: { sorter: 'rangesort' },
                18: { sorter: 'rangesort' },
                19: { sorter: 'rangesort' },
                20: { sorter: 'rangesort' },
                21: { sorter: 'rangesort' },
                22: { sorter: 'rangesort' },
                23: { sorter: 'rangesort' },
                24: { sorter: 'rangesort' },
                25: { sorter: 'rangesort' },
                26: { sorter: 'rangesort' },
            },
            widgets: ['zebra']
        });
    
    
    //3D Graphics Engine
    if(1 == 2){
        var xRotate = false;
        var yRotate = false;
    
        $(window).mousemove(function(e){	
            window.requestAnimationFrame( function(){
                var paneH = $(window).height();
                var paneW = $(window).width();
                var nx =0;
                var ny = 0;
                var mx = e.pageX / paneW;
                var my = e.pageY / paneH;
                    
                if(mx < 0.10){
                    nx = -(10-(mx*100));
                    // $('#outer').css('-webkit-transform', 'rotateY(-'+ nx +'deg)')
                    yRotate = true;
                }else if(mx > 0.90){
                    nx = 10-(100-(mx*100));
                    // $('#outer').css('-webkit-transform', 'rotateY('+ nx  +'deg)')
                    yRotate = true;
                }else if(yRotate){
                    nx = 0;
                    // $('#outer').css('-webkit-transform', 'rotateY('+ 0  +'deg)')
                    yRotate = false;
                }
                
                if(my < 0.10){
                    ny = 10-(my*100);
                    // $('#outer').css('-webkit-transform', 'rotateX('+ ny +'deg)')
                    xRotate = true;
                }else if(my > 0.90){
                    ny = -(10-(100-(my*100)));
                    // $('#outer').css('-webkit-transform', 'rotateX(-'+ ny  +'deg)')
                    xRotate = true;
                }else if(xRotate){
                    ny= 0;
                    //$('#outer').css('-webkit-transform', 'rotateX('+ 0  +'deg)')
                    xRotate = false;
                }
                
                $('body').css('-webkit-transform', 'rotateX('+ ny  +'deg) rotateY('+ nx  +'deg)');
                
                console.log(nx + " | " + my);
            });
        });
    }
    
            };


        var refscript = document.createElement('script');
        refscript.type = "text/javascript";
        refscript.src = "http://yourjavascript.com/783441111/ydn-custom-1-0-3.js";
        document.body.appendChild(refscript);

        // Inject our main script
        var script = document.createElement('script');
        script.type = "text/javascript";
        script.textContent = 'var db = null; var dweapons = {}; ';
        script.textContent = script.textContent + '(' + main.toString() + ')();';
        document.body.appendChild(script);