pepa / StargateStavby

// ==UserScript==
// @name         StargateStavby
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Pomocník pro snadnější zástavbu planet
// @author       Marek Simunek
// @match        https://www.stargate-game.cz/stavby.php*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';
// ----------------------variables------------------------------------------------------
    var keys  = ['park', 'bs', 'sdi', 'po'];
    var defaultMinValues = [4000, 500, 500, 200];
    var defaultWantedValues = [4000, 500, 500, 200];
    var settings = {};

// ----------------------functions -----------------------------------------------------
    function setBuildingValues (){
        var i, key, len;
        for (i = 0, len = keys.length; i < len; i++) {
            key = keys[i];
            var buildingVal = {
                'min' : GM_getValue(key + 'min') || defaultMinValues[i],
                'wanted': GM_getValue(key) || defaultMinValues[i],
                'destroy' : false
            };
            settings[key] = buildingVal;
        }
        settings['park'].destroy = true;
        console.log(settings);
    }
/**
Find elements and set wanted values if minValue is reached
placeholder is type of building
*/
    function createBuildingInput(querySelector, minValueToBuild, wantedValue, placeholder){
        var actualValue =document.querySelector(querySelector).value;
        var actualInfo = `<p class="info">Postaveno <span class="neutralni">${actualValue}</span></p>`;
        document.querySelector(querySelector).nextElementSibling.insertAdjacentHTML('afterend', actualInfo);
        if(parseInt(actualValue.replace('.','')) < parseInt(minValueToBuild)){
            document.querySelector(querySelector).value = wantedValue;
        }

        var inputString = `<div>
<div style='display:inline-block; width:170px;'>Pokud je <label>${placeholder}</label> méně než</div>
<input class='variable-to-save' data-id='${placeholder}min' type="number"style="margin:10px;" placeholder='${placeholder}' value='${minValueToBuild}'/>
tak nastav
`;
        var inputStringMin = `
<input class='variable-to-save' data-id='${placeholder}' type="number"style="margin:10px;" placeholder='${placeholder}' value='${wantedValue}'/>
</div>`;
        document.querySelector('form').insertAdjacentHTML('afterend', inputString + inputStringMin);
    }

    function createBuildingInputWithDestroying(querySelector, minValueToBuild, wantedValue, placeholder){
        var actualVal = parseInt(document.querySelector(querySelector).value.replace('.',''));
        if(actualVal <= parseInt(minValueToBuild)){
            document.querySelector('input#vyrobna').value = actualVal + parseInt(document.querySelector('input#vyrobna').value.replace('.',''))  - parseInt(wantedValue);
        }
        createBuildingInput(querySelector, minValueToBuild, wantedValue, placeholder);
    }

    function saveValues(){
        var inputs = document.querySelectorAll('.variable-to-save');
        var i, item, len;
        for (i = 0, len = inputs.length; i < len; i++) {
            item = inputs[i];
            GM_setValue(item.dataset.id, item.value);
            console.log(item.dataset.id+ " "+ GM_getValue(item.dataset.id));
        }
        location.reload();

    }

    function createInputSaveBtn(){
        var inputString = `<div ><input id='save-values-btn' type="submit" value="Ulozit hodnoty"/></div>`;
        document.querySelector('form').insertAdjacentHTML('afterend', inputString);
        document.getElementById ("save-values-btn").addEventListener ("click", saveValues, false);
    }

    function refresh(){
        setBuildingValues();
        var i, key, len;
        var queryEl = 'input#';

        for (i = 0, len = keys.length; i < len; i++) {
            key = keys[i];
            if(settings[key].destroy){
                createBuildingInputWithDestroying(queryEl +key, settings[key].min, settings[key].wanted, key);
            }else{
                createBuildingInput(queryEl + key, settings[key].min,  settings[key].wanted, key);
            }
        }
    }
    //bind key P to build button
    function doc_keyUp(e) {
        switch (e.keyCode) {
            case 80:
                document.querySelector('input[name="postavit"]').click();

                break;
        }
    }

// ------------------- program------------------------------------------

    createInputSaveBtn();
    refresh();
    document.addEventListener('keyup', doc_keyUp, false);
})();