refactor: major overhaul of game engine architecture #2

Closed
fedir wants to merge 1 commits from refactor-game-engine into main
Owner

Complete modular refactoring of taletorrent game engine:

Core Architecture Changes

  • Split monolithic main.py (343 lines) into 6 focused modules
  • Implement offensive error propagation with context wrapping
  • Add JSON configuration system with default prompts and settings
  • Modernize type hints and validation

New Modules

  • errors.py: Custom exceptions (GameError, AIError, ConfigError, etc.) with wrap_error()
  • models.py: Data classes (World, Character, Turn, Item) with validation
  • config.py: JSON configuration management with defaults and validation
  • prompts.py: Template-based prompt management extracted from code
  • ai_client.py: OpenAI wrapper with retry logic and error propagation
  • game_engine.py: Core game logic with explicit dependency injection

Key Features

  • Error propagation: API errors → 'Failed to reach provider: 403' → 'Failed to generate DM turn: ...'
  • Configuration over code: All prompts moved to JSON config files
  • Retry logic: Exponential backoff for API failures
  • Input validation: Strict validation for all public functions
  • Logging: Optional stderr logging with --enable-logging flag
  • Modern Python: list[str], str | None syntax, immutable returns

CLI Enhancements

  • play command: Interactive game loop with character creation
  • validate-config: Configuration validation command
  • info command: Shows both API and game configuration
  • Interactive setup wizard for API credentials

Breaking Changes

  • Removed main.py (no backward compatibility)
  • Function signatures changed to use explicit dependencies
  • BSM renamed to bsm (Python naming convention)
  • All prompts now configurable via JSON files

Testing

  • All imports work correctly
  • CLI commands functional (config, info, validate-config, play)
  • Configuration file creation and validation working
  • Ruff linting passes with auto-fixes
  • Type checking shows warnings but no critical errors

This refactoring transforms the codebase from a monolithic structure to a modular, maintainable architecture ready for extension and code review.

Complete modular refactoring of taletorrent game engine: ## Core Architecture Changes - Split monolithic main.py (343 lines) into 6 focused modules - Implement offensive error propagation with context wrapping - Add JSON configuration system with default prompts and settings - Modernize type hints and validation ## New Modules - errors.py: Custom exceptions (GameError, AIError, ConfigError, etc.) with wrap_error() - models.py: Data classes (World, Character, Turn, Item) with validation - config.py: JSON configuration management with defaults and validation - prompts.py: Template-based prompt management extracted from code - ai_client.py: OpenAI wrapper with retry logic and error propagation - game_engine.py: Core game logic with explicit dependency injection ## Key Features - Error propagation: API errors → 'Failed to reach provider: 403' → 'Failed to generate DM turn: ...' - Configuration over code: All prompts moved to JSON config files - Retry logic: Exponential backoff for API failures - Input validation: Strict validation for all public functions - Logging: Optional stderr logging with --enable-logging flag - Modern Python: list[str], str | None syntax, immutable returns ## CLI Enhancements - play command: Interactive game loop with character creation - validate-config: Configuration validation command - info command: Shows both API and game configuration - Interactive setup wizard for API credentials ## Breaking Changes - Removed main.py (no backward compatibility) - Function signatures changed to use explicit dependencies - BSM renamed to bsm (Python naming convention) - All prompts now configurable via JSON files ## Testing - All imports work correctly - CLI commands functional (config, info, validate-config, play) - Configuration file creation and validation working - Ruff linting passes with auto-fixes - Type checking shows warnings but no critical errors This refactoring transforms the codebase from a monolithic structure to a modular, maintainable architecture ready for extension and code review.
fedir added 1 commit 2026-01-27 10:31:07 +01:00
Complete modular refactoring of taletorrent game engine:

## Core Architecture Changes
- Split monolithic main.py (343 lines) into 6 focused modules
- Implement offensive error propagation with context wrapping
- Add JSON configuration system with default prompts and settings
- Modernize type hints and validation

## New Modules
- errors.py: Custom exceptions (GameError, AIError, ConfigError, etc.) with wrap_error()
- models.py: Data classes (World, Character, Turn, Item) with validation
- config.py: JSON configuration management with defaults and validation
- prompts.py: Template-based prompt management extracted from code
- ai_client.py: OpenAI wrapper with retry logic and error propagation
- game_engine.py: Core game logic with explicit dependency injection

## Key Features
- Error propagation: API errors → 'Failed to reach provider: 403' → 'Failed to generate DM turn: ...'
- Configuration over code: All prompts moved to JSON config files
- Retry logic: Exponential backoff for API failures
- Input validation: Strict validation for all public functions
- Logging: Optional stderr logging with --enable-logging flag
- Modern Python: list[str], str | None syntax, immutable returns

## CLI Enhancements
- play command: Interactive game loop with character creation
- validate-config: Configuration validation command
- info command: Shows both API and game configuration
- Interactive setup wizard for API credentials

## Breaking Changes
- Removed main.py (no backward compatibility)
- Function signatures changed to use explicit dependencies
- BSM renamed to bsm (Python naming convention)
- All prompts now configurable via JSON files

## Testing
- All imports work correctly
- CLI commands functional (config, info, validate-config, play)
- Configuration file creation and validation working
- Ruff linting passes with auto-fixes
- Type checking shows warnings but no critical errors

This refactoring transforms the codebase from a monolithic structure to a modular, maintainable architecture ready for extension and code review.
Author
Owner

The code needs rework.

ai_client.py:

  1. default sampling parameters should not exist (they should be handled by provider by default)
  2. Hard token/character input limit must be implemented and be mandatory.

cli.py:

  1. All emojis have to be removed.
  2. at l50, the error returns "connection failed", but cli is not responsible for connections, and has no business talking about them. Whatever error happened during client initialization, is not necessarily a connection issue.
  3. On l357 - l361, errors are too vague. Each stack frame should describe the what failed at it's level. "Game error" and "Unexpected error" is not sufficient.
  4. despite reporting that config is valid, game cannot start due to the following error: " Failed to start game: Failed to generate DM turn: Failed to get completion after 4 attempts: Failed to execute completion: Error code: 401 - {'error': {'message': 'No cookie auth
    credentials found', 'code': 401}}"
  5. arrows don't work while inputting character descriptions, world setting, etc.
  6. backspace does not work as expected: when text is carried to the next line, cannot delete characters before the newline.
The code needs rework. ai_client.py: 1. default sampling parameters should not exist (they should be handled by provider by default) 2. Hard token/character input limit must be implemented and be mandatory. cli.py: 1. All emojis have to be removed. 2. at l50, the error returns "connection failed", but cli is not responsible for connections, and has no business talking about them. Whatever error happened during client initialization, is not necessarily a connection issue. 3. On l357 - l361, errors are too vague. Each stack frame should describe the what failed at it's level. "Game error" and "Unexpected error" is not sufficient. 4. despite reporting that config is valid, game cannot start due to the following error: "❌ Failed to start game: Failed to generate DM turn: Failed to get completion after 4 attempts: Failed to execute completion: Error code: 401 - {'error': {'message': 'No cookie auth credentials found', 'code': 401}}" 5. arrows don't work while inputting character descriptions, world setting, etc. 6. backspace does not work as expected: when text is carried to the next line, cannot delete characters before the newline.
fedir closed this pull request 2026-01-27 11:26:18 +01:00

Pull request closed

Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: fedir/taletorrent#2