public Task Should_Check_For_Correct_Command_Type()
            {
                return(Assert.ThrowsAnyAsync <ArgumentException>(async() =>
                {
                    var commandHandler = new TestCommandHandler(_testOutputHelper);

                    var registration = new CommandHandlerRegistration();
                    registration.Register(() => (ICommandHandler <DoSomethingCommand>)commandHandler);

                    CommandHandlerDelegate commandHandlerDelegate = registration.ResolveCommandHandler <DoSomethingCommand>();

                    Assert.NotNull(commandHandlerDelegate);

                    try
                    {
                        // This delegate handles DoSomethingCommand, but was passed in a DoSomethingWithCancellationCommand.
                        await commandHandlerDelegate.Invoke(new DoSomethingForSpecifiedDurationCommand(100));
                    }
                    catch (Exception ex)
                    {
                        _testOutputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
예제 #2
0
 public static void HandleChat(WorldSession session, ClientChat chat)
 {
     if (chat.Message.StartsWith("!"))
     {
         CommandManager.ParseCommand(chat.Message, out string command, out string[] parameters);
         CommandHandlerDelegate handler = CommandManager.GetCommandHandler(command);
         handler?.Invoke(session, parameters);
     }
 }
예제 #3
0
        private static void Main()
        {
            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

            Console.Title = Title;
            log.Info("Initialising...");

            ConfigurationManager <WorldServerConfiguration> .Initialise("WorldServer.json");

            DatabaseManager.Initialise(ConfigurationManager <WorldServerConfiguration> .Config.Database);

            GameTableManager.Initialise();

            EntityManager.Initialise();
            EntityCommandManager.Initialise();

            AssetManager.Initialise();
            ServerManager.Initialise();

            MessageManager.Initialise();
            CommandManager.Initialise();
            NetworkManager <WorldSession> .Initialise(ConfigurationManager <WorldServerConfiguration> .Config.Network);

            WorldManager.Initialise(lastTick =>
            {
                NetworkManager <WorldSession> .Update(lastTick);
                MapManager.Update(lastTick);
            });

            log.Info("Ready!");

            while (true)
            {
                Console.Write(">> ");
                string line = Console.ReadLine();
                CommandManager.ParseCommand(line, out string command, out string[] parameters);

                CommandHandlerDelegate handler = CommandManager.GetCommandHandler(command);
                if (handler != null)
                {
                    try
                    {
                        handler.Invoke(null, parameters);
                    }
                    catch (Exception exception)
                    {
                        log.Error(exception);
                    }
                }
                else
                {
                    Console.WriteLine("Invalid command!");
                }
            }
        }
예제 #4
0
 public static void HandleChat(WorldSession session, ClientChat chat)
 {
     if (chat.Message.StartsWith(CommandPrefix))
     {
         try
         {
             CommandManager.ParseCommand(chat.Message, out string command, out string[] parameters);
             CommandHandlerDelegate handler = CommandManager.GetCommandHandler(command);
             handler?.Invoke(session, parameters);
         }
         catch (Exception e)
         {
             log.Warn(e.Message);
         }
     }
 }
예제 #5
0
        /// <summary>
        /// Dispatch the command to the registered command handler asynchronously.
        /// </summary>
        /// <typeparam name="TCommand">Type of command to dispatch.</typeparam>
        /// <param name="command">Command to dispatch.</param>
        /// <param name="cancellationToken">Optional cancellation token to support cancellation in command handlers.</param>
        /// <returns>Asynchronous task which completes after the command handler has processed the command.</returns>
        public Task DispatchAsync <TCommand>(TCommand command, CancellationToken cancellationToken = default(CancellationToken)) where TCommand : class, ICommand
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            CommandHandlerDelegate commandHandlerDelegate = _resolver.ResolveCommandHandler <TCommand>();

            if (commandHandlerDelegate == null)
            {
                Type commandType = typeof(TCommand);
                throw new NoCommandHandlerResolvedException($"No command handler is registered to handle command of type: {commandType.Name}.", commandType);
            }

            return(commandHandlerDelegate.Invoke(command, cancellationToken));
        }
            public void Should_Register_All_Command_Handlers()
            {
                var commandHandler = new TestCommandHandler(_testOutputHelper);

                var registration = new CommandHandlerRegistration();

                registration.Register(() => (ICommandHandler <DoSomethingCommand>)commandHandler);

                CommandHandlerDelegate commandHandlerDelegate = registration.ResolveCommandHandler <DoSomethingCommand>();

                Assert.NotNull(commandHandlerDelegate);

                // Delegate should invoke the actual command handler - TestCommandHandler.
                commandHandlerDelegate.Invoke(new DoSomethingCommand());

                Assert.Equal(1, commandHandler.HandledCommands.Count);
                Assert.Contains(commandHandler.HandledCommands, c => c is DoSomethingCommand);
            }
            public async Task Should_Invoke_The_Actual_Registered_Command_Handler()
            {
                var commandHandler = new TestCommandHandler(_testOutputHelper);

                var registration = new CommandHandlerRegistration();

                registration.Register(() => (ICommandHandler <DoSomethingCommand>)commandHandler);

                CommandHandlerDelegate commandHandlerDelegate = registration.ResolveCommandHandler <DoSomethingCommand>();

                Assert.NotNull(commandHandlerDelegate);

                // Invoke.
                await commandHandlerDelegate.Invoke(new DoSomethingCommand());

                // Check if actual command handler instance was invoked.
                Assert.Equal(1, commandHandler.HandledCommands.Count);
                Assert.Contains(commandHandler.HandledCommands, c => c is DoSomethingCommand);
            }
예제 #8
0
            public void Should_Register_All_Command_Handlers()
            {
                var container = new Container();

                container.Register <ICommandHandler <DoSomethingCommand> >(() => new TestCommandHandler(_testOutputHelper), Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerCommandHandlerResolver(containerAdapter);

                CommandHandlerDelegate commandHandlerDelegate = resolver.ResolveCommandHandler <DoSomethingCommand>();

                // Delegate should invoke the actual command handler - TestCommandHandler.
                commandHandlerDelegate.Invoke(new DoSomethingCommand());

                // Get instance from container to see if it was invoked.
                var registeredCommandHandler = (TestCommandHandler)container.GetInstance <ICommandHandler <DoSomethingCommand> >();

                Assert.Equal(1, registeredCommandHandler.HandledCommands.Count);
                Assert.Contains(registeredCommandHandler.HandledCommands, c => c is DoSomethingCommand);
            }