Ejemplo n.º 1
0
        public async Task <IActionResult> Fight(FightRequestDto request)
        {
            ServiceResponse <FightResultDto> response = await _fightService.Fight(request);

            if (!response.Success)
            {
                return(BadRequest(response));
            }
            return(Ok(response));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Fight(FightRequestDto fightRequestDto)
        {
            var response = await _service.Fight(fightRequestDto);

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

            return(Ok(response));
        }
Ejemplo n.º 3
0
        public async Task <ServiceResponse <FightResultDto> > Fight(FightRequestDto fightRequestDto)
        {
            ServiceResponse <FightResultDto> res = new ServiceResponse <FightResultDto>
            {
                Data = new FightResultDto()
            };

            try{
                List <Character> characters = await _context.Characters
                                              .Include(c => c.Weapon)
                                              .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill)
                                              .Where(c => fightRequestDto.CharacterId.Contains(c.Id))
                                              .ToListAsync();
            }
            catch (Exception ex)
            {
                res.Success = false;
                res.Message = ex.Message;
            }
            return(res);
        }
Ejemplo n.º 4
0
        public async Task <ServiceResponse <FightResultDto> > Fight(FightRequestDto request)
        {
            ServiceResponse <FightResultDto> response = new ServiceResponse <FightResultDto>
            {
                Data = new FightResultDto()
            };

            try
            {
                List <Character> characters = await _context.Characters
                                              .Include(c => c.Weapon)
                                              .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill)
                                              .Where(c => request.CharacterIds.Contains(c.Id)).ToListAsync();

                bool defeated = false;
                while (!defeated)
                {
                    foreach (Character attacker in characters)
                    {
                        List <Character> opponents = characters.Where(c => c.Id != attacker.Id).ToList();
                        Character        opponent  = opponents[new Random().Next(opponents.Count)];

                        int    damage     = 0;
                        string attackUsed = string.Empty;

                        bool useWeapon = new Random().Next(2) == 0;
                        if (useWeapon)
                        {
                            attackUsed = attacker.Weapon.Name;
                            damage     = DoWeaponAttack(attacker, opponent);
                        }
                        else
                        {
                            int randomSkill = new Random().Next(attacker.CharacterSkills.Count);
                            attackUsed = attacker.CharacterSkills[randomSkill].Skill.Name;
                            damage     = DoSkillAttack(attacker, opponent, attacker.CharacterSkills[randomSkill]);
                        }

                        response.Data.Log.Add($"{attacker.Name} attacks {opponent.Name} using {attackUsed} with {(damage >= 0 ? damage : 0)} damage.");

                        if (opponent.HitPoints <= 0)
                        {
                            defeated = true;
                            attacker.Victories++;
                            opponent.Defeats++;
                            response.Data.Log.Add($"{opponent.Name} has been defeated");
                            response.Data.Log.Add($"{attacker.Name} wins with {attacker.HitPoints} HP left!");
                            break;
                        }
                    }
                }
                characters.ForEach(c =>
                {
                    c.Fights++;
                    c.HitPoints = 100;
                });
                _context.Characters.UpdateRange(characters);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
Ejemplo n.º 5
0
 public async Task <IActionResult> Fight(FightRequestDto request)
 {
     return(Ok(await _fightService.Fight(request)));
 }
Ejemplo n.º 6
0
 public async Task <ActionResult <ServiceResponse <FightRequestDto> > > Fight(FightRequestDto request)
 {
     return(Ok(await _fightService.Fight(request)));
 }
Ejemplo n.º 7
0
        public async Task <ServiceResponse <FightResultDto> > Fight(FightRequestDto fightRequest)
        {
            ServiceResponse <FightResultDto> response = new ServiceResponse <FightResultDto>();

            try
            {
                List <Character> characters = await _characterService.GetGroupOfCharacters(fightRequest.CharacterIds);

                response.Data = new FightResultDto();

                bool defeated = false;
                while (!defeated)
                {
                    foreach (Character attacker in characters)
                    {
                        List <Character> defenders = characters.Where(ch => ch.Id != attacker.Id).ToList();
                        Character        defender  = defenders[new Random().Next(defenders.Count)];

                        int    damage;
                        string attackUsed;

                        bool useWeapon = new Random().Next(2) == 0;
                        if (useWeapon || attacker.CharacterSkills.Count == 0)
                        {
                            damage     = DoWeaponAttack(attacker, defender);
                            attackUsed = attacker.Weapon?.Name ?? "Fist";
                        }
                        else
                        {
                            int   randomSkill = new Random().Next(attacker.CharacterSkills.Count);
                            Skill skill       = attacker.CharacterSkills[randomSkill].Skill;
                            attackUsed = skill.Name;
                            damage     = DoSkillAttack(attacker, defender, skill);
                        }

                        response.Data.Log.Add($"{attacker.Name} attacks {defender.Name} using {attackUsed} with {(damage >= 0 ? damage.ToString() : 0.ToString())} damage");

                        if (defender.Health <= 0)
                        {
                            defeated = true;
                            attacker.Victories++;
                            defender.Defeats++;
                            response.Data.Log.Add($"{attacker.Name} wins with {attacker.Health} HP left");
                            response.Data.Log.Add($"{defender.Name} has been defeated!");
                            break;
                        }
                    }
                }
                foreach (Character character in characters)
                {
                    character.Fights++;
                    character.Health = 100;
                }
                _context.Characters.UpdateRange(characters);
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = e.Message;
            }

            return(response);
        }
Ejemplo n.º 8
0
        public async Task <ServiceResponse <FightResutlDto> > Fight(FightRequestDto fightRequestDto)
        {
            var response = new ServiceResponse <FightResutlDto>();
            var log      = new List <string>();

            try
            {
                var characters = await _context.Characters
                                 .Include(c => c.Weapon)
                                 .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill)
                                 .Where(c => fightRequestDto.CharacterIds.Contains(c.Id))
                                 .ToListAsync();

                if (characters.Count >= 2)
                {
                    var defeated = false;

                    while (!defeated)
                    {
                        foreach (var attacker in characters)
                        {
                            var opponents = characters.Where(c => c.Id != attacker.Id).ToList();
                            var opponent  = opponents[new Random().Next(opponents.Count)];

                            var damage     = 0;
                            var attackUsed = string.Empty;

                            var useWeapon = new Random().Next(2) == 0;

                            if (useWeapon)
                            {
                                attackUsed = attacker.Weapon?.Name;
                                damage     = DoWeaponAttack(attacker, opponent);
                            }
                            else
                            {
                                var randomSkill = new Random().Next(attacker.CharacterSkills.Count);
                                attackUsed = attacker.CharacterSkills.FirstOrDefault(cs => cs.SkillId == randomSkill)?.Skill.Name;
                                damage     = DoSkillAttack(randomSkill, attacker, opponent);
                            }

                            log.Add($"{attacker.Name} attacks {opponent.Name} using {attackUsed} with {(damage >= 0? damage : 0)} damage.");

                            if (opponent.HitPoints <= 0)
                            {
                                defeated = true;

                                attacker.Victories++;
                                opponent.Defeats++;

                                log.Add($"{opponent.Name} has been defeated!");
                                log.Add($"{attacker.Name} wins with {attacker.HitPoints} HP left!");
                                break;
                            }
                        }
                    }

                    characters.ForEach(c =>
                    {
                        c.Fights++;
                        c.HitPoints = 100;
                    });

                    await _context.SaveChangesAsync();

                    response.Data = new FightResutlDto {
                        Log = log
                    };
                }
                else
                {
                    response.Message = "insufficient characters";
                    response.Success = false;
                }
            }
            catch (Exception e)
            {
                response.Message = e.Message;
                response.Success = false;
            }

            return(response);
        }
Ejemplo n.º 9
0
        public async Task <ServiceResponse <FightResultDto> > Fight(FightRequestDto request)
        {
            ServiceResponse <FightResultDto> response = new ServiceResponse <FightResultDto>
            {
                Data = new FightResultDto(),
            };

            try
            {
                List <Character> characters =
                    await _dataContext.Characters
                    .Include(c => c.Weapon)
                    .Include(c => c.CharacterSkills).ThenInclude(c => c.Skill)
                    .Where(c => request.CharacterIds.Contains(c.Id)).ToListAsync();

                bool defeated = false;
                while (!defeated)
                {
                    foreach (Character attacker in characters)
                    {
                        List <Character> opponents = characters.Where(c => c.Id != attacker.Id).ToList();
                        Character        opponent  = opponents[new Random().Next(opponents.Count)];

                        int    damage     = 0;
                        string attackUsed = string.Empty;

                        bool useWeapon = new Random().Next(2) == 0;
                        if (useWeapon)
                        {
                            if (attacker.Weapon != null)
                            {
                                attackUsed = attacker.Weapon.Name;
                                damage     = DoWeaponAttack(attacker, opponent);
                            }
                        }
                        else
                        {
                            int totalSkills = attacker.CharacterSkills.Count;
                            if (totalSkills > 0)
                            {
                                int randomSkill = new Random().Next(totalSkills);
                                attackUsed = attacker.CharacterSkills[randomSkill].Skill.Name;
                                damage     = DoSkillAttack(attacker.CharacterSkills[randomSkill], attacker, opponent);
                            }
                        }

                        response.Data.Log.Add(string.Format("{0} attacks {1} using {2} with  {3} damage", attacker.Name, opponent.Name, attackUsed, damage));
                        if (opponent.HitPoints <= 0)
                        {
                            defeated = true;
                            attacker.Victories++;
                            opponent.Defeats++;
                            response.Data.Log.Add(string.Format("{0} has been defeated", opponent.Name));
                            response.Data.Log.Add(string.Format("{0} wins with {1} HP left", attacker.Name, attacker.HitPoints));
                            break;
                        }
                    }
                }

                characters.ForEach(c =>
                {
                    c.Fights++;
                    c.HitPoints = 100;
                });
                _dataContext.Characters.UpdateRange(characters);
                await _dataContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = e.Message;
            }


            return(response);
        }
Ejemplo n.º 10
0
        public async Task <ServiceResponse <FightResultDto> > Fight(FightRequestDto request)
        {
            ServiceResponse <FightResultDto> response = new ServiceResponse <FightResultDto>
            {
                Data = new FightResultDto()
            };

            try
            {
                // get all combatants, include the weapon,character skills, and skills entities
                List <Character> characters = await _context.Characters
                                              .Include(c => c.Weapon)
                                              .Include(c => c.CharacterSkills)
                                              .ThenInclude(cs => cs.Skill)
                                              .Where(c => request.CharacterIds.Contains(c.Id))
                                              .ToListAsync();

                bool defeated = false;

                while (!defeated)
                {
                    // each combatant takes their turn attacking
                    foreach (Character attacker in characters)
                    {
                        // get all the opponents of the attacker
                        List <Character> opponents = characters.Where(c => c.Id != attacker.Id).ToList();

                        // randomly select an opponent, pass opponent count to use as an index for the opponents list
                        Character opponent = opponents[new Random().Next(opponents.Count)];

                        int    damage     = 0;
                        string attackUsed = string.Empty;

                        // attack with either weapon or skill - if 0 we use weapon else skill
                        bool useWeapon = new Random().Next(2) == 0;

                        if (useWeapon)
                        {
                            attackUsed = attacker.Weapon.Name;
                            damage     = DoWeaponAttack(attacker, opponent);
                        }
                        else
                        {
                            int randomSkill = new Random().Next(attacker.CharacterSkills.Count);
                            attackUsed = attacker.CharacterSkills[randomSkill].Skill.Name;
                            damage     = DoSkillAttack(attacker, opponent, attacker.CharacterSkills[randomSkill]);
                        }

                        response.Data.Log.Add($"{attacker.Name} has attacked {opponent.Name} using {attackUsed} inflicting {(damage >= 0 ? damage : 0)} damage.");

                        if (opponent.HitPoints <= 0)
                        {
                            defeated = true;
                            attacker.Victories++;
                            opponent.Defeats++;
                            response.Data.Log.Add($"{opponent.Name} has been defeated! {attacker.Name} has {attacker.HitPoints} left.");
                            break;
                        }
                    }
                }
                // increment fight count and reset hp of all combatants
                characters.ForEach(c =>
                {
                    c.Fights++;
                    c.HitPoints = 100;
                });

                // update characters and save changes
                _context.Characters.UpdateRange(characters);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = "Fighting execution failed: " + ex.Message;
            }

            return(response);
        }
Ejemplo n.º 11
0
 public async Task <IActionResult> Fight(FightRequestDto fightRequest) =>
 ActionResultHandler(await _fightService.Fight(fightRequest));