Esempio n. 1
0
        public override bool ProcessCommand(string commandLine) // TODO: refactoring needed: almost same code in Player
        {
            // ! means repeat last command
            if (commandLine != null && commandLine.Length >= 1 && commandLine[0] == '!')
            {
                commandLine          = LastCommand;
                LastCommandTimestamp = DateTime.Now;
            }
            else
            {
                LastCommand          = commandLine;
                LastCommandTimestamp = DateTime.Now;
            }

            // If an input state machine is running, send commandLine to machine
            if (CurrentStateMachine != null && !CurrentStateMachine.IsFinalStateReached)
            {
                CurrentStateMachine.ProcessInput(this, commandLine);
                return(true);
            }
            else
            {
                CurrentStateMachine = null; // reset current state machine if not currently running one
                // Extract command and parameters
                bool extractedSuccessfully = CommandHelpers.ExtractCommandAndParameters(Aliases, commandLine, out var command, out var rawParameters, out var parameters, out var forceOutOfGame);
                if (!extractedSuccessfully)
                {
                    Log.Default.WriteLine(LogLevels.Warning, "Command and parameters not extracted successfully");
                    Send("Invalid command or parameters");
                    return(false);
                }

                // Execute command
                bool executedSuccessfully;
                if (forceOutOfGame || (Impersonating == null && Incarnating == null)) // neither incarnating nor impersonating
                {
                    Log.Default.WriteLine(LogLevels.Debug, "[{0}] executing [{1}]", DisplayName, commandLine);
                    executedSuccessfully = ExecuteCommand(command, rawParameters, parameters);
                }
                else if (Incarnating != null) // incarnating
                {
                    Log.Default.WriteLine(LogLevels.Debug, "[{0}]|[{1}] executing [{2}]", DisplayName, Incarnating.DebugName, commandLine);
                    executedSuccessfully = Incarnating.ExecuteCommand(command, rawParameters, parameters);
                }
                else if (Impersonating != null) // impersonating
                {
                    Log.Default.WriteLine(LogLevels.Debug, "[{0}]|[{1}] executing [{2}]", DisplayName, Impersonating.DebugName, commandLine);
                    executedSuccessfully = Impersonating.ExecuteCommand(command, rawParameters, parameters);
                }
                else
                {
                    Log.Default.WriteLine(LogLevels.Error, "[{0}] is neither out of game, nor impersonating, nor incarnating");
                    executedSuccessfully = false;
                }
                if (!executedSuccessfully)
                {
                    Log.Default.WriteLine(LogLevels.Warning, "Error while executing command");
                }
                return(executedSuccessfully);
            }
        }
Esempio n. 2
0
        protected virtual bool DoIncarnate(string rawParameters, params CommandParameter[] parameters)
        {
            if (parameters.Length == 0)
            {
                if (Incarnating != null)
                {
                    DependencyContainer.Instance.GetInstance <IServer>().Wiznet($"{DisplayName} stops incarnating {Incarnating.DebugName}.", WiznetFlags.Incarnate);

                    Send("%M%You stop incarnating %C%{0}%x%.", Incarnating.DisplayName);
                    StopIncarnating();
                }
                else
                {
                    Send("Syntax: Incarnate <room|item|mob> <name|id>");
                }
            }
            else if (parameters.Length == 1)
            {
                Send("Syntax: Incarnate <room|item|mob> <name|id>");
            }
            else if (parameters.Length == 2)
            {
                IEntity incarnateTarget = null;
                string  kind            = parameters[0].Value;
                if ("room".StartsWith(kind))
                {
                    if (parameters[1].IsNumber)
                    {
                        int id = parameters[1].AsNumber;
                        incarnateTarget = DependencyContainer.Instance.GetInstance <IWorld>().Rooms.FirstOrDefault(x => x.Blueprint?.Id == id);
                    }
                    else
                    {
                        incarnateTarget = FindHelpers.FindByName(DependencyContainer.Instance.GetInstance <IWorld>().Rooms, parameters[1]);
                    }
                }
                else if ("item".StartsWith(kind))
                {
                    incarnateTarget = FindHelpers.FindByName(DependencyContainer.Instance.GetInstance <IWorld>().Items, parameters[1]);
                }
                else if ("mob".StartsWith(kind))
                {
                    incarnateTarget = FindHelpers.FindByName(DependencyContainer.Instance.GetInstance <IWorld>().Characters, parameters[1]);
                }
                if (incarnateTarget == null)
                {
                    Send("Target not found");
                }
                else
                {
                    //Log.Default.WriteLine(LogLevels.Info, $"{DisplayName} incarnates {incarnateTarget.DisplayName}");
                    if (Incarnating != null)
                    {
                        DependencyContainer.Instance.GetInstance <IServer>().Wiznet($"{DisplayName} stops incarnating {Incarnating.DebugName}.", WiznetFlags.Incarnate);

                        Send("%M%You stop incarnating %C%{0}%x%.", Incarnating.DisplayName);
                        Incarnating.ChangeIncarnation(null);
                    }

                    DependencyContainer.Instance.GetInstance <IServer>().Wiznet($"{DisplayName} starts incarnating {incarnateTarget.DebugName}.", WiznetFlags.Incarnate);

                    Send("%M%You start incarnating %C%{0}%x%.", incarnateTarget.DisplayName);
                    incarnateTarget.ChangeIncarnation(this);
                    Incarnating = incarnateTarget;
                    PlayerState = PlayerStates.Impersonating;
                }
            }
            return(true);
        }