Files
wibosim/server/public/js/state.js
T
db9979andClaude Opus 5.5 0aa9c9b086 0.32.0: Nachrichten zwischen Spielern im Posteingang
Ordner Nachrichten mit Unterhaltungen, Neue Nachricht, Profil-Knopf,
Einstellung wer schreiben darf (Voreinstellung Freunde & Clan),
Blockieren, Melden mit Admin-Moderation, E-Mail bei neuer Nachricht.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
2026-12-04 19:53:03 +00:00

95 lines
4.1 KiB
JavaScript

// Gemeinsamer Client-Zustand und ein kleiner Ereignisbus („was hat sich geändert“).
import { store } from './util.js';
export const S = {
info: null, // /api/info
market: null, // market (ohne tickers)
tickers: [], // ticker[]
tickerMap: new Map(),
prevLast: new Map(), // für Aufblitzen bei Kursänderung
news: [], events: [],
parliament: null, government: null, leaderboard: null, motd: '',
profile: null, // eigenes Profil (null = nicht angemeldet)
portfolio: null, battlePortfolio: null, battle: null,
fundPortfolio: null, // Clan-Fonds (nur für den Fondsmanager)
clan: null, clans: null, tournaments: null,
battles: null, quests: null, league: null, friends: null, live: [],
inbox: { items: [], unread: 0 },
dms: { convs: [], unread: 0, policy: 'friends', blocks: [] }, // Nachrichten zwischen Spielern (dm.js)
calendar: null, briefing: null, journal: null, cases: null,
ipo: null, // Börsengänge (Zeichnung, Historie)
agm: null, // Hauptversammlungen
predict: null, // Prognose-Tippspiel
plan: null, // Plan für den nächsten Handelstag
symbol: store.get('symbol', 'NVLT'),
book: null, // {depth, tape, candle, indicative, status}
candles: [],
company: null,
view: 'markt',
account: 'main', // Ticket/Depot: 'main' | 'battle' | 'fund'
simple: store.get('simple', true),
spectate: null, // beobachtetes Battle
};
const subs = new Map();
/** @param {string} key @param {Function} fn */
export function sub(key, fn) {
for (const k of key.split(' ')) {
if (!subs.has(k)) subs.set(k, new Set());
subs.get(k).add(fn);
}
}
// ---- Scroll-Verankerung
// Safari kennt keine Scroll-Verankerung: Kommen oberhalb der Leseposition Zeilen hinzu (Umsätze,
// Nachrichten, Orderbuch …), rutscht dort die ganze Seite. Vor jedem Neuzeichnen merken wir uns
// deshalb, was gerade oben im Bild steht, und gleichen danach die Verschiebung aus. Zeilen, die
// neu erzeugt werden, finden wir über id bzw. data-k/-id/-sym wieder.
const KEY_ATTRS = ['k', 'id', 'sym', 'key'];
const keySel = (e) => {
if (e.id) return `#${CSS.escape(e.id)}`;
for (const a of KEY_ATTRS) { const v = e.dataset?.[a]; if (v) return `${e.tagName.toLowerCase()}[data-${a}="${CSS.escape(v)}"]`; }
return null;
};
function anchorTake() {
if (typeof document === 'undefined' || !(window.scrollY > 0) || document.documentElement.classList.contains('modal-open')) return null;
const topH = parseFloat(document.documentElement.style.getPropertyValue('--top-h')) || 60;
let e = document.elementFromPoint(innerWidth / 2, Math.min(innerHeight - 1, topH + 24));
const list = [];
for (; e && e !== document.body && e !== document.documentElement && list.length < 12; e = e.parentElement) {
if (getComputedStyle(e).position === 'fixed') return null;
list.push({ e, sel: keySel(e), top: e.getBoundingClientRect().top });
}
return list.length ? list : null;
}
function anchorRestore(list) {
for (const a of list) {
let e = a.e.isConnected ? a.e : null;
if (!e && a.sel) e = [...document.querySelectorAll(a.sel)].find((x) => x.getClientRects().length) ?? null;
if (!e || !e.getClientRects().length) continue;
const d = e.getBoundingClientRect().top - a.top;
if (Math.abs(d) >= 1) window.scrollBy(0, d);
return;
}
}
let depth = 0;
/** @param {...string} keys */
export function changed(...keys) {
const anchor = depth++ === 0 ? anchorTake() : null;
try {
const done = new Set();
for (const k of keys) for (const fn of subs.get(k) ?? []) {
if (done.has(fn)) continue;
done.add(fn);
try { fn(k); } catch (e) { console.error(`[render ${k}]`, e); }
}
} finally {
depth--;
if (anchor) try { anchorRestore(anchor); } catch { /* nie das Rendern stören */ }
}
}
export const loggedIn = () => !!S.profile;
/** aktives Konto für Ticket/Depot */
export const activePortfolio = () => (S.account === 'battle' && S.battlePortfolio ? S.battlePortfolio : S.account === 'fund' && S.fundPortfolio ? S.fundPortfolio : S.portfolio);