DH3 KindaSafe Cheat

The cheat script for DH3

当前为 2020-08-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         DH3 KindaSafe Cheat
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  The cheat script for DH3
// @author       Lasse Brustad
// @match        https://dh3.diamondhunt.co/
// @grant        none
// ==/UserScript==

/* jshint esversion:6 */

(function() {
    'use strict';

    // config for cheats
    const cheats = {
        // cheat 1 - auto smelt ores, click the ore under the mining tab to choose
        // will automatically fill the furnace to it's maximum capacity
        autoSmelt: true,
        // cheat 2 - auto chop trees, it's fully automated with a tiny issue that doesn't matter
        autoChop: true,
        // cheat 3 - auto consume stardust potions you have
        autoDrinkSD: true,
        // cheat 4 - auto send/collect the Oxygen Tank, you can't get any fish at all with this on
        autoOxy: true
    };

    // config for legal mods
    const mods = {
        // mod 1 - right click the axe to chop down all fully grown trees
        chopTrees: true,
        // mod 2 - right click the rake to harvest all the ready farms
        harvestCrops: true
    };

    /**
     * Code below here is functional code, but you can enable/disable everything in the configs above
     *
     * Remember, cheats is safe to use as it is, but if you mess with the code,
     * don't blame me if get banned! Cheats is ofc not allowed, but who cares?
     */

    // cheat 1 - auto smelt ores
    if (cheats.autoSmelt) {
        window.smeltOre = localStorage.getItem('lbdata-smeltOre') || 'copper'; // the ore to smelt
        window.addEventListener('unload', e => {
            localStorage.setItem('lbdata-smeltOre', window.smeltOre);
        });
    }

    const ores = {
        sand: {
            oil: 1,
            time: 1
        },
        copper: {
            oil: 1,
            time: 1
        },
        iron: {
            oil: 5,
            time: 5
        },
        silver: {
            oil: 20,
            time: 20
        },
        gold: {
            oil: 100,
            time: 60
        },
        promethium: {
            oil: 1000,
            time: 200
        }
    };

    function cheatSmelt() {
        if (typeof ores[window.smeltOre] !== 'object') return console.log(`DH3 KindaSafe Cheat - Unknown ore: "${window.smeltOre}"`);
        function doit() { // TODO: check if it's enough oil too to smelt it
            const hasAmount = parseInt(window['var_' + window.smeltOre]);
            if (window.isSmelting()) {
                // wait till current smelting is over
                console.log('isSmelting() === true');
                safeDelay(doit, (window.var_smeltingRequestedAmount - window.var_smeltingCurrentAmount) * window.var_smeltingNeededTimer * 1e3);
                return;
            }

            if (isNaN(hasAmount) || hasAmount < parseInt(window.var_furnaceCapacity) || (ores[window.smeltOre].oil * 1e3) > parseInt(window.var_oil)) {
                // wait even longer when stuff is missing
                safeDelay(doit, 6e3);
                return;
            }

            console.log(`Starts smelting ${window.var_furnaceCapacity} ${window.smeltOre}s!`);
            window.smelt(window.smeltOre, window.var_furnaceCapacity);
            safeDelay(cheatSmelt, ores[window.smeltOre].time * window.var_furnaceCapacity * 1e3);
        }

        safeDelay(doit);
    }

    function clicksItemMod() {
        const orig = window.clicksItem;
        window.clicksItem = (item, ...args) => {
            if (typeof ores[item] === 'object') {
                window.smeltOre = item;
            }
            orig(item, ...args);
        };
    }

    // cheat 2 - auto chop trees
    function cheatChop() {
        const treeData = {
            click(n) {
                console.log(`Auto Chop - Chopping patch ${n}`);
                document.getElementById(`tree-section-${n}`).click();
                setTimeout(() => window.closeDialogue('dialogue-confirm'), 1e3);
            },
            timer(n) {
                return window[`var_treeTimer${n}`] * 1;
            },
            isActive(n) {
                return window[`var_tree${n}`] !== 'none';
            }
        };

        const tasks = [null, false, false, false, false, false, false];
        function chop(num, waitMinSec = 0) {
            if (tasks[num]) return;
            tasks[num] = true;
            safeDelay(() => {
                tasks[num] = false;
                if (!treeData.isActive(num)) return;
                treeData.click(num);
            }, waitMinSec * 1e3);
        }

        function loop() {
            for (let i = 1; i < tasks.length; i++) {
                const timeLeft = treeData.timer(i);
                if (timeLeft < 10) chop(i, timeLeft);
            }
        }
        setInterval(loop, 1e4);
    }

    // cheat 3 - auto drink stardust potion
    let willConsumeSDPot = false;
    function autoSDPot() {
        const timeLeft = parseInt(window.var_stardustPotionTimer);
        if (timeLeft <= (5 * 60) && parseInt(window.var_stardustPotion) > 0 && !willConsumeSDPot) {
            willConsumeSDPot = true;
            safeDelay(() => {
                window.sendBytes('DRINK=stardustPotion');
                setTimeout(() => {
                    willConsumeSDPot = false;
                }, 3e3);
            }, Math.floor(Math.random() * 60) * 1e3);
        }
    }

    // cheat 4 - auto oxygen tank
    function cheatOxygenTank() {
        const oxyTimeLeft = window.getItem("oxygenTankTimer");
        switch (oxyTimeLeft) {
            case 0:
                window.sendBytes("SEND_BOAT=oxygenTank");
                break;
            case 1:
                window.sendBytes("COLLECT_BOAT=oxygenTank");
                break;
        }
        safeDelay(cheatOxygenTank, oxyTimeLeft * 1e3 + 1e3);
    }

    function initCheats() {
        if (cheats.autoSmelt) {
            cheatSmelt();
            clicksItemMod();
            console.log('Initialized DH3 KindaSafe Cheat: Auto Smelt');
        }

        if (cheats.autoChop) {
            cheatChop();
            console.log('Initialized DH3 KindaSafe Cheat: Auto Chop');
        }

        if (cheats.autoDrinkSD) {
            setInterval(autoSDPot, 2e3);
            console.log('Initialized DH3 KindaSafe Cheat: Auto Drink Stardust Potion');
        }

        if (cheats.autoOxy) {
            cheatOxygenTank();
            console.log('Initialized DH3 KindaSafe Cheat: Auto Oxygen Tank');
        }
    }

    // mod 1 - RightClick axe to chop all trees
    function chopTrees(e) {
        const trees = getEls('#tree-section-woodcutting > center > div');
        e.preventDefault();

        trees.each(el => {
            const timeEl = el.querySelector('.tree-secton-timer');
            const time = timeEl !== null ? timeEl.innerText : '';
            if (time === 'READY') el.click();
        });

        console.log('Chopped all trees');
    }

    // mod 2 - RightClick rake to harvest the crops
    function harvestCrops(e) {
        const trees = getEls('#plot-section-farming > center > div');
        e.preventDefault();

        trees.each(el => {
            const timeEl = el.querySelector('.tree-secton-timer');
            const time = timeEl !== null ? timeEl.innerText : '';
            if (time === 'READY') el.click();
        });

        console.log('Harvested all crops');
    }

    function initMods() {
        if (mods.chopTrees) {
            // init mod 1
            getEls('#item-section-woodcutting-1 [data-tooltip-id=tooltip-axe]')
                .each(el => el.addEventListener('contextmenu', chopTrees));
        }

        if (mods.harvestCrops) {
            // init mod 2
            getEls('#item-section-farming-1 [data-tooltip-id=tooltip-rake]')
                .each(el => el.addEventListener('contextmenu', harvestCrops));
        }
    }

    function init() {
        if (typeof window.var_username === "string" && window.var_username.length >= 3) {
            initMods();
            console.log('Initialized DH3 KindaSafe Mods');

            initCheats();
            console.log('Initialized DH3 KindaSafe Cheat');
        } else {
            setTimeout(init, 2e3);
        }
    }

    setTimeout(init, 2e3);

    /**
     * minor functions for this script to work well with clean code
     */

    function getEls(qSel) {
        let els = document.querySelectorAll(qSel);

        return {
            getRaw() {
                return els;
            },
            getNum(x) {
                return els[x];
            },
            each(fn) {
                els.forEach(fn);
            },
            ids() {
                let res = [];
                for (let key of els) res.push(key.id || 'none');
                return res;
            }
        };
    }

    // A safe cheat delayer
    function safeDelay(fn, minTime = 0) {
        setTimeout(fn, Math.floor(Math.random() * 9e3) + 1e3 + minTime);
    }
})();