WorldSceneSystem - jimdroberts/FishMMO GitHub Wiki
Manages world scene connections, queues, and scene assignment for players in the MMO server. Handles open world and instanced scene logic, connection authentication, and database updates to ensure players are assigned to the correct scenes and instances in FishMMO.
-
private const int MAX_CLIENTS_PER_INSTANCE
Maximum number of clients allowed per scene instance.
-
private WorldServerAuthenticator loginAuthenticator
Reference to the world server authenticator for login/authentication events.
-
public WorldSceneDetailsCache WorldSceneDetailsCache
Cache of world scene details, including max clients per scene.
-
public Dictionary<string, HashSet> WaitingOpenWorldConnections
Connections waiting for a scene to finish loading, mapped by scene name.
-
public Dictionary<NetworkConnection, string> OpenWorldConnectionScenes
Maps connections to the open world scene they are waiting for.
-
public Dictionary<long, HashSet> WaitingInstanceConnections
Connections waiting for an instanced scene to finish loading, mapped by instance ID.
-
public Dictionary<NetworkConnection, long> InstanceConnectionScenes
Maps connections to the instance scene they are waiting for.
-
public int ConnectionCount { get; private set; }
Total number of connections managed by this system (waiting + active).
-
private float waitQueueRate
Interval (in seconds) between wait queue updates.
-
private float nextWaitQueueUpdate
Time remaining until the next wait queue update.
-
public override void InitializeOnce()
Initializes the world scene system, subscribes to authentication and connection events.
-
public override void Destroying()
Unsubscribes from events and deletes world scene data from the database when the system is destroyed.
-
private void ServerManager_OnRemoteConnectionState(NetworkConnection conn, RemoteConnectionStateArgs args)
Handles remote connection state changes. Removes connections from queues when they disconnect. conn: NetworkConnection - The network connection. args: RemoteConnectionStateArgs - Remote connection state arguments.
-
private void LateUpdate()
Unity LateUpdate callback. Periodically processes open world and instance queues, and updates connection count.
-
private void ProcessOpenWorldQueue(NpgsqlDbContext dbContext, string sceneName)
Processes the queue for open world scenes, assigning connections to available scenes or enqueuing new scene requests. dbContext: NpgsqlDbContext - Database context. sceneName: string - Name of the scene to process.
-
private void ProcessInstanceConnection(NpgsqlDbContext dbContext, NetworkConnection conn)
Tries to process an Instance scene for the connection character otherwise falls back to the world scene. dbContext: NpgsqlDbContext - Database context. conn: NetworkConnection - Network connection to process.
-
private void UpdateConnectionCount(NpgsqlDbContext dbContext)
Updates the total connection count by summing waiting and active connections across all scenes. dbContext: NpgsqlDbContext - Database context.
-
private void Authenticator_OnClientAuthenticationResult(NetworkConnection conn, bool authenticated)
Handles client authentication results. Processes instance connection or falls back to world scene. conn: NetworkConnection - Network connection. authenticated: bool - True if client authenticated successfully.
-
private void AddToQueue(NetworkConnection conn, T key, Dictionary<T, HashSet> queue, Dictionary<NetworkConnection, T> reverseMap)
Adds a connection to a queue for a scene or instance, updating both forward and reverse maps. conn: NetworkConnection - Network connection. key: T - Scene name or instance ID. queue: Dictionary<T, HashSet> - Queue mapping key to connections. reverseMap: Dictionary<NetworkConnection, T> - Reverse map from connection to key.
-
private void RemoveFromQueue(NetworkConnection conn, Dictionary<NetworkConnection, T> reverseMap, Dictionary<T, HashSet> queue)
Removes a connection from a queue for a scene or instance, updating both forward and reverse maps. conn: NetworkConnection - Network connection. reverseMap: Dictionary<NetworkConnection, T> - Reverse map from connection to key. queue: Dictionary<T, HashSet> - Queue mapping key to connections.
-
private void FallbackToWorldScene(NpgsqlDbContext dbContext, NetworkConnection conn, string accountName)
Fallbacks a connection to the world scene if instance scene assignment fails. dbContext: NpgsqlDbContext - Database context. conn: NetworkConnection - Network connection. accountName: string - Account name for the connection.
-
private bool IsValidConnection(NetworkConnection conn, out string accountName)
Checks if a connection is valid and retrieves the account name. conn: NetworkConnection - Network connection. accountName: out string - Output account name. Returns: bool - True if connection is valid and account name is found.
-
private void Kick(NetworkConnection conn, string reason)
Kicks a connection from the server with a specified reason. conn: NetworkConnection - Network connection. reason: string - Reason for kicking.
-
private int GetMaxClients(string sceneName)
Gets the maximum number of clients allowed for a given scene, using cached details if available. sceneName: string - Name of the scene. Returns: int - Maximum number of clients for the scene.
- Requires FishNet networking, FishMMO server infrastructure, and a configured database connection.
- Attach the WorldSceneSystem to a server GameObject. Ensure WorldSceneDetailsCache is assigned and database settings are configured.
- The system initializes itself on startup, subscribing to authentication and connection events.
- No manual queue management is required; the system automatically manages player assignment to scenes and instances.
// Example 1: Setting up WorldSceneSystem
// Attach WorldSceneSystem to a server GameObject and ensure dependencies are set up.
void Start()
{
// WorldSceneSystem will initialize automatically if attached and enabled.
}
// Example 2: Handling player assignment to scenes
// The system automatically assigns players to open world or instance scenes as needed.
// No manual invocation is required in typical usage.
- Ensure WorldSceneDetailsCache is up to date and contains all valid scene names and max client settings.
- Use the waitQueueRate setting to balance queue update frequency and server performance.
- Handle scene assignment failures gracefully and provide user feedback if a scene cannot be loaded.
- Clean up all event subscriptions and database entries on shutdown to prevent resource leaks.
- Use IsValidConnection and queue management helpers to safely interact with player connections.