Torn Display Case Tracker (with Flags)

Track flowers and plushies in display case. Shows totals + missing vs highest-owned. Import tags with initials + flags (B.B 🏪 for Bits’n’Bobs).

当前为 2025-10-01 提交的版本,查看 最新版本

// ==UserScript==
// @name         Torn Display Case Tracker (with Flags)
// @namespace    http://tampermonkey.net/
// @version      2.3
// @description  Track flowers and plushies in display case. Shows totals + missing vs highest-owned. Import tags with initials + flags (B.B 🏪 for Bits’n’Bobs).
// @author       Nova
// @match        https://www.torn.com/displaycase.php*
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
  'use strict';

  const FLOWERS = {
    "Dahlia": "CH 🇨🇭",
    "Orchid": "HW 🇭🇹",  // Haiti / Caribbean
    "African Violet": "SA 🇿🇦",
    "Cherry Blossom": "JP 🇯🇵",
    "Peony": "CN 🇨🇳",
    "Ceibo Flower": "AR 🇦🇷",
    "Edelweiss": "CH 🇨🇭",
    "Crocus": "NL 🇳🇱",
    "Heather": "UK 🇬🇧",
    "Tribulus Omanense": "AE 🇦🇪",
    "Banana Orchid": "KY 🇰🇾"
  };

  const PLUSHIES = {
    "Sheep Plushie": "B.B 🏪",
    "Teddy Bear Plushie": "B.B 🏪",
    "Kitten Plushie": "B.B 🏪",
    "Jaguar Plushie": "MX 🇲🇽",
    "Wolverine Plushie": "CA 🇨🇦",
    "Nessie Plushie": "UK 🇬🇧",
    "Red Fox Plushie": "CA 🇨🇦",
    "Monkey Plushie": "AF 🌍",  // general Africa
    "Chamois Plushie": "CH 🇨🇭",
    "Panda Plushie": "CN 🇨🇳",
    "Lion Plushie": "SA 🇿🇦",
    "Camel Plushie": "AE 🇦🇪",
    "Stingray Plushie": "AU 🇦🇺"
  };

  GM_addStyle(`
    #setTrackerPanel {
      position: fixed;
      top: 100px;
      left: 20px;
      width: 320px;
      background: #111;
      color: #eee;
      font-family: monospace;
      font-size: 11px;
      border: 1px solid #666;
      border-radius: 6px;
      z-index: 2147483647;
      box-shadow: 0 0 8px rgba(0,0,0,0.5);
      max-height: 65vh;
      overflow-y: auto;
      line-height: 1.25;
    }
    #setTrackerHeader {
      background: #222;
      padding: 5px;
      cursor: pointer;
      font-weight: bold;
      font-size: 12px;
      border-bottom: 1px solid #444;
    }
    #setTrackerContent { padding: 6px; display: none; }
    #setTrackerPanel button {
      margin: 3px 3px 6px 0;
      font-size: 11px;
      padding: 1px 5px;
      background: #333;
      color: #eee;
      border: 1px solid #555;
      border-radius: 4px;
      cursor: pointer;
    }
    #setTrackerPanel button:hover { background: #444; }
    #setTrackerPanel ul { margin: 2px 0 6px 14px; padding:0; }
    #setTrackerPanel li { margin: 1px 0; list-style: none; }
    .item-name { display:inline-block; width:150px; }
    .item-stats { display:inline-block; width:70px; text-align:right; }
    .item-loc { display:inline-block; color:#bbb; font-size:10px; }
  `);

  const panel = document.createElement('div');
  panel.id = 'setTrackerPanel';
  panel.innerHTML = `
    <div id="setTrackerHeader">▶ Display Case Tracker</div>
    <div id="setTrackerContent">
      <div class="controls">
        <button id="tc_refresh">Refresh</button>
        <button id="tc_setkey">Set API Key</button>
        <button id="tc_resetkey">Reset Key</button>
      </div>
      <div id="tc_status">Waiting for key...</div>
      <div id="tc_content" style="margin-top:6px;"></div>
    </div>
  `;
  document.body.appendChild(panel);

  const headerEl = panel.querySelector('#setTrackerHeader');
  const contentBox = panel.querySelector('#setTrackerContent');
  headerEl.addEventListener('click', () => {
    const open = contentBox.style.display === 'block';
    contentBox.style.display = open ? 'none' : 'block';
    headerEl.textContent = (open ? '▶' : '▼') + ' Display Case Tracker';
  });

  const statusEl = panel.querySelector('#tc_status');
  const contentEl = panel.querySelector('#tc_content');
  panel.querySelector('#tc_refresh').addEventListener('click', () => loadData());
  panel.querySelector('#tc_setkey').addEventListener('click', () => askKey(true));
  panel.querySelector('#tc_resetkey').addEventListener('click', () => {
    GM_setValue('tornAPIKey', null);
    apiKey = null;
    statusEl.textContent = 'Key cleared. Click Set API Key.';
    contentEl.innerHTML = '';
  });

  let apiKey = GM_getValue('tornAPIKey', null);

  async function askKey(force) {
    if (!apiKey || force) {
      const k = prompt('Enter your Torn PUBLIC API key (public/minimal access):', apiKey || '');
      if (k) {
        apiKey = k.trim();
        GM_setValue('tornAPIKey', apiKey);
      }
    }
    if (apiKey) loadData();
  }

  function aggregateDisplay(data) {
    const items = {};
    const displayRaw = data.display || data.displaycase || null;
    if (!displayRaw) return items;

    const entries = Array.isArray(displayRaw) ? displayRaw : Object.values(displayRaw);
    for (const e of entries) {
      if (!e) continue;
      const name = e.name || e.item_name || e.title || e.item || null;
      let qty = e.quantity || e.qty || e.amount || 0;
      items[name] = (items[name] || 0) + Number(qty);
    }
    return items;
  }

  function compareMode(required, items) {
    const names = Object.keys(required);
    const counts = names.map(n => items[n] || 0);
    const highest = counts.length ? Math.max(...counts) : 0;

    const diff = {};
    names.forEach(name => {
      const total = items[name] || 0;
      diff[name] = { total, missing: highest - total, loc: required[name] };
    });
    return { highest, diff };
  }

  function render(items) {
    const flowers = compareMode(FLOWERS, items);
    const plushies = compareMode(PLUSHIES, items);

    let html = '';
    html += `<div><strong>Flowers (highest: ${flowers.highest})</strong></div><ul>`;
    Object.keys(FLOWERS).forEach(name => {
      const d = flowers.diff[name];
      html += `<li><span class="item-name">${name}</span><span class="item-stats">${d.total} (ms ${d.missing})</span><span class="item-loc">${d.loc}</span></li>`;
    });
    html += `</ul>`;

    html += `<div><strong>Plushies (highest: ${plushies.highest})</strong></div><ul>`;
    Object.keys(PLUSHIES).forEach(name => {
      const d = plushies.diff[name];
      html += `<li><span class="item-name">${name}</span><span class="item-stats">${d.total} (ms ${d.missing})</span><span class="item-loc">${d.loc}</span></li>`;
    });
    html += `</ul>`;

    contentEl.innerHTML = html;
  }

  async function loadData() {
    contentEl.innerHTML = '';
    if (!apiKey) {
      statusEl.textContent = 'No API key set. Click "Set API Key".';
      return;
    }
    statusEl.textContent = 'Fetching display via API...';
    try {
      const url = `https://api.torn.com/user/?selections=display&key=${encodeURIComponent(apiKey)}`;
      const res = await fetch(url);
      const data = await res.json();

      if (data.error) {
        statusEl.textContent = `API error: ${data.error.error} (code ${data.error.code})`;
        contentEl.innerHTML = '';
        return;
      }

      const items = aggregateDisplay(data);
      render(items);
      statusEl.textContent = 'Loaded.';
    } catch (err) {
      statusEl.textContent = 'Fetch failed.';
      contentEl.innerHTML = `<div style="color:#f88;">${err.message}</div>`;
    }
  }

  if (!apiKey) askKey(false);
  else loadData();

})();