private static int DoSkillAttack(Charecter attacker, Charecter opponent, CharecterSkill charecterSkill) { int damage = Convert.ToInt32(charecterSkill.Skill.Damage) + (new Random().Next(attacker.Intelligence)); damage -= new Random().Next(opponent.Defence); if (damage > 0) { opponent.HitPoints -= damage; } return(damage); }
public async Task <ServiceResponse <AttackResultDto> > SkillAttack(SkillAttackDto request) { ServiceResponse <AttackResultDto> response = new ServiceResponse <AttackResultDto>(); try{ Charecter attacker = await _context.charecters.Include(chars => chars.CharecterSkills).ThenInclude(cs => cs.Skills) .FirstOrDefaultAsync(c => c.Id == request.AttackerId); Charecter opponent = await _context.charecters.FirstOrDefaultAsync(c => c.Id == request.OpponentId); CharecterSkill charecterSkill = attacker.CharecterSkills.FirstOrDefault(cs => cs.Skills.Id == request.SkillId); if (charecterSkill == null) { response.Message = $"{attacker.Name} does not known Skil"; response.Success = false; return(response); } int damage = int.Parse(charecterSkill.Skills.Damage) + (new Random().Next(attacker.Intelligence)); damage -= new Random().Next(opponent.Defence); if (damage > 0) { opponent.HitPoints -= damage; } if (opponent.HitPoints <= 0) { response.Message = $"{opponent.Name} has been defeated !"; } _context.charecters.Update(opponent); await _context.SaveChangesAsync(); response.data = new AttackResultDto { Attacker = attacker.Name, AttackerHP = attacker.HitPoints.ToString(), Opponent = opponent.Name, OpponentHP = opponent.HitPoints.ToString(), Damage = damage }; } catch (Exception ex) { response.Message = ex.ToString(); response.Success = false; } return(response); }
public async Task <ServiceResponse <GetCharecterDto> > AddCharecterSkill(AddCharecterSkillDto addCharecterSkill) { ServiceResponse <GetCharecterDto> response = new ServiceResponse <GetCharecterDto>(); try { Charecter chars = await _context.charecters .Include(c => c.Weapons) .Include(c => c.CharecterSkills) .ThenInclude(we => we.Skills) .FirstOrDefaultAsync(c => c.Id == addCharecterSkill.CharecterId && c.Users.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); if (chars == null) { response.Success = false; response.Message = "No Charecter Record found"; return(response); } Skill skill = await _context.Skills.SingleOrDefaultAsync(s => s.Id == addCharecterSkill.SkillId); if (skill == null) { response.Success = false; response.Message = "No skill Record found"; return(response); } CharecterSkill charecter = new CharecterSkill { charecters = chars, Skills = skill }; await _context.CharecterSkills.AddAsync(charecter); await _context.SaveChangesAsync(); response.data = _mapper.Map <GetCharecterDto>(chars); } catch (Exception e) { response.Success = false; response.Message = e.Message; } return(response); }
public async Task <ServiceResponce <AttackResultDTO> > SkillAttack(SKillAttackDTO request) { ServiceResponce <AttackResultDTO> responce = new ServiceResponce <AttackResultDTO>(); try { Charecter attacker = await _context.Charecters .Include(c => c.CharecterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == request.AttackerId); Charecter opponent = await _context.Charecters.FirstOrDefaultAsync(c => c.Id == request.OpponentId); CharecterSkill charecterSkill = attacker.CharecterSkills.FirstOrDefault(cs => cs.Skill.Id == request.SkillId); if (charecterSkill == null) { responce.Success = false; responce.Message = $"{attacker.Name} doesnt know that skill!"; return(responce); } int damage = DoSkillAttack(attacker, opponent, charecterSkill); if (opponent.HitPoints <= 0) { responce.Message = $"{opponent.Name} has been defeated!"; } _context.Charecters.Update(opponent); await _context.SaveChangesAsync(); responce.Data = new AttackResultDTO { Attacker = attacker.Name, AttackerHp = attacker.HitPoints, Opponent = opponent.Name, OpponentHp = opponent.HitPoints, Damage = damage }; } catch (Exception ex) { responce.Success = false; responce.Message = ex.Message; } return(responce); }
public async Task <ServiceResponce <GetCharecterDTO> > AddCharecterSkill(AddCharecterSkillDTO newCharecterSkill) { ServiceResponce <GetCharecterDTO> responce = new ServiceResponce <GetCharecterDTO>(); try{ Charecter charecter = await _context.Charecters .Include(c => c.Weapon) .Include(c => c.CharecterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharecterSkill.CharecterId && c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); if (charecter == null) { responce.Success = false; responce.Message = "Charecter not found."; return(responce); } Skill skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newCharecterSkill.SkillId); if (skill == null) { responce.Success = false; responce.Message = "Skill not found."; return(responce); } CharecterSkill characterSkill = new CharecterSkill() { Charecter = charecter, Skill = skill }; await _context.CharecterSkills.AddAsync(characterSkill); await _context.SaveChangesAsync(); responce.Data = _mapper.Map <GetCharecterDTO>(charecter); } catch (Exception ex) { responce.Success = false; responce.Message = ex.Message; } return(responce); }