Add server chat slash commands for me and uptime

This commit is contained in:
Jage9
2026-02-27 04:33:54 -05:00
parent 10e7a01e73
commit 464d39f78b
9 changed files with 179 additions and 2 deletions

View File

@@ -40,6 +40,10 @@
"keys": "Slash", "keys": "Slash",
"description": "Start chat" "description": "Start chat"
}, },
{
"keys": "Slash commands",
"description": "In chat, use /me <action> for action text or /up for server uptime"
},
{ {
"keys": "Comma / Period", "keys": "Comma / Period",
"description": "Previous/next message" "description": "Previous/next message"

View File

@@ -1,5 +1,5 @@
// Maintainer-controlled web client version. // Maintainer-controlled web client version.
// Format: YYYY.MM.DD Rn (example: 2026.02.20 R2) // Format: YYYY.MM.DD Rn (example: 2026.02.20 R2)
window.CHGRID_WEB_VERSION = "2026.02.27 R292"; window.CHGRID_WEB_VERSION = "2026.02.27 R293";
// Optional display timezone for timestamps. Falls back to America/Detroit if unset/invalid. // Optional display timezone for timestamps. Falls back to America/Detroit if unset/invalid.
window.CHGRID_TIME_ZONE = "America/Detroit"; window.CHGRID_TIME_ZONE = "America/Detroit";

View File

@@ -238,6 +238,7 @@ const SYSTEM_SOUND_URLS = {
logout: withBase('sounds/logout.ogg'), logout: withBase('sounds/logout.ogg'),
notify: withBase('sounds/notify.ogg'), notify: withBase('sounds/notify.ogg'),
} as const; } as const;
const ACTION_SOUND_URL = SYSTEM_SOUND_URLS.notify;
const FOOTSTEP_SOUND_URLS = Array.from({ length: 11 }, (_, index) => withBase(`sounds/step-${index + 1}.ogg`)); const FOOTSTEP_SOUND_URLS = Array.from({ length: 11 }, (_, index) => withBase(`sounds/step-${index + 1}.ogg`));
const FOOTSTEP_GAIN = 0.7; const FOOTSTEP_GAIN = 0.7;
const TELEPORT_START_SOUND_URL = withBase('sounds/teleport_start.ogg'); const TELEPORT_START_SOUND_URL = withBase('sounds/teleport_start.ogg');
@@ -1820,6 +1821,7 @@ const onAppMessage = createOnMessageHandler({
getAudioLayers: () => audioLayers, getAudioLayers: () => audioLayers,
pushChatMessage, pushChatMessage,
classifySystemMessageSound, classifySystemMessageSound,
ACTION_SOUND_URL,
SYSTEM_SOUND_URLS, SYSTEM_SOUND_URLS,
playSample: (url, gain = 1) => { playSample: (url, gain = 1) => {
void audio.playSample(url, gain); void audio.playSample(url, gain);

View File

@@ -54,6 +54,7 @@ type MessageHandlerDeps = {
getAudioLayers: () => { world: boolean; item: boolean }; getAudioLayers: () => { world: boolean; item: boolean };
pushChatMessage: (message: string) => void; pushChatMessage: (message: string) => void;
classifySystemMessageSound: (message: string) => 'logon' | 'logout' | 'notify' | null; classifySystemMessageSound: (message: string) => 'logon' | 'logout' | 'notify' | null;
ACTION_SOUND_URL: string;
SYSTEM_SOUND_URLS: { logon: string; logout: string; notify: string }; SYSTEM_SOUND_URLS: { logon: string; logout: string; notify: string };
playSample: (url: string, gain?: number) => void; playSample: (url: string, gain?: number) => void;
updateStatus: (message: string) => void; updateStatus: (message: string) => void;
@@ -225,7 +226,10 @@ export function createOnMessageHandler(deps: MessageHandlerDeps): (message: Inco
} }
case 'chat_message': { case 'chat_message': {
if (message.system) { if (message.action) {
deps.pushChatMessage(message.message);
deps.playSample(deps.ACTION_SOUND_URL, 1);
} else if (message.system) {
deps.pushChatMessage(message.message); deps.pushChatMessage(message.message);
const sound = deps.classifySystemMessageSound(message.message); const sound = deps.classifySystemMessageSound(message.message);
if (sound) { if (sound) {

View File

@@ -185,6 +185,7 @@ export const chatMessageSchema = z.object({
senderId: z.string().optional(), senderId: z.string().optional(),
senderNickname: z.string().optional(), senderNickname: z.string().optional(),
system: z.boolean().optional(), system: z.boolean().optional(),
action: z.boolean().optional(),
}); });
export const pongSchema = z.object({ export const pongSchema = z.object({

View File

@@ -16,6 +16,9 @@ This document is the authoritative keymap for the client.
- `U`: Speak connected users - `U`: Speak connected users
- `N`: Edit nickname - `N`: Edit nickname
- `/`: Start chat - `/`: Start chat
- In chat, commands are supported when `/` is the first character:
- `/me <action>`: Send action text without `name:`
- `/up`: Show server uptime (self only)
- `Shift+Z`: Admin menu (when role permissions allow) - `Shift+Z`: Admin menu (when role permissions allow)
- `,` / `.`: Previous/next message - `,` / `.`: Previous/next message
- `<` / `>`: First/last message - `<` / `>`: First/last message

View File

@@ -280,6 +280,7 @@ class BroadcastChatMessagePacket(BasePacket):
senderId: str | None = None senderId: str | None = None
senderNickname: str | None = None senderNickname: str | None = None
system: bool = False system: bool = False
action: bool = False
class PongPacket(BasePacket): class PongPacket(BasePacket):

View File

@@ -184,6 +184,7 @@ class SignalingServer:
self._clock_announce_task: asyncio.Task[None] | None = None self._clock_announce_task: asyncio.Task[None] | None = None
self._clock_top_of_hour_markers: dict[str, str] = {} self._clock_top_of_hour_markers: dict[str, str] = {}
self._clock_alarm_markers: dict[str, str] = {} self._clock_alarm_markers: dict[str, str] = {}
self._started_at_monotonic = time.monotonic()
@staticmethod @staticmethod
def _resolve_server_version() -> str: def _resolve_server_version() -> str:
@@ -1572,6 +1573,81 @@ class SignalingServer:
AdminActionResultPacket(type="admin_action_result", ok=ok, action=action, message=message), AdminActionResultPacket(type="admin_action_result", ok=ok, action=action, message=message),
) )
@staticmethod
def _format_duration(total_seconds: int) -> str:
"""Format a duration value as compact human-readable text."""
seconds = max(0, int(total_seconds))
days, remainder = divmod(seconds, 24 * 60 * 60)
hours, remainder = divmod(remainder, 60 * 60)
minutes, secs = divmod(remainder, 60)
parts: list[str] = []
if days:
parts.append(f"{days}d")
if hours:
parts.append(f"{hours}h")
if minutes:
parts.append(f"{minutes}m")
if secs or not parts:
parts.append(f"{secs}s")
return " ".join(parts)
def _format_uptime(self) -> str:
"""Return current server uptime text."""
elapsed_seconds = int(max(0.0, time.monotonic() - self._started_at_monotonic))
return self._format_duration(elapsed_seconds)
async def _handle_chat_command(self, client: ClientConnection, message: str) -> bool:
"""Handle slash commands in chat input; return True when handled."""
if not message.startswith("/"):
return False
command_line = message[1:]
command_token, separator, remainder = command_line.partition(" ")
command = command_token.casefold()
if command == "me":
if not separator or remainder == "":
await self._send(
client.websocket,
BroadcastChatMessagePacket(
type="chat_message",
message="Usage: /me <action>",
system=True,
),
)
return True
await self._broadcast(
BroadcastChatMessagePacket(
type="chat_message",
message=f"{client.nickname} {remainder}",
senderId=client.id,
senderNickname=client.nickname,
system=False,
action=True,
)
)
return True
if command == "up":
await self._send(
client.websocket,
BroadcastChatMessagePacket(
type="chat_message",
message=f"Server uptime: {self._format_uptime()}",
system=True,
),
)
return True
await self._send(
client.websocket,
BroadcastChatMessagePacket(
type="chat_message",
message=f"Unknown command: /{command_token}",
system=True,
),
)
return True
async def _handle_admin_packet(self, client: ClientConnection, packet: ClientPacket) -> bool: async def _handle_admin_packet(self, client: ClientConnection, packet: ClientPacket) -> bool:
"""Handle role/user administration packets with permission checks.""" """Handle role/user administration packets with permission checks."""
@@ -2023,6 +2099,8 @@ class SignalingServer:
), ),
) )
return return
if await self._handle_chat_command(client, packet.message):
return
await self._broadcast( await self._broadcast(
BroadcastChatMessagePacket( BroadcastChatMessagePacket(
type="chat_message", type="chat_message",

View File

@@ -413,3 +413,87 @@ async def test_update_position_rate_reject_sends_self_correction(monkeypatch: py
assert correction.id == "u1" assert correction.id == "u1"
assert correction.x == 5 assert correction.x == 5
assert correction.y == 5 assert correction.y == 5
@pytest.mark.asyncio
async def test_chat_me_command_broadcasts_action(monkeypatch: pytest.MonkeyPatch) -> None:
server = SignalingServer("127.0.0.1", 8765, None, None)
ws = _fake_ws()
client = ClientConnection(websocket=ws, id="u1", nickname="Tester")
server.clients[ws] = client
broadcast_payloads: list[object] = []
send_payloads: list[object] = []
async def fake_broadcast(packet: object, exclude: ServerConnection | None = None) -> None:
broadcast_payloads.append(packet)
async def fake_send(websocket: ServerConnection, packet: object) -> None:
send_payloads.append(packet)
monkeypatch.setattr(server, "_broadcast", fake_broadcast)
monkeypatch.setattr(server, "_send", fake_send)
await server._handle_message(client, json.dumps({"type": "chat_message", "message": "/Me waves hello"}))
assert send_payloads == []
assert len(broadcast_payloads) == 1
packet = broadcast_payloads[0]
assert getattr(packet, "type", "") == "chat_message"
assert packet.action is True
assert packet.system is False
assert packet.message == "Tester waves hello"
@pytest.mark.asyncio
async def test_chat_up_command_sends_sender_only(monkeypatch: pytest.MonkeyPatch) -> None:
server = SignalingServer("127.0.0.1", 8765, None, None)
ws = _fake_ws()
client = ClientConnection(websocket=ws, id="u1", nickname="Tester")
server.clients[ws] = client
broadcast_payloads: list[object] = []
send_payloads: list[object] = []
async def fake_broadcast(packet: object, exclude: ServerConnection | None = None) -> None:
broadcast_payloads.append(packet)
async def fake_send(websocket: ServerConnection, packet: object) -> None:
send_payloads.append(packet)
monkeypatch.setattr(server, "_broadcast", fake_broadcast)
monkeypatch.setattr(server, "_send", fake_send)
monkeypatch.setattr(server, "_format_uptime", lambda: "1h 2m 3s")
await server._handle_message(client, json.dumps({"type": "chat_message", "message": "/UP"}))
assert broadcast_payloads == []
assert len(send_payloads) == 1
packet = send_payloads[0]
assert getattr(packet, "type", "") == "chat_message"
assert packet.system is True
assert packet.message == "Server uptime: 1h 2m 3s"
@pytest.mark.asyncio
async def test_chat_command_requires_leading_slash(monkeypatch: pytest.MonkeyPatch) -> None:
server = SignalingServer("127.0.0.1", 8765, None, None)
ws = _fake_ws()
client = ClientConnection(websocket=ws, id="u1", nickname="Tester")
server.clients[ws] = client
broadcast_payloads: list[object] = []
async def fake_broadcast(packet: object, exclude: ServerConnection | None = None) -> None:
broadcast_payloads.append(packet)
monkeypatch.setattr(server, "_broadcast", fake_broadcast)
await server._handle_message(client, json.dumps({"type": "chat_message", "message": " /up"}))
assert len(broadcast_payloads) == 1
packet = broadcast_payloads[0]
assert getattr(packet, "type", "") == "chat_message"
assert packet.system is False
assert packet.action is False
assert packet.message == " /up"