TitanGiftsRS

Extension for HeroWarsHelper script

// ==UserScript==
// @name            TitanGiftsRS
// @name:en         TitanGiftsRS
// @name:ru         TitanGiftsRS
// @namespace       TitanGiftsRS
// @version         1.01
// @description     Extension for HeroWarsHelper script
// @description:en  Extension for HeroWarsHelper script
// @description:ru  Расширение для скрипта HeroWarsHelper
// @author          Green
// @license         Copyright Green
// @match           https://www.hero-wars.com/*
// @match           https://apps-1701433570146040.apps.fbsbx.com/*
// @run-at          document-start
// ==/UserScript==

(function () {
    if (!this.HWHClasses) {
        console.log('%cObject for extension not found', 'color: red');
        return;
    }

    console.log('%cStart Extension ' + GM_info.script.name + ', v' + GM_info.script.version + ' by ' + GM_info.script.author, 'color: red');
    const { addExtentionName } = HWHFuncs;
    addExtentionName(GM_info.script.name, GM_info.script.version, GM_info.script.author);

    const { buttons } = HWHData;

    const { popup, confShow, setProgress, hideProgress } = HWHFuncs;

    // Добавление кнопок в окно Разное
    const { othersPopupButtons } = HWHData;
    othersPopupButtons.push(
        {
            get msg() {
                return "Потратить искры мощи";
            },
            result: async function () {
                await spendSparksPower();
            },
            get title() {
                return "Потратить искры мощи";
            },
        },
        {
            get msg() {
                return "Сбросить дары стихий";
            },
            result: async function () {
                await resetTitanGifts();
            },
            get title() {
                return "Сбросить дары стихий";
            },
        },
    );

    // Потратить искры мощи
    async function spendSparksPower(){
        let [heroGetAll, inventory, user] = await new Caller(['heroGetAll', 'inventoryGet', 'userGetInfo']).execute()
        let heroes = Object.values(heroGetAll).sort((a, b) => a.titanGiftLevel - b.titanGiftLevel);
        const titanGiftLib = lib.getData('titanGift');
        let titanGift = inventory.consumable[24];
        let titanGiftMax = titanGift;
        let gold = user.gold;
        let userLevel = user.level;
        let calls = [];
        let minTitanGiftLevel = heroes[0].titanGiftLevel;
        let titanGiftLevel = minTitanGiftLevel;
        let titanGiftUpgradeCounter = 0;
        let message = "";

        if (userLevel < 30 || minTitanGiftLevel == 30){
            confShow("Нечего улучшать");
            return;
        }
        titanGift = +(await popup.confirm('Доступно <span style="color: green;">' + titanGift.toLocaleString() + '</span> искр мощи <br> Укажите сколько искр мощи потратить', [
            { result: 0, isClose: true },
            { msg: "Потратить искры мощи", isInput: true, default: titanGift.toString() },
        ]));
        if (titanGift == 0){
            return;
        }
        if (!titanGift || titanGift < 0 || titanGift > titanGiftMax) {
            confShow("Некорректное значение");
            return;
        }
        setProgress("Улучшаем дар стихий...", false, hideProgress);
        let cycle = true;
        while(cycle){
            for (const hero of heroes) {
                if (titanGiftLevel >= 30){
                    message = "<br>Достигнут 30 уровень дара стихий у всех героев";
                    cycle = false;
                    break;
                }
                if (hero.titanGiftLevel > titanGiftLevel){
                    break;
                }
                let nextLevelCost = titanGiftLib[hero.titanGiftLevel + 1].cost;
                if (titanGift < nextLevelCost.consumable[24] || gold < nextLevelCost.gold) {
                    if (titanGiftUpgradeCounter == 0 && calls.length == 0){
                        setProgress("", true, hideProgress);
                        confShow("Недостаточно золота или искр мощи");
                        return;
                    }
                    cycle = false;
                    break;
                }
                calls.push({name: "heroTitanGiftLevelUp", args: {heroId: hero.id}, ident: "group_" + calls.length + "_body"});
                titanGift -= nextLevelCost.consumable[24];
                gold -= nextLevelCost.gold;
            }
            if (calls.length >= 1){
                await Send({calls});
                console.log(calls);
                titanGiftUpgradeCounter += calls.length;
                heroGetAll = await new Caller(['heroGetAll']).execute()
                heroes = Object.values(heroGetAll).sort((a, b) => a.titanGiftLevel - b.titanGiftLevel);
                calls = [];
                titanGiftLevel++;
                setProgress('Дар стихий улучшен до <span style="color: green;">'+ titanGiftLevel + '</span> уровня', false, hideProgress);
            }
        }
        await new Promise((e) => setTimeout(e, 2000));
        setProgress("", true, hideProgress);
        confShow('Дар стихий улучшен <span style="color: green;">'+ titanGiftUpgradeCounter + '</span> раз(а)' + message );
    }

    //Сбросить дары стихий
    async function resetTitanGifts(){
        const [heroGetAll, user] = await new Caller(['heroGetAll', 'userGetInfo']).execute();
        const heroes = Object.values(heroGetAll).sort((a, b) => a.titanGiftLevel - b.titanGiftLevel);
        const userLevel = user.level;
        let calls = [];
        let maxResetTitanGiftLevel = 0;
        let message = "";
        for (const hero of heroes) {
            if (hero.titanGiftLevel > 0){
                maxResetTitanGiftLevel = hero.titanGiftLevel;
                break;
            }
        }
        if (userLevel < 30 || maxResetTitanGiftLevel == 0){
            confShow("Нечего сбрасывать");
            return;
        }
        if (maxResetTitanGiftLevel == 30){
            message += 'Остались герои только с <span style="color:red;">30</span> уровнем дара стихий <br> <br>'
        }
        else {
            maxResetTitanGiftLevel = +(await popup.confirm('Укажите <span style="color:red;">максимальный</span> сбрасываемый уровень дара стихий <br> Диапазон от 1 до 30', [
                { result: 0, isClose: true },
                { msg: "Сбросить дар стихий", isInput: true, default: maxResetTitanGiftLevel.toString() },
            ]));
        }
        if (maxResetTitanGiftLevel == 0){
            console.log("Закрыли окно");
            return;
        }
        if (!maxResetTitanGiftLevel || maxResetTitanGiftLevel < 0 || maxResetTitanGiftLevel > 30) {
            confShow("Некорректное значение");
            return;
        }
        if (maxResetTitanGiftLevel >= 30){
            message+= 'Вы уверены, что хотите сбросить дар стихий <span style="color:red;">30</span> уровня у <span style="color:red;">ВСЕХ</span> героев?';
            let resultPopup = await popup.confirm(message, [
                { msg: I18N('BTN_ACCEPT'), result: true },
                { msg: I18N('BTN_CANCEL'), result: false },
                {isClose: true, result: false},
            ]);
            if(!resultPopup){
                return;
            }
        }
        for (const hero of heroes) {
            if (hero.titanGiftLevel == 0){
                continue;
            }
            if (hero.titanGiftLevel > maxResetTitanGiftLevel){
                break;
            }
            calls.push({name: "heroTitanGiftDrop", args: {heroId: hero.id}, ident: "group_" + calls.length + "_body"});
        }
        if (calls.length == 0){
            confShow("Нечего сбрасывать");
            return;
        }
        await Send({calls});
        confShow('Дар стихий сброшен у <span style="color: green;">'+ calls.length + '</span> героя(ев)');
    }

})();