/// <inheritdoc /> public SectionId Compute(string inputName, CharacterClassRace classRace) { if (string.IsNullOrEmpty(inputName)) { throw new ArgumentException("Value cannot be null or empty.", nameof(inputName)); } if (!Enum.IsDefined(typeof(CharacterClassRace), classRace)) { throw new ArgumentOutOfRangeException(nameof(classRace), "Value should be defined in the CharacterClassRace enum."); } if (!CharacterNameValidator.isNameValidCharacters(inputName)) { throw new ArgumentException("Provided name must contain only letters or digits", nameof(inputName)); } //Sum the ASCII values modulo 10 + the class offset //Then grab the last digit which maps to the SectionId return((SectionId)(inputName.Aggregate(classRace.GetSectionIdOffset(), (sum, c) => sum + c % 10) % 10)); }
public CharacterDatabaseModel(int accountId, string characterName, string creationIp) { if (string.IsNullOrWhiteSpace(characterName)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(characterName)); } if (!CharacterNameValidator.isNameValidCharacters(characterName)) { throw new ArgumentException($"Provided Name: {characterName} must contain only letters or digits.", nameof(characterName)); } if (string.IsNullOrWhiteSpace(creationIp)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(creationIp)); } //TODO: Should we validate it's an IpAddress? AccountId = accountId; CharacterName = characterName; CreationIp = creationIp; }
public async Task <JsonResult> CheckCharacterNameAvailability([FromBody] CharacterNameValidationRequest request, [FromServices] IReadonlyCharacterRepository characterRepository) { if (!ModelState.IsValid) { return(GenerateValidationResponse(CharacterNameValidationResponseCode.GeneralServerError)); } if (String.IsNullOrWhiteSpace(request.CharacterName)) { return(GenerateValidationResponse(CharacterNameValidationResponseCode.NameContainsInvalidCharacters)); } if (!CharacterNameValidator.isNameValidLength(request.CharacterName)) { return(GenerateValidationResponse(CharacterNameValidationResponseCode.NameIsInvalidLength)); } if (!CharacterNameValidator.isNameValidCharacters(request.CharacterName)) { return(GenerateValidationResponse(CharacterNameValidationResponseCode.NameContainsInvalidCharacters)); } bool isNameTaken; //We wrap this in a try because something could be broken db/network wise. We just want to tell the user that something broke instead of //breaking their client with malformed responses too try { //At this point the name looks valid but we still don't know if we have another character with that name. isNameTaken = await characterRepository.DoesNameExist(request.CharacterName); } catch (Exception e) { //TODO: Logging return(GenerateValidationResponse(CharacterNameValidationResponseCode.GeneralServerError)); } return(!isNameTaken?GenerateValidationResponse(CharacterNameValidationResponseCode.Success) : GenerateValidationResponse(CharacterNameValidationResponseCode.NameAlreadyTaken)); }