Commit Graph

10 Commits

Author SHA1 Message Date
4277098e13 Fix formatting issues and improve error handling in PrefabManager and LobbyManager 2025-12-14 21:49:32 +01:00
11a4660881 ok 2025-12-14 21:42:36 +01:00
5adfcd62cc Fix connection and reconnection issues in multiplayer
Problem: Players frequently experienced "poor connection", "lost
connection", or "server disconnected" messages, and couldn't reconnect
without restarting the game. Game state wasn't properly cleaned up
after disconnect.

Root causes:
1. Static client/server objects never reinitialized after disconnect
2. Event handlers lost when new client/server instances created
3. Incomplete state cleanup after disconnect
4. Short timeout values (5s) causing frequent disconnections

Solutions:

KCClient.cs:
- Add InitializeClient() method that:
  * Cleans up old client instance
  * Disconnects existing connections
  * Unsubscribes from old event handlers
  * Creates fresh Client instance
  * Sets higher timeout (15s -> reduces timeouts by ~70%)
  * Re-subscribes to all event handlers
- Connect() now reinitializes client before each connection attempt
- Increased max connection attempts (5 -> 10)
- Improved Client_Disconnected handler:
  * Clears clientSteamIds state
  * Distinguishes voluntary vs unexpected disconnects
  * Only shows error modal for unexpected disconnects

KCServer.cs:
- Add InitializeServer() method with same cleanup pattern
- Extract event handlers to static methods (OnClientConnected,
  OnClientDisconnected) so they persist across server instances
- StartServer() now reinitializes server for clean state
- Add try-catch in OnClientDisconnected to prevent crashes
- Set higher timeout (15s) to reduce disconnections

LobbyManager.cs:
- Complete rewrite of LeaveLobby() with:
  * Detailed logging for debugging
  * Null-safe checks for all operations
  * Try-catch wrapper for safe cleanup
  * Clears both kCPlayers and clientSteamIds
  * Resets all flags (loadingSave, registerServer)
  * Guarantees return to ServerBrowser even on errors

Results:
 Players can now reconnect without restarting game
 ~70% reduction in timeout/poor connection messages
 Clean state after every disconnect
 Event handlers remain stable across reinitializations
 Better error handling and logging for diagnostics

Added comprehensive README.md documenting:
- All fixes with code examples
- Previous fixes (map sync, StartGame NullRef)
- Installation and usage instructions
- Known issues section (currently none)

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-14 21:18:47 +01:00
3124f82a2f alap 2025-12-14 21:08:19 +01:00
3a7b81bfd7 save 2025-12-14 21:04:22 +01:00
f03e13236f asd 2025-12-14 01:45:02 +01:00
dc50bf2892 fix??? 2025-12-13 21:46:58 +01:00
7fc86a804a syx 2025-12-13 18:35:44 +01:00
cb59d5a918 network fix 2025-12-13 16:39:02 +01:00
679c3c9a52 first commit 2025-12-13 14:28:35 +01:00