Add radio channel property with stereo/mono/left/right

This commit is contained in:
Jage9
2026-02-21 01:48:20 -05:00
parent f7e8bc5949
commit 2d20e255a2
5 changed files with 72 additions and 4 deletions

View File

@@ -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",

View File

@@ -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):

View File

@@ -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()