/// <summary> /// Handles when the connected descriptor sends input /// </summary> /// <param name="e">the events of the message</param> public override void OnMessage(string message) { if (_currentPlayer == null) { OnError(new Exception("Invalid character; please reload the client and try again.")); return; } IEnumerable <string> errors = Interpret.Render(message, _currentPlayer); //It only sends the errors if (errors.Any(str => !string.IsNullOrWhiteSpace(str))) { SendOutput(errors); } }
/// <summary> /// Handles when the connected descriptor sends input /// </summary> /// <param name="e">the events of the message</param> protected override void OnMessage(MessageEventArgs e) { if (_currentPlayer == null) { SendWrapper("Invalid character; please reload the client and try again."); this.Context.WebSocket.Close(CloseStatusCode.Abnormal, "connection aborted - no player");; } var errors = Interpret.Render(e.Data, _currentPlayer); //It only sends the errors if (errors.Any(str => !string.IsNullOrWhiteSpace(str))) { SendWrapper(errors); } }
/// <summary> /// Handles initial connection /// </summary> protected override void OnOpen() { var authTicketValue = Context.CookieCollection[".AspNet.ApplicationCookie"].Value; GetUserIDFromCookie(authTicketValue); UserManager = new ApplicationUserManager(new UserStore <ApplicationUser>(new ApplicationDbContext())); var authedUser = UserManager.FindById(_userId); var currentCharacter = authedUser.GameAccount.Characters.FirstOrDefault(ch => ch.ID.Equals(authedUser.GameAccount.CurrentlySelectedCharacter)); if (currentCharacter == null) { Send("<p>No character selected</p>"); return; } //Try to see if they are already live _currentPlayer = LiveCache.Get <Player>(currentCharacter.ID); //Check the backup if (_currentPlayer == null) { var hotBack = new HotBackup(System.Web.Hosting.HostingEnvironment.MapPath("/HotBackup/")); _currentPlayer = hotBack.RestorePlayer(currentCharacter.AccountHandle, currentCharacter.ID); } //else new them up if (_currentPlayer == null) { _currentPlayer = new Player(currentCharacter); } _currentPlayer.Descriptor = this; //We need to barf out to the connected client the welcome message. The client will only indicate connection has been established. var welcomeMessage = new List <String>(); welcomeMessage.Add(string.Format("Welcome to alpha phase twinMUD, {0}", currentCharacter.FullName())); welcomeMessage.Add("Please feel free to LOOK around."); _currentPlayer.WriteTo(welcomeMessage); //Send the look command in Interpret.Render("look", _currentPlayer); }
/// <summary> /// Validates the game account from the aspnet cookie /// </summary> /// <param name="handshake">the headers from the http request</param> private void ValidateUser(Cookie cookie) { //Grab the user GetUserIDFromCookie(cookie.Value); ApplicationUser authedUser = UserManager.FindById(_userId); IPlayerTemplate currentCharacter = authedUser.GameAccount.Characters.FirstOrDefault(ch => ch.Id.Equals(authedUser.GameAccount.CurrentlySelectedCharacter)); if (currentCharacter == null) { Send("<p>No character selected</p>"); return; } //Try to see if they are already live _currentPlayer = LiveCache.GetAll <IPlayer>().FirstOrDefault(player => player.Descriptor != null && player.Descriptor._userId == _userId); //Check the backup if (_currentPlayer == null) { PlayerData playerDataWrapper = new PlayerData(); _currentPlayer = playerDataWrapper.RestorePlayer(currentCharacter.AccountHandle, currentCharacter); } //else new them up if (_currentPlayer == null) { _currentPlayer = new Player(currentCharacter); } _currentPlayer.Descriptor = this; //We need to barf out to the connected client the welcome message. The client will only indicate connection has been established. List <string> welcomeMessage = new List <string> { string.Format("Welcome to alpha phase Under the Eclipse, {0}", currentCharacter.FullName()), "Please feel free to LOOK around." }; _currentPlayer.WriteTo(welcomeMessage); //Send the look command in Interpret.Render("look", _currentPlayer); try { IEnumerable <IPlayer> validPlayers = LiveCache.GetAll <IPlayer>().Where(player => player.Descriptor != null && player.Template <IPlayerTemplate>().Account.Config.WantsNotification(_currentPlayer.AccountHandle, false, AcquaintenceNotifications.EnterGame)); foreach (IPlayer player in validPlayers) { player.WriteTo(new string[] { string.Format("{0} has entered the game.", _currentPlayer.AccountHandle) }); } if (authedUser.GameAccount.Config.GossipSubscriber) { GossipClient gossipClient = LiveCache.Get <GossipClient>("GossipWebClient"); if (gossipClient != null) { gossipClient.SendNotification(authedUser.GlobalIdentityHandle, Notifications.EnterGame); } } } catch (Exception ex) { LoggingUtility.LogError(ex, LogChannels.SocketCommunication); } }