Apply client settings without reloading the plugin

This commit is contained in:
Liza 2024-03-01 10:05:51 +01:00
parent ec814f7ca6
commit 533b87118e
Signed by: liza
GPG Key ID: 7199F8D727D55F67
4 changed files with 45 additions and 17 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<Version>0.5</Version>
<Version>0.6</Version>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

View File

@ -15,21 +15,23 @@ namespace Influx.Influx;
internal sealed class InfluxStatisticsClient : IDisposable
{
private readonly InfluxDBClient _influxClient;
private InfluxDBClient? _influxClient;
private readonly IChatGui _chatGui;
private readonly Configuration _configuration;
private readonly IClientState _clientState;
private readonly IPluginLog _pluginLog;
private readonly IReadOnlyDictionary<byte, byte> _classJobToArrayIndex;
private readonly IReadOnlyDictionary<byte, string> _classJobNames;
private readonly Dictionary<sbyte, string> _expToJobs;
public InfluxStatisticsClient(IChatGui chatGui, Configuration configuration, IDataManager dataManager,
IClientState clientState)
IClientState clientState, IPluginLog pluginLog)
{
_influxClient = new InfluxDBClient(configuration.Server.Server, configuration.Server.Token);
_chatGui = chatGui;
_configuration = configuration;
_clientState = clientState;
_pluginLog = pluginLog;
UpdateClient();
_classJobToArrayIndex = dataManager.GetExcelSheet<ClassJob>()!.Where(x => x.RowId > 0)
.ToDictionary(x => (byte)x.RowId, x => (byte)x.ExpArrayIndex);
@ -41,7 +43,20 @@ internal sealed class InfluxStatisticsClient : IDisposable
.ToDictionary(x => x.ExpArrayIndex, x => x.Abbreviation.ToString());
}
public bool Enabled => _configuration.Server.Enabled;
public bool Enabled => _configuration.Server.Enabled &&
!string.IsNullOrEmpty(_configuration.Server.Server) &&
!string.IsNullOrEmpty(_configuration.Server.Token) &&
!string.IsNullOrEmpty(_configuration.Server.Organization) &&
!string.IsNullOrEmpty(_configuration.Server.Bucket);
public void UpdateClient()
{
_influxClient?.Dispose();
_influxClient = null;
if (Enabled)
_influxClient = new InfluxDBClient(_configuration.Server.Server, _configuration.Server.Token);
}
public void OnStatisticsUpdate(StatisticsUpdate update)
{
@ -53,9 +68,13 @@ internal sealed class InfluxStatisticsClient : IDisposable
var validFcIds = currencyStats.Keys
.Where(x => x.CharacterType == CharacterType.Character)
.Where(x => _configuration.IncludedCharacters.SingleOrDefault(config => config.LocalContentId == x.CharacterId)?.IncludeFreeCompany == true)
.Where(x => _configuration.IncludedCharacters
.SingleOrDefault(config => config.LocalContentId == x.CharacterId)?.IncludeFreeCompany == true)
.Select(x => x.FreeCompanyId)
.ToList();
var client = _influxClient;
if (client == null)
return;
Task.Run(async () =>
{
try
@ -124,7 +143,8 @@ internal sealed class InfluxStatisticsClient : IDisposable
.Tag("id", character.CharacterId.ToString())
.Tag("player_name", character.Name)
.Tag("type", character.CharacterType.ToString())
.Tag("fc_id", character.FreeCompanyId > 0 ? character.FreeCompanyId.ToString() : null)
.Tag("fc_id",
character.FreeCompanyId > 0 ? character.FreeCompanyId.ToString() : null)
.Tag("job", abbreviation)
.Field("level", level)
.Timestamp(date, WritePrecision.S));
@ -225,7 +245,7 @@ internal sealed class InfluxStatisticsClient : IDisposable
}
}
var writeApi = _influxClient.GetWriteApiAsync();
var writeApi = client.GetWriteApiAsync();
await writeApi.WritePointsAsync(
values,
_configuration.Server.Bucket, _configuration.Server.Organization);
@ -234,6 +254,7 @@ internal sealed class InfluxStatisticsClient : IDisposable
}
catch (Exception e)
{
_pluginLog.Error(e, "Unable to update statistics");
_chatGui.PrintError(e.Message);
}
});
@ -241,6 +262,6 @@ internal sealed class InfluxStatisticsClient : IDisposable
public void Dispose()
{
_influxClient.Dispose();
_influxClient?.Dispose();
}
}

View File

@ -58,12 +58,13 @@ public class InfluxPlugin : IDalamudPlugin
_submarineTrackerIpc = new SubmarineTrackerIpc(dalamudReflector);
_localStatsCalculator = new LocalStatsCalculator(pluginInterface, clientState, addonLifecycle, pluginLog, dataManager);
_fcStatsCalculator = new FcStatsCalculator(this, pluginInterface, clientState, addonLifecycle, gameGui, framework, pluginLog);
_influxStatisticsClient = new InfluxStatisticsClient(chatGui, _configuration, dataManager, clientState);
_influxStatisticsClient = new InfluxStatisticsClient(chatGui, _configuration, dataManager, clientState, _pluginLog);
_windowSystem = new WindowSystem(typeof(InfluxPlugin).FullName);
_statisticsWindow = new StatisticsWindow();
_windowSystem.AddWindow(_statisticsWindow);
_configurationWindow = new ConfigurationWindow(_pluginInterface, clientState, _configuration);
_configurationWindow.ConfigUpdated += (_, _) => _influxStatisticsClient.UpdateClient();
_windowSystem.AddWindow(_configurationWindow);
_commandManager.AddHandler("/influx", new CommandInfo(ProcessCommand));

View File

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Dalamud.Interface.Colors;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Interface.Windowing;
@ -23,6 +24,8 @@ internal sealed class ConfigurationWindow : Window
_configuration = configuration;
}
public event EventHandler? ConfigUpdated;
public override void Draw()
{
using var tabBar = ImRaii.TabBar("InfluxConfigTabs");
@ -43,35 +46,35 @@ internal sealed class ConfigurationWindow : Window
if (ImGui.Checkbox("Enable Server Connection", ref enabled))
{
_configuration.Server.Enabled = enabled;
Save();
Save(true);
}
string server = _configuration.Server.Server;
if (ImGui.InputText("InfluxDB URL", ref server, 128))
{
_configuration.Server.Server = server;
Save();
Save(true);
}
string token = _configuration.Server.Token;
if (ImGui.InputText("Token", ref token, 128, ImGuiInputTextFlags.Password))
{
_configuration.Server.Token = token;
Save();
Save(true);
}
string organization = _configuration.Server.Organization;
if (ImGui.InputText("Organization", ref organization, 128))
{
_configuration.Server.Organization = organization;
Save();
Save(true);
}
string bucket = _configuration.Server.Bucket;
if (ImGui.InputText("Bucket", ref bucket, 128))
{
_configuration.Server.Bucket = bucket;
Save();
Save(true);
}
}
@ -160,8 +163,11 @@ internal sealed class ConfigurationWindow : Window
}
}
private void Save()
private void Save(bool sendEvent = false)
{
_pluginInterface.SavePluginConfig(_configuration);
if (sendEvent)
ConfigUpdated?.Invoke(this, EventArgs.Empty);
}
}