public async Task <ServiceResponce <List <GetCharacterDto> > > AddCharacter(AddCharacterDto newCharacter) { ServiceResponce <List <GetCharacterDto> > serviceResponce = new ServiceResponce <List <GetCharacterDto> >(); Character character = _mapper.Map <Character>(newCharacter); //character.Id = characters.Max(i => i.Id) + 1; not needed since database is in SQL Server character.User = await _context.Users.FirstOrDefaultAsync(u => u.Id == GetUserId()); await _context.Characters.AddAsync(character); await _context.SaveChangesAsync(); serviceResponce.Data = _context.Characters.Where(c => c.User.Id == GetUserId()).Select(c => _mapper.Map <GetCharacterDto>(c)).ToList(); return(serviceResponce); }
public async Task <ServiceResponce <AttackResultDto> > WeaponAttack(WeaponAttackDto request) { ServiceResponce <AttackResultDto> responce = new ServiceResponce <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) { responce.Message = $"{opponent.Name} is defeated"; } _context.Characters.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 IHttpContextAccessor Accessor { get; } public async Task <ServiceResponce <GetCharacterDto> > AddCharacterSkill(AddCharacterSkillDto newCharacterSkillDto) { ServiceResponce <GetCharacterDto> responce = new ServiceResponce <GetCharacterDto>(); try { Character character = await _context.Characters.Include(c => c.Weapon) .Include(c => c.Class) .Include(c => c.CharacterSkills).ThenInclude(cs => cs.Skill) .FirstOrDefaultAsync(c => c.Id == newCharacterSkillDto.CharacterId && c.User.Id == int.Parse(_accessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); if (character == null) { responce.Success = false; responce.Message = "Character not found."; return(responce); } Skill skill = await _context.Skills .FirstOrDefaultAsync(s => s.Id == newCharacterSkillDto.SkillId); if (skill == null) { responce.Success = false; responce.Message = "Skill not found."; return(responce); } CharacterSkill characterSkill = new CharacterSkill { Character = character, Skill = skill, }; await _context.ChararacterSkills.AddAsync(characterSkill); await _context.SaveChangesAsync(); responce.Data = _mapper.Map <GetCharacterDto>(character); } catch (Exception ex) { responce.Success = false; responce.Message = ex.Message; } return(responce); }
public async Task <ServiceResponce <int> > Register(User user, string password) { ServiceResponce <int> responce = new ServiceResponce <int>(); if (await UserExist(user.Username)) { responce.Success = false; responce.Message = "User already exists"; return(responce); } Utility.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; await _context.Users.AddAsync(user); await _context.SaveChangesAsync(); responce.Data = user.Id; return(responce); }
public async Task <ServiceResponce <GetCharacterDto> > AddWeapon(AddWeaponDto newWeapon) { ServiceResponce <GetCharacterDto> responce = new ServiceResponce <GetCharacterDto>(); try { Character character = await _context.Characters.Include(c => c.Weapon) .Include(c => c.Class) .FirstOrDefaultAsync(c => c.Id == newWeapon.CharacterId && c.User.Id == int.Parse(_accessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); if (character == null) { responce.Success = false; responce.Message = "Character not found."; return(responce); } Weapon weapon = new Weapon { WeaponName = newWeapon.WeaponName, Damage = newWeapon.WeaponDamage, CharacterId = newWeapon.CharacterId }; await _context.Weapons.AddAsync(weapon); await _context.SaveChangesAsync(); responce.Data = _mapper.Map <GetCharacterDto>(character); } catch (Exception ex) { responce.Success = false; responce.Message = ex.Message; } return(responce); }