WIP: Add input broadcasting and client-side prediction features

Changes include:
- Client: INPUT_BROADCAST packet handling and opponent prediction rendering
- Client: Protocol parsing for INPUT_BROADCAST packets
- Server: Input broadcasting to all clients except sender

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Vladyslav Doloman
2025-10-19 15:17:16 +03:00
parent c4a8501635
commit ed5cb14b30
5 changed files with 408 additions and 2 deletions

View File

@@ -571,6 +571,33 @@ class GameServer:
if player_id is None:
return
logging.debug("INPUT from player_id=%d: %d events (base_tick=%d)", player_id, len(events), base_tick)
# Apply inputs to snake buffer (client-side filtering already applied)
snake = self.runtime.state.snakes.get(player_id)
if snake is not None:
for ev in events:
# Apply input buffer rules from project plan:
# - Max capacity 3
# - If opposite to last buffered, replace last
# - Drop duplicates
# - Overflow: replace last
if len(snake.input_buf) > 0:
last_dir = snake.input_buf[-1]
# Check if opposite (XOR == 2)
if (int(ev.direction) ^ int(last_dir)) == 2:
# Replace last
snake.input_buf[-1] = ev.direction
continue
# Check if duplicate
if ev.direction == last_dir:
continue
# Add to buffer
if len(snake.input_buf) < 3:
snake.input_buf.append(ev.direction)
else:
# Overflow: replace last
snake.input_buf[-1] = ev.direction
# Relay to others immediately for prediction
await self.relay_input_broadcast(
from_player_id=player_id,