diff --git a/Influx/Influx/InfluxStatisticsClient.cs b/Influx/Influx/InfluxStatisticsClient.cs index ea84013..78f3e99 100644 --- a/Influx/Influx/InfluxStatisticsClient.cs +++ b/Influx/Influx/InfluxStatisticsClient.cs @@ -37,9 +37,9 @@ internal class InfluxStatisticsClient : IDisposable return; DateTime date = DateTime.UtcNow; - IReadOnlyDictionary stats = update.Currencies; + IReadOnlyDictionary currencyStats = update.Currencies; - var validFcIds = stats.Keys + var validFcIds = currencyStats.Keys .Where(x => x.CharacterType == CharacterType.Character) .Select(x => x.FreeCompanyId) .ToList(); @@ -48,7 +48,7 @@ internal class InfluxStatisticsClient : IDisposable try { List values = new(); - foreach (var (character, currencies) in stats) + foreach (var (character, currencies) in currencyStats) { if (character.CharacterType == CharacterType.Character) { @@ -64,7 +64,7 @@ internal class InfluxStatisticsClient : IDisposable } else if (character.CharacterType == CharacterType.Retainer) { - var owner = stats.Keys.First(x => x.CharacterId == character.OwnerId); + var owner = currencyStats.Keys.First(x => x.CharacterId == character.OwnerId); values.Add(PointData.Measurement("currency") .Tag("id", character.CharacterId.ToString()) .Tag("player_name", owner.Name) @@ -90,6 +90,23 @@ internal class InfluxStatisticsClient : IDisposable } } + foreach (var (fc, subs) in update.Submarines) + { + if (validFcIds.Contains(fc.CharacterId)) + { + foreach (var sub in subs) + { + values.Add(PointData.Measurement("submersibles") + .Tag("id", fc.CharacterId.ToString()) + .Tag("fc_name", fc.Name) + .Tag("sub_id", $"{fc.CharacterId}_{sub.Id}") + .Tag("sub_name", sub.Name) + .Field("level", sub.Level) + .Timestamp(date, WritePrecision.S)); + } + } + } + var writeApi = _influxClient.GetWriteApiAsync(); await writeApi.WritePointsAsync( values, diff --git a/Influx/InfluxPlugin.cs b/Influx/InfluxPlugin.cs index 2760fdc..48c83ec 100644 --- a/Influx/InfluxPlugin.cs +++ b/Influx/InfluxPlugin.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Linq; using System.Threading; using Dalamud.Game.ClientState; using Dalamud.Game.Command; @@ -10,6 +11,7 @@ using Dalamud.Plugin; using ECommons; using Influx.AllaganTools; using Influx.Influx; +using Influx.SubmarineTracker; using Influx.Windows; namespace Influx; @@ -24,6 +26,7 @@ public class InfluxPlugin : IDalamudPlugin private readonly ClientState _clientState; private readonly CommandManager _commandManager; private readonly AllaganToolsIpc _allaganToolsIpc; + private readonly SubmarineTrackerIpc _submarineTrackerIpc; private readonly InfluxStatisticsClient _influxStatisticsClient; private readonly WindowSystem _windowSystem; private readonly StatisticsWindow _statisticsWindow; @@ -40,6 +43,7 @@ public class InfluxPlugin : IDalamudPlugin _clientState = clientState; _commandManager = commandManager; _allaganToolsIpc = new AllaganToolsIpc(pluginInterface, chatGui, _configuration); + _submarineTrackerIpc = new SubmarineTrackerIpc(chatGui); _influxStatisticsClient = new InfluxStatisticsClient(chatGui, _configuration); _windowSystem = new WindowSystem(typeof(InfluxPlugin).FullName); @@ -76,9 +80,12 @@ public class InfluxPlugin : IDalamudPlugin try { + var currencies = _allaganToolsIpc.CountCurrencies(); + var characters = currencies.Keys.ToList(); var update = new StatisticsUpdate { - Currencies = _allaganToolsIpc.CountCurrencies(), + Currencies = currencies, + Submarines = _submarineTrackerIpc.GetSubmarineStats(characters), }; _statisticsWindow.OnStatisticsUpdate(update); _influxStatisticsClient.OnStatisticsUpdate(update); diff --git a/Influx/StatisticsUpdate.cs b/Influx/StatisticsUpdate.cs index 3344ada..fc8696b 100644 --- a/Influx/StatisticsUpdate.cs +++ b/Influx/StatisticsUpdate.cs @@ -1,9 +1,11 @@ using System.Collections.Generic; using Influx.AllaganTools; +using Influx.SubmarineTracker; namespace Influx; internal sealed class StatisticsUpdate { public required IReadOnlyDictionary Currencies { get; init; } + public required Dictionary> Submarines { get; init; } } diff --git a/Influx/SubmarineTracker/SubmarineStats.cs b/Influx/SubmarineTracker/SubmarineStats.cs new file mode 100644 index 0000000..36186c6 --- /dev/null +++ b/Influx/SubmarineTracker/SubmarineStats.cs @@ -0,0 +1,8 @@ +namespace Influx.SubmarineTracker; + +public class SubmarineStats +{ + public string Name { get; init; } + public int Id { get; init; } + public ushort Level { get; init; } +} diff --git a/Influx/SubmarineTracker/SubmarineTrackerIpc.cs b/Influx/SubmarineTracker/SubmarineTrackerIpc.cs new file mode 100644 index 0000000..aac78e2 --- /dev/null +++ b/Influx/SubmarineTracker/SubmarineTrackerIpc.cs @@ -0,0 +1,86 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using Dalamud.Game.Gui; +using ECommons.Reflection; +using Influx.AllaganTools; + +namespace Influx.SubmarineTracker; + +internal sealed class SubmarineTrackerIpc +{ + private readonly ChatGui _chatGui; + + public SubmarineTrackerIpc(ChatGui chatGui) + { + _chatGui = chatGui; + } + + public Dictionary> GetSubmarineStats(List characters) + { + if (DalamudReflector.TryGetDalamudPlugin("Submarine Tracker", out var it, false, true)) + { + var submarineData = it.GetType().Assembly.GetType("SubmarineTracker.Data.Submarines"); + var knownSubmarineData = submarineData!.GetField("KnownSubmarines")!; + return ((IEnumerable)knownSubmarineData.GetValue(null)!).Cast() + .Select(x => new + { + OwnerId = (ulong)x.GetType().GetProperty("Key")!.GetValue(x)!, + FcWrapper = x.GetType().GetProperty("Value")!.GetValue(x)! + }) + .Select(x => new + { + Owner = characters.FirstOrDefault(y => y.CharacterId == x.OwnerId), + Subs = new FcSubmarines(x.FcWrapper).Submarines, + }) + .Where(x => x.Owner != null) + .Select(x => new + { + x.Subs, + Fc = characters.FirstOrDefault(y => y.CharacterId == x.Owner!.FreeCompanyId) + }) + .Where(x => x.Fc != null) + .ToDictionary( + x => x.Fc!, + x => x.Subs.Select(y => new SubmarineStats + { + Id = x.Subs.IndexOf(y), + Name = y.Name, + Level = y.Level, + }).ToList()); + } + else + return new Dictionary>(); + } +} + +public sealed class FcSubmarines +{ + private readonly object _delegate; + + public FcSubmarines(object @delegate) + { + _delegate = @delegate; + Submarines = ((IEnumerable)_delegate.GetType().GetField("Submarines")!.GetValue(_delegate)!) + .Cast() + .Select(x => new Submarine(x)) + .ToList(); + } + + public List Submarines { get; } +} + +public sealed class Submarine +{ + private readonly object _delegate; + + public Submarine(object @delegate) + { + _delegate = @delegate; + Name = (string)_delegate.GetType().GetProperty("Name")!.GetValue(_delegate)!; + Level = (ushort)_delegate.GetType().GetProperty("Rank")!.GetValue(_delegate)!; + } + + public string Name { get; set; } + public ushort Level { get; } +}