Skip to content

Commit

Permalink
fix bis fetch
Browse files Browse the repository at this point in the history
Signed-off-by: Mira <koenari@koenari.de>
  • Loading branch information
Koenari committed Jan 4, 2025
1 parent 59fedaf commit 48f591b
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 142 deletions.
28 changes: 26 additions & 2 deletions HimbeertoniRaidTool/Connectors/BaseConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,38 @@ public interface IReadOnlyGearConnector
public bool BelongsToThisService(string url);
public string GetId(string url);
public string GetWebUrl(string id);
public IList<string> GetNames(string id);
public IReadOnlyDictionary<string, string> GetBiSList(Job job);
public IList<ExternalBiSDefinition> GetPossibilities(string id);
public IList<ExternalBiSDefinition> GetBiSList(Job job);
internal HrtUiMessage UpdateAllSets(bool updateAll, int maxAgeInDays);
public void RequestGearSetUpdate(GearSet set, Action<HrtUiMessage>? messageCallback = null,
string taskName = "Gearset Update");
public HrtUiMessage UpdateGearSet(GearSet set);
}

public readonly struct ExternalBiSDefinition
{
public readonly GearSetManager Service = GearSetManager.Unknown;
public readonly string Id = string.Empty;
public readonly int Idx = 0;
public readonly string Name = string.Empty;

public ExternalBiSDefinition(GearSetManager service, string id, int idx, string name)
{
Service = service;
Id = id;
Idx = idx;
Name = name;
}

public GearSet ToGearSet() => new(Service, Name)
{
ExternalId = Id,
ExternalIdx = Idx,
};

public bool Equals(GearSet? set) => Service == set?.ManagedBy && Id == set.ExternalId && set.ExternalIdx == Idx;
}

internal abstract class WebConnector
{
private readonly ConcurrentDictionary<string, (DateTime time, HttpResponseMessage response)> _cachedRequests;
Expand Down
3 changes: 1 addition & 2 deletions HimbeertoniRaidTool/Connectors/ConnectorPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public bool TryGetConnector(GearSetManager type, [NotNullWhen(true)] out IReadOn
_ => null,
};

public (GearSetManager Service, string Id) GetDefaultBiS(Job job) =>
(GearSetManager.Etro, _etroConnector.GetDefaultBiS(job));
public ExternalBiSDefinition GetDefaultBiS(Job job) => _etroConnector.GetDefaultBiS(job);

public void Dispose() => LodestoneConnector.Dispose();
}
16 changes: 8 additions & 8 deletions HimbeertoniRaidTool/Connectors/EtroConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class EtroConnector : WebConnector, IReadOnlyGearConnector
private const string GEARSET_WEB_BASE_URL = WEB_BASE_URL + "gearset/";
private const string RELIC_API_BASE_URL = API_BASE_URL + "relic/";
private const string BIS_API_BASE_URL = GEARSET_API_BASE_URL + "bis/";
private readonly Dictionary<Job, Dictionary<string, string>> _bisCache = [];
private readonly Dictionary<Job, List<ExternalBiSDefinition>> _bisCache = [];
private readonly Dictionary<uint, FoodItem> _foodLookup = [];
private readonly TaskManager _taskManager;
private readonly HrtDataManager _hrtDataManager;
Expand All @@ -30,7 +30,7 @@ internal EtroConnector(HrtDataManager hrtDataManager, TaskManager tm, ILogger lo
_taskManager = tm;
foreach (var job in Enum.GetValues<Job>())
{
_bisCache.Add(job, new Dictionary<string, string>());
_bisCache.Add(job, []);
}
_taskManager.RegisterTask(new HrtTask<HrtUiMessage>(FillBisList,
msg =>
Expand Down Expand Up @@ -59,17 +59,17 @@ internal EtroConnector(HrtDataManager hrtDataManager, TaskManager tm, ILogger lo
MissingMemberHandling = MissingMemberHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};
public IReadOnlyDictionary<string, string> GetBiSList(Job job) => _bisCache[job];
public string GetDefaultBiS(Job job) => _bisCache[job].Keys.FirstOrDefault("");
public IList<string> GetNames(string id)
public IList<ExternalBiSDefinition> GetBiSList(Job job) => _bisCache[job];
public ExternalBiSDefinition GetDefaultBiS(Job job) => _bisCache[job].FirstOrDefault(new ExternalBiSDefinition());
public IList<ExternalBiSDefinition> GetPossibilities(string id)
{
if (id.Equals("")) return [];
var httpResponse = MakeWebRequest(GEARSET_API_BASE_URL + id);
if (httpResponse is not { IsSuccessStatusCode: true }) return [];
var readTask = httpResponse.Content.ReadAsStringAsync();
readTask.Wait();
var etroSet = JsonConvert.DeserializeObject<EtroGearSet>(readTask.Result, JsonSettings);
return etroSet?.name is null ? [] : [etroSet.name];
return etroSet?.name is null ? [] : [new ExternalBiSDefinition(GearSetManager.Etro, id, 0, etroSet.name)];
}
private HrtUiMessage FillBisList()
{
Expand All @@ -82,13 +82,13 @@ private HrtUiMessage FillBisList()
foreach (var set in sets)
{
if (set.id != null)
_bisCache[set.job][set.id] = set.name ?? set.id;
_bisCache[set.job].Add(new ExternalBiSDefinition(GearSetManager.Etro, set.id, 0, set.name ?? set.id));
}
return new HrtUiMessage(GeneralLoc.EtroConnector_FillBisList_Success, HrtUiMessageType.Success);
}

public bool BelongsToThisService(string url) => url.StartsWith(WEB_BASE_URL);
public string GetId(string url) => url[GEARSET_WEB_BASE_URL.Length..];
public string GetId(string url) => BelongsToThisService(url) ? url[GEARSET_WEB_BASE_URL.Length..] : url;
public string GetWebUrl(string id) => GEARSET_WEB_BASE_URL + id;
public void RequestGearSetUpdate(GearSet set, Action<HrtUiMessage>? messageCallback = null,
string taskName = "Etro Update")
Expand Down
8 changes: 4 additions & 4 deletions HimbeertoniRaidTool/Connectors/LodestoneConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ private async Task<HrtUiMessage> UpdateCharacterAsync(Player p)
FillItem(newGearset.Ring1, GearSetSlot.Ring1);
FillItem(newGearset.Ring2, GearSetSlot.Ring2);

return new HrtUiMessage(
string.Format(GeneralLoc.LodestoneConnector_msg_Success, p.MainChar.Name, classToChange.Job),
HrtUiMessageType.Success);

void FillItem(GearEntry? gearPiece, GearSetSlot slot)
{
if (gearPiece == null)
Expand Down Expand Up @@ -136,10 +140,6 @@ void FillItem(GearEntry? gearPiece, GearSetSlot slot)
classToChange.CurGear[slot].AddMateria(new MateriaItem(materiaCategory, materiaLevel));
}
}

return new HrtUiMessage(
string.Format(GeneralLoc.LodestoneConnector_msg_Success, p.MainChar.Name, classToChange.Job),
HrtUiMessageType.Success);
}
catch (Exception e)
{
Expand Down
31 changes: 16 additions & 15 deletions HimbeertoniRaidTool/Connectors/XivGearAppConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,33 @@ internal class XivGearAppConnector(HrtDataManager hrtDataManager, TaskManager ta
};


public bool BelongsToThisService(string url) => url.StartsWith(WEB_BASE_URL) || url.StartsWith(API_BASE_URL);
public string GetId(string url) => HttpUtility.UrlDecode(url).Split('|')[^1];
public string GetWebUrl(string id) => $"{GEAR_WEB_BASE_URL}{id}";
public IReadOnlyDictionary<string, string> GetBiSList(Job job) => new Dictionary<string, string>();
public bool IsSheet(string id)
public bool BelongsToThisService(string url)
{
var httpResponse = MakeWebRequest(GEAR_API_BASE_URL + id);
if (httpResponse is null || !httpResponse.IsSuccessStatusCode) return false;
var readTask = httpResponse.Content.ReadAsStringAsync();
readTask.Wait();
return IsSheetInternal(readTask.Result);
url = HttpUtility.UrlDecode(url);
return url.StartsWith(WEB_BASE_URL) || url.StartsWith(API_BASE_URL);
}
public string GetId(string url) => HttpUtility.UrlDecode(url).Split('|')[^1];
public string GetWebUrl(string id) => $"{GEAR_WEB_BASE_URL}{id}";
public IList<ExternalBiSDefinition> GetBiSList(Job job) => [];
private static bool IsSheetInternal(string content) =>
JsonConvert.DeserializeObject<XivGearSheet>(content)?.sets?.Count > 0;

public IList<string> GetNames(string id)
public IList<ExternalBiSDefinition> GetPossibilities(string id)
{
Logger.Debug($"Getting possibilities for {id}");
var httpResponse = MakeWebRequest(GEAR_API_BASE_URL + id);
if (httpResponse is null || !httpResponse.IsSuccessStatusCode) return [];
var readTask = httpResponse.Content.ReadAsStringAsync();
readTask.Wait();
var sheet = JsonConvert.DeserializeObject<XivGearSheet>(readTask.Result);
if (sheet?.sets != null)
return sheet.sets?.ConvertAll(set => set.name ?? "") ?? [];
var set = JsonConvert.DeserializeObject<XivGearSheet>(readTask.Result);
return set is null ? [] : [set.name ?? ""];
if (sheet?.sets == null)
{
var set = JsonConvert.DeserializeObject<XivGearSheet>(readTask.Result);
return set is null ? [] : [new ExternalBiSDefinition(GearSetManager.XivGear, id, 0, set.name ?? "")];
}
int idx = 0;
return sheet.sets.Select(set => new ExternalBiSDefinition(GearSetManager.XivGear, id, idx++, set.name ?? ""))
.ToList();
}

public void RequestGearSetUpdate(GearSet set, Action<HrtUiMessage>? messageCallback = null,
Expand Down
54 changes: 54 additions & 0 deletions HimbeertoniRaidTool/Localization/GeneralLoc.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions HimbeertoniRaidTool/Localization/GeneralLoc.resx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions HimbeertoniRaidTool/Modules/Core/ChangeLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ public class ChangeLog
{
public static readonly IReadOnlyList<SingleVersionChangelog> Entries = new List<SingleVersionChangelog>
{
new(new Version(1, 8, 0, 2))
{
MinorFeatures =
{
new ChangeLogEntry(UserInterface, "Improved usability of BiS section in gear edit"),
new ChangeLogEntry(
Bugfix, "2nd,3rd,... set from XivGear.app sheets are now fetched correctly, not always the 1st"),
},
},
new(new Version(1, 8, 0, 1))
{
MinorFeatures =
Expand Down
19 changes: 7 additions & 12 deletions HimbeertoniRaidTool/Modules/LootMaster/LootMasterModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,16 @@ private bool FillCharacter(ref Character destination, IPlayerCharacter source)
{
curClass.Level = source.Level;
var gearDb = Services.HrtDataManager.GearDb;
if (!gearDb.Search(
entry => entry?.ExternalId
== Services.ConnectorPool.GetDefaultBiS(curClass.Job).Id,
out var etroSet))
var defaultBis = Services.ConnectorPool.GetDefaultBiS(curClass.Job);
if (!gearDb.Search(defaultBis.Equals, out var bisSet))
{
var defaultBis = Services.ConnectorPool.GetDefaultBiS(curClass.Job);
etroSet = new GearSet(defaultBis.Service)
{
ExternalId = defaultBis.Id,
};
gearDb.TryAdd(etroSet);

bisSet = defaultBis.ToGearSet();
gearDb.TryAdd(bisSet);
if (Services.ConnectorPool.TryGetConnector(defaultBis.Service, out var connector))
connector.RequestGearSetUpdate(etroSet, HandleMessage);
connector.RequestGearSetUpdate(bisSet, HandleMessage);
}
curClass.CurBis = etroSet;
curClass.CurBis = bisSet;
gearDb.TryAdd(curClass.CurGear);
}
Services.HrtDataManager.Save();
Expand Down
6 changes: 2 additions & 4 deletions HimbeertoniRaidTool/Services/ExamineGearDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,11 @@ private void GetItemInfos()

targetClass = targetChar.AddClass(targetJob);
var defaultBiS = _connectorPool.GetDefaultBiS(targetJob);
if (_hrtDataManager.GearDb.Search(
entry => entry?.ManagedBy == defaultBiS.Service && entry.ExternalId == defaultBiS.Id,
out var existingBis))
if (_hrtDataManager.GearDb.Search(defaultBiS.Equals, out var existingBis))
targetClass.CurBis = existingBis;
else
{
(targetClass.CurBis.ManagedBy, targetClass.CurBis.ExternalId) = defaultBiS;
targetClass.CurBis = defaultBiS.ToGearSet();
if (_hrtDataManager.GearDb.TryAdd(targetClass.CurBis)
&& _connectorPool.TryGetConnector(defaultBiS.Service, out var connector))
connector.RequestGearSetUpdate(targetClass.CurBis);
Expand Down
Loading

0 comments on commit 48f591b

Please sign in to comment.