SettlersCombatSimulator_MissingGenerals

Modifies the request to add the missing generals in the Settlers Online Combat Simulator.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         SettlersCombatSimulator_MissingGenerals
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Modifies the request to add the missing generals in the Settlers Online Combat Simulator.
// @author       [email protected]
// @match        *://*.settlerscombatsimulator.com/*
// @grant        none
// @run-at       document-start
// @connect      www.settlerscombatsimulator.com
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const IMG_NAR = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAGT0lEQVR4AYyVeWwc1R3Hv3PtMXsvXu/63sVX4vVR4lBwaCy3zR80hDQC9xBVUeuGSi1tXFVNI1VV2kKhUZu23H8gCAELpIA4BYFArMQxgZDLUfCBz43X9np9rL2749ljZmeGt7KJ4tgbePp933vzfm9+nzfvvXmPxjdPlBEocRgsW20254+t1rz7LEbHHeR1A5GLSEeU074JiNHr9VU2Xvd8ube41+crPlXl9x/ZdNttL9f4/R+X5rlea64r+qS60HaEZ3A3IVFEa+xrQUYav6ku81ze/6f7f7mzxWP90fdLsa3BAz1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YmBpCwlDBycsKpkY0WNuzsDhyLVrm6cKEoYA32xDurmzY2i1Ec4tAVd+o3uCJpMkg5ZBktK6trVtY+qgCngHLcPD4Uej0NcF1sSeyC7g4dfkGEm5kI/NeHrqU41jJpiJ0rQf2xpwGjc5AdU1jRkdH/18TMSD/JJMzQFNJOff7grJqNPIa4OzGhp8zCyJeASraRTB18YSekxfu1dUhbJke3NlWCDueiKr6JpSUV6CysuYUsdS6daQGKiWQVlnV/LCWy8M4dPDhl7EIcrGBovcJBC0t+IeXh0+89WE2axD+oXswOk5DI79dtfmDqORnPQMCaGiTt+5lSZ9MrohKSOZwzyWlYEAqxxfJHESEB8LRsAdGtF7oG5ti3YHz6JVRkJiYrLyYmnFT1C+OJgZSElM0HUj98Wpo6+ja8yvnFjctnaNq6xRiQ2QUAjydsWPnLgR/nohxlQnOnrsgO3gs4buySv5G4q4RQvIaz0idfxmFHc/6w87EX83OuJqpGJYp4ei3GfY+7yM3r1S1dcfu0UxOzgFSfIjEOIlpNd2K/j+BP/JcHhGfwol0cw9qSrl0Bb4Bb4nfDok8fTO3yGNEqUwmxeRYye8MehWQehsHpTJZWnev2C+dc2cNl9fsL5fLjxHfVhJaIaQG/wEAAP//PKSTTAAAAAZJREFUAwDL7TjzhT9exwAAAABJRU5ErkJggg==";

    const IMAGE_MAP = {
        "assets/img/uniticons/IMG_GEN_NAR.PNG": IMG_NAR,
        "assets/img/uniticons/IMG_GEN_BRO.PNG": IMG_BRO,
    };

    function attachImageFallbacks(root = document) {
        root.querySelectorAll('img').forEach(img => {
            const fallback = IMAGE_MAP[img.getAttribute('src')];
            if (fallback && img.src !== fallback) {
                img.onerror = function() {
                    this.src = fallback;
                    this.onerror = null; // zapobiega pętli
                };
            }
        });
    }

    function createGeneralBrohman() {
        return {
            "id": "Brohmann",
            "name": "Brohmann, The Raider",
            "shortName": "",
            "description": "General Brohmann added by TamperMonkey script",
            "priority": 936,
            "hitPoints": 500,
            "hitDamage": 3000,
            "missDamage": 1500,
            "accuracy": 0.7,
            "experience": 0,
            "icon": "IMG_GEN_BRO.PNG",
            "value": 0,
            "isWeak": false,
            "isElite": false,
            "isGeneral": true,
            "isPlayer": true,
            "timeBonus": 300,
            "maxUnits": 125,
            "sortIndex": 1,
            "strike": 0,
            "combatantType": "CombatGeneral",
            "isBoss": false,
            "combatModifier": [
                { "name": "Player", "item": "MaxAttackDamage", "type": "Cavalry", "chance": 1, "adder": 30, "channel": "specialist" },
                { "name": "Player", "item": "MinAttackDamage", "type": "Cavalry", "chance": 1, "adder": 30, "channel": "specialist" },
                { "name": "Player", "item": "MaxAttackDamage", "type": "Knight", "chance": 1, "adder": 30, "channel": "specialist" },
                { "name": "Player", "item": "MinAttackDamage", "type": "Knight", "chance": 1, "adder": 30, "channel": "specialist" },
                { "name": "Player", "item": "GainSkill_Flanking", "type": "Knight", "chance": 1, "channel": "specialist" }
            ],
            "skillSplashDamage": true,
            "skillAttackWeakestFirst": true,
            "skillIgnoreAC": false,
            "skillName": "Trait_CombatGeneral",
            "modifierCombatXPMultiplier": 1.2
        };
    };

    function createGeneralNarcissistic() {
        //<GeneralRecoverySpeed multiplier="0.75" />
        return {
            "id": "Narcissistic",
            "name": "Narcissistic General",
            "shortName": "",
            "description": "General Narcissistic added by TamperMonkey script",
            "priority": 1113,
            "hitPoints": 250,
            "hitDamage": 6000,
            "missDamage": 5400,
            "accuracy": 0.7,
            "experience": 0,
            "icon": "IMG_GEN_NAR.PNG",
            "value": 0,
            "isWeak": false,
            "isElite": false,
            "isGeneral": true,
            "isPlayer": true,
            "timeBonus": 300,
            "maxUnits": 10,
            "sortIndex": 1,
            "strike": 0,
            "combatantType": "CombatGeneral",
            "isBoss": false,
            "combatModifier": [
                { "name": "Player", "item": "CombatXP", "type": "CombatGeneral", "chance": 1, "multiplier": 0.2,  "channel": "specialist" },
                { "name": "Player", "item": "InstantRecover", "type": "CombatGeneral", "value": 1, "chance": 1.0, "channel": "specialist" },
                { "name": "Enemy", "item": "UnitHP", "type": "Boss", "chance": 1, "multiplier": 0.75, "channel": "specialist" }
            ],
            "skillSplashDamage": false,
            "skillAttackWeakestFirst": true,
            "skillIgnoreAC": false,
            "skillName": "Trait_NarcissisticGeneral",
            "modifierCombatXPMultiplier": 0.2
        };
    };

    // funtion injects the unit into the JSON
    function injectUnitIntoJson(jsonObj) {
        try {
            // check if structure matches expected
            if (jsonObj && jsonObj.data && jsonObj.data[0] && Array.isArray(jsonObj.data[0].units)) {
                jsonObj.data[0].units.push(createGeneralBrohman());
                jsonObj.data[0].units.push(createGeneralNarcissistic());
                console.log(`$====== Successfully injected Generals into JSON response.`);
                return jsonObj;
            } else {
                console.warn(`$====== JSON structure mismatch. Could not find data[0].units`, jsonObj);
                return jsonObj; // return original
            }
        } catch (e) {
            console.error(`$====== Error during injection logic:`, e);
            return jsonObj;
        }
    }

    const originalXMLHttpRequest = window.XMLHttpRequest;
    window.XMLHttpRequest = function() {
        const xhr = new originalXMLHttpRequest();
        const send = xhr.send;

        xhr.send = function() {
            this.addEventListener("readystatechange", function() {
                // verify URL and state
                if (this.readyState === 4 && this.responseURL && this.responseURL.includes("/api/init")) {
                    console.log(`====== XHR intercepted: ${this.responseURL}`);
                    try {
                        const originalText = this.responseText;
                        let json = JSON.parse(originalText);

                        // injection
                        json = injectUnitIntoJson(json);
                        const newText = JSON.stringify(json);

                        Object.defineProperty(this, 'responseText', { get: () => newText });
                        Object.defineProperty(this, 'response', { get: () => newText });
                    } catch (err) {
                        console.error(`====== XHR modification failed:`, err);
                    }
                }
            }, false);
            return send.apply(this, arguments);
        };
        return xhr;
    };

    console.log(`====== Injector loaded and watching for /api/init`);
    attachImageFallbacks();
    const observer = new MutationObserver(() => attachImageFallbacks());
    observer.observe(document.documentElement, { childList: true, subtree: true });

})();