public RegisteredClient Register(string identity, string displayName, string photo) { RegisteredClient rc = new RegisteredClient(Guid.NewGuid().ToString(), identity, displayName, photo); _registrationList.TryAdd(rc.RegistrationID, rc); return(rc); }
/// <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); }
public static void SetState(RegisteredClient rc, HttpContext context, IDataProtectionProvider provider) { // Save the cookie state Byte[] identity = Encoding.UTF8.GetBytes(rc.Identity); Byte[] encrypted = provider.CreateProtector("ShootR.Identity").Protect(identity); var temp = new RegisteredClient(rc.RegistrationID, WebEncoders.Base64UrlEncode(encrypted), rc.DisplayName, rc.Photo, rc.Role); var state = JsonConvert.SerializeObject(temp); context.Response.Cookies.Append("shootr.state", state, new CookieOptions { Expires = DateTime.Now.AddDays(30) }); }
/// <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 User(string connectionID, Ship ship, RegisteredClient rc) { RegistrationTicket = rc; ConnectionID = connectionID; MyShip = ship; ReadyForPayloads = false; Viewport = new Size(0, 0); // Initialize the viewport to 0 by 0 RemoteControllers = new List<User>(); NotificationManager = new NotificationManager(); IdleManager = new IdleManager(ship, NotificationManager); Connected = true; if (ship != null) { ship.Host = this; } }
public User(string connectionID, Ship ship, RegisteredClient rc) { RegistrationTicket = rc; ConnectionID = connectionID; MyShip = ship; ReadyForPayloads = false; Viewport = new Size(0, 0); // Initialize the viewport to 0 by 0 RemoteControllers = new List <User>(); NotificationManager = new NotificationManager(); IdleManager = new IdleManager(ship, NotificationManager); Connected = true; if (ship != null) { ship.Host = this; } }
private RegisteredClient GetClientState(HttpContext context) { // New client state var shootrState = GetCookieValue(context, "shootr.state"); RegisteredClient clientState = null; if (String.IsNullOrEmpty(shootrState)) { clientState = new RegisteredClient(); } else { clientState = JsonConvert.DeserializeObject <RegisteredClient>(shootrState); } return(clientState); }
public User(string connectionID, Ship ship, RegisteredClient rc) { RegistrationTicket = rc; MyShip = ship; ConnectionID = connectionID; Viewport = new Size(600, 600); NotificationManager = new NotificationManager(); IdleManager = new IdleManager(ship, NotificationManager); Connected = true; ReadyForPayloads = false; if (ship != null) { ship.Host = this; } }
public static void AddOrUpdateState(RegisteredClient rc, HttpContext context) { // Save the cokie state Byte[] identity = Encoding.UTF8.GetBytes(rc.Identity); Byte[] encrypted = MachineKey.Protect(identity, "ShootR.Identity"); RegisteredClient temp = new RegisteredClient(rc.RegistrationID, HttpServerUtility.UrlTokenEncode(encrypted), rc.DisplayName, rc.Photo); var state = JsonConvert.SerializeObject(temp); if (context.Response.Cookies["shootr.state"] == null) { var cookie = new HttpCookie("shootr.state", state); cookie.Expires = DateTime.Now.AddDays(30); context.Response.Cookies.Add(cookie); } else { context.Response.Cookies["shootr.state"].Value = state; context.Response.Cookies["shootr.state"].Expires = DateTime.Now.AddDays(30); } }
/// <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); }
private RegisteredClient GetClientState(HttpContext context) { // New client state var shootrState = GetCookieValue(context, "shootr.state"); RegisteredClient clientState = null; if (String.IsNullOrEmpty(shootrState)) { clientState = new RegisteredClient(); } else { clientState = JsonConvert.DeserializeObject<RegisteredClient>(shootrState); } return clientState; }
/// <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; }
/// <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; }
public RegisteredClient Register(RegisteredClient existing) { existing.RegistrationID = Guid.NewGuid().ToString(); _registrationList.TryAdd(existing.RegistrationID, existing); return(existing); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, Game game, IDataProtectionProvider provider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseFileServer(); app.UseAuthentication(); app.Use((context, next) => { var state = context.Request.Cookies["shootr.state"]; if (state != null) { if (!context.User.Identity.IsAuthenticated) { // Clear the state cookie. context.Response.Cookies.Delete("shootr.state"); } else { try { string decoded = HttpUtility.UrlDecode(state); var rc = JsonConvert.DeserializeObject <RegisteredClient>(decoded); if (rc.Identity == "Guest") { rc.DisplayName = "Guest" + Interlocked.Increment(ref GuestID); rc.Identity = "Guest" + Guid.NewGuid().ToString(); rc.RegistrationID = null; rc.Photo = ""; } else { Byte[] encryptedIdentity = WebEncoders.Base64UrlDecode(rc.Identity); var unprotectedIdentity = provider.CreateProtector("ShootR.Identity").Unprotect(encryptedIdentity); rc.Identity = Encoding.UTF8.GetString(unprotectedIdentity); } rc.DisplayName = System.Net.WebUtility.HtmlEncode(rc.DisplayName); game.RegistrationHandler.Register(rc); SetState(rc, context, provider); } catch { } } } else if (context.User.Identity.IsAuthenticated) { //hack var id = context.User.Claims.Single(c => c.Type == ClaimTypes.NameIdentifier).Value; var name = context.User.Claims.Single(c => c.Type == ClaimTypes.Name).Value; var role = context.User.Claims.Single(c => c.Type == ClaimTypes.Role).Value; var photo = context.User.Claims.SingleOrDefault(c => c.Type == "photo")?.Value; var rc = new RegisteredClient(null, id, name, photo, role); game.RegistrationHandler.Register(rc); SetState(rc, context, provider); } return(next()); }); app.UseMvc(); app.UseSignalR(routes => { routes.MapHub <GameHub>("/Game"); }); }
public User(string connectionID, RegisteredClient rc) : this(connectionID, null, rc) { }
public RegisteredClient Register(RegisteredClient existing) { existing.RegistrationID = Guid.NewGuid().ToString(); _registrationList.TryAdd(existing.RegistrationID, existing); return existing; }
public RegisteredClient Register(string identity, string displayName, string photo) { RegisteredClient rc = new RegisteredClient(Guid.NewGuid().ToString(), identity, displayName, photo); _registrationList.TryAdd(rc.RegistrationID, rc); return rc; }
public void ProcessRequest(HttpContext context) { try { string apiKey = ConfigurationManager.AppSettings["janrainAPIKey"]; if (String.IsNullOrEmpty(apiKey)) { // Do nothing context.Response.Redirect("~/", false); context.ApplicationInstance.CompleteRequest(); return; } var registeredClient = GetClientState(context); // We have an identifier, we're already authenticated if (registeredClient.Identity != null) { Game.Instance.RegistrationHandler.Register(registeredClient); AddOrUpdateState(registeredClient, context); } else { string token = context.Request.Form["token"]; if (String.IsNullOrEmpty(token)) { context.Response.Redirect(HttpRuntime.AppDomainAppVirtualPath, false); context.ApplicationInstance.CompleteRequest(); return; } var response = new WebClient().DownloadString(String.Format(VerifyTokenUrl, apiKey, token)); if (String.IsNullOrEmpty(response)) { context.Response.Redirect(HttpRuntime.AppDomainAppVirtualPath, false); context.ApplicationInstance.CompleteRequest(); return; } dynamic j = JsonConvert.DeserializeObject(response); if (j.stat.ToString() != "ok") { context.Response.Redirect(HttpRuntime.AppDomainAppVirtualPath, false); context.ApplicationInstance.CompleteRequest(); return; } string identity = j.profile.identifier.ToString(), displayName = System.Net.WebUtility.HtmlEncode(j.profile.preferredUsername.ToString()), photo = ""; if (j.profile.photo != null) { photo = j.profile.photo; } else if (j.profile.email != null) { photo = "http://www.gravatar.com/avatar/" + ToMD5(j.profile.email.ToString()) + "?d=404"; } RegisteredClient rc = Game.Instance.RegistrationHandler.Register(identity, displayName, photo); AddOrUpdateState(rc, context); } string path = context.Request.QueryString["path"]; context.Response.Redirect(HttpRuntime.AppDomainAppVirtualPath + path, false); context.ApplicationInstance.CompleteRequest(); } catch (Exception e) { ErrorLog.Instance.Log(e, "Crashd in LoginHandler.ashx.cs"); } }