Esempio n. 1
0
        public async Task <Battle> GetBattleByIdAsync(int battleId)
        {
            Battle battle = await _battleService.GetBattleByIdAsync(battleId);

            lock (CacheLock)
            {
                return(_cache ?? (_cache = battle));
            }
        }
Esempio n. 2
0
        public async Task <Army> ExecuteAsync(Army offensiveArmy)
        {
            Battle battle = await _battleService.GetBattleByIdAsync(offensiveArmy.BattleId);

            return
                (battle.Armies
                 .Where(x => !x.IsDead && x.Id != offensiveArmy.Id)
                 .OrderByDescending(x => x.NumberOfUnits - x.NumberOfAttacks)
                 .FirstOrDefault());
        }
Esempio n. 3
0
        public async Task Start(int battleId, IJobCancellationToken cancellationToken)
        {
            Battle battle = await _battleService.GetBattleByIdAsync(battleId);

            var workers = new List <Task>();

            foreach (Army army in battle.Armies.Where(x => !x.IsDead))
            {
                workers.Add(Task.Run(() => Execute(army, cancellationToken)));
            }

            await Task.WhenAll(workers);

            battle.Stop();
            await _unitOfWork.SaveChanges();
        }
        public async Task <Army> ExecuteAsync(Army offensiveArmy)
        {
            Battle battle = await _battleService.GetBattleByIdAsync(offensiveArmy.BattleId);

            IEnumerable <Army> defensiveArmies =
                battle.Armies
                .Where(x => !x.IsDead && x.Id != offensiveArmy.Id)
                .ToList();

            if (defensiveArmies.Any())
            {
                return(defensiveArmies.ElementAt(Random.Next(defensiveArmies.Count())));
            }

            return(null);
        }
        public async Task <IResponse> Start(int battleId)
        {
            Battle battle = await _battleService.GetBattleByIdAsync(battleId);

            if (battle == null)
            {
                return(new ErrorResponse("Battle not found"));
            }

            await ResetAsync(battleId);

            IResponse response = battle.Start();

            if (!response.Success)
            {
                return(response);
            }

            await _battleSimulatorService.SimulateAsync(battle);

            await _unitOfWork.SaveChanges();

            return(new SuccessResponse());
        }