public async Task <BotDto> CreateBot(BotToCreateDto botToCreate)
        {
            var bot   = _botToCreateMapper.Map(botToCreate);
            var arena = await _arenaLogic.GetArena();

            var player = await _playerRepository.Single(x => x.Id == botToCreate.PlayerId);

            if (player.LastDeployment >= DateTime.UtcNow.AddMinutes(-_configurationHelper.BotDeploymentLimit))
            {
                throw new LogicException("You are not allowed to create multiple robots in rapid succession!");
            }

            player.LastDeployment = DateTime.UtcNow;

            bot.Player      = player;
            bot.Orientation = _randomHelper.Get <PossibleOrientations>();
            var bots = await GetAllActiveBots();

            var freeLocations      = BuildFreeLocation(arena, bots);
            var randomFreeLocation = freeLocations[_randomHelper.Get(freeLocations.Count)];

            bot.X              = randomFreeLocation.X;
            bot.Y              = randomFreeLocation.Y;
            bot.FromX          = bot.X;
            bot.FromY          = bot.Y;
            bot.CurrentHealth  = bot.MaximumHealth;
            bot.CurrentStamina = bot.MaximumStamina;
            bot.Memory         = new Dictionary <String, String>().Serialize();
            bot.TimeOfDeath    = DateTime.MaxValue;

            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                bot = await _botScriptRepository.Create(bot);

                var botScript = await _scriptRepository.Single(x => x.Id == bot.Id);

                botScript.Script = botToCreate.Script;
                await _scriptRepository.Update(botScript);

                await _playerRepository.Update(player);

                transaction.Complete();
            }

            var createdBot = _botMapper.Map(bot);

            return(createdBot);
        }
        public async Task Process()
        {
            var arena = await _arenaLogic.GetArena();

            var bots = await _botLogic.GetAllLiveBots();

            var context = ProcessingContext.Build(arena, bots);

            await _preprocessor.Go(context);

            await _processor.Go(context);

            await _postprocessor.Go(context);

            await _botLogic.UpdateBots(context.Bots);
        }
        public async Task <BotDto> CreateBot(BotToCreateDto botToCreate)
        {
            var bot   = _botToCreateMapper.Map(botToCreate);
            var arena = await _arenaLogic.GetArena();

            var player = await _playerRepository.Single(x => x.Id == botToCreate.PlayerId);

            bot.Player      = player;
            bot.Orientation = _randomHelper.Get <PossibleOrientations>();
            var bots = await GetAllActiveBots();

            var freeLocations      = BuildFreeLocation(arena, bots);
            var randomFreeLocation = freeLocations[_randomHelper.Get(freeLocations.Count)];

            bot.X              = randomFreeLocation.X;
            bot.Y              = randomFreeLocation.Y;
            bot.FromX          = bot.X;
            bot.FromY          = bot.Y;
            bot.CurrentHealth  = bot.MaximumHealth;
            bot.CurrentStamina = bot.MaximumStamina;
            bot.Memory         = new Dictionary <String, String>().Serialize();
            bot.TimeOfDeath    = DateTime.MaxValue;

            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                bot = await _botScriptRepository.Create(bot);

                var botScript = await _scriptRepository.Single(x => x.Id == bot.Id);

                botScript.Script = botToCreate.Script;
                await _scriptRepository.Update(botScript);

                transaction.Complete();
            }

            var createdBot = _botMapper.Map(bot);

            return(createdBot);
        }
        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;

                Console.WriteLine(
                    $"{elapsedArena}ms, {elapsedBots}ms, {elapsedPreprocessing}ms, {elapsedProcessing}ms, {elapsedPostprocessing}ms, {elapsedUpdateBots}ms, {elapsedCreateMessages}ms");
            }
        }
        public async Task <BotInfo> CreateBot(BotToCreate botToCreate)
        {
            var arena = await _arenaLogic.GetArena();

            var bots = await GetBots();

            var bot = new Bot
            {
                Name           = botToCreate.Name,
                Orientation    = _randomHelper.Get <PossibleOrientations>(),
                MaximumStamina = 100,
                CurrentStamina = 100,
                Move           = PossibleMoves.Idling,
                Memory         = new Dictionary <string, string>().Serialize(),
                Script         = botToCreate.Script
            };

            var freeLocations = GetFreeLocation(arena, bots);

            (bot.X, bot.Y) = freeLocations[_randomHelper.Get(freeLocations.Count)];

            await _dbContext.Bots.AddAsync(bot);

            await _dbContext.SaveChangesAsync();

            return(new BotInfo
            {
                Id = bot.Id,
                Name = bot.Name,
                Orientation = bot.Orientation,
                X = bot.X,
                Y = bot.Y,
                Move = bot.Move,
                MaximumStamina = bot.MaximumStamina,
                CurrentStamina = bot.CurrentStamina
            });
        }
Exemplo n.º 6
0
    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);
    }