McBelzebuth / ScriptKerknok

// ==UserScript==
// @name ScriptKerknok
// @namespace http://tampermonkey.net/
// @description Script for lol
// @author McBelzebuth
// @copyright 2017, McBelzebuth (https://openuserjs.org/users/McBelzebuth)
// @license MIT
// @version 0.6
// @updateURL https://openuserjs.org/meta/McBelzebuth/ScriptKerknok.meta.js
// @downloadURL https://openuserjs.org/src/scripts/McBelzebuth/ScriptKerknok.user.js
// @include http://landsoflords.com/*
// @include http://www.landsoflords.com/*
// @include https://landsoflords.com/*
// @include https://www.landsoflords.com/*
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// ==/UserScript==

// ==OpenUserJS==
// @author McBelzebuth
// ==/OpenUserJS==

(function() {
    var debug = true;
    var script = {
        domain: '',
        gold: '0',
        user: '',
        hab: '0',
        date: '',
        happyness: '0',
        dist: '0',
        start: function() {
            this.checkPage();
        },
        checkPage : function() {
            this.log("CheckPage");
            var limit = true;
            var tmp;
            var user;
            var time;
            var date;
            var dateRet;
            var dateString;
            var year;
            if (window.location.pathname.indexOf("/mgmt/") > -1 && window.location.pathname.indexOf("/res:") > -1) {
                tmp = window.location.pathname.substr(window.location.pathname.indexOf('/mgmt/')+6);
                this.domain = tmp.substr(0,tmp.indexOf('/'));
                user = document.getElementById('user');
                this.user = user.getAttribute('title');
                date = new Date();
                dateRet = date.toUTCString();
                dateString = "";
                dateString += date.getDate() + "/";
                dateString += (date.getMonth() + 1) + "/";
                dateString += date.getFullYear();
                this.date = this.date_diff_indays("30/12/1899",dateString);
                var keyM = this.domain+'money';
                if (limit && GM_getValue(keyM) && this.date == GM_getValue(keyM))
                    this.log('money already exported this day');
                else
                {
                    this.log('money not exported this day');
                    GM_setValue(keyM, this.date);
                    this.getMoney();
                }
            }
            else if (window.location.pathname.indexOf("/mgmt/") > -1 && window.location.pathname.indexOf("/units") > -1) {
                tmp = window.location.pathname.substr(window.location.pathname.indexOf('/mgmt/')+6);
                this.domain = tmp.substr(0,tmp.indexOf('/'));
                user = document.getElementById('user');
                this.user = user.getAttribute('title');
                date = new Date();
                dateRet = date.toUTCString();
                dateString = "";
                dateString += date.getDate() + "/";
                dateString += (date.getMonth() + 1) + "/";
                dateString += date.getFullYear();
                this.date = this.date_diff_indays("30/12/1899",dateString);
                var keyH = this.domain+'hab';
                if (limit && GM_getValue(keyH) && this.date == GM_getValue(keyH))
                    this.log('hab already exported this day');
                else
                {
                    this.log('hab not exported this day');
                    GM_setValue(keyH, this.date);
                    this.getHab();
                }
            }
            else if (window.location.pathname.indexOf("/map/") > -1) {
                if (window.location.pathname.length > 15)
                {
                    var menu = document.getElementsByClassName("content");
                    var inputX = GM_getValue("inputX") ? GM_getValue("inputX") : 0;
                    var inputY = GM_getValue("inputY") ? GM_getValue("inputY") : 0;
                    var coord = window.location.pathname.substr(window.location.pathname.indexOf("/map/")+5,window.location.pathname.length-window.location.pathname.indexOf("/map/")+5);
                    var Ax = coord.substr(0,coord.indexOf("W"));
                    var Ay = coord.substr(coord.indexOf("W")+1,coord.length-2-coord.indexOf("W"));
                    this.dist = Math.round(Math.sqrt((Ax-inputX)*(Ax-inputX)+(Ay-inputY)*(Ay-inputY)));//Math.abs(Ax-inputX)+Math.abs(Ay-inputY);
                    var html = "<p>You are located at "+this.dist+" acres</p></div>";
                    html += menu[1].innerHTML;
                    html += "<div><input  style=\"width:90%\" type=\"submit\" value=\"submit actual case\" id=\"submit_case\">";
                    html += "<p><span><input id=\"inputX\" value=\""+inputX+"\" style=\"width: 35%\" name=\"inputX\" type=\"number\"></span><input id=\"inputY\" value=\""+inputY+"\" style=\"width: 35%\" name=\"inputY\" type=\"number\">";
                    html += "</p><input  style=\"width:90%\" type=\"submit\" value=\"submit\" id=\"submit_distance\"></div>";

                    menu[1].innerHTML = html;
                    document.getElementById('submit_distance').addEventListener('click', this.calcul_distance, false);
                    document.getElementById('submit_case').addEventListener('click', this.calcul_case, false);
                }
            }
        },
        calcul_distance: function() {
            if (document.getElementById('inputX').value)
                GM_setValue("inputX", document.getElementById('inputX').value);
            else
                GM_setValue("inputX", 0);
            if (document.getElementById('inputY').value)
                GM_setValue("inputY", document.getElementById('inputY').value);
            else
                GM_setValue("inputY", 0);
        },
        calcul_case: function() {
            var coord = window.location.pathname.substr(window.location.pathname.indexOf("/map/")+5,window.location.pathname.length-window.location.pathname.indexOf("/map/")+5);
            var Ax = coord.substr(0,coord.indexOf("W"));
            var Ay = coord.substr(coord.indexOf("W")+1,coord.length-2-coord.indexOf("W"));
            if (Ax)
                GM_setValue("inputX", Ax);
            else
                GM_setValue("inputX", 0);
            if (Ay)
                GM_setValue("inputY", Ay);
            else
                GM_setValue("inputY", 0);
        },
        date_diff_indays: function(date1, date2) {
            dt1 = new Date(date1.split('/')[2],date1.split('/')[1],date1.split('/')[0]);
            dt2 = new Date(date2.split('/')[2],date2.split('/')[1],date2.split('/')[0]);
            return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
        },
        getMoney: function() {
            var golds = document.getElementById('money');
            this.gold = golds.innerText.replace('.',',');
            this.postToGoogleMoney();
        },
        getHab: function() {
            var x = document.getElementsByClassName("mgmtinfo");
            var span = x[0].getElementsByTagName("span");
            this.hab = span[0].innerHTML;
            span = x[2].getElementsByTagName("span");
            var happyness = span[0].innerHTML;
            this.happyness = happyness.replace('%','');
            this.postToGoogleHab();
            this.postToGoogleHappyness();
        },

        log: function(data) {
            if (debug) console.log(data);
        },

        postToGoogleMoney: function(data) {
            $.ajax({
                url: "https://docs.google.com/forms/d/1xbo9RtwvxfNnb3LWbk7s0s3Qm22jExfXkw6_0xPqOwo/formResponse",
                data: {"entry.1654479590": this.user,
                       "entry.1139378047": this.domain,
                       "entry.853885129": this.gold,
                       "entry.1620731810": this.date},
                type: "POST",
                dataType: "xml",
                statusCode: {
                    0: function() {
                        this.log("datas send to google form");
                    },
                    200: function() {
                        this.log("Error to send datas");
                    }
                }
            });
        },
        postToGoogleHab: function(data) {
            $.ajax({
                url: "https://docs.google.com/forms/d/13cIbMFZ0n2lvmQDZaEwQ3SWRiz0YQTbM70zJmPqCndo/formResponse",
                data: {"entry.1875594956": this.user,
                       "entry.291494610": this.domain,
                       "entry.1965609127": this.hab,
                       "entry.1383645315": this.date},
                type: "POST",
                dataType: "xml",
                statusCode: {
                    0: function() {
                        this.log("datas send to google form");
                    },
                    200: function() {
                        this.log("Error to send datas");
                    }
                }
            });
        },
        postToGoogleHappyness: function(data) {
            $.ajax({
                url: "https://docs.google.com/forms/d/1ZgyyieJTmZmRSxJZb1cRH2Y8eTbZiTSLQUKiMFgABqc/formResponse",
                data: {"entry.655181608": this.user,
                       "entry.1187718450": this.domain,
                       "entry.1774053714": this.happyness,
                       "entry.123012134": this.date},
                type: "POST",
                dataType: "xml",
                statusCode: {
                    0: function() {
                        this.log("datas send to google form");
                    },
                    200: function() {
                        this.log("Error to send datas");
                    }
                }
            });
        }
    };
    script.start();
})();