MAX OWNED LAND

Keeps track of "max owned land"

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==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();

})();