Skip to content

Commit

Permalink
add linkshell search
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenari committed Dec 23, 2024
1 parent b839cd6 commit 165e93c
Show file tree
Hide file tree
Showing 13 changed files with 734 additions and 179 deletions.
68 changes: 66 additions & 2 deletions NetStone.Test/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using NetStone.GameData.Packs;
using NetStone.Model.Parseables.Character;
using NetStone.Search.Character;
using NetStone.Search.CWLS;
using NetStone.Search.FreeCompany;
using NetStone.Search.Linkshell;
using NetStone.StaticData;
using NUnit.Framework;
using SortKind = NetStone.Search.Character.SortKind;
Expand Down Expand Up @@ -599,9 +599,10 @@ public async Task CheckCrossworldLinkShellSearch()
var query = new CrossWorldLinkShellSearchQuery()
{
Name = "Hell",
ActiveMembers = CrossWorldLinkShellSearchQuery.ActiveMemberChoice.ElevenToThirty,
ActiveMembers = LinkshellSizeCategory.ElevenToThirty,
DataCenter = "Chaos",
};
bool first = true;
var results = await this.lodestone.SearchCrossWorldLinkshell(query);
Assert.IsNotNull(results);
Assert.True(results.HasResults);
Expand All @@ -610,6 +611,13 @@ public async Task CheckCrossworldLinkShellSearch()
{
foreach (var result in results.Results)
{
if (first)
{
first = false;
var shell = await result.GetCrossWorldLinkShell();
Assert.IsNotNull(shell);
Assert.AreEqual(result.Name, shell.Name);
}
Console.WriteLine($"{result.Name} ({result.Id}): {result.ActiveMembers}\n");
}
results = await results.GetNextPage();
Expand Down Expand Up @@ -639,4 +647,60 @@ public async Task CheckLinkshell()
}

}

[Test]
public async Task CheckLinkShellSearch()
{
var emptyQuery = new LinkShellSearchQuery()
{
Name = "abcedfas",
};
var emptyResult = await this.lodestone.SearchLinkshell(emptyQuery);
Assert.IsNotNull(emptyResult);
Assert.False(emptyResult.HasResults);
var query = new LinkShellSearchQuery()
{
Name = "Hell",
ActiveMembers = LinkshellSizeCategory.ElevenToThirty,
DataCenter = "Chaos",
};
bool first = true;
var results = await this.lodestone.SearchLinkshell(query);
Assert.IsNotNull(results);
Assert.True(results.HasResults);
Assert.AreEqual(2, results.NumPages);
while (results is not null)
{
foreach (var result in results.Results)
{
if (first)
{
first = false;
var shell = await result.GetLinkshell();
Assert.IsNotNull(shell);
Assert.AreEqual(result.Name, shell.Name);
}
Console.WriteLine($"{result.Name} ({result.Id}): {result.ActiveMembers}\n");
}
results = await results.GetNextPage();
}
query = new LinkShellSearchQuery()
{
Name = "Hell",
ActiveMembers = LinkshellSizeCategory.ElevenToThirty,
HomeWorld = "Spriggan",
};
results = await this.lodestone.SearchLinkshell(query);
Assert.IsNotNull(results);
Assert.True(results.HasResults);
Assert.AreEqual(1, results.NumPages);
while (results is not null)
{
foreach (var result in results.Results)
{
Console.WriteLine($"{result.Name} ({result.Id}): {result.ActiveMembers}\n");
}
results = await results.GetNextPage();
}
}
}
5 changes: 5 additions & 0 deletions NetStone/Definitions/DefinitionsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public abstract class DefinitionsContainer : IDisposable
/// Definitions for link shell members
/// </summary>
public LinkShellMemberDefinition LinkShellMember { get; protected set; }

/// <summary>
/// Definitions for link-shell searches
/// </summary>
public PagedDefinition<LinkShellSearchEntryDefinition> LinkShellSearch { get; protected set; }

#endregion

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Newtonsoft.Json;

namespace NetStone.Definitions.Model.Linkshell;

/// <summary>
/// Definition container for one Link-Shell search result entry
/// </summary>
public class LinkShellSearchEntryDefinition : PagedEntryDefinition
{
/// <summary>
/// ID
/// </summary>
[JsonProperty("ID")] public DefinitionsPack Id { get; set; }

/// <summary>
/// Name
/// </summary>
[JsonProperty("NAME")] public DefinitionsPack Name { get; set; }

/// <summary>
/// Rank
/// </summary>
[JsonProperty("SERVER")] public DefinitionsPack Server { get; set; }

/// <summary>
/// Rank Icon
/// </summary>
[JsonProperty("ACTIVE_MEMBERS")] public DefinitionsPack ActiveMembers { get; set; }
}
1 change: 1 addition & 0 deletions NetStone/Definitions/XivApiDefinitionsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public override async Task Reload()

this.LinkShell = await GetDefinition<LinkShellDefinition>("linkshell/ls.json");
this.LinkShellMember = await GetDefinition<LinkShellMemberDefinition>("linkshell/members.json");
this.LinkShellSearch = await GetDefinition<PagedDefinition<LinkShellSearchEntryDefinition>>("search/linkshell.json");
}

private async Task<T> GetDefinition<T>(string path) where T : IDefinition
Expand Down
13 changes: 12 additions & 1 deletion NetStone/LodestoneClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
using NetStone.Model.Parseables.Search.Character;
using NetStone.Model.Parseables.Search.CWLS;
using NetStone.Model.Parseables.Search.FreeCompany;
using NetStone.Model.Parseables.Search.Linkshell;
using NetStone.Search.Character;
using NetStone.Search.CWLS;
using NetStone.Search.FreeCompany;
using NetStone.Search.Linkshell;

namespace NetStone;

Expand Down Expand Up @@ -184,6 +185,16 @@ await GetParsed($"/lodestone/crossworld_linkshell/{query.BuildQueryString()}&pag
await GetParsed($"/lodestone/linkshell/{id}?page={page}",
node => new LodestoneLinkShell(this, node, this.Definitions,id));

/// <summary>
/// Search lodestone for a linkshell with the specified query.
/// </summary>
/// <param name="query"><see cref="LinkShellSearchQuery"/> object detailing search parameters</param>
/// <param name="page">The page of search results to fetch.</param>
/// <returns><see cref="LinkShellSearchPage"/> containing search results.</returns>
public async Task<LinkShellSearchPage?> SearchLinkshell(LinkShellSearchQuery query, int page = 1) =>
await GetParsed($"/lodestone/linkshell/{query.BuildQueryString()}&page={page}",
node => new LinkShellSearchPage(this, node, this.Definitions.LinkShellSearch, query));

#endregion

#region FreeCompany
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Threading.Tasks;
using HtmlAgilityPack;
using NetStone.Definitions.Model.CWLS;
using NetStone.Model.Parseables.Character;
using NetStone.Model.Parseables.CWLS;

namespace NetStone.Model.Parseables.Search.CWLS;

/// <summary>
/// Models one entry in the cwls search results list
/// </summary>
public class CrossWorldLinkShellSearchEntry : LodestoneParseable
{
private readonly LodestoneClient client;
Expand All @@ -27,15 +30,24 @@ public CrossWorldLinkShellSearchEntry(LodestoneClient client, HtmlNode rootNode,
/// Lodestone Id
/// </summary>
public string? Id => ParseHrefId(this.definition.Id);

/// <summary>
/// Datacenter
/// </summary>
public string DataCenter => Parse(this.definition.Dc);


/// <summary>
/// Number of active members
/// </summary>
public int ActiveMembers => int.TryParse(Parse(this.definition.ActiveMembers), out var parsed) ? parsed : -1;

/// <summary>
/// Fetch character profile
/// Fetch cross world link shell
/// </summary>
/// <returns>Task of retrieving character</returns>
public async Task<LodestoneCharacter?> GetCharacter() =>
this.Id is null ? null : await this.client.GetCharacter(this.Id);
/// <returns>Task of retrieving cwls</returns>
public async Task<LodestoneCrossWorldLinkShell?> GetCrossWorldLinkShell() =>
this.Id is null ? null : await this.client.GetCrossworldLinkshell(this.Id);

///<inheritdoc />
public override string ToString() => this.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using NetStone.Definitions;
using NetStone.Definitions.Model;
using NetStone.Definitions.Model.CWLS;
using NetStone.Search.CWLS;
using NetStone.Search.Linkshell;

namespace NetStone.Model.Parseables.Search.CWLS;

Expand Down
53 changes: 53 additions & 0 deletions NetStone/Model/Parseables/Search/Linkshell/LinkShellSearchEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Threading.Tasks;
using HtmlAgilityPack;
using NetStone.Definitions.Model.Linkshell;
using NetStone.Model.Parseables.Linkshell;

namespace NetStone.Model.Parseables.Search.Linkshell;

/// <summary>
/// Models one entry in the linkshell search results list
/// </summary>
public class LinkShellSearchEntry : LodestoneParseable
{
private readonly LodestoneClient client;
private readonly LinkShellSearchEntryDefinition definition;

///
public LinkShellSearchEntry(LodestoneClient client, HtmlNode rootNode, LinkShellSearchEntryDefinition definition) :
base(rootNode)
{
this.client = client;
this.definition = definition;
}

/// <summary>
/// Character name
/// </summary>
public string Name => Parse(this.definition.Name);

/// <summary>
/// Lodestone Id
/// </summary>
public string? Id => ParseHrefId(this.definition.Id);

/// <summary>
/// Homeworld / Server
/// </summary>
public string HomeWorld => Parse(this.definition.Server);

/// <summary>
/// Number of active members
/// </summary>
public int ActiveMembers => int.TryParse(Parse(this.definition.ActiveMembers), out var parsed) ? parsed : -1;

/// <summary>
/// Fetch character profile
/// </summary>
/// <returns>Task of retrieving character</returns>
public async Task<LodestoneLinkShell?> GetLinkshell() =>
this.Id is null ? null : await this.client.GetLinkshell(this.Id);

///<inheritdoc />
public override string ToString() => this.Name;
}
Loading

0 comments on commit 165e93c

Please sign in to comment.