예제 #1
0
    public void ScriptGlobals_LoadFromMemory_Should_Return_A_Default_Value_For_Unknown_Key()
    {
        // Arrange
        var botProperties = BuildBotProperties();
        var scriptGlobals = ScriptGlobals.Build(botProperties);
        var key           = "VAL";

        // Act
        var result = scriptGlobals.LoadFromMemory <int>(key);

        // Assert
        result.Should().BeZero();
    }
예제 #2
0
    public void ScriptGlobals_Talk_Should_Add_Entry_To_Messages()
    {
        // Arrange
        var botProperties = BuildBotProperties();
        var scriptGlobals = ScriptGlobals.Build(botProperties);
        var message       = "Message!";

        // Act
        scriptGlobals.Talk(message);

        // Assert
        botProperties.Messages.Should().HaveCount(1);
        botProperties.Messages.Should().Contain(message);
    }
예제 #3
0
    public void ScriptGlobals_Action_Correctly_Ignores_CurrentMove_If_Not_Idling(Action <ScriptGlobals> action, PossibleMoves originalMove)
    {
        // Arrange
        var botProperties = BuildBotProperties();

        botProperties.CurrentMove = originalMove;
        var scriptGlobals = ScriptGlobals.Build(botProperties);

        // Act
        action(scriptGlobals);

        // Assert
        originalMove.Should().NotBe(PossibleMoves.Idling);
        botProperties.CurrentMove.Should().Be(originalMove);
    }
예제 #4
0
    public void ScriptGlobals_StoreInMemory_Should_Store_A_Value_In_Memory()
    {
        // Arrange
        var botProperties = BuildBotProperties();
        var scriptGlobals = ScriptGlobals.Build(botProperties);
        var key           = "VAL";
        var value         = 42;

        // Act
        scriptGlobals.StoreInMemory(key, value);

        // Assert
        botProperties.Memory.Should().HaveCount(1);
        botProperties.Memory.Should().ContainKey(key);
        botProperties.Memory.Should().ContainValue(value.Serialize());
    }
예제 #5
0
    public void ScriptGlobals_Action_Correctly_Sets_CurrentMove_If_Idling(Action <ScriptGlobals> action, PossibleMoves expectedMove, int moveDestinationX, int moveDestinationY)
    {
        // Arrange
        var botProperties = BuildBotProperties();
        var scriptGlobals = ScriptGlobals.Build(botProperties);
        var originalMove  = botProperties.CurrentMove;

        // Act
        action(scriptGlobals);

        // Assert
        originalMove.Should().Be(PossibleMoves.Idling);
        botProperties.CurrentMove.Should().Be(expectedMove);
        botProperties.MoveDestinationX.Should().Be(moveDestinationX);
        botProperties.MoveDestinationY.Should().Be(moveDestinationY);
    }
예제 #6
0
    public void ScriptGlobals_RemoveFromMemory_Should_Remove_A_Stored_Value()
    {
        // Arrange
        var botProperties = BuildBotProperties();
        var scriptGlobals = ScriptGlobals.Build(botProperties);
        var key           = "VAL";
        var value         = 42;

        scriptGlobals.StoreInMemory(key, value);

        // Act
        scriptGlobals.RemoveFromMemory(key);

        // Assert
        botProperties.Memory.Should().HaveCount(0);
    }
예제 #7
0
    public void ScriptGlobals_LoadFromMemory_Should_Return_A_Stored_Value()
    {
        // Arrange
        var botProperties = BuildBotProperties();
        var scriptGlobals = ScriptGlobals.Build(botProperties);
        var key           = "VAL";
        var value         = 42;

        scriptGlobals.StoreInMemory(key, value);

        // Act
        var result = scriptGlobals.LoadFromMemory <int>(key);

        // Assert
        result.Should().Be(value);
    }
        public async Task Process(BotDto bot, ProcessingContext context)
        {
            var botProperties = context.GetBotProperties(bot.Id);

            try
            {
                var botScript = await GetCompiledBotScript(bot);

                var scriptGlobals = ScriptGlobals.Build(botProperties);
                await botScript.RunAsync(scriptGlobals);
            }
            catch
            {
                botProperties.CurrentMove = PossibleMoves.ScriptError;
            }
        }
예제 #9
0
    public void ScriptGlobals_Properties_Represent_BotProperties_Correctly()
    {
        // Arrange
        var botProperties = BuildBotProperties();

        // Act
        var scriptGlobals = ScriptGlobals.Build(botProperties);

        // Assert
        scriptGlobals.Should().NotBeNull();
        scriptGlobals.Should().BeEquivalentTo(botProperties, o => o
                                              .Including(x => x.Width)
                                              .Including(x => x.Height)
                                              .Including(x => x.X)
                                              .Including(x => x.Y)
                                              .Including(x => x.Orientation)
                                              .Including(x => x.LastMove)
                                              .Including(x => x.MaximumHealth)
                                              .Including(x => x.CurrentHealth)
                                              .Including(x => x.MaximumStamina)
                                              .Including(x => x.CurrentStamina));
    }
예제 #10
0
        public async Task <ScriptValidationResponse> Validate(ScriptValidationRequest request)
        {
            var scriptValidation = new ScriptValidationResponse();

            var botScript = await PrepareScript(request.Script);

            ImmutableArray <Diagnostic> diagnostics;

            using (var sw = new SimpleStopwatch())
            {
                diagnostics = botScript.Compile();
                scriptValidation.CompilationTimeInMilliseconds = sw.ElapsedMilliseconds;
            }

            if (!diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
            {
                var task = Task.Run(() =>
                {
                    var arena = new ArenaDto {
                        Width = 10, Height = 10
                    };
                    var bot = new BotDto
                    {
                        Id             = Guid.NewGuid(),
                        Name           = "Bot",
                        MaximumHealth  = 100,
                        CurrentHealth  = 100,
                        MaximumStamina = 100,
                        CurrentStamina = 100,
                        X           = 1,
                        Y           = 1,
                        Orientation = PossibleOrientations.South,
                        Memory      = new Dictionary <string, string>().Serialize()
                    };
                    var friendBot = new BotDto
                    {
                        Id             = Guid.NewGuid(),
                        Name           = "Friend",
                        MaximumHealth  = 100,
                        CurrentHealth  = 100,
                        MaximumStamina = 100,
                        CurrentStamina = 100,
                        X           = 1,
                        Y           = 3,
                        Orientation = PossibleOrientations.North
                    };
                    var enemyBot = new BotDto
                    {
                        Id             = Guid.NewGuid(),
                        Name           = "Enemy",
                        MaximumHealth  = 100,
                        CurrentHealth  = 100,
                        MaximumStamina = 100,
                        CurrentStamina = 100,
                        X           = 1,
                        Y           = 5,
                        Orientation = PossibleOrientations.North
                    };
                    var botProperties = BotProperties.Build(bot, arena, new[] { bot, friendBot, enemyBot }.ToList());
                    var scriptGlobals = ScriptGlobals.Build(botProperties);
                    using (var sw = new SimpleStopwatch())
                    {
                        try
                        {
                            botScript.RunAsync(scriptGlobals).Wait();
                        }
                        catch (Exception ex)
                        {
                            scriptValidation.ValidationMessages.Add(new ScriptValidationMessage
                            {
                                Message = "Runtime error: " + ex.Message
                            });
                        }
                        scriptValidation.RunTimeInMilliseconds = sw.ElapsedMilliseconds;
                    }
                });

                if (!task.Wait(TimeSpan.FromSeconds(2)))
                {
                    scriptValidation.ValidationMessages.Add(new ScriptValidationMessage
                    {
                        Message = "Your script did not finish in a timely fashion!"
                    });

                    scriptValidation.RunTimeInMilliseconds = long.MaxValue;
                }
            }

            foreach (var diagnostic in diagnostics)
            {
                if (diagnostic.Severity == DiagnosticSeverity.Error)
                {
                    scriptValidation.ValidationMessages.Add(new ScriptValidationMessage
                    {
                        Message       = diagnostic.GetMessage(),
                        LocationStart = diagnostic.Location.SourceSpan.Start,
                        LocationEnd   = diagnostic.Location.SourceSpan.End
                    });
                }
            }

            return(scriptValidation);
        }