BiteFight Healthbar

Adds an healthbar and energybar to BiteFight

Устаревшая версия за 18.11.2024. Перейдите к последней версии.

// ==UserScript==
// @name         BiteFight Healthbar
// @namespace    https://lobby.bitefight.gameforge.com/
// @version      0.3
// @description  Adds an healthbar and energybar to BiteFight
// @author       Spychopat
// @match        https://*.bitefight.gameforge.com/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    'use strict';

    // Script storage keys
    const KEY_CHARACTER = 'character';

    // Define character object
    const CHARACTER = GM_getValue(KEY_CHARACTER, {
        energy: 0,
        maxEnergy: 0,
        health: 0,
        maxHealth: 0,
        regenHealth: 0
    });

    // Get Stats
    var allStatsElement = document.getElementsByClassName("gold")[0];
    var statsValues = allStatsElement.textContent.split("\n");
    statsValues = statsValues.map(value => value.trim());
    statsValues.shift();

    // Extract energy, fragments, gold, health, and hellStones
    var energy = statsValues[3].trim();
    var currentEnergy = energy.split("/")[0];
    var maxEnergy = energy.split("/")[1];

    if (currentEnergy && maxEnergy) {
        CHARACTER.energy = parseInt(currentEnergy); // Use parseFloat to preserve decimals
        CHARACTER.maxEnergy = parseInt(maxEnergy); // Use parseFloat to preserve decimals
    }

    var health = statsValues[4].trim();
    var currentHealth = formatNumber(health.split("/")[0]);
    var maxHealth = formatNumber(health.split("/")[1]);

    if (currentHealth && maxHealth) {
        CHARACTER.health = parseInt(currentHealth);
        CHARACTER.maxHealth = parseInt(maxHealth);
    }

    checkRegenHealth();
    updateCharacter();
    removeTheShit();
    insertProgressBars(); // Insert progress bars after updating the character
    console.log(CHARACTER);

        function removeTheShit() {
        GM_addStyle(`
        #upgrademsg {
            display: none;
        }
        #premium > img {
            display: none;
        }
        #mmonetbar {
            display: none !important;
            visibility: hidden;
        }
    `);
    }

    // Format texts to return as numbers (no thousand separators)
    function formatNumber(value) {
        while (value.indexOf(".") > 0) value = value.replace(".", "");
        return value;
    }

    function checkRegenHealth() {
        var elements = document.getElementsByClassName("triggerTooltip");

        // Loop through the elements
        for (let i = 0; i < elements.length; i++) {
            // Check if the inner text or inner HTML contains "/ h"
            if (elements[i].innerText.includes("/ h") || elements[i].innerHTML.includes("/ h")) {
                CHARACTER.regenHealth = parseInt(elements[i].textContent);
                //console.log("Regen per hour found : ", parseInt(elements[i].textContent));
                break; // Exit the loop once the element is found
            }
        }
    }

    // Update character in local storage
    function updateCharacter() {
        GM_setValue(KEY_CHARACTER, CHARACTER);
    }

    // Function to insert progress bars into the page
    function insertProgressBars() {
        // Check if progress bars are already inserted
        if (document.getElementById('energyProgressBar') && document.getElementById('healthProgressBar')) {
            return; // If bars are already present, do nothing
        }

        // Create container for the progress bars
        let container = document.createElement('div');
        container.style.marginTop = '0px';
        container.style.paddingTop = '3px';
        container.style.paddingBottom = '8px';
        container.style.paddingLeft = '60px';
        container.style.paddingRight = '60px';
        container.style.textAlign = 'center';
        container.style.display = 'flex'; // Use flexbox for side-by-side layout
        container.style.justifyContent = 'space-between'; // Space out the bars

        // Energy Progress Bar
        let energyBarContainer = document.createElement('div');
        energyBarContainer.style.flex = '1'; // Ensure bars take equal space
        energyBarContainer.style.backgroundColor = 'black'; // Black background for the container
        energyBarContainer.style.borderRadius = '0px'; // Rounded corners for container
        energyBarContainer.style.padding = '2px'; // Padding for spacing between container and bar
        energyBarContainer.style.outline = '2px solid #333'; // Outline around the container
        energyBarContainer.style.position = 'relative'; // Positioning context for the text
        energyBarContainer.style.marginRight = '100px';
        energyBarContainer.id = 'energyProgressBar'; // Set an ID to easily find it later

        let energyBar = document.createElement('div');
        energyBar.style.height = '20px';
        energyBar.style.width = `${(CHARACTER.energy / CHARACTER.maxEnergy) * 100}%`;
        energyBar.style.backgroundColor = '#0000a4';
        energyBar.style.borderRadius = '0px';
        energyBar.style.outline = '2px solid #000'; // Outline for the progress bar

        // Create the text for the energy value
        let energyText = document.createElement('div');
        energyText.textContent = `${CHARACTER.energy}`;
        energyText.style.position = 'absolute';
        energyText.style.top = '50%';
        energyText.style.left = '50%';
        energyText.style.transform = 'translate(-50%, -50%)';
        energyText.style.color = 'white';
        energyText.style.fontSize = '12px';
        energyText.style.fontWeight = 'bold';
        energyText.style.fontFamily = 'monospace';

        energyBarContainer.appendChild(energyBar);
        energyBarContainer.appendChild(energyText); // Add the text to the container
        container.appendChild(energyBarContainer);

        // Health Progress Bar
        let healthBarContainer = document.createElement('div');
        healthBarContainer.style.flex = '1'; // Ensure bars take equal space
        healthBarContainer.style.backgroundColor = 'black'; // Black background for the container
        healthBarContainer.style.borderRadius = '0px'; // Rounded corners for container
        healthBarContainer.style.padding = '2px'; // Padding for spacing between container and bar
        healthBarContainer.style.outline = '2px solid #333'; // Outline around the container
        healthBarContainer.style.position = 'relative'; // Positioning context for the text
        healthBarContainer.id = 'healthProgressBar'; // Set an ID to easily find it later

        let healthBar = document.createElement('div');
        healthBar.style.height = '20px';
        healthBar.style.width = `${(CHARACTER.health / CHARACTER.maxHealth) * 100}%`;
        healthBar.style.backgroundColor = '#b80000';
        healthBar.style.borderRadius = '0px';
        healthBar.style.outline = '2px solid #000'; // Outline for the progress bar

        // Create the text for the health value
        let healthText = document.createElement('div');
        healthText.textContent = `${CHARACTER.health > 999 ? CHARACTER.health.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.') : CHARACTER.health}`;
        healthText.style.position = 'absolute';
        healthText.style.top = '50%';
        healthText.style.left = '50%';
        healthText.style.transform = 'translate(-50%, -50%)';
        healthText.style.color = 'white';
        healthText.style.fontSize = '12px';
        healthText.style.fontWeight = 'bold';
        healthText.style.fontFamily = 'monospace';

        healthBarContainer.appendChild(healthBar);
        healthBarContainer.appendChild(healthText); // Add the text to the container
        container.appendChild(healthBarContainer);

        // Insert the container into the body or any desired element on the page
        document.getElementsByClassName("gold")[0].appendChild(container);
    }

    // Start real-time health regeneration
    function startHealthRegeneration() {
        const regenInterval = 100; // Update every second (1000 ms)
        const regenPerSecond = CHARACTER.regenHealth / 36000; // Convert regenHealth from per hour to per second

        setInterval(() => {
            if (CHARACTER.health < CHARACTER.maxHealth) {
                CHARACTER.health = Math.min(CHARACTER.maxHealth, CHARACTER.health + regenPerSecond);
                updateCharacter();
                updateProgressBars(); // Update the existing progress bars
            }
        }, regenInterval);
    }

    // Update the existing progress bars
    function updateProgressBars() {
        // Update Energy Progress Bar
        const energyBar = document.getElementById('energyProgressBar').children[0];
        energyBar.style.width = `${(CHARACTER.energy / CHARACTER.maxEnergy) * 100}%`;
        const energyText = document.getElementById('energyProgressBar').children[1];
        energyText.textContent = `${CHARACTER.energy}`;

        // Update Health Progress Bar
        const healthBar = document.getElementById('healthProgressBar').children[0];
        healthBar.style.width = `${(CHARACTER.health / CHARACTER.maxHealth) * 100}%`;
        const healthText = document.getElementById('healthProgressBar').children[1];
        // Ensure health is an integer and remove decimals
        let healthWithoutDecimals = Math.floor(CHARACTER.health); // Use Math.floor() to round down (or use Math.trunc() to simply cut decimals off)
        // Format the health value with thousands separators
        healthText.textContent = `${healthWithoutDecimals > 999 ? healthWithoutDecimals.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.') : healthWithoutDecimals}`;

    }

    // Start health regeneration on page load
    startHealthRegeneration();
})();