Beispiel #1
0
        private void NormalizeDestination()
        {
            if (BotProperties.MoveDestinationX < 0 || BotProperties.MoveDestinationX > BotProperties.Width - 1)
            {
                BotProperties.MoveDestinationX = _randomHelper.Get(BotProperties.Width);
            }

            if (BotProperties.MoveDestinationY < 0 || BotProperties.MoveDestinationY > BotProperties.Height - 1)
            {
                BotProperties.MoveDestinationY = _randomHelper.Get(BotProperties.Height);
            }
        }
        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 <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 <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
            });
        }