NetMud.DataAccess.FileSystem.PlayerData.RestorePlayer C# (CSharp) Method

RestorePlayer() public method

Restores one character from their Current backup
public RestorePlayer ( string accountHandle, long charID ) : IPlayer
accountHandle string Global Account Handle for the account
charID long Which character to load
return IPlayer
        public IPlayer RestorePlayer(string accountHandle, long charID)
        {
            IPlayer newPlayerToLoad = null;

            try
            {
                var currentBackupDirectory = BaseDirectory + accountHandle + "/" + CurrentDirectoryName + charID.ToString() + "/";

                //No backup directory? No live data.
                if (!VerifyDirectory(currentBackupDirectory, false))
                    return null;

                var playerDirectory = new DirectoryInfo(currentBackupDirectory);

                var playerFilePath = playerDirectory + GetPlayerFilename(charID);

                var fileData = ReadCurrentFileByPath(playerFilePath);

                //no player file to load, derp
                if (fileData.Length == 0)
                    return null;

                var blankEntity = Activator.CreateInstance(typeof(IPlayer)) as IPlayer;
                newPlayerToLoad = (IPlayer)blankEntity.FromBytes(fileData);

                //bad load, dump it
                if (newPlayerToLoad == null)
                    return null;

                //abstract this out to a helper maybe?
                var locationAssembly = Assembly.GetAssembly(typeof(ILocation));

                //We have the player in live cache now so make it move to the right place
                newPlayerToLoad.GetFromWorldOrSpawn();
                newPlayerToLoad.UpsertToLiveWorldCache();

                //We'll need one of these per container on players
                if (Directory.Exists(playerDirectory + "Inventory/"))
                {
                    var inventoryDirectory = new DirectoryInfo(playerDirectory + "Inventory/");

                    foreach (var file in inventoryDirectory.EnumerateFiles())
                    {
                        var blankObject = Activator.CreateInstance(typeof(IInanimate)) as IInanimate;

                        var newObj = (IInanimate)blankObject.FromBytes(ReadFile(file));
                        newObj.UpsertToLiveWorldCache();
                        newPlayerToLoad.MoveInto(newObj);
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingUtility.LogError(ex);
            }

            return newPlayerToLoad;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Validates the game account from the aspnet cookie
        /// </summary>
        /// <param name="handshake">the headers from the http request</param>
        private void ValidateUser(string handshake)
        {
            //Grab the user
            var authTicketValue = new Regex(".AspNet.ApplicationCookie=(.*)").Match(handshake).Groups[1].Value.Trim();

            GetUserIDFromCookie(authTicketValue);

            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<IPlayer>(currentCharacter.ID);

            //Check the backup
            if (_currentPlayer == null)
            {
                var playerDataWrapper = new PlayerData();
                _currentPlayer = playerDataWrapper.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);
        }