Checking S0mething

Find S0mething in s0mewhere

// ==UserScript==
// @name         Checking S0mething
// @version      1.666.3
// @description  Find S0mething in s0mewhere
// @author       S0me1
// @match        https://www.erepublik.com/en
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addStyle
// @namespace https://greasyfork.org/users/1144622
// ==/UserScript==

(function () {
    // Declare variables
    var cid = erepublik.citizen.countryLocationId;
    var divi = 1;
    var countryList = [9, 42, 23, 14, 70, 39, 48, 49, 54, 56, 71, 72, 66, 57, 67, 35, 53, 81, 1, 68, 30, 40, 24, 79, 52]; // Define the countryList array
    var dataBattle = {}; // Change from [] to {}
    var idbattle = []; // New array to store battle IDs
    var dataDivision = {}; // New object to store relevant data from battles object
    var emptyBattle = []; // Array to store battle IDs with missing div data
    var citynameA = []; // Array to store city name

    // Function to fetch data and process it
    async function fetchData(url) {
        const response = await fetch(url);
        return response.json();
    }

    // Function to process data for a specific idbattle from dataBattle and dataDivision
    function processDataForId(battleId) {
        if (dataBattle.hasOwnProperty(battleId)) {
            // Get the data from dataBattle using the battleId as the index
            var zoneId = dataBattle[battleId].zone_id;
            var cityname = dataBattle[battleId].city.name;

            if (dataDivision.hasOwnProperty(battleId)) {
                // Get the data from dataDivision using the battleId as the index
                var groundZoneId = dataDivision[battleId].groundZoneId;
                var aircraftZoneId = dataDivision[battleId].aircraftZoneId;

                // Fetch JSON from "https://www.erepublik.com/en/military/battle-stats/" + battleId
                fetchData("https://www.erepublik.com/en/military/battle-stats/" + battleId)
                    .then(data => {
                        // Step 1: Check if zoneId exists in the fetched JSON data
                        const finished = data.zone_finished;
                        const zoneIdData = data.stats.current[zoneId];

                        // Step 2: Check if "divi" and "divi[cid][groundZoneId]" exist in the zoneIdData object
                        if (zoneIdData && zoneIdData[divi] && zoneIdData[divi][cid] && zoneIdData[divi][cid][groundZoneId]) {
                            // Further processing can be done here if "divi[cid][groundZoneId]" exists
                        } else {
                            if (finished == false) {
                                emptyBattle.push("https://www.erepublik.com/en/military/battlefield/" + battleId + "/" + groundZoneId);
                                citynameA.push(cityname + " - R" + zoneId);
                            }
                        }
                    })
                    .catch(error => {
                        console.error("Error fetching JSON:", error);
                    });
            }
        }
    }

    // Function to display the results in the sidebar and open links in new tabs
    function displayResults() {
        var sidebarDiv = document.querySelector('.sidebar');
        var resultDiv = document.createElement('div');
        resultDiv.classList.add('results'); // Add a class for styling (optional)

        for (var i = 0; i < emptyBattle.length; i++) {
            var battleURL = emptyBattle[i];
            var cityName = citynameA[i];

            var anchorElement = document.createElement('a');
            anchorElement.href = battleURL;
            anchorElement.textContent = cityName;
            anchorElement.style.display = 'block'; // Add some spacing between links

            resultDiv.appendChild(anchorElement);

            // Open link in a new tab with a delay of 30 seconds
            setTimeout(function (url) {
                return function () {
                    window.open(url, "_blank");
                };
            }(battleURL), 30000 * (i + 1)); // 30 seconds delay for each link
        }

        // Remove any existing results
        var existingResults = document.querySelectorAll('.results');
        existingResults.forEach(result => {
            result.remove();
        });

        // Append the new results div to the sidebar
        sidebarDiv.appendChild(resultDiv);
    }


// Main function to execute the script
    async function main() {
        try {
            // Fetch the first JSON file and process it
            const firstData = await fetchData("https://www.erepublik.com/en/military/campaignsJson/list");

            // Get battles object
            var battles = firstData.battles;

            // Loop through each object inside the battles object
            for (var battleId in battles) {
                if (battles.hasOwnProperty(battleId)) {
                    var battle = battles[battleId];
                    var dw = battle.war_type;

                    if (dw == "direct"){
                     // main if
                        var inv = battle.inv.id;
                        var def = battle.def.id;
                        var inva = battle.inv.allies;
                        var defa = battle.def.allies;

                        if (countryList.includes(inv) && countryList.includes(def)) {
                            // Check if inv.id or def.id matches cid
                            if (inv === cid || def === cid) {
                                // Store the battle data in the dataBattle object using battle ID as the index
                                dataBattle[battle.id] = battle;

                                // Push the battle ID to the idbattle array
                                idbattle.push(battle.id);
                            }

                            if (inva.includes(cid) || defa.includes(cid)){
                                // Store the battle data in the dataBattle object using battle ID as the index
                                dataBattle[battle.id] = battle;

                                // Push the battle ID to the idbattle array
                                idbattle.push(battle.id);
                            }
                        }
                    }
                }
            }

            // Show dataBattle and idbattle in the console
            console.log("dataBattle:", dataBattle);
            console.log("idbattle:", idbattle);

            // Fetch the second JSON file and process it manually
            const secondData = await fetchData("https://www.erepublik.com/en/military/campaignsJson/citizen");

            // Get battles object from the second JSON file
            var battles2 = secondData.battles;

            // Process data for each idbattle using the processDataForId function
            for (let i = 0; i < idbattle.length; i++) {
                let battleId = idbattle[i];

                // Check if the battleId exists in the battles object from the second JSON file
                if (battles2.hasOwnProperty(battleId)) {
                    // Store the relevant battle data in the dataDivision object using battle ID as the index
                    dataDivision[battleId] = battles2[battleId];
                }
            }

            // Show dataDivision in the console
            console.log("dataDivision:", dataDivision);

            // Process data for each battle ID
            idbattle.forEach(id => {
                processDataForId(id);
            });

            // Show emptyBattle in the console
            console.log("emptyBattle:", emptyBattle);
            console.log("city name:", citynameA);


            // Add a small delay before calling displayResults
            setTimeout(displayResults, 3000);
        } catch (error) {
            console.error("Error:", error);
        }
    }

    // Call the main function to start the script
    main();
})();