public async Task <ActionResult <CharacterLevelUpResponse> > PostCharacterLevelUpAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int characterId, CharacterLevelUpRequest request ) { try { var levelUpResult = await _characterService.LevelUpCharacterAsync(executionContext, characterId, request); return(_mapper.Map <CharacterLevelUpResponse>(levelUpResult)); } catch (SpecialityNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } catch (InvalidTargetLevelUpRequestedException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } }
public async Task <LevelUpResult> LevelUpCharacterAsync(NaheulbookExecutionContext executionContext, int characterId, CharacterLevelUpRequest request) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var character = await uow.Characters.GetWithGroupAsync(characterId); if (character == null) { throw new CharacterNotFoundException(characterId); } _authorizationUtil.EnsureCharacterAccess(executionContext, character); if (character.Level + 1 != request.TargetLevelUp) { throw new InvalidTargetLevelUpRequestedException(character.Level, request.TargetLevelUp); } var specialities = await uow.CharacterSpecialities.GetWithModiferWithSpecialByIdsAsync(request.SpecialityIds); if (specialities.Count < request.SpecialityIds.Distinct().Count()) { throw new SpecialityNotFoundException(); } var origin = await uow.Origins.GetWithAllDataAsync(character.OriginId); var levelUpResult = _characterUtil.LevelUpCharacter(character, origin, specialities, request); uow.CharacterModifiers.AddRange(levelUpResult.NewModifiers); uow.CharacterSkills.AddRange(levelUpResult.NewSkills); uow.CharacterSpecialities.AddRange(levelUpResult.NewSpecialities); character.AddHistoryEntry(_characterHistoryUtil.CreateLogLevelUp(character.Id, character.Level)); var notificationSession = _notificationSessionFactory.CreateSession(); notificationSession.NotifyCharacterLevelUp(character.Id, levelUpResult); await uow.SaveChangesAsync(); await notificationSession.CommitAsync(); return(levelUpResult); } }
private static CharacterModifier CreateLevelUpCharacterModifier(Character character, CharacterLevelUpRequest request) { var levelUpCharacterModifier = new CharacterModifier { IsActive = true, Name = "LevelUp: " + request.TargetLevelUp, Values = new List <CharacterModifierValue>(), Permanent = true, DurationType = "forever", CharacterId = character.Id }; levelUpCharacterModifier.Values.Add(new CharacterModifierValue { StatName = request.EvOrEa, Value = request.EvOrEaValue, Type = "ADD" }); levelUpCharacterModifier.Values.Add(new CharacterModifierValue { StatName = request.StatToUp, Value = 1, Type = "ADD" }); return(levelUpCharacterModifier); }
public LevelUpResult LevelUpCharacter(Character character, Origin origin, List <Speciality> specialities, CharacterLevelUpRequest request) { var levelUpResult = new LevelUpResult(); character.Level++; levelUpResult.NewLevel = character.Level; levelUpResult.NewModifiers.Add(CreateLevelUpCharacterModifier(character, request)); if (request.SkillId.HasValue) { levelUpResult.NewSkills.Add(CreateCharacterSkill(character, request.SkillId.Value)); } foreach (var speciality in specialities) { levelUpResult.NewSpecialities.Add(CreateCharacterSpeciality(character, speciality)); } if ((character.Level == 2 || character.Level == 3) && _originUtil.HasFlag(origin, "CHA_+1_LVL2_LVL3")) { levelUpResult.NewModifiers.Add(CreateChaLevelUpCharacterModifier(character)); } return(levelUpResult); }