/// <summary> /// Retrieves the game's configuration /// </summary> /// <returns>The game's configuration</returns> public object initializeClient(string connectionId, RegisteredClient rc) { if (!UserHandler.UserExistsAndReady(connectionId)) { _gameLock.Wait(); try { User user = null;//UserHandler.FindUserByIdentity(rc.Identity); Ship ship = null; if (user == null) { if (UserHandler.TotalActiveUsers >= 50 /*RuntimeConfiguration.MaxServerUsers*/) { return(new { ServerFull = true }); } else { ship = new Ship(this, RespawnManager.GetRandomStartPosition(), GameHandler.BulletManager); ship.Name = rc.DisplayName; user = new User(connectionId, ship, rc) { Controller = false }; UserHandler.AddUser(user); } } GameHandler.AddShipToGame(ship); return(new { Configuration = Configuration, ServerFull = false, CompressionContracts = new { PayloadContract = _payloadManager.Compressor.PayloadCompressionContract, CollidableContract = _payloadManager.Compressor.CollidableCompressionContract, ShipContract = _payloadManager.Compressor.ShipCompressionContract, BulletContract = _payloadManager.Compressor.BulletCompressionContract, LeaderboardEntryContract = _payloadManager.Compressor.LeaderboardEntryCompressionContract, PowerupContract = _payloadManager.Compressor.PowerupCompressionContract }, ShipID = UserHandler.GetUserShip(connectionId).ID, ShipName = UserHandler.GetUserShip(connectionId).Name }); } catch { } finally { _gameLock.Release(); } } return(null); }
/// <summary> /// Retrieves the game's configuration /// </summary> /// <returns>The game's configuration</returns> public object initializeController(string connectionId, RegisteredClient rc) { if (!UserHandler.UserExistsAndReady(connectionId)) { try { User main = UserHandler.FindUserByIdentity(rc.Identity); if (main != null) { User controllerUser = new User(connectionId, rc) { Controller = true }; controllerUser.MyShip = main.MyShip; UserHandler.AddUser(controllerUser); main.RemoteControllers.Add(controllerUser); main.NotificationManager.Notify("Controller attached."); return(new { Configuration = Configuration, CompressionContracts = new { PayloadContract = _payloadManager.Compressor.PayloadCompressionContract, CollidableContract = _payloadManager.Compressor.CollidableCompressionContract, ShipContract = _payloadManager.Compressor.ShipCompressionContract, BulletContract = _payloadManager.Compressor.BulletCompressionContract, LeaderboardEntryCompressionContract = _payloadManager.Compressor.LeaderboardEntryCompressionContract } }); } else { return(new { FailureMessage = "Could not find logged in user to control." }); } } catch (Exception e) { ErrorLog.Instance.Log(e); } } return(null); }
public async Task OnDisconnectedAsync(string connectionId) { await _gameLock.WaitAsync(); try { if (_userHandler.UserExistsAndReady(connectionId)) { User user = _userHandler.GetUser(connectionId); //It's possible for a controller to disconnect without a ship if (!user.Controller) { user.MyShip.Dispose(); user.Connected = false; } else { // Remove me from the ship hosts remote controllers if (user.MyShip != null) { user.MyShip.Host.RemoteControllers.Remove(user); user.MyShip.Host.NotificationManager.Notify("Detached controller."); user.MyShip = null; } _userHandler.RemoveUser(connectionId); } // Leave the leaderboard group just in case user was in it await _gameHub.Groups.RemoveFromGroupAsync(connectionId, Leaderboard.LEADERBOARD_REQUESTEE_GROUP); // Clear controllers foreach (User u in user.RemoteControllers) { u.MyShip = null; await _gameHub.Clients.Client(u.ConnectionID).SendAsync("stopController", "Primary account has been stopped!"); } user.RemoteControllers.Clear(); } } catch (Exception e) { //ErrorLog.Instance.Log(e); } finally { _gameLock.Release(); } }
/// <summary> /// On disconnect we need to remove the ship from our list of ships within the gameHandler. /// This also means we need to notify clients that the ship has been removed. /// </summary> public void OnDisconnected(string connectionId) { lock (_locker) { try { if (_userHandler.UserExistsAndReady(connectionId)) { User user = _userHandler.GetUser(connectionId); //It's possible for a controller to disconnect without a ship if (!user.Controller) { user.MyShip.Dispose(); user.Connected = false; } else { // Remove me from the ship hosts remote controllers if (user.MyShip != null) { user.MyShip.Host.RemoteControllers.Remove(user); user.MyShip.Host.NotificationManager.Notify("Detached controller."); user.MyShip = null; } _userHandler.RemoveUser(connectionId); } // Leave the leaderboard group just in case user was in it IHubContext context = Game.GetContext(); context.Groups.Remove(connectionId, Leaderboard.LEADERBOARD_REQUESTEE_GROUP); // Clear controllers foreach (User u in user.RemoteControllers) { u.MyShip = null; context.Clients.Client(u.ConnectionID).stopController("Primary account has been stopped!"); } user.RemoteControllers.Clear(); } } catch (Exception e) { ErrorLog.Instance.Log(e); } } }
/// <summary> /// Retrieves the game's configuration /// </summary> /// <returns>The game's configuration</returns> public object initializeClient(string connectionId, RegisteredClient rc) { if (!UserHandler.UserExistsAndReady(connectionId)) { try { lock (_locker) { User user = UserHandler.FindUserByIdentity(rc.Identity); Ship ship; if (user == null) { if (UserHandler.TotalActiveUsers >= RuntimeConfiguration.MaxServerUsers) { return(new { ServerFull = true }); } else { ship = new Ship(RespawnManager.GetRandomStartPosition(), GameHandler.BulletManager); ship.Name = rc.DisplayName; user = new User(connectionId, ship, rc) { Controller = false }; UserHandler.AddUser(user); } } else { string previousConnectionID = user.ConnectionID; UserHandler.ReassignUser(connectionId, user); ship = user.MyShip; if (user.Connected) // Check if it's a duplicate login { GetContext().Clients.Client(previousConnectionID).controlTransferred(); user.NotificationManager.Notify("Transfering control to this browser. You were already logged in."); } else { ship.Disposed = false; ship.LifeController.HealFull(); user.Connected = true; } user.IdleManager.RecordActivity(); user.IdleManager.Idle = false; } GameHandler.AddShipToGame(ship); } return(new { Configuration = Configuration, ServerFull = false, CompressionContracts = new { PayloadContract = _payloadManager.Compressor.PayloadCompressionContract, CollidableContract = _payloadManager.Compressor.CollidableCompressionContract, ShipContract = _payloadManager.Compressor.ShipCompressionContract, BulletContract = _payloadManager.Compressor.BulletCompressionContract, LeaderboardEntryContract = _payloadManager.Compressor.LeaderboardEntryCompressionContract, PowerupContract = _payloadManager.Compressor.PowerupCompressionContract }, ShipID = UserHandler.GetUserShip(connectionId).ID, ShipName = UserHandler.GetUserShip(connectionId).Name }); } catch (Exception e) { ErrorLog.Instance.Log(e); } } return(null); }