public async Task <FreeCompanySearchResult> Process(FreeCompanySearchCriteria criteria) { _validator.ValidateAndThrow(criteria); _logger.LogTrace("Processing Free Company search. Query: '{Query}'.", criteria.Query); var(name, server) = NameServerEngine.Parse(criteria.Query); var searchQuery = new SearchFreeCompanyQuery() { Name = name, Server = server }; _logger.LogDebug("Searching Free Company. Name: '{Name}'. Server: '{Server}'.", searchQuery.Name, searchQuery.Server); var searchData = await _xivApiAccessor.SearchFreeCompany(searchQuery); _logger.LogTrace("Search yielded {Count} results.", searchData.Pagination?.ResultsTotal); var result = new FreeCompanySearchResult() { FreeCompanies = searchData.Results?.Select(freeCompany => new Abstractions.Business.Models.FreeCompanySearch.FreeCompany() { Crest = freeCompany.Crest, Id = freeCompany.Id, Name = freeCompany.Name, Server = freeCompany.Server }) }; return(result); }
public void ShouldSplit(string input, string expectedName, string expectedServer) { var(name, server) = NameServerEngine.Parse(input); Assert.Equal(expectedName, name); Assert.Equal(expectedServer, server); }
public async Task <CharacterSearchResult> Process(CharacterSearchCriteria criteria) { _validator.ValidateAndThrow(criteria); _logger.LogTrace("Processing character search. Query: '{Query}'.", criteria.Query); var(name, server) = NameServerEngine.Parse(criteria.Query); var searchQuery = new SearchCharacterQuery() { Name = name, Server = server }; _logger.LogDebug("Searching character. Name: '{Name}'. Server: '{Server}'.", searchQuery.Name, searchQuery.Server); var searchData = await _xivApiAccessor.SearchCharacter(searchQuery); _logger.LogTrace("Search yielded {Count} results. Taking top five.", searchData.Pagination?.ResultsTotal); var topFiveCharacters = searchData.Results.Take(5); return(new CharacterSearchResult() { Characters = ProcessDetails(topFiveCharacters) }); }
public async Task <LinkCharacterResult> Process(LinkCharacterCriteria criteria) { _validator.ValidateAndThrow(criteria); (var name, _) = NameServerEngine.Parse(criteria.Query); var optionsResult = await _guildOptionsAccessor.GetOptions(new() { GuildId = criteria.GuildId }); var server = optionsResult?.FreeCompany?.Server; _logger.LogTrace("Searching for character with name {Name} and server {Server}", name, server); var searchData = await _xivApiAccessor.SearchCharacter(new() { Name = name, Server = server, }); var character = searchData.Results?.SingleOrDefault(); if (character is null) { return(new() { Success = false, FailureMessage = $"Could not find character! Query '{criteria.Query}'; Server '{server}'" }); } var characterId = character.Id; _logger.LogDebug("Got character for {Name}: Id {Id}", name, characterId); var dataUser = await _userAccessor.GetUser(criteria.UserId) ?? new() { Id = criteria.UserId }; var mergedUser = dataUser.Merge(characterId); await _userAccessor.SaveUser(mergedUser); return(new() { Success = true, CharacterId = characterId, }); } }
public async Task <VerifyCharacterResult> Process(VerifyCharacterCriteria criteria) { _verifyCharacterValidator.ValidateAndThrow(criteria); // Get the guild options for free company definition. _logger.LogTrace("Getting guild options for guild {Id}.", criteria.GuildId); var guildOptionsQuery = new GetOptionsQuery() { GuildId = criteria.GuildId }; var guildOptions = await _guildAccessor.GetOptions(guildOptionsQuery); if (guildOptions?.FreeCompany is null || guildOptions.VerifiedRoleId == 0) { _logger.LogDebug("Free Company options not defined for guild {Id}.", criteria.GuildId); return(new VerifyCharacterResult() { Status = Status.FreeCompanyUndefined }); } var result = new VerifyCharacterResult() { FreeCompanyName = guildOptions.FreeCompany.Name, VerifiedRoleId = guildOptions.VerifiedRoleId }; // Parse the query into name/server. _logger.LogTrace("Parsing query: {Query}.", criteria.Query); var(name, _) = NameServerEngine.Parse(criteria.Query); // Search for the character. _logger.LogTrace("Searching for {CharacterName} on {ServerName}.", name, guildOptions.FreeCompany.Server); var searchQuery = new SearchCharacterQuery() { Name = name, Server = guildOptions.FreeCompany.Server }; var searchData = await _xivApiAccessor.SearchCharacter(searchQuery); var characterId = searchData.Results?.FirstOrDefault()?.Id; _logger.LogDebug("Got character Id {Id}.", characterId); if (characterId is null) { result.Status = Status.NotVerified; result.Name = searchData.Results?.FirstOrDefault()?.Name; return(result); } // Check if character is already attached to a user. _logger.LogTrace("Checking database if {CharacterName} has already been tied to a user. CharacterId: {CharacterId}", name, characterId); var checkQuery = new SearchUserQuery() { CharacterId = characterId.Value }; var user = await _userAccessor.SearchUser(checkQuery); if (user is object) { _logger.LogDebug("{CharacterName} ({CharacterId}) has already been tied to UserId {UserId}.", name, characterId, user.Id); result.Status = Status.CharacterAlreadyVerified; result.Name = searchData.Results?.FirstOrDefault()?.Name; result.VerifiedUserId = user.Id; return(result); } // Get the character. _logger.LogTrace("Getting character with Id {Id}.", characterId); var getQuery = new GetCharacterQuery() { Id = characterId.Value }; var getData = await _xivApiAccessor.GetCharacter(getQuery); var characterFcId = getData?.Character?.FreeCompanyId; _logger.LogDebug("Got character Free Company Id {FcId}.", characterFcId); result.Name = getData?.Character?.Name; if (characterFcId != guildOptions.FreeCompany.Id) { result.Status = Status.NotVerified; _logger.LogDebug("{Name} failed verification. Character FC: {CFcId}. Guild FC: {FcId}", result.Name, characterFcId, guildOptions.FreeCompany.Id); return(result); } result.Status = Status.Verified; result.VerifiedUserId = criteria.UserId; _logger.LogDebug("{Name} has been verified with Free Company Id {FcId}.", result.Name, guildOptions.FreeCompany.Id); // Save character-user map to database. _logger.LogDebug("Saving {Name} to database with User Id {UserId}.", result.Name, criteria.UserId); var dataUser = await _userAccessor.GetUser(criteria.UserId) ?? new() { Id = criteria.UserId }; var mergedUser = dataUser.Merge(characterId.Value); mergedUser.Name = criteria.Name; mergedUser.Nicknames[criteria.GuildId] = result.Name ?? ""; await _userAccessor.SaveUser(mergedUser); return(result); } }
public async Task <SetVerificationResult> SetVerification(SetVerificationCriteria criteria) { _setVerificationValidator.ValidateAndThrow(criteria); _logger.LogDebug("Setting verification options for guild '{GuildId}' with role '{RoleId}' and free company '{Query}'.", criteria.GuildId, criteria.RoleId, criteria.FreeCompanyAndServer); var(name, server) = NameServerEngine.Parse(criteria.FreeCompanyAndServer); var fcSearchQuery = new SearchFreeCompanyQuery() { Name = name, Server = server }; _logger.LogDebug("Searching for free company '{FreeCompanyName}' on server '{ServerName}'", name, server); var fcSearchData = await _xivApiAccessor.SearchFreeCompany(fcSearchQuery); // Find single, exact match. var fc = fcSearchData.Results?.SingleOrDefault(x => (x.Name?.Equals(name, StringComparison.OrdinalIgnoreCase) ?? false) && (x.Server?.Equals(server, StringComparison.OrdinalIgnoreCase) ?? false)); if (fc is null) { _logger.LogDebug("Could not find single exact match of '{FreeCompanyName}' on server '{ServerName}'", name, server); return(new SetVerificationResult() { Status = SetVerificationStatus.FreeCompanyNotFound }); } _logger.LogDebug("Found free company '{FreeCompanyName}' with Id '{FreeCompanyId}'. Saving verification options to database.", fc.Name, fc.Id); var getOptionsQuery = new GetOptionsQuery() { GuildId = criteria.GuildId }; var options = await _guildAccessor.GetOptions(getOptionsQuery) ?? new GuildOptions(); options.Id = criteria.GuildId; options.VerifiedRoleId = criteria.RoleId; options.FreeCompany = new Abstractions.Data.Storage.Models.Guild.FreeCompany() { Id = fc.Id, Name = fc.Name, Server = fc.Server }; var saveOptionsQuery = new SaveOptionsQuery() { Options = options }; await _guildAccessor.SaveOptions(saveOptionsQuery); return(new SetVerificationResult() { Status = SetVerificationStatus.Success }); }