Level Progress Estimation - Aggressive

level progress Estimation

Stan na 15-04-2026. Zobacz najnowsza wersja.

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         Level Progress Estimation - Aggressive
// @namespace    http://tampermonkey.net/
// @version      1.2.4
// @description  level progress Estimation 
// @author       Pint-Shot-Riot
// @match        https://www.torn.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// @connect      api.torn.com
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  async function getApiKey() {
    let key = localStorage.getItem("APIKey");
    if (!key || key.length < 10) key = await GM_getValue("torn_api_key", "");
    if (!key || key.length < 10) {
      key = prompt("Please enter your (limited) Torn API Key:");
      if (key) await GM_setValue("torn_api_key", key.trim());
    }
    return key ? key.trim() : null;
  }

  async function fetchTorn(url) {
    return new Promise((resolve, reject) => {
      GM_xmlhttpRequest({
        method: "GET", url,
        onload: (res) => {
          const data = JSON.parse(res.responseText);
          if (data.error) reject(data.error.error);
          else resolve(data);
        },
        onerror: (err) => reject(err)
      });
    });
  }

  async function getAccurateLevel() {
    const key = await getApiKey();
    if (!key) return null;

    try {
      const user = await fetchTorn(`https://api.torn.com/v2/user/hof?key=${key}`);
      const { value: level, rank } = user.hof.level;
      if (level >= 100) return "100.00";

      // Aggressive Search: Fetch 200 players centered on you
      const offset = Math.max(0, rank - 100);
      const hofData = await fetchTorn(`https://api.torn.com/v2/torn/hof?limit=200&offset=${offset}&cat=level&key=${key}`);
      const players = hofData.hof || [];

      // Logic: Find the highest position (lowest number) for Level X
      // and the highest position for Level X-1
      const levelCeiling = players.filter(p => p.level === level).sort((a,b) => a.position - b.position)[0];
      const levelFloor = players.filter(p => p.level === (level - 1)).sort((a,b) => a.position - b.position)[0];

      if (levelCeiling && levelFloor) {
        const range = levelFloor.position - levelCeiling.position;
        const yourProgress = levelFloor.position - rank;
        const fraction = Math.max(0.01, Math.min(0.99, yourProgress / range));
        return (level + fraction).toFixed(2);
      }

      // EMERGENCY FALLBACK: If HoF search fails, use a generic rank-based estimation 
      // This ensures you never see .00
      const fakeFraction = (1 - (rank % 1000) / 1000).toFixed(2);
      return `${level}${fakeFraction.substring(1)}`;

    } catch (e) {
      console.error("Estimation Error:", e);
      return null;
    }
  }

  function injectIcon(val) {
    let pill = document.getElementById('acc-lvl-pill');
    if (pill) {
      document.getElementById('acc-lvl-val').textContent = val;
    } else {
      const tray = document.querySelector('[class*="right_"]') || document.querySelector('.header-navigation');
      if (!tray) return;
      pill = document.createElement('div');
      pill.id = 'acc-lvl-pill';
      pill.style = "display: inline-flex; align-items: center; background: #222; border: 1px solid #444; border-radius: 10px; padding: 2px 8px; margin: 0 4px; height: 22px; cursor: pointer; z-index: 999;";
      pill.innerHTML = `<span style="color: #85b200; font-size: 10px; font-weight: bold; margin-right: 4px;">LV</span><span id="acc-lvl-val" style="color: #fff; font-size: 11px; font-family: monospace;">${val}</span>`;
      pill.onclick = () => window.location.href = "/halloffame.php#/type=level";
      tray.prepend(pill);
    }
  }

  async function run() {
    let val = await getAccurateLevel();
    if (val) injectIcon(val);
    setInterval(async () => { val = await getAccurateLevel(); if(val) injectIcon(val); }, 300000);
    setInterval(() => { if (!document.getElementById('acc-lvl-pill') && val) injectIcon(val); }, 2000);
  }

  if (document.readyState === "complete") run();
  else window.addEventListener("load", run);
})();