From 2d20e255a2191611aa333d577027e6b373cc551b Mon Sep 17 00:00:00 2001 From: Jage9 Date: Sat, 21 Feb 2026 01:48:20 -0500 Subject: [PATCH] Add radio channel property with stereo/mono/left/right --- client/public/version.js | 2 +- client/src/main.ts | 24 ++++++++++++++++-- server/app/item_catalog.py | 2 +- server/app/server.py | 13 ++++++++++ server/tests/test_item_use_cooldown.py | 35 ++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/client/public/version.js b/client/public/version.js index 5746ae0..79bd528 100644 --- a/client/public/version.js +++ b/client/public/version.js @@ -1,3 +1,3 @@ // Maintainer-controlled web client version. // Format: YYYY.MM.DD Rn (example: 2026.02.20 R2) -window.CHGRID_WEB_VERSION = "2026.02.21 R77"; +window.CHGRID_WEB_VERSION = "2026.02.21 R78"; diff --git a/client/src/main.ts b/client/src/main.ts index 5fb5f21..7d16b68 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -93,6 +93,7 @@ const EDITABLE_ITEM_PROPERTY_KEYS = new Set([ 'title', 'streamUrl', 'enabled', + 'channel', 'volume', 'effect', 'effectValue', @@ -100,8 +101,11 @@ const EDITABLE_ITEM_PROPERTY_KEYS = new Set([ 'sides', 'number', ]); +const RADIO_CHANNEL_OPTIONS = ['stereo', 'mono', 'left', 'right'] as const; +type RadioChannelMode = (typeof RADIO_CHANNEL_OPTIONS)[number]; const OPTION_ITEM_PROPERTY_VALUES: Partial> = { effect: EFFECT_SEQUENCE.map((effect) => effect.id), + channel: [...RADIO_CHANNEL_OPTIONS], }; const APP_BASE_URL = import.meta.env.BASE_URL || '/'; function withBase(path: string): string { @@ -352,7 +356,7 @@ function beginItemSelection(context: 'pickup' | 'delete' | 'edit' | 'use' | 'ins function getEditableItemPropertyKeys(item: WorldItem): string[] { const keys = ['title']; if (item.type === 'radio_station') { - keys.push('streamUrl', 'enabled', 'volume', 'effect', 'effectValue'); + keys.push('streamUrl', 'enabled', 'channel', 'volume', 'effect', 'effectValue'); } else if (item.type === 'dice') { keys.push('sides', 'number'); } else if (item.type === 'wheel') { @@ -486,6 +490,12 @@ function normalizeRadioEffectValue(effectValue: unknown): number { return clampEffectLevel(effectValue); } +function normalizeRadioChannel(channel: unknown): RadioChannelMode { + if (typeof channel !== 'string') return 'stereo'; + const normalized = channel.trim().toLowerCase() as RadioChannelMode; + return (RADIO_CHANNEL_OPTIONS as readonly string[]).includes(normalized) ? normalized : 'stereo'; +} + function applyRadioEffect( output: ItemRadioOutput, audioCtx: AudioContext, @@ -575,6 +585,7 @@ function updateRadioStationSpatialAudio(): void { const normalizedVolume = Number.isFinite(volume) ? Math.max(0, Math.min(100, volume)) / 100 : 0.5; const effect = normalizeRadioEffect(item.params.effect); const effectValue = normalizeRadioEffectValue(item.params.effectValue); + const channel = normalizeRadioChannel(item.params.channel); applyRadioEffect(output, audioCtx, effect, effectValue); if (!streamUrl || !enabled) { output.gain.gain.linearRampToValueAtTime(0, audioCtx.currentTime + 0.05); @@ -593,7 +604,15 @@ function updateRadioStationSpatialAudio(): void { } output.gain.gain.linearRampToValueAtTime(gainValue * normalizedVolume, audioCtx.currentTime + 0.1); if (output.panner) { - output.panner.pan.linearRampToValueAtTime(Math.max(-1, Math.min(1, panValue)), audioCtx.currentTime + 0.1); + let resolvedPan = Math.max(-1, Math.min(1, panValue)); + if (channel === 'mono') { + resolvedPan = 0; + } else if (channel === 'left') { + resolvedPan = -1; + } else if (channel === 'right') { + resolvedPan = 1; + } + output.panner.pan.linearRampToValueAtTime(resolvedPan, audioCtx.currentTime + 0.1); } } } @@ -733,6 +752,7 @@ function getItemPropertyValue(item: WorldItem, key: string): string { if (key === 'capabilities') return item.capabilities.join(', ') || 'none'; if (key === 'useSound') return item.useSound ?? 'none'; if (key === 'enabled') return item.params.enabled === false ? 'off' : 'on'; + if (key === 'channel') return normalizeRadioChannel(item.params.channel); if (key === 'effect') return normalizeRadioEffect(item.params.effect); if (key === 'effectValue') return String(normalizeRadioEffectValue(item.params.effectValue)); const globalValue = ITEM_TYPE_GLOBAL_PROPERTIES[item.type]?.[key]; diff --git a/server/app/item_catalog.py b/server/app/item_catalog.py index 031c277..087fd6a 100644 --- a/server/app/item_catalog.py +++ b/server/app/item_catalog.py @@ -20,7 +20,7 @@ ITEM_DEFINITIONS: dict[ItemType, ItemDefinition] = { default_title="radio", capabilities=("editable", "carryable", "deletable", "usable"), use_sound=None, - default_params={"streamUrl": "", "enabled": True, "volume": 50, "effect": "off", "effectValue": 50}, + default_params={"streamUrl": "", "enabled": True, "channel": "stereo", "volume": 50, "effect": "off", "effectValue": 50}, ), "dice": ItemDefinition( default_title="Dice", diff --git a/server/app/server.py b/server/app/server.py index f184c39..018d766 100644 --- a/server/app/server.py +++ b/server/app/server.py @@ -49,6 +49,7 @@ LOGGER = logging.getLogger("chgrid.server") PACKET_LOGGER = logging.getLogger("chgrid.server.packet") CLIENT_PACKET_ADAPTER = TypeAdapter(ClientPacket) RADIO_EFFECT_IDS = {"reverb", "echo", "flanger", "high_pass", "low_pass", "off"} +RADIO_CHANNEL_IDS = {"stereo", "mono", "left", "right"} class SignalingServer: @@ -615,6 +616,18 @@ class SignalingServer: return next_params["effect"] = effect + channel = str(next_params.get("channel", "stereo")).strip().lower() + if channel not in RADIO_CHANNEL_IDS: + await self._send_item_result( + client, + False, + "update", + "channel must be one of stereo, mono, left, right.", + item.id, + ) + return + next_params["channel"] = channel + try: effect_value = int(next_params.get("effectValue", 50)) except (TypeError, ValueError): diff --git a/server/tests/test_item_use_cooldown.py b/server/tests/test_item_use_cooldown.py index 3b6b873..82e34e7 100644 --- a/server/tests/test_item_use_cooldown.py +++ b/server/tests/test_item_use_cooldown.py @@ -82,3 +82,38 @@ async def test_radio_use_toggles_enabled(monkeypatch: pytest.MonkeyPatch) -> Non assert send_payloads[-1].ok is True assert any(getattr(packet, "type", "") == "item_upsert" for packet in broadcast_payloads) + + +@pytest.mark.asyncio +async def test_radio_channel_update_validates(monkeypatch: pytest.MonkeyPatch) -> None: + server = SignalingServer("127.0.0.1", 8765, None, None) + ws = _fake_ws() + client = ClientConnection(websocket=ws, id="u1", nickname="tester", x=5, y=6) + server.clients[ws] = client + item = server.item_service.default_item(client, "radio_station") + server.item_service.add_item(item) + + send_payloads: list[object] = [] + + async def fake_send(websocket: ServerConnection, packet: object) -> None: + send_payloads.append(packet) + + async def fake_broadcast(packet: object, exclude: ServerConnection | None = None) -> None: + return + + monkeypatch.setattr(server, "_send", fake_send) + monkeypatch.setattr(server, "_broadcast", fake_broadcast) + + await server._handle_message( + client, + json.dumps({"type": "item_update", "itemId": item.id, "params": {"channel": "left"}}), + ) + assert send_payloads[-1].ok is True + assert item.params.get("channel") == "left" + + await server._handle_message( + client, + json.dumps({"type": "item_update", "itemId": item.id, "params": {"channel": "invalid"}}), + ) + assert send_payloads[-1].ok is False + assert "channel must be one of" in send_payloads[-1].message.lower()