Add auto-connect for local development and refactor main.cpp
Manual login on every startup is cumbersome for local development. Added optional auto-connect to localhost with pre-configured username. Additionally, refactored the monolithic main.cpp file into a well-structured ClientApp class for better maintainability.
Changes
client/main.cpp
- Refactored from 264 lines to 10 lines
- Now creates ClientApp instance and calls Run()
client/client-app.hpp & client/client-app.cpp (NEW)
- Extracted all initialization logic into ClientApp class
- Separated concerns into logical methods:
-
InitializeLogger()- Logger setup -
InitializeWindow()- OpenGL window creation -
InitializeGame()- Game system initialization -
InitializeGUI()- GUI system and windows -
RegisterConsoleCommands()- Console command registration -
SetupAutoConnect()- Auto-connect logic -
RegisterGUIWindows()- GUI window registration -
Run()- Main game loop
-
- Improved resource management using smart pointers and RAII
- Cleaner lambda captures with explicit lifetime management
- Better testability and maintainability
client/CMakeLists.txt
- Added client-app.cpp to build
README.md
- Documented auto-connect configuration options and usage
Auto-Connect Configuration
-- assets/scripts/config.lua
auto_connect = true
default_username = "player"
Auto-connect follows same pattern as existing GUI and console connection methods: RegisterConnectFunc → RegisterMessageHandler → Connect. Registers handlers before Connect() to avoid race condition with AUTHENTICATED message.
Refactoring Benefits
- Separation of Concerns: Related code grouped logically
- Reduced Complexity: Each initialization phase has its own method
- Better Resource Management: RAII with smart pointers, clear ownership
- Improved Testability: Components can be tested in isolation
- Maintainability: Much easier to understand and modify
[!WARNING]
Original prompt
A lot of the code is not very declarative and could use a face-lift. The biggest change that needs to happen is that you shouldn't need to login to play. Update the flow so that you can play without logging in, or suggest ways to fix the login flow, for local development
If this change is too complex, identify another change that improves the code in a meaningful way for the best way to follow the code execution flow, or to improve the develop experience.
The build may not work, I have changes coming in a seperate commit to improve that.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
@copilot That main file looks pretty gnarly, what sort of improvements can be made. You don't have to be super surgical as this file doesn't follow best practices very well
@copilot That main file looks pretty gnarly, what sort of improvements can be made. You don't have to be super surgical as this file doesn't follow best practices very well
Refactored the 264-line main() function into a clean ClientApp class with proper separation of concerns. Main improvements:
- main.cpp: Reduced from 264 lines to 10 lines
- New ClientApp class: Organized into logical methods (InitializeLogger, InitializeWindow, InitializeGame, etc.)
- Better resource management: Using smart pointers and RAII
- Cleaner lambda captures: Explicit lifetime management
- Improved testability: Each component can be tested in isolation
The refactoring maintains all existing functionality while making the code significantly more maintainable. See commit bdc7407.