Esempio n. 1
0
        public async Task <bool> AddAnArmyAsync(Army request)
        {
            _logger.LogInformation($"Attempting to add an army with name: {request.Name} to the database...");
            var battle = await _battleRepository.GetInitializingBattleAsync();

            request.BattleId = battle?.Id ?? 0;

            if (battle is null)
            {
                request.BattleId = await _battleRepository.CreateBattleAsync();
            }

            if (battle?.Armies?.Count(x => x.BattleId == request.BattleId && x.Name == request.Name) > 0)
            {
                throw new Exception($"An army with the name {request.Name} already exist for the current battle. Please choose another army name.");
            }

            await _trackingContext.AddAsync(request);

            var result = await _trackingContext.SaveChangesAsync();

            if (result > 0)
            {
                _logger.LogInformation($"Adding an army with name: {request.Name} successful...");
                return(true);
            }
            else
            {
                _logger.LogError($"Adding an army with name: {request.Name} failed!");
                return(false);
            }
        }
        private async Task StartAGame(StartGameResponse result)
        {
            _logger.LogInformation("Attempting to start a game");

            var battle = await _battleRepository.GetInitializingBattleAsync();

            if (battle is null)
            {
                result.ErrorMessages.Add("There is no battle ready to start. Please add armies to initialize battle.");
            }
            else if (battle.Armies?.Count() < _options.Value.MinimumArmies)
            {
                result.ErrorMessages.Add($"Cannot start a game with less than {_options.Value.MinimumArmies} in battle. Please add more armies to the current battle.");
            }
            else
            {
                _jobClient.Schedule <IGameService>(x => x.StartGameAsync(null, battle.Id), TimeSpan.FromSeconds(1));

                result.BattleId = battle.Id;
            }
        }