Skip to content

Add WebSocket Bot Client#112

Merged
tgrunnagle merged 4 commits into
mainfrom
issue_97_websocket_bot_client_2026-01-26
Jan 26, 2026
Merged

Add WebSocket Bot Client#112
tgrunnagle merged 4 commits into
mainfrom
issue_97_websocket_bot_client_2026-01-26

Conversation

@tgrunnagle
Copy link
Copy Markdown
Owner

Closes #97

Summary

Implements a new WebSocketBotClient class that connects bot runners (NeuralNetBotRunner or RuleBasedBotRunner) to game servers via WebSocket for real-time gameplay. This component bridges the gap between bot inference logic and game server communication, enabling bots to join existing game rooms as real players. The client manages the WebSocket lifecycle, forwards game state updates to bot runners, and handles connection errors gracefully.

Changes Made

bot2/

  • Added WebSocketBotClient class in bot/service/websocket_bot.py
    • Manages WebSocket connection lifecycle with start(), stop(), and reset() methods
    • Injects GameClient into bot runner for action sending
    • Registers message handler to receive and forward game state updates
    • Implements error handling to prevent bot failures from crashing WebSocket listener
  • Added BotRunnerProtocol for duck typing support across bot runner types
  • Added WebSocketBotClientConfig dataclass for connection configuration
  • Created bot/service/__init__.py package with proper exports
  • Added comprehensive unit tests in tests/unit/service/test_websocket_bot.py
    • 14 test classes covering initialization, lifecycle, message handling, and edge cases
    • Uses mocking to avoid external dependencies

Claude Code Commands

  • Enhanced /code-review-for with comprehensive review process, severity categorization, and structured output format
  • Enhanced /generate-pr-summary-for with detailed generation guidelines and quality checklist
  • Enhanced /implement-issue with structured implementation process and validation steps
  • Added /address-review-feedback for systematic resolution of code review issues

Implementation Details

  • Follows the RuleBasedOpponent pattern from opponent_manager.py but generalizes to support any bot runner
  • Uses GameClient in ClientMode.WEBSOCKET for real-time communication
  • Client injection strategy: WebSocketBotClient creates the GameClient and sets it on the runner's client attribute during start()
  • Message filtering: Only processes messages with type == "GameState" and uses the cached state from client._game_state
  • State tracking: _running flag prevents processing messages after stop() is called
  • Error isolation: Wraps runner.on_game_state() calls in try/except to prevent bot errors from crashing the listener

Testing

  • Unit tests cover all key functionality:
    • Initialization with custom and default configuration
    • Client creation in WebSocket mode with correct parameters
    • Connection and room joining with proper credentials
    • Message handler registration and filtering
    • Game state forwarding to bot runner
    • Error handling (runner errors don't crash client)
    • Lifecycle safety (safe to call stop() multiple times, reset() before start)
    • Properties (player_id, is_connected) reflect correct state
  • All tests use mocking to avoid network dependencies
  • Tests follow bot2 conventions: class-based organization, AsyncMock for async methods

Additional Notes

  • No new dependencies required—uses existing GameClient and models
  • Bot runners must implement BotRunnerProtocol (requires client attribute, on_game_state() method, and reset() method)
  • This component is part of the "Add Bots to End-User Games" epic (Epic: Add Bots to End-User Games #95) and depends on Neural Network Bot Runner (Neural Network Bot Runner #96)
  • Future work: Bot Manager (TASK-003) will use this client for lifecycle management

@tgrunnagle tgrunnagle merged commit d1983f4 into main Jan 26, 2026
3 of 4 checks passed
@tgrunnagle tgrunnagle deleted the issue_97_websocket_bot_client_2026-01-26 branch January 26, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebSocket Bot Client

1 participant