public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto request)
        {
            ServiceResponse <AttackResultDto> response = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await _context.Characters.Include(c => c.Weapon).FirstOrDefaultAsync(c => c.Id == request.AttackerId);

                Character opponent = await _context.Characters.FirstOrDefaultAsync(c => c.Id == request.OpponentId);

                int damage = DoWeaponAttack(attacker, opponent);
                if (opponent.HitPoints <= 0)
                {
                    response.Message = $"{opponent.Name} has been defeated!";
                }

                _context.Characters.Update(opponent);
                await _context.SaveChangesAsync();

                response.Data = new AttackResultDto
                {
                    Attacker   = attacker.Name,
                    AttackerHP = attacker.HitPoints,
                    Opponent   = opponent.Name,
                    OpponentHP = opponent.HitPoints,
                    Damage     = damage
                };
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return(response);
        }
Exemple #2
0
        public async Task <IActionResult> WeaponAttack(WeaponAttackDto request)
        {
            ServiceResponse <AttackResultDto> response = await _fightService.WeaponAttack(request);

            if (!response.Success)
            {
                return(BadRequest(response));
            }
            return(Ok(response));
        }
        public async Task <IActionResult> WeaponAttack(WeaponAttackDto weaponAttackDto)
        {
            var response = await _service.WeaponAttack(weaponAttackDto);

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

            return(Ok(response));
        }
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto weaponAttackDto)
        {
            ServiceResponse <AttackResultDto> serviceResponse = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await _context.Characters
                                     .Include(c => c.Weapon)
                                     .FirstOrDefaultAsync(c => c.Id == weaponAttackDto.AttackerID);

                if (attacker == null)
                {
                    serviceResponse.Success = false;
                    serviceResponse.Message = "Attacker not found";
                    return(serviceResponse);
                }

                Character opponent = await _context.Characters
                                     .FirstOrDefaultAsync(c => c.Id == weaponAttackDto.OpponentId);

                if (opponent == null)
                {
                    serviceResponse.Success = false;
                    serviceResponse.Message = "Opponent not found";
                    return(serviceResponse);
                }

                int damage = DoWeaponAttack(attacker, opponent);

                if (opponent.HitPoints <= 0)
                {
                    serviceResponse.Message = $"{opponent.Name} has been defeated";
                }

                _context.Characters.Update(opponent);
                await _context.SaveChangesAsync();

                serviceResponse.Data = new AttackResultDto()
                {
                    Attacker    = attacker.Name,
                    Opponent    = opponent.Name,
                    AttackerHP  = attacker.HitPoints,
                    OpponenetHP = opponent.HitPoints,
                    Damage      = damage
                };
            }
            catch (System.Exception ex)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = ex.Message;
            }

            return(serviceResponse);
        }
Exemple #5
0
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto weaponAttackDto)
        {
            var response = new ServiceResponse <AttackResultDto>();

            try
            {
                var attacker = await _context.Characters.Include(c => c.Weapon).FirstOrDefaultAsync(c => c.Id == weaponAttackDto.AttackerId);

                var opponent = await _context.Characters.FirstOrDefaultAsync(c => c.Id == weaponAttackDto.OpponentId);

                if (attacker != null && opponent != null)
                {
                    int damage = DoWeaponAttack(attacker, opponent);

                    if (opponent.HitPoints <= 0)
                    {
                        response.Message = $"{opponent.Name} has been defeated!";
                        attacker.Fights++;
                        opponent.Fights++;

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

                        attacker.HitPoints = 100;
                        opponent.HitPoints = 100;
                    }

                    await _context.SaveChangesAsync();

                    response.Data = new AttackResultDto
                    {
                        Attacker   = attacker.Name,
                        Opponent   = opponent.Name,
                        AttackerHP = attacker.HitPoints,
                        OpponentHP = opponent.HitPoints
                    };
                }
                else
                {
                    response.Message = "no such characters";
                    response.Success = false;
                }
            }
            catch (Exception e)
            {
                response.Message = e.Message;
                response.Success = false;
            }

            return(response);
        }
Exemple #6
0
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto request)
        {
            ServiceResponse <AttackResultDto> serviceResponse = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await _context.Characters
                                     .Include(c => c.Weapon)
                                     .FirstOrDefaultAsync(c => c.Id == request.AttackerId);

                Character opponent = await _context.Characters
                                     .FirstOrDefaultAsync(c => c.Id == request.OpponentId);

                if (request.AttackerId == request.OpponentId)
                {
                    serviceResponse.Success = false;
                    serviceResponse.Message = "Character cannot attack itself";
                    return(serviceResponse);
                }

                /*
                 * Damage formula
                 * We can write the formula of our own choice
                 */
                int damage = WeaponAttack(attacker, opponent);
                if (opponent.HitPoints <= 0)
                {
                    serviceResponse.Message = $"{opponent.Name} has been defeated by the attacker {attacker.Name}";
                }

                _context.Characters.Update(opponent);
                await _context.SaveChangesAsync();


                serviceResponse.Data = new AttackResultDto
                {
                    Attacker   = attacker.Name,
                    AttackerHP = attacker.HitPoints,
                    Opponent   = opponent.Name,
                    OpponentHP = opponent.HitPoints,
                    Damage     = damage
                };
            }
            catch (Exception e)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = e.Message;
            }
            return(serviceResponse);
        }
Exemple #7
0
        // Weapon Attacks
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto request)
        {
            // wrapper
            ServiceResponse <AttackResultDto> response = new ServiceResponse <AttackResultDto>();

            try
            {
                // get attacker by the request id, includes the weapon entity
                Character attacker = await _context.Characters
                                     .Include(c => c.Weapon)
                                     .FirstOrDefaultAsync(c => c.Id == request.AttackerId);

                // get opponent by request id
                Character opponent = await _context.Characters
                                     .FirstOrDefaultAsync(c => c.Id == request.OpponentId);

                // attack with weapon algorithm
                int damage = DoWeaponAttack(attacker, opponent);

                // if health falls to/or below 0
                if (opponent.HitPoints <= 0)
                {
                    response.Message = $"You have defeated {opponent.Name} in combat!";
                }

                // update opponent stats and save changes
                _context.Characters.Update(opponent);
                await _context.SaveChangesAsync();

                // wrapper data consists of the attack/opponent Name, attacker/opponent health and damage inflicted
                response.Data = new AttackResultDto
                {
                    Attacker   = attacker.Name,
                    AttackerHp = attacker.HitPoints,
                    Opponent   = opponent.Name,
                    OpponentHp = opponent.HitPoints,
                    Damage     = damage
                };
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = "Failed to execute attack: " + ex.Message;
            }
            return(response);
        }
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto request)
        {
            ServiceResponse <AttackResultDto> response = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await _dbContext.Characters.
                                     Include(c => c.Weapon).
                                     FirstOrDefaultAsync(c => c.Id == request.AttackerId);

                Character opponent = await _dbContext.Characters.FirstOrDefaultAsync(c => c.Id == request.OpponentId);

                //int damage = attacker.Weapon.Damage + (new Random().Next(attacker.Strength));

                int damage = DoWeaponAttack(attacker, opponent);
                damage -= new Random().Next(opponent.Defense);
                if (damage > 0)
                {
                    opponent.HitPoints -= (int)damage;
                }
                if (opponent.HitPoints <= 0)
                {
                    response.Message = $"{opponent.Name} has been defeated!";
                }

                _dbContext.Characters.Update(opponent);
                await _dbContext.SaveChangesAsync();

                response.Data = new AttackResultDto
                {
                    Attacker   = attacker.Name,
                    AttackerHP = attacker.HitPoints,
                    Opponent   = opponent.Name,
                    OpponentHP = opponent.HitPoints,
                    Damage     = damage
                };
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = $"An error occured. Error: {ex.Message} Inner Exception : {ex.InnerException}";
            }
            return(response);
        }
Exemple #9
0
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto request)
        {
            ServiceResponse <AttackResultDto> response = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await this._dataContext.Characters.Include(c => c.Weapon)
                                     .FirstOrDefaultAsync(c => c.Id == request.AttackerId && c.User.Id == this.GetUserId());

                Character opponent = await this._dataContext.Characters
                                     .FirstOrDefaultAsync(c => c.Id == request.OpponentId);

                if (attacker == null || opponent == null)
                {
                    response.Success = false;
                    response.Message = "Character not found.";
                    return(response);
                }
                int damage = FightService.ExecuteWeaponAttack(attacker, opponent);
                if (opponent.HitPoints <= 0)
                {
                    response.Message = $"{ opponent.Name } has been defeated.";
                }

                this._dataContext.Characters.Update(opponent);
                await this._dataContext.SaveChangesAsync();

                response.Data = new AttackResultDto
                {
                    Attacker          = attacker.Name,
                    AttackerHitPoint  = attacker.HitPoints,
                    Opponent          = opponent.Name,
                    OpponentHitPoints = opponent.HitPoints,
                    Damage            = damage
                };
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return(response);
        }
Exemple #10
0
        public async Task <ServiceResponse <AttackResultDto> > weaponAttack(WeaponAttackDto request)
        {
            ServiceResponse <AttackResultDto> response = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await _context.Characters
                                     .Include(c => c.weapon)
                                     .FirstOrDefaultAsync(c => c.Id == request.AttackerId);

                Character Opponent = await _context.Characters.FirstOrDefaultAsync(c => c.Id == request.OpponentId);

                int damage = attacker.weapon.Damage + (new Random().Next(attacker.Strength));
                damage -= new Random().Next(Opponent.Defense);
                if (damage > 0)
                {
                    Opponent.HitPoints -= damage;
                }
                if (Opponent.HitPoints <= 0)
                {
                    response.Message = $"{Opponent.Name} has been defeated";
                }
                _context.Characters.Update(Opponent);
                await _context.SaveChangesAsync();

                response.Data = new AttackResultDto
                {
                    Attacker   = attacker.Name,
                    AttackerHP = attacker.HitPoints,
                    Opponent   = Opponent.Name,
                    OpponentHP = Opponent.HitPoints,
                    Damage     = damage
                };
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return(response);
        }
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto weaponAttack)
        {
            ServiceResponse <AttackResultDto> response = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await _characterService.GetCharacterById(weaponAttack.AttackerId, AuthType.WithAuth);

                if (attacker == null)
                {
                    throw new Exception("Attacker not found.");
                }

                Character defender = await _characterService.GetCharacterById(weaponAttack.DefenderId, AuthType.NoneAuth);

                if (defender == null)
                {
                    throw new Exception("Defender not found.");
                }

                int damage = DoWeaponAttack(attacker, defender);
                if (defender.Health <= 0)
                {
                    response.Message = $"{defender.Name} has fallen!";
                }

                await _characterService.UpdateCharacter(_mapper.Map <UpdateCharacterDto>(defender));

                response.Data = AttackResultHandler(attacker, defender, damage);
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = e.Message;
            }

            return(response);
        }
Exemple #12
0
        public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto request)
        {
            ServiceResponse <AttackResultDto> serviceResponse = new ServiceResponse <AttackResultDto>();

            try
            {
                Character attacker = await _dataContext.Characters
                                     .Include(c => c.Weapon)
                                     .FirstOrDefaultAsync(c => c.Id == request.AttackerId);

                Character opponent = await _dataContext.Characters
                                     .FirstOrDefaultAsync(c => c.Id == request.OpponentId);

                var damage = DoWeaponAttack(attacker, opponent);

                if (opponent.HitPoints < 0)
                {
                    serviceResponse.Message = string.Format("{0} has been defeated.", opponent.Name);
                }

                _dataContext.Characters.Update(opponent);
                await _dataContext.SaveChangesAsync();

                serviceResponse.Data = new AttackResultDto
                {
                    Attacker = attacker.Name, AttackerHp = attacker.HitPoints, Opponent = opponent.Name, OpponentHP = opponent.HitPoints, Damage = damage
                };
            }
            catch (Exception e)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = e.Message;
            }

            return(serviceResponse);
        }
Exemple #13
0
 public async Task <IActionResult> WeaponAttack(WeaponAttackDto request)
 {
     return(Ok(await _fightService.WeaponAttack(request)));
 }
Exemple #14
0
 public async Task <ActionResult <ServiceResponse <AttackResultDto> > > WeaponAttack(WeaponAttackDto request)
 {
     return(Ok(await _fightService.WeaponAttack(request)));
 }
Exemple #15
0
 public async Task <IActionResult> WeaponAttack(WeaponAttackDto weaponAttack) =>
 ActionResultHandler(await _fightService.WeaponAttack(weaponAttack));