public async Task <ServiceResponse <AttactResultDto> > 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 <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)); } int damage; damage = attacker.Weapon.Damage + attacker.Strength; 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); _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> WeaponAtk(WeaponAtkDto request) { return(Ok(await _charService.WeaponAtk(request))); }