Ejemplo n.º 1
0
    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)
        });
    }
Ejemplo n.º 2
0
    public async Task <SearchCharacterData> SearchCharacter(SearchCharacterQuery query)
    {
        var name   = WebUtility.UrlEncode(query.Name);
        var server = query.Server is object?WebUtility.UrlEncode(query.Server) : null;

        var uri = $"/character/search?private_key={_privateKey}&name={name}";

        if (!string.IsNullOrEmpty(server))
        {
            uri += $"&server={server}";
        }

        var data = await Send <SearchCharacterData>(uri);

        return(data);
    }
Ejemplo n.º 3
0
    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);
    }
}