Esempio n. 1
0
    public async Task <IActionResult> Template(PlayViewModel vm)
    {
        if (HttpContext.Session.Keys.Contains("PLAYER"))
        {
            var player = HttpContext.Session.GetObject <PlayerDto>("PLAYER");

            var templates = await _templateLogic.GetAllTemplates();

            var    valid      = IsValid(vm);
            string sadMessage = "You have made some errors!";

            if (valid)
            {
                var script = templates.Single(x => x.Id == vm.SelectedScript).Script.Base64Encode();

                var scriptValidationResult = await _scriptValidationHelper.Validate(new ScriptToValidateDto(script));

                if (scriptValidationResult != null && scriptValidationResult.Messages.Count == 0)
                {
                    var botToCreate = new BotToCreateDto(player.Id, vm.BotName, vm.BotHealth, vm.BotStamina, script);

                    try
                    {
                        await _botLogic.CreateBot(botToCreate);
                    }
                    catch (LogicException ex)
                    {
                        valid      = false;
                        sadMessage = ex.Message;
                    }
                }
                else
                {
                    valid = false;
                    if (scriptValidationResult == null)
                    {
                        sadMessage = "Your script could not be validated for an unknown reason.";
                    }
                    else
                    {
                        var scriptErrors = string.Join(", ", scriptValidationResult.Messages.Select(x => x.Message));
                        sadMessage = $"Your script contains some compile errors: {scriptErrors}";
                    }
                }
            }

            vm = new PlayViewModel
            {
                PlayerName     = player.Name,
                BotName        = vm.BotName,
                BotHealth      = vm.BotHealth,
                BotStamina     = vm.BotStamina,
                SelectedScript = vm.SelectedScript,
                Scripts        = templates
            };

            if (valid)
            {
                vm.HappyMessage = $"{vm.BotName} for player {vm.PlayerName} has been created successfully!";
            }
            else
            {
                vm.SadMessage = sadMessage;
            }

            ViewData["ArenaUrl"]          = _configuration.GetValue <string>("ARENA_URL");
            ViewData["ScriptTemplateUrl"] = _configuration.GetValue <string>("SCRIPT_TEMPLATE_URL");
            return(View(vm));
        }

        return(RedirectToAction("Index", "Home"));
    }
Esempio n. 2
0
        public async Task <IActionResult> Template(PlayViewModel vm)
        {
            if (HttpContext.Session.Keys.Contains("PLAYER"))
            {
                var player = HttpContext.Session.GetObject <PlayerDto>("PLAYER");

                var    valid      = IsValid(vm);
                string sadMessage = "You have made some errors!";

                if (valid)
                {
                    var script = BotScripts.All.Single(x => x.Id == vm.SelectedScript).Script.Base64Encode();

                    var scriptValidationResult = await _scriptValidationHelper.Validate(
                        new ScriptToValidateDto { Script = script });

                    if (scriptValidationResult != null && scriptValidationResult.Messages.Count == 0)
                    {
                        var botToCreate = new BotToCreateDto
                        {
                            PlayerId       = player.Id,
                            Name           = vm.BotName,
                            MaximumHealth  = vm.BotHealth,
                            MaximumStamina = vm.BotStamina,
                            Script         = script
                        };

                        await _botLogic.CreateBot(botToCreate);
                    }
                    else
                    {
                        valid = false;
                        if (scriptValidationResult == null)
                        {
                            sadMessage = "Your script could not be validated for an unknown reason.";
                        }
                        else
                        {
                            var scriptErrors = string.Join(", ", scriptValidationResult.Messages.Select(x => x.Message));
                            sadMessage = $"Your script contains some compile errors: {scriptErrors}";
                        }
                    }
                }

                vm = new PlayViewModel
                {
                    PlayerName     = player.Name,
                    BotName        = vm.BotName,
                    BotHealth      = vm.BotHealth,
                    BotStamina     = vm.BotStamina,
                    SelectedScript = vm.SelectedScript,
                    Scripts        = BotScripts.All
                };

                if (valid)
                {
                    vm.HappyMessage = $"{vm.BotName} for player {vm.PlayerName} has been created successfully!";
                }
                else
                {
                    vm.SadMessage = sadMessage;
                }

                return(View(vm));
            }

            return(RedirectToAction("Index", "Home"));
        }
 public override Task <ScriptValidationResponse> Validate(ScriptValidationRequest request, ServerCallContext context)
 {
     return(_scriptValidationHelper.Validate(request));
 }
Esempio n. 4
0
 public override Task <ScriptValidationResponse> Validate(ScriptValidationRequest request, ServerCallContext context)
 {
     _logger.LogInformation("Validator in action!");
     return(_scriptValidationHelper.Validate(request));
 }