Add persistent web chat log

This commit is contained in:
Tanner committed 2026-11-14 22:02:08 +00:00
1 parent 9be381c2de
commit 492238aa22
12 files changed
+506 -51

No files matched your search

+19 -1
View File
@@ -78,7 +78,24 @@ h1 { margin: 5px 0 3px; font-size: 34px; line-height: 1.05; letter-spacing: -.02
.food-fill { background: var(--amber); }
.content-grid { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(275px, .8fr); gap: 8px; margin-top: 8px; align-items: start; }
.inventory-column { min-width: 0; display: grid; gap: 8px; }
.inventory-panel { padding: 15px; }
.chat-panel { min-width: 0; padding: 15px; }
.chat-clear { min-height: 28px; padding: 4px 10px; border: 1px solid var(--border); border-radius: 3px; color: var(--muted); background: #171a17; font: 10px var(--mono); cursor: pointer; }
.chat-clear:hover { border-color: #68894e; color: var(--accent); background: #22291f; }
.chat-clear:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.chat-log { display: flex; min-height: 56px; max-height: 220px; flex-direction: column; overflow: auto; padding: 5px 7px; border: 1px solid var(--border-soft); border-radius: 3px; background: #111411; }
.chat-entry { display: grid; grid-template-columns: 42px 48px minmax(0, 1fr); gap: 6px; padding: 4px 2px; border-bottom: 1px solid #252a25; font-size: 11px; line-height: 1.4; }
.chat-entry:last-child { border-bottom: 0; }
.chat-entry-time { color: var(--subtle); font: 9px/1.6 var(--mono); }
.chat-entry-kind { color: #92a08d; font: 8px/1.8 var(--mono); letter-spacing: .03em; }
.chat-entry-message { min-width: 0; color: var(--text); overflow-wrap: anywhere; white-space: pre-wrap; }
.chat-entry-dm .chat-entry-kind { color: var(--amber); }
.chat-entry-outgoing .chat-entry-kind { color: var(--accent); }
.chat-entry-join .chat-entry-message { color: var(--accent); }
.chat-entry-leave .chat-entry-message { color: #d19b82; }
.chat-entry-system .chat-entry-message { color: var(--muted); }
.chat-empty { padding: 15px 4px; color: var(--muted); text-align: center; font-size: 11px; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 11px; }
h2 { margin: 3px 0 0; font-size: 20px; letter-spacing: -.01em; font-weight: 600; }
.inventory-summary { display: flex; gap: 8px; align-items: center; color: var(--muted); font: 11px var(--mono); }
@@ -242,7 +259,8 @@ h2 { margin: 3px 0 0; font-size: 20px; letter-spacing: -.01em; font-weight: 600;
.stat-card { min-height: 78px; padding: 8px; }
.stat-value { margin-top: 6px; font-size: 20px; }
.content-grid { gap: 5px; margin-top: 5px; }
.inventory-panel { padding: 10px 7px; }
.inventory-panel, .chat-panel { padding: 10px 7px; }
.chat-log { max-height: 180px; }
.inventory-summary { font-size: 10px; gap: 5px; }
.inventory-grid { --slot-gap: 3px; gap: 6px; padding: 5px; }
.slot-number { top: 2px; left: 2px; font-size: 9px; }
+129
View File
@@ -6,6 +6,8 @@
const MINIMAP_MIN_RADIUS = 4;
const MINIMAP_MAX_RADIUS = 36;
const MINIMAP_ZOOM_STEP = 4;
const CHAT_LOG_LIMIT = 300;
const CHAT_STORAGE_KEY = `mosfet.chat-log.v1:${window.location.port || 'default'}`;
const $ = (id) => document.getElementById(id);
let itemNames = {};
let lastInventorySignature = '';
@@ -19,6 +21,11 @@
let minimapLarge = false;
let minimapRequestPending = false;
let lastMinimapData = null;
const storedChatState = loadChatState();
let chatEntries = storedChatState.entries;
let chatClearBefore = storedChatState.clearBefore;
let chatSeenIds = new Set(chatEntries.map((entry) => entry.id));
let latestChatEventId = chatEntries.at(-1)?.id || chatClearBefore;
const escapeHTML = (value) => String(value ?? '').replace(/[&<>"']/g, (ch) => ({
'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'
@@ -578,6 +585,118 @@
positionFloating(menu, rect.left, rect.bottom + 4);
}
function loadChatState() {
try {
const stored = JSON.parse(localStorage.getItem(CHAT_STORAGE_KEY) || '{}');
const entries = Array.isArray(stored.entries)
? stored.entries.filter((entry) => entry && typeof entry.id === 'string')
.slice(-CHAT_LOG_LIMIT)
: [];
return {
entries,
clearBefore: typeof stored.clearBefore === 'string'
? stored.clearBefore : '',
};
} catch (_) {
return {entries: [], clearBefore: ''};
}
}
function saveChatState() {
try {
localStorage.setItem(CHAT_STORAGE_KEY, JSON.stringify({
entries: chatEntries,
clearBefore: chatClearBefore,
}));
} catch (_) {
// Keep the current tab usable when storage is disabled or full.
}
}
function renderChatLog() {
const log = $('chat-log');
const stickToBottom = log.scrollHeight - log.scrollTop - log.clientHeight < 24;
log.replaceChildren();
if (!chatEntries.length) {
const empty = document.createElement('div');
empty.className = 'chat-empty';
empty.textContent = 'No chat messages yet';
log.append(empty);
return;
}
for (const event of chatEntries) {
const row = document.createElement('div');
row.className = `chat-entry chat-entry-${event.type}`;
const timestamp = Number(event.timestamp);
const time = document.createElement('time');
time.className = 'chat-entry-time';
if (Number.isFinite(timestamp)) {
const date = new Date(timestamp * 1000);
time.dateTime = date.toISOString();
time.textContent = date.toLocaleTimeString([], {
hour: '2-digit', minute: '2-digit', hour12: false,
});
} else {
time.textContent = '';
}
const kind = document.createElement('span');
kind.className = 'chat-entry-kind';
kind.textContent = ({
chat: 'CHAT', dm: 'DM', outgoing: 'SENT', system: 'SERVER',
join: 'JOIN', leave: 'LEAVE',
})[event.type] || 'SERVER';
const message = document.createElement('span');
message.className = 'chat-entry-message';
if (event.type === 'chat' || event.type === 'dm' ||
event.type === 'outgoing') {
message.textContent = `${event.sender || 'Unknown'}: ${event.message}`;
} else {
message.textContent = event.message;
}
row.append(time, kind, message);
log.append(row);
}
if (stickToBottom) log.scrollTop = log.scrollHeight;
}
function syncChatLog(events) {
if (!Array.isArray(events)) return;
let changed = false;
for (const event of events) {
if (!event || typeof event.id !== 'string' ||
!String(event.message || '').trim()) continue;
if (!latestChatEventId || event.id > latestChatEventId) {
latestChatEventId = event.id;
}
if (chatClearBefore && event.id <= chatClearBefore) {
chatSeenIds.add(event.id);
continue;
}
if (chatSeenIds.has(event.id)) continue;
const type = ['chat', 'dm', 'outgoing', 'system', 'join', 'leave'].includes(event.type)
? event.type : 'system';
chatEntries.push({
id: event.id,
timestamp: Number(event.timestamp),
type,
sender: String(event.sender || ''),
message: String(event.message || ''),
});
chatSeenIds.add(event.id);
changed = true;
}
if (chatEntries.length > CHAT_LOG_LIMIT) {
const removed = chatEntries.splice(0, chatEntries.length - CHAT_LOG_LIMIT);
removed.forEach((event) => chatSeenIds.delete(event.id));
changed = true;
}
if (changed) {
saveChatState();
renderChatLog();
}
}
function showToast(message, isError = false) {
const toast = $('toast');
toast.textContent = message;
@@ -688,6 +807,7 @@
renderInventory(state);
renderPlayers(state);
syncChatLog(state.chat_log);
$('last-update').textContent = `Updated ${new Date().toLocaleTimeString()}`;
}
@@ -827,6 +947,14 @@
positionFloating($('inventory-menu'), rect.left, rect.bottom + 4);
});
$('chat-clear').addEventListener('click', () => {
chatClearBefore = latestChatEventId || chatEntries.at(-1)?.id || chatClearBefore;
chatEntries = [];
chatSeenIds.clear();
saveChatState();
renderChatLog();
});
$('minimap-zoom-out').addEventListener('click', () => {
minimapRadius = Math.min(MINIMAP_MAX_RADIUS,
minimapRadius + MINIMAP_ZOOM_STEP);
@@ -869,6 +997,7 @@
});
loadItems();
renderChatLog();
drawMinimap(null);
poll();
pollMinimap();
+31 -16
View File
@@ -52,24 +52,39 @@
</section>
<section class="content-grid">
<section class="inventory-panel panel">
<div class="section-heading">
<div>
<div class="eyebrow">PLAYER STORAGE</div>
<h2>Inventory</h2>
<div class="inventory-column">
<section class="inventory-panel panel">
<div class="section-heading">
<div>
<div class="eyebrow">PLAYER STORAGE</div>
<h2>Inventory</h2>
</div>
<div class="inventory-summary">
<span id="inventory-count">0 items</span>
<span class="heading-divider"></span>
<span id="inventory-slots">0 / 46 slots</span>
</div>
</div>
<div class="inventory-summary">
<span id="inventory-count">0 items</span>
<span class="heading-divider"></span>
<span id="inventory-slots">0 / 46 slots</span>
<div id="inventory-warning" class="inventory-warning" hidden>
Some item components are not decoded yet; displayed contents may be partial.
</div>
</div>
<div id="inventory-warning" class="inventory-warning" hidden>
Some item components are not decoded yet; displayed contents may be partial.
</div>
<div id="inventory-grid" class="inventory-grid" aria-label="Minecraft inventory"></div>
<div class="inventory-legend"><span class="legend-swatch"></span> Empty slot <span class="legend-selected"></span> Selected hotbar slot <span class="legend-note">Hover for details · click an item for actions</span></div>
</section>
<div id="inventory-grid" class="inventory-grid" aria-label="Minecraft inventory"></div>
<div class="inventory-legend"><span class="legend-swatch"></span> Empty slot <span class="legend-selected"></span> Selected hotbar slot <span class="legend-note">Hover for details · click an item for actions</span></div>
</section>
<section class="chat-panel panel" aria-label="Server chat log">
<div class="section-heading">
<div>
<div class="eyebrow">SERVER · CHAT</div>
<h2>Chat Log</h2>
</div>
<button id="chat-clear" class="chat-clear" type="button">Clear</button>
</div>
<div id="chat-log" class="chat-log" role="log" aria-label="Chat messages">
<div class="chat-empty">No chat messages yet</div>
</div>
</section>
</div>
<aside class="side-column">
<section id="minimap-panel" class="side-panel panel minimap-panel">