MAX OWNED LAND

Keeps track of "max owned land"

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         MAX OWNED LAND
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Keeps track of "max owned land"
// @author       OpenFront Masters x NewYearNewPhil
// @match        https://openfront.io/*
// @license      MIT
// @grant        none
// @run-at       document-idle
// @dowloadURL   https://greasyfork.org/en/scripts/558114-max-owned-land/code
// ==/UserScript==

(function() {
    'use strict';

    let maxOwned = JSON.parse(localStorage.getItem('maxOwned')) || {};
    let lastUrl = location.href;

    function getGameId(url) {
        const match = url.match(/\/game\/([^?]+)/);
        return match ? match[1] : null;
    }

    function formatPercentage(str) {
        return parseFloat(str.replace('%','').trim());
    }

    function updateMax(name, value) {
        if(!maxOwned[name] || value > maxOwned[name]) {
            maxOwned[name] = value;
            localStorage.setItem('maxOwned', JSON.stringify(maxOwned));
            return true;
        }
        return false;
    }

    function displayMax(node, name, currentValue) {
        let maxValue = maxOwned[name] || currentValue;
        let span = node.querySelector('.max-owned');
        if(!span) {
            span = document.createElement('span');
            span.className = 'max-owned';
            span.style.fontSize = '0.8em';
            span.style.color = '#ccc';
            span.style.marginLeft = '3px';
            node.appendChild(span);
        }
        span.textContent = `(max: ${maxValue}%)`;
    }

    function processRow(cols, nameIndex, ownedIndex) {
        const name = cols[nameIndex].textContent.trim();
        const ownedNode = cols[ownedIndex];
        const currentOwned = formatPercentage(ownedNode.textContent);
        const isNewMax = updateMax(name, currentOwned);
        displayMax(ownedNode, name, currentOwned);
        if(isNewMax) {
            ownedNode.querySelector('.max-owned').style.color = '#0f0';
            setTimeout(() => {
                ownedNode.querySelector('.max-owned').style.color = '#ccc';
            }, 1000);
        }
    }

    function processLeaderboard() {
        document.querySelectorAll('leader-board .contents').forEach(row => {
            const cols = row.children;
            if(cols.length >= 5) processRow(cols, 1, 2);
        });
    }

    function processTeamStats() {
        document.querySelectorAll('team-stats .contents').forEach(row => {
            const cols = row.children;
            if(cols.length >= 4) processRow(cols, 0, 1);
        });
    }

    function resetMaxOwned() {
        console.log('[MAX OWNED] Resetting data for new game');
        maxOwned = {};
        localStorage.removeItem('maxOwned');
        document.querySelectorAll('.max-owned').forEach(span => span.remove());
    }

    function checkGameChange() {
        const currentGameId = getGameId(location.href);
        const storedGameId = localStorage.getItem('gameId');

        if (!currentGameId) return;

        if (!storedGameId) {
            console.log('[MAX OWNED] Setting initial gameId:', currentGameId);
            localStorage.setItem('gameId', currentGameId);
            return;
        }

        if (currentGameId !== storedGameId) {
            resetMaxOwned();
            localStorage.setItem('gameId', currentGameId);
        }
    }

    function processAll() {
        if(location.href !== lastUrl) {
            checkGameChange();
            lastUrl = location.href;
        }

        processLeaderboard();
        processTeamStats();
    }

    let scheduled = false;
    const observer = new MutationObserver(() => {
        if(!scheduled) {
            scheduled = true;
            setTimeout(() => {
                processAll();
                scheduled = false;
            }, 200);
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

    checkGameChange();
    processAll();

})();