public async Task <ServiceResponse <AttackResultDto> > SkillAttack(SkillAttackDto skillAttackDto) { ServiceResponse <AttackResultDto> res = new ServiceResponse <AttackResultDto>(); try { Character attacker = _context.Characters .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefault(c => c.Id == skillAttackDto.AttackerId); Character opponent = _context.Characters .FirstOrDefault(c => c.Id == skillAttackDto.OpponentId); CharacterSkill characterSkill = attacker.CharacterSkills .FirstOrDefault(cs => cs.SkillId == skillAttackDto.SkillId); if (characterSkill == null) { res.Success = false; res.Message = $"{attacker.Name} doesn't have skill"; return(res); } int damage = DoSkillAttack(attacker, opponent, characterSkill); if (opponent.HitPoints <= 0) { res.Message = $"{opponent.Name} has been defeated"; } _context.Characters.Update(opponent); await _context.SaveChangesAsync(); AttackResultDto r = new AttackResultDto { Attacker = attacker.Name, AttackerHP = attacker.HitPoints, Opponent = opponent.Name, OpponentHP = opponent.HitPoints, Damage = damage }; res.Data = r; } catch (Exception ex) { res.Success = false; res.Message = ex.Message; } return(res); }
/* Public methods */ 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); if (attacker == null) { response.Success = false; response.Message = $"Attacker with id {request.AttackerId} not found!"; return(response); } Character opponent = await _context.Characters .FirstOrDefaultAsync(c => c.Id == request.OpponentId); if (opponent == null) { response.Success = false; response.Message = $"Opponent with id {request.OpponentId} not found!"; return(response); } int damage = _DoWeaponAttack(attacker, opponent); if (opponent.HitPoints <= 0) { response.Message = $"{opponent.Name} has been defeated!"; } _context.Characters.Update(opponent); await _context.SaveChangesAsync(); var attackResultDto = new AttackResultDto { Attacker = attacker.Name, AttackerHP = attacker.HitPoints, Opponent = attacker.Name, OpponentHP = opponent.HitPoints, Damage = damage }; response.Data = attackResultDto; } catch (System.Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <ServiceResponse <AttackResultDto> > WeaponAttack(WeaponAttackDto weaponAttackDto) { ServiceResponse <AttackResultDto> res = new ServiceResponse <AttackResultDto>(); try { Character attacker = _context.Characters .Include(c => c.Weapon) .FirstOrDefault(c => c.Id == weaponAttackDto.AttackerId); Character opponent = _context.Characters .FirstOrDefault(c => c.Id == weaponAttackDto.OpponentId); int damage = DoWeaponAttack(attacker, opponent); if (opponent.HitPoints <= 0) { res.Message = $"{opponent.Name} has been defeated"; } _context.Characters.Update(opponent); await _context.SaveChangesAsync(); AttackResultDto r = new AttackResultDto { Attacker = attacker.Name, AttackerHP = attacker.HitPoints, Opponent = opponent.Name, OpponentHP = opponent.HitPoints, Damage = damage }; res.Data = r; } catch (Exception ex) { res.Success = false; res.Message = ex.Message; } return(res); }
public async Task <ServiceResponse <AttackResultDto> > SkillAtk(SkillAtkDto request) { try { _log.LogInformation("Start process."); var attacker = await _dBContext.Characters .Include(x => x.Weapon) .Include(x => x.CharacterSkill).ThenInclude(x => x.Skill) .FirstOrDefaultAsync(x => x.Id == request.AttackerId); if (attacker is null) { var msg = $"This attackerId {request.AttackerId} not found."; _log.LogError(msg); return(ResponseResult.Failure <AttackResultDto>(msg)); } var opponent = await _dBContext.Characters .Include(x => x.Weapon) .FirstOrDefaultAsync(x => x.Id == request.OpponentId); if (opponent is null) { var msg = $"This attackerId {request.OpponentId} not found."; _log.LogError(msg); return(ResponseResult.Failure <AttackResultDto>(msg)); } var charSkill = await _dBContext.CharacterSkills .Include(x => x.Skill) .FirstOrDefaultAsync(x => x.CharacterId == request.AttackerId && x.SkillId == request.SkillId); if (charSkill is null) { var msg = $"This Attacker doesn't know this skill {request.OpponentId}."; _log.LogError(msg); return(ResponseResult.Failure <AttackResultDto>(msg)); } int damage; damage = charSkill.Skill.Damage + attacker.Intelligence; damage -= opponent.Defense; if (damage > 0) { opponent.HitPoints -= damage; } string atkResultMessage; if (opponent.HitPoints <= 0) { atkResultMessage = $"{opponent.Name} id dead."; } else { atkResultMessage = $"{opponent.Name} HP Remain {opponent.HitPoints}"; } _dBContext.Characters.Update(opponent); await _dBContext.SaveChangesAsync(); var dto = new AttackResultDto { AttackName = attacker.Name, AttackHP = attacker.HitPoints, OpponentName = opponent.Name, OpponentHP = opponent.HitPoints, Damage = damage, AttackResultMessage = atkResultMessage }; _log.LogInformation("End process."); return(ResponseResult.Success(dto)); } catch (System.Exception ex) { _log.LogError(ex.Message); return(ResponseResult.Failure <AttackResultDto>(ex.Message)); } }
public async Task <ServiceResponse <AttackResultDto> > WeaponAtk(WeaponAtkDto request) { try { var attacker = await _dbContext.Characters .Include(x => x.Weapon) .FirstOrDefaultAsync(x => x.Id == request.AttackerId); if (attacker is null) { var msg = $"This attackerId {request.AttackerId} not found."; _log.LogError(msg); return(ResponseResult.Failure <AttackResultDto>(msg)); } var opponent = await _dbContext.Characters .Include(x => x.Weapon) .FirstOrDefaultAsync(x => x.Id == request.OpponentId); if (opponent is null) { var msg = $"This opponentId {request.OpponentId} not found."; _log.LogError(msg); return(ResponseResult.Failure <AttackResultDto>(msg)); } int damage; damage = attacker.Weapon.Damage + attacker.Strength; damage -= opponent.Defense; if (damage > 0) { opponent.HisPoints -= damage; } string atkResultMessage; if (opponent.HisPoints <= 0) { atkResultMessage = $"{opponent.Name} is dead."; } else { atkResultMessage = $"{opponent.Name} HP Remain {opponent.HisPoints}"; } _dbContext.Characters.Update(opponent); await _dbContext.SaveChangesAsync(); var dto = new AttackResultDto { AttackerName = attacker.Name, AttackHP = attacker.HisPoints, OpponentName = opponent.Name, OpponentHP = opponent.HisPoints, Damage = damage, AttackResultMessage = atkResultMessage }; _log.LogInformation("Weapon attack done."); return(ResponseResult.Success(dto)); } catch (Exception ex) { _log.LogError(ex.Message); return(ResponseResult.Failure <AttackResultDto>(ex.Message)); } }