Improve piano instruments, previews, and key-stream safeguards

This commit is contained in:
Jage9
2026-02-22 23:51:13 -05:00
parent 1319c044dd
commit 89c6aa7e9b
10 changed files with 176 additions and 10 deletions

View File

@@ -32,9 +32,22 @@ INSTRUMENT_OPTIONS: tuple[str, ...] = (
"bass",
"violin",
"synth_lead",
"nintendo",
"drum_kit",
)
DEFAULT_ENVELOPE_BY_INSTRUMENT: dict[str, tuple[int, int]] = {
"piano": (15, 45),
"electric_piano": (12, 40),
"guitar": (8, 35),
"organ": (25, 70),
"bass": (10, 35),
"violin": (22, 75),
"synth_lead": (6, 30),
"nintendo": (2, 28),
"drum_kit": (1, 22),
}
PROPERTY_METADATA: dict[str, dict[str, object]] = {
"title": {"valueType": "text", "tooltip": "Display name spoken and shown for this item.", "maxLength": 80},
"instrument": {"valueType": "list", "tooltip": "Instrument voice used when playing this piano."},
@@ -62,6 +75,7 @@ def validate_update(_item: WorldItem, next_params: dict) -> dict:
instrument = str(next_params.get("instrument", "piano")).strip().lower()
if instrument not in INSTRUMENT_OPTIONS:
raise ValueError(f"instrument must be one of: {', '.join(INSTRUMENT_OPTIONS)}.")
previous_instrument = str(_item.params.get("instrument", "piano")).strip().lower()
next_params["instrument"] = instrument
try:
@@ -70,14 +84,17 @@ def validate_update(_item: WorldItem, next_params: dict) -> dict:
raise ValueError("attack must be an integer between 0 and 100.") from exc
if not (0 <= attack <= 100):
raise ValueError("attack must be between 0 and 100.")
next_params["attack"] = attack
try:
decay = int(next_params.get("decay", 45))
except (TypeError, ValueError) as exc:
raise ValueError("decay must be an integer between 0 and 100.") from exc
if not (0 <= decay <= 100):
raise ValueError("decay must be between 0 and 100.")
# When instrument changes, reset envelope to instrument-appropriate defaults.
if instrument != previous_instrument:
attack, decay = DEFAULT_ENVELOPE_BY_INSTRUMENT.get(instrument, (15, 45))
next_params["attack"] = attack
next_params["decay"] = decay
try:

View File

@@ -68,6 +68,7 @@ from .models import (
LOGGER = logging.getLogger("chgrid.server")
PACKET_LOGGER = logging.getLogger("chgrid.server.packet")
CLIENT_PACKET_ADAPTER = TypeAdapter(ClientPacket)
MAX_ACTIVE_PIANO_KEYS_PER_CLIENT = 32
class SignalingServer:
@@ -92,6 +93,7 @@ class SignalingServer:
self.clients: dict[ServerConnection, ClientConnection] = {}
self.item_service = ItemService(state_file=state_file)
self.item_last_use_ms: dict[str, int] = {}
self.active_piano_keys_by_client: dict[str, set[str]] = {}
self.grid_size = max(1, grid_size)
self.instance_id = str(uuid.uuid4())
self.server_version = self._resolve_server_version()
@@ -260,6 +262,7 @@ class SignalingServer:
finally:
if websocket in self.clients:
disconnected = self.clients.pop(websocket)
self.active_piano_keys_by_client.pop(disconnected.id, None)
for item in self.item_service.drop_carried_items_for_disconnect(disconnected):
await self._broadcast_item(item)
self.item_service.save_state()
@@ -666,6 +669,13 @@ class SignalingServer:
return
if item.carrierId is None and (item.x != client.x or item.y != client.y):
return
active_keys = self.active_piano_keys_by_client.setdefault(client.id, set())
if packet.on:
if packet.keyId not in active_keys and len(active_keys) >= MAX_ACTIVE_PIANO_KEYS_PER_CLIENT:
return
active_keys.add(packet.keyId)
else:
active_keys.discard(packet.keyId)
instrument = str(item.params.get("instrument", "piano")).strip().lower()
attack = int(item.params.get("attack", 15)) if isinstance(item.params.get("attack", 15), (int, float)) else 15
decay = int(item.params.get("decay", 45)) if isinstance(item.params.get("decay", 45), (int, float)) else 45