public async Task <ServiceResponse <AttactResultDto> > SkillAtk(SkillAtkDto request) { try { var attacker = await _dBContext.Characters .Include(x => x.CharacterSkills).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 <AttactResultDto>(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 <AttactResultDto>(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 <AttactResultDto>(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} is dead."; } else { atkResultMessage = $"{opponent.Name} HP Remain {opponent.HitPoints}"; } _dBContext.Characters.Update(opponent); await _dBContext.SaveChangesAsync(); var dto = new AttactResultDto { AttackerName = attacker.Name, AttackHP = attacker.HitPoints, OpponentName = opponent.Name, OpponentHP = opponent.HitPoints, Damage = damage, AttackResultMessage = atkResultMessage }; _log.LogInformation(atkResultMessage); _log.LogInformation("Weapon attack done."); return(ResponseResult.Success(dto, atkResultMessage)); } catch (Exception ex) { _log.LogError(ex.Message); return(ResponseResult.Failure <AttactResultDto>(ex.Message)); } }
public async Task <IActionResult> SkillAtk(SkillAtkDto request) { return(Ok(await _charService.SkillAtk(request))); }