Torn Chainz War Attack

Provide attack recommendations during war using settings at https://chainz.top/war

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         Torn Chainz War Attack
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Provide attack recommendations during war using settings at https://chainz.top/war
// @author       Ballig [2965527]
// @match        https://www.torn.com/page.php?sid=attack&user2ID=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @connect      chainz.top
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    GM_addStyle(`
.tcwa_recommend_title {
    font-weight: bold;
    font-size: 125%;
    margin: auto;
    text-shadow: 1px 1px 2px black;
}
.tcwa_recommend_hosp {
    color: purple;
}
.tcwa_recommend_leave {
    color: goldenrod;
}
.tcwa_recommend_outside {
    color: red;
}
.tcwa_faded {
    opacity: 0.25;
}
.tcwa_button {
  color: OrangeRed !important;
  border: 2px solid OrangeRed !important;
}
`);

    const isTornPDA = typeof window.flutter_inappwebview !== 'undefined';
    let recommendation = 0
    let target = 0;
    let playerid = get_playerid();

    function get_playerid() {
        let p_id = 0;

        const scriptTag = document.querySelector('script[server_name]');

        if (scriptTag) {
            p_id = scriptTag.getAttribute('playerid') || 0;
        }

        return p_id;
    }
    async function initialise() {
        const queryString = window.location.search;
        const urlParams = new URLSearchParams(queryString);
        try {
            target = Number(urlParams.get('user2ID') ?? 0);
        } catch (e) {
            target = 0;
        }
        if (target > 0) {
            const targetNode = document.getElementById("react-root");

            if (targetNode) {
                const config = { childList: true, subtree: true };

                const check_root_container = (mutationsList, observer) => {
                    // Look through all the mutations that occurred
                    for (const mutation of mutationsList) {
                        // Check if nodes were added
                        if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                            for (const node of mutation.addedNodes) {
                                // Ensure the node is an actual element node (not text, etc.)
                                if (node.nodeType === Node.ELEMENT_NODE) {
                                    const buttons = node.querySelectorAll("div[class^='dialogButtons___'] button");
                                    buttons.forEach((button) => {
                                        process_button(button);
                                    });
                                }
                            }
                        }
                    }
                };
                const observer = new MutationObserver(check_root_container);
                observer.observe(targetNode, config);
            }
            GM_xmlhttpRequest({
                method: 'POST',
                url: 'https://chainz.top/api/war/hosp',
                headers: { 'Content-Type': 'application/json' },
                data: JSON.stringify({
                    "user": playerid,
                    "target": target
                }),
                onload: (response) => {
                    if (response.status >= 200 && response.status < 300) {
                        let js = null;
                        try {
                            js = JSON.parse(response.responseText);
                        } catch (e) {
                            console.error('[TCWA] Could not parse JSON:', e);
                            js = null;
                        }
                        if (js !== null && js.status) {
                            const titleContainer = document.querySelector("div[class^='titleContainer___']");

                            if (titleContainer) {
                                let rec_div = document.createElement("div");
                                rec_div.classList.add('tcwa_recommend_title');
                                recommendation = js.recommendation; //(Math.random() >= 0.66 ? 1 : (Math.random() >= 0.5 ? 3 : 4));
                                switch (recommendation) {
                                    case 1:
                                        rec_div.innerText = "LEAVE";
                                        rec_div.classList.add('tcwa_recommend_leave');
                                        break;
                                    case 3:
                                        rec_div.innerText = "HOSPITALISE";
                                        rec_div.classList.add('tcwa_recommend_hosp');
                                        break;
                                    case 4:
                                        rec_div.innerText = "OUTSIDE HIT";
                                        rec_div.classList.add('tcwa_recommend_outside');
                                        break;
                                }
                                titleContainer.append(rec_div);
                            }
                        }
                    }
                },
                onerror: (err) => {
                    console.error('[TCWA] Error:', err);
                }
            });
        }
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initialise);
    } else {
        initialise();
    }

    function process_button(button) {
        if (recommendation > 0 && recommendation < 4) {
            if (button.innerText.toLowerCase() === "leave") {
                if (recommendation == 1) {
                    button.classList.add('tcwa_button');
                }
                else {
                    button.classList.add('tcwa_faded');
                }
            }
            else if (button.innerText.toLowerCase() === "mug") {
                if (recommendation == 2) {
                    button.classList.add('tcwa_button');
                }
                else {
                    button.classList.add('tcwa_faded');
                }
            }
            if (button.innerText.toLowerCase() === "hospitalize") {
                if (recommendation == 3) {
                    button.classList.add('tcwa_button');
                }
                else {
                    button.classList.add('tcwa_faded');
                }
            }
        }
    }
})();