public async Task Postprocessor_Go_Should_Kill_Bots_That_Have_Negative_Health_Left() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var postprocessor = new Postprocessor(randomHelper.Object); var arena = new ArenaDto { Width = 1, Height = 1 }; var bot = new BotDto { Id = Guid.NewGuid(), CurrentHealth = -1 }; var bots = new List <BotDto>(new[] { bot }); var context = ProcessingContext.Build(arena, bots); var botProperties = BotProperties.Build(bot, arena, bots); botProperties.CurrentMove = PossibleMoves.Idling; context.AddBotProperties(bot.Id, botProperties); // Act await postprocessor.Go(context); // Assert context.Bots.Should().HaveCount(1); context.Bots.Should().ContainEquivalentOf(new BotDto { CurrentHealth = 0, Move = PossibleMoves.Died }, c => c.Including(p => p.CurrentHealth) .Including(p => p.Move)); }
public async Task Postprocessor_Go_Should_Ignore_Bots_That_Have_Zero_Stamina_Left() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var postprocessor = new Postprocessor(randomHelper.Object); var arena = new ArenaDto { Width = 3, Height = 3 }; var bot = new BotDto { Id = Guid.NewGuid(), X = 1, Y = 1, CurrentHealth = 1, CurrentStamina = 0 }; var bots = new List <BotDto>(new[] { bot }); var context = ProcessingContext.Build(arena, bots); var botProperties = BotProperties.Build(bot, arena, bots); botProperties.CurrentMove = PossibleMoves.WalkForward; context.AddBotProperties(bot.Id, botProperties); // Act await postprocessor.Go(context); // Assert context.Bots.Should().HaveCount(1); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 1, Y = 1, CurrentStamina = 0, Move = PossibleMoves.Idling }, c => c.Including(p => p.X) .Including(p => p.Y) .Including(p => p.CurrentStamina) .Including(p => p.Move)); }
public void ProcessingContext_GetOrderedBotProperties_Should_Order_BotProperties() { // Arrange var arena = new ArenaDto(4, 5); var bot1 = new BotDto { Id = Guid.NewGuid() }; var bot2 = new BotDto { Id = Guid.NewGuid() }; var bots = new List <BotDto>(new[] { bot1, bot2 }); var processingContext = ProcessingContext.Build(arena, bots); var botProperties1 = BotProperties.Build(bot1, arena, bots); botProperties1.CurrentMove = PossibleMoves.WalkForward; var botProperties2 = BotProperties.Build(bot2, arena, bots); botProperties2.CurrentMove = PossibleMoves.MeleeAttack; processingContext.AddBotProperties(bot1.Id, botProperties1); processingContext.AddBotProperties(bot2.Id, botProperties2); // Act var result = processingContext.GetOrderedBotProperties(); // Assert result.Should().NotBeNull(); result.Should().ContainInOrder(botProperties2, botProperties1); }
public async Task Processor_Go_Should_Call_Into_The_BotProcessingFactory_For_Every_Bot_Provided() { // Arrange var botProcessingFactory = new Mock <IBotProcessingFactory>(); var processor = new Processor(botProcessingFactory.Object); var arena = new ArenaDto { Width = 4, Height = 6 }; var bot1 = new BotDto { Id = Guid.NewGuid() }; var bot2 = new BotDto { Id = Guid.NewGuid() }; var bot3 = new BotDto { Id = Guid.NewGuid() }; var bots = new List <BotDto>(new[] { bot1, bot2, bot3 }); var processingContext = ProcessingContext.Build(arena, bots); // Act await processor.Go(processingContext); // Assert botProcessingFactory.Verify(x => x.Process(It.IsAny <BotDto>(), It.IsAny <ProcessingContext>()), Times.Exactly(3)); }
public async Task BotProcessingFactory_Process_With_Valid_Script_Should_Register_Correct_Move() { // Arrange var botLogic = new Mock <IBotLogic>(); var botScriptCompiler = new BotScriptCompiler(); var botScriptCache = new BotScriptCache(); var logger = new Mock <ILogger <BotProcessingFactory> >(); var botProcessingFactory = new BotProcessingFactory( botLogic.Object, botScriptCompiler, botScriptCache, logger.Object); var arena = new ArenaDto { Width = 4, Height = 3 }; var bot = new BotDto { Id = Guid.NewGuid() }; var bots = new List <BotDto>(new[] { bot }); var context = ProcessingContext.Build(arena, bots); context.AddBotProperties(bot.Id, BotProperties.Build(bot, arena, bots)); var botScript = "WalkForward();".Base64Encode(); // Mock botLogic.Setup(x => x.GetBotScript(It.IsAny <Guid>())).ReturnsAsync(botScript); // Act await botProcessingFactory.Process(bot, context); // Assert context.GetBotProperties(bot.Id).CurrentMove.Should().Be(PossibleMoves.WalkForward); }
public async Task BotProcessingFactory_Process_Without_Valid_Script_Should_Register_ScriptError() { // Arrange var botLogic = new Mock <IBotLogic>(); var botScriptCompiler = new Mock <IBotScriptCompiler>(); var botScriptCache = new Mock <IBotScriptCache>(); var logger = new Mock <ILogger <BotProcessingFactory> >(); var botProcessingFactory = new BotProcessingFactory( botLogic.Object, botScriptCompiler.Object, botScriptCache.Object, logger.Object); var arena = new ArenaDto { Width = 4, Height = 3 }; var bot = new BotDto { Id = Guid.NewGuid() }; var bots = new List <BotDto>(new[] { bot }); var context = ProcessingContext.Build(arena, bots); context.AddBotProperties(bot.Id, BotProperties.Build(bot, arena, bots)); // Act await botProcessingFactory.Process(bot, context); // Assert context.GetBotProperties(bot.Id).CurrentMove.Should().Be(PossibleMoves.ScriptError); }
public async Task Postprocessor_Go_Should_Allow_A_Bot_To_Teleport_Onto_Another_Idling_Bot() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var postprocessor = new Postprocessor(randomHelper.Object); var arena = new ArenaDto(4, 1); var bot1 = new BotDto { Id = Guid.NewGuid(), X = 0, Y = 0, Orientation = PossibleOrientations.East, CurrentStamina = 15, CurrentHealth = 1 }; var bot2 = new BotDto { Id = Guid.NewGuid(), X = 3, Y = 0, Orientation = PossibleOrientations.West, CurrentStamina = 1, CurrentHealth = 1 }; var bots = new List <BotDto>(new[] { bot1, bot2 }); var context = ProcessingContext.Build(arena, bots); var bot1Properties = BotProperties.Build(bot1, arena, bots); var bot2Properties = BotProperties.Build(bot2, arena, bots); bot1Properties.CurrentMove = PossibleMoves.Teleport; bot1Properties.MoveDestinationX = 3; bot1Properties.MoveDestinationY = 0; bot2Properties.CurrentMove = PossibleMoves.Idling; context.AddBotProperties(bot1.Id, bot1Properties); context.AddBotProperties(bot2.Id, bot2Properties); // Act await postprocessor.Go(context); // Assert context.Bots.Should().HaveCount(2); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 3, Y = 0, FromX = 0, FromY = 0, Orientation = PossibleOrientations.East, CurrentStamina = 15 - Constants.STAMINA_ON_TELEPORT, Move = PossibleMoves.Teleport }, c => c .Including(p => p.X) .Including(p => p.Y) .Including(p => p.FromX) .Including(p => p.FromY) .Including(p => p.Orientation) .Including(p => p.CurrentStamina) .Including(p => p.Move)); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 0, Y = 0, Orientation = PossibleOrientations.West, CurrentStamina = 1, Move = PossibleMoves.Idling }, c => c.Including(p => p.X) .Including(p => p.Y) .Including(p => p.Orientation) .Including(p => p.CurrentStamina) .Including(p => p.Move)); }
public async Task Postprocessor_Go_Should_Execute_A_Teleport_Before_A_Regular_Move_And_Thus_Block_Other_Bots_In_Their_Path() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var postprocessor = new Postprocessor(randomHelper.Object); var arena = new ArenaDto { Width = 5, Height = 1 }; var bot1 = new BotDto { Id = Guid.NewGuid(), X = 0, Y = 0, Orientation = PossibleOrientations.East, CurrentStamina = 15, CurrentHealth = 1 }; var bot2 = new BotDto { Id = Guid.NewGuid(), X = 4, Y = 0, Orientation = PossibleOrientations.West, CurrentStamina = 1, CurrentHealth = 1 }; var bots = new List <BotDto>(new[] { bot1, bot2 }); var context = ProcessingContext.Build(arena, bots); var bot1Properties = BotProperties.Build(bot1, arena, bots); var bot2Properties = BotProperties.Build(bot2, arena, bots); bot1Properties.CurrentMove = PossibleMoves.Teleport; bot1Properties.MoveDestinationX = 3; bot1Properties.MoveDestinationY = 0; bot2Properties.CurrentMove = PossibleMoves.WalkForward; context.AddBotProperties(bot1.Id, bot1Properties); context.AddBotProperties(bot2.Id, bot2Properties); // Act await postprocessor.Go(context); // Assert context.Bots.Should().HaveCount(2); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 3, Y = 0, Orientation = PossibleOrientations.East, CurrentStamina = 15 - Constants.STAMINA_ON_TELEPORT, Move = PossibleMoves.Teleport }, c => c.Including(p => p.X) .Including(p => p.Y) .Including(p => p.Orientation) .Including(p => p.CurrentStamina) .Including(p => p.Move)); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 4, Y = 0, Orientation = PossibleOrientations.West, CurrentStamina = 1, Move = PossibleMoves.Idling }, c => c.Including(p => p.X) .Including(p => p.Y) .Including(p => p.Orientation) .Including(p => p.CurrentStamina) .Including(p => p.Move)); }
public async Task Postprocessor_Go_Should_Not_Move_Two_Bots_That_Are_Facing_Each_Other() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var postprocessor = new Postprocessor(randomHelper.Object); var arena = new ArenaDto { Width = 4, Height = 1 }; var bot1 = new BotDto { Id = Guid.NewGuid(), X = 1, Y = 0, Orientation = PossibleOrientations.East, CurrentStamina = 3, CurrentHealth = 1 }; var bot2 = new BotDto { Id = Guid.NewGuid(), X = 2, Y = 0, Orientation = PossibleOrientations.West, CurrentStamina = 4, CurrentHealth = 1 }; var bots = new List <BotDto>(new[] { bot1, bot2 }); var context = ProcessingContext.Build(arena, bots); var bot1Properties = BotProperties.Build(bot1, arena, bots); var bot2Properties = BotProperties.Build(bot2, arena, bots); bot1Properties.CurrentMove = PossibleMoves.WalkForward; bot2Properties.CurrentMove = PossibleMoves.WalkForward; context.AddBotProperties(bot1.Id, bot1Properties); context.AddBotProperties(bot2.Id, bot2Properties); // Act await postprocessor.Go(context); // Assert context.Bots.Should().HaveCount(2); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 1, Y = 0, Orientation = PossibleOrientations.East, CurrentStamina = 3, Move = PossibleMoves.Idling }, c => c.Including(p => p.X) .Including(p => p.Y) .Including(p => p.Orientation) .Including(p => p.CurrentStamina) .Including(p => p.Move)); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 2, Y = 0, Orientation = PossibleOrientations.West, CurrentStamina = 4, Move = PossibleMoves.Idling }, c => c.Including(p => p.X) .Including(p => p.Y) .Including(p => p.Orientation) .Including(p => p.CurrentStamina) .Including(p => p.Move)); }
public async Task Preprocessor_Go_Should_Not_Add_BotProperties_If_No_Bots_Provided() { // Arrange var arena = new ArenaDto(4, 6); var bots = new List <BotDto>(); var processingContext = ProcessingContext.Build(arena, bots); var preprocessor = new Preprocessor(); // Act await preprocessor.Go(processingContext); // Assert processingContext.GetOrderedBotProperties().Should().HaveCount(0); }
public void ProcessingContext_Build_Should_Build_A_Valid_ProcessingContext() { // Arrange var arena = new ArenaDto(4, 5); var bots = new List <BotDto>(); // Act var result = ProcessingContext.Build(arena, bots); // Assert result.Should().NotBeNull(); result.Arena.Should().BeSameAs(arena); result.Bots.Should().BeSameAs(bots); }
public async Task Processor_Go_Should_Not_Call_Into_The_BotProcessingFactory_If_No_Bots_Are_Provided() { // Arrange var botProcessingFactory = new Mock <IBotProcessingFactory>(); var processor = new Processor.Middleware.Processor(botProcessingFactory.Object); var arena = new ArenaDto(4, 6); var bots = new List <BotDto>(); var processingContext = ProcessingContext.Build(arena, bots); // Act await processor.Go(processingContext); // Assert botProcessingFactory.Verify(x => x.Process(It.IsAny <BotDto>(), It.IsAny <ProcessingContext>()), Times.Never); }
public void ProcessingContext_AddBotProperties_Should_Add_BotProperties() { // Arrange var arena = new ArenaDto(4, 5); var bot = new BotDto { Id = Guid.NewGuid() }; var bots = new List <BotDto>(new[] { bot }); var processingContext = ProcessingContext.Build(arena, bots); var botProperties = BotProperties.Build(bot, arena, bots); // Act processingContext.AddBotProperties(bot.Id, botProperties); var result = processingContext.GetBotProperties(bot.Id); // Assert result.Should().NotBeNull(); result.Should().BeSameAs(botProperties); }
public async Task Postprocessor_Go_Should_Work() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var postprocessor = new Postprocessor(randomHelper.Object); var arena = new ArenaDto { Width = 1, Height = 3 }; var bot = new BotDto { Id = Guid.NewGuid(), X = 0, Y = 2, Orientation = PossibleOrientations.North, CurrentStamina = 10, CurrentHealth = 1 }; var bots = new List <BotDto>(new[] { bot }); var context = ProcessingContext.Build(arena, bots); var botProperties = BotProperties.Build(bot, arena, bots); botProperties.CurrentMove = PossibleMoves.WalkForward; context.AddBotProperties(bot.Id, botProperties); // Act await postprocessor.Go(context); // Assert context.Bots.Should().HaveCount(1); context.Bots.Should().ContainEquivalentOf(new BotDto { X = 0, Y = 1, FromX = 0, FromY = 2, Orientation = PossibleOrientations.North, CurrentStamina = 9, Move = PossibleMoves.WalkForward }, c => c .Including(p => p.X) .Including(p => p.Y) .Including(p => p.FromX) .Including(p => p.FromY) .Including(p => p.Orientation) .Including(p => p.CurrentStamina) .Including(p => p.Move)); }
public async Task Preprocessor_Go_Should_Add_BotProperties_For_Each_Bot() { // Arrange var arena = new ArenaDto(4, 6); var bot = new BotDto { Id = Guid.NewGuid() }; var bots = new List <BotDto>(new[] { bot }); var processingContext = ProcessingContext.Build(arena, bots); var preprocessor = new Preprocessor(); // Act await preprocessor.Go(processingContext); // Assert var botProperties = processingContext.GetBotProperties(bot.Id); botProperties.Should().NotBeNull(); botProperties.BotId.Should().Be(bot.Id); processingContext.GetOrderedBotProperties().Should().HaveCount(1); }
public async Task Process() { using var stopwatch = new SimpleStopwatch(); var arena = await _arenaLogic.GetArena(); var elapsedArena = stopwatch.ElapsedMilliseconds; var bots = await _botLogic.GetAllLiveBots(); var elapsedBots = stopwatch.ElapsedMilliseconds - elapsedArena; var context = ProcessingContext.Build(arena, bots); await _preprocessor.Go(context); var elapsedPreprocessing = stopwatch.ElapsedMilliseconds - elapsedBots - elapsedArena; await _processor.Go(context); var elapsedProcessing = stopwatch.ElapsedMilliseconds - elapsedPreprocessing - elapsedBots - elapsedArena; await _postprocessor.Go(context); var elapsedPostprocessing = stopwatch.ElapsedMilliseconds - elapsedProcessing - elapsedPreprocessing - elapsedBots - elapsedArena; await _botLogic.UpdateBots(context.Bots); var elapsedUpdateBots = stopwatch.ElapsedMilliseconds - elapsedPostprocessing - elapsedProcessing - elapsedPreprocessing - elapsedBots - elapsedArena; //await _messageLogic.CreateMessages(context.Messages); var elapsedCreateMessages = stopwatch.ElapsedMilliseconds - elapsedUpdateBots - elapsedPostprocessing - elapsedProcessing - elapsedPreprocessing - elapsedBots - elapsedArena; _logger.LogInformation("{elapsedArena}ms, {elapsedBots}ms, {elapsedPreprocessing}ms, {elapsedProcessing}ms, {elapsedPostprocessing}ms, {elapsedUpdateBots}ms, {elapsedCreateMessages}ms", elapsedArena, elapsedBots, elapsedPreprocessing, elapsedProcessing, elapsedPostprocessing, elapsedUpdateBots, elapsedCreateMessages); }