-
Notifications
You must be signed in to change notification settings - Fork 75
instance.py
kiankyars edited this page Jul 7, 2025
·
1 revision
- 942 lines - Way too long
- Multiple responsibilities - Violates Single Responsibility Principle
- Hard to read and maintain
- Difficult to test individual components
- Tight coupling between different concerns
# fle/env/core/instance.py
class FactorioInstance:
"""Main orchestrator class - keeps only essential coordination logic"""
def __init__(self, **kwargs):
self.connection_manager = ConnectionManager(**kwargs)
self.tool_manager = ToolManager(self.connection_manager)
self.state_manager = StateManager(self.connection_manager)
self.namespace_manager = NamespaceManager(self.connection_manager)
# ... minimal coordination logic# fle/env/core/connection.py
class ConnectionManager:
"""Handles RCON connections, server communication, and basic commands"""
def __init__(self, address, tcp_port, **kwargs):
self.rcon_client = self._connect_to_server(address, tcp_port)
def connect_to_server(self, address, tcp_port):
# Connection logic from current instance.py
def send_command(self, command, *parameters):
# Command sending logic
def execute_transaction(self, transaction):
# Transaction execution logic# fle/env/core/tools.py
class ToolManager:
"""Handles tool loading, setup, and hook management"""
def __init__(self, connection_manager):
self.connection_manager = connection_manager
self.controllers = {}
self.pre_tool_hooks = {}
self.post_tool_hooks = {}
def setup_tools(self, lua_script_manager):
# Tool setup logic from current instance.py
def register_pre_tool_hook(self, tool_name, callback):
# Hook registration logic
def execute_pre_tool_hooks(self, tool_name, tool_instance, *args, **kwargs):
# Hook execution logic# fle/env/core/state.py
class StateManager:
"""Handles game state, reset, initialization, and persistence"""
def __init__(self, connection_manager):
self.connection_manager = connection_manager
self.initial_inventory = {}
self.all_technologies_researched = True
def reset(self, game_state=None):
# Reset logic from current instance.py
def initialise(self, fast=True):
# Initialization logic
def _reset(self, inventories):
# Internal reset logic# fle/env/core/namespace.py
class NamespaceManager:
"""Handles Python namespace creation and management"""
def __init__(self, connection_manager, num_agents=1):
self.connection_manager = connection_manager
self.namespaces = [FactorioNamespace(connection_manager, i) for i in range(num_agents)]
def create_factorio_namespace(self):
# Namespace creation logic from current instance.py
def run_func_in_factorio_env(self, func):
# Environment execution logic# fle/env/core/screenshots.py
class ScreenshotManager:
"""Handles screenshot capture and file management"""
def __init__(self, connection_manager):
self.connection_manager = connection_manager
def screenshot(self, script_output_path, resolution="1920x1080", save_path=None, zoom=None):
# Screenshot logic from current instance.py
def _get_latest_screenshot(self, script_output_path, max_wait=2):
# Screenshot file management logic
def calculate_optimal_zoom(self, bounds, resolution="1920x1080"):
# Zoom calculation logic# fle/env/core/warnings.py
class WarningManager:
"""Handles game warnings and alerts"""
def __init__(self, connection_manager):
self.connection_manager = connection_manager
def get_warnings(self, seconds=10):
# Warning retrieval logic from current instance.py- Create
ConnectionManagerclass - Move connection-related methods from
FactorioInstance - Update
FactorioInstanceto useConnectionManager
- Create
ToolManagerclass - Move tool setup and hook management
- Update dependencies
- Create
StateManagerclass - Move reset and initialization logic
- Update state-related methods
- Create remaining manager classes
- Move respective functionality
- Update all references
- Simplify
FactorioInstanceto orchestrate managers - Remove duplicate code
- Improve documentation
- Readability: Each class has a single, clear responsibility
- Testability: Each component can be tested independently
- Maintainability: Changes to one area don't affect others
- Reusability: Components can be reused in different contexts
- Debugging: Easier to isolate issues to specific components
fle/env/core/
├── __init__.py
├── instance.py # Main orchestrator (~150 lines)
├── connection.py # RCON and communication (~100 lines)
├── tools.py # Tool management (~150 lines)
├── state.py # Game state management (~120 lines)
├── namespace.py # Namespace management (~100 lines)
├── screenshots.py # Screenshot handling (~80 lines)
└── warnings.py # Warning management (~50 lines)