Add dashboard chat controls and suppress echoes
This commit is contained in:
1 parent
5a3b299aa9
commit
c3a045d17a
10 files changed
+178
-13
No files matched your search
@@ -96,6 +96,17 @@ h1 { margin: 5px 0 3px; font-size: 34px; line-height: 1.05; letter-spacing: -.02
|
||||
.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; }
|
||||
.chat-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 5px; margin-top: 7px; }
|
||||
.chat-form input { min-width: 0; height: 32px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 3px; outline: none; color: var(--text); background: #111411; font: 11px var(--sans); }
|
||||
.chat-form input:focus { border-color: #68894e; }
|
||||
.chat-form input::placeholder { color: var(--subtle); }
|
||||
.chat-form input:disabled { opacity: .55; }
|
||||
.chat-form button { min-width: 56px; padding: 4px 9px; border: 1px solid #536a42; border-radius: 3px; color: var(--accent); background: #22291f; font: 10px var(--mono); cursor: pointer; }
|
||||
.chat-form button:hover:not(:disabled) { border-color: var(--accent); background: #2c3825; }
|
||||
.chat-form button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
|
||||
.chat-form button:disabled { opacity: .45; cursor: not-allowed; }
|
||||
.chat-send-status { min-height: 14px; margin-top: 4px; color: var(--subtle); font-size: 10px; }
|
||||
.chat-send-status.is-error { color: var(--red); }
|
||||
.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); }
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
let minimapNorthLocked = true;
|
||||
let minimapLarge = false;
|
||||
let minimapRequestPending = false;
|
||||
let chatSendPending = false;
|
||||
let lastMinimapData = null;
|
||||
const storedChatState = loadChatState();
|
||||
let chatEntries = storedChatState.entries;
|
||||
@@ -676,12 +677,27 @@
|
||||
if (chatSeenIds.has(event.id)) continue;
|
||||
const type = ['chat', 'dm', 'outgoing', 'system', 'join', 'leave'].includes(event.type)
|
||||
? event.type : 'system';
|
||||
const sender = String(event.sender || '');
|
||||
const message = String(event.message || '');
|
||||
const eventTime = Number(event.timestamp);
|
||||
const botName = String(lastState?.name || '').toLowerCase();
|
||||
// Servers echo sent messages back as player chat; keep the SENT entry only.
|
||||
const isBotEcho = type === 'chat' && botName &&
|
||||
sender.toLowerCase() === botName && chatEntries.some((sent) => {
|
||||
const age = eventTime - Number(sent.timestamp);
|
||||
return sent.type === 'outgoing' && sent.sender === event.sender &&
|
||||
sent.message === message && age >= -1 && age <= 30;
|
||||
});
|
||||
if (isBotEcho) {
|
||||
chatSeenIds.add(event.id);
|
||||
continue;
|
||||
}
|
||||
chatEntries.push({
|
||||
id: event.id,
|
||||
timestamp: Number(event.timestamp),
|
||||
timestamp: eventTime,
|
||||
type,
|
||||
sender: String(event.sender || ''),
|
||||
message: String(event.message || ''),
|
||||
sender,
|
||||
message,
|
||||
});
|
||||
chatSeenIds.add(event.id);
|
||||
changed = true;
|
||||
@@ -706,6 +722,43 @@
|
||||
toastTimer = window.setTimeout(() => toast.classList.remove('visible'), 2400);
|
||||
}
|
||||
|
||||
async function sendChatMessage(event) {
|
||||
event.preventDefault();
|
||||
const input = $('chat-input');
|
||||
const send = $('chat-send');
|
||||
const status = $('chat-send-status');
|
||||
const message = input.value.trim();
|
||||
if (!message) return;
|
||||
|
||||
chatSendPending = true;
|
||||
input.disabled = true;
|
||||
send.disabled = true;
|
||||
status.classList.remove('is-error');
|
||||
status.textContent = 'Sending…';
|
||||
try {
|
||||
const response = await fetch('/api/chat', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({message}),
|
||||
});
|
||||
const result = await response.json();
|
||||
if (!response.ok || !result.ok) {
|
||||
throw new Error(result.error || `API ${response.status}`);
|
||||
}
|
||||
input.value = '';
|
||||
status.textContent = result.command ? 'Command sent' : 'Message sent';
|
||||
} catch (error) {
|
||||
status.textContent = error.message;
|
||||
status.classList.add('is-error');
|
||||
} finally {
|
||||
chatSendPending = false;
|
||||
const online = Boolean(lastState?.connected) &&
|
||||
lastState.connection_status === 'in_game';
|
||||
input.disabled = !online;
|
||||
send.disabled = !online;
|
||||
}
|
||||
}
|
||||
|
||||
async function runInventoryAction(action) {
|
||||
if (!selectedInventoryItem) return;
|
||||
const selected = {...selectedInventoryItem};
|
||||
@@ -804,6 +857,8 @@
|
||||
$('afk-state').textContent = state.afk ? 'YES' : 'NO';
|
||||
$('movement-error').hidden = !state.movement_error;
|
||||
$('movement-error').textContent = state.movement_error ? `Movement error: ${state.movement_error}` : '';
|
||||
$('chat-input').disabled = !online || chatSendPending;
|
||||
$('chat-send').disabled = !online || chatSendPending;
|
||||
|
||||
renderInventory(state);
|
||||
renderPlayers(state);
|
||||
@@ -947,6 +1002,7 @@
|
||||
positionFloating($('inventory-menu'), rect.left, rect.bottom + 4);
|
||||
});
|
||||
|
||||
$('chat-form').addEventListener('submit', sendChatMessage);
|
||||
$('chat-clear').addEventListener('click', () => {
|
||||
chatClearBefore = latestChatEventId || chatEntries.at(-1)?.id || chatClearBefore;
|
||||
chatEntries = [];
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
<meta name="theme-color" content="#10130f" />
|
||||
<meta name="description" content="Live Minecraft bot dashboard" />
|
||||
<title>Mosfet · Minecraft Bot</title>
|
||||
<link rel="stylesheet" href="/static/css/dashboard.css?v=20" />
|
||||
<script defer src="/static/js/dashboard.js?v=23"></script>
|
||||
<link rel="stylesheet" href="/static/css/dashboard.css?v=21" />
|
||||
<script defer src="/static/js/dashboard.js?v=25"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="shell">
|
||||
@@ -83,6 +83,13 @@
|
||||
<div id="chat-log" class="chat-log" role="log" aria-label="Chat messages">
|
||||
<div class="chat-empty">No chat messages yet</div>
|
||||
</div>
|
||||
<form id="chat-form" class="chat-form">
|
||||
<input id="chat-input" type="text" maxlength="256"
|
||||
placeholder="Message or /command…" aria-label="Chat message or command"
|
||||
autocomplete="off" disabled>
|
||||
<button id="chat-send" type="submit" disabled>Send</button>
|
||||
</form>
|
||||
<div id="chat-send-status" class="chat-send-status" role="status" aria-live="polite"></div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in new issue
Block a user