// ==UserScript==
// @name Torn Display Case Tracker (Thin Font + Flags)
// @namespace http://tampermonkey.net/
// @version 2.4
// @description Track flowers and plushies in display case. Thin font, initials + flags, cleaner spacing to avoid overlap.
// @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 🇭🇹",
"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 🌍",
"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: 290px;
background: #111;
color: #eee;
font-family: monospace, monospace;
font-size: 10px;
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.3;
}
#setTrackerHeader {
background: #222;
padding: 4px;
cursor: pointer;
font-weight: bold;
font-size: 11px;
border-bottom: 1px solid #444;
}
#setTrackerContent { padding: 5px; display: none; }
#setTrackerPanel button {
margin: 2px 2px 5px 0;
font-size: 10px;
padding: 1px 4px;
background: #333;
color: #eee;
border: 1px solid #555;
border-radius: 3px;
cursor: pointer;
}
#setTrackerPanel button:hover { background: #444; }
#setTrackerPanel ul { margin: 2px 0 5px 10px; padding:0; }
#setTrackerPanel li { margin: 1px 0; list-style: none; }
.item-name { display:inline-block; width:130px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.item-stats { display:inline-block; width:60px; text-align:right; }
.item-loc { display:inline-block; width:70px; text-align:right; color:#bbb; font-size:9px; }
`);
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:5px;"></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();
})();