Skip to content

Commit

Permalink
Add login and logout events to control the stopwatch.
Browse files Browse the repository at this point in the history
Queue existing data only once on logout and plugin init.
  • Loading branch information
Kurochi51 committed Mar 3, 2024
1 parent 6382bbd commit e57e44a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
44 changes: 30 additions & 14 deletions PetScale/PetScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public sealed class PetScale : IDalamudPlugin
private readonly ICommandManager commandManager;
private readonly IFramework framework;
private readonly IPluginLog log;
private readonly IClientState clientState;
private readonly Utilities utilities;
public readonly IClientState clientState;

private readonly StringComparison ordinalComparison = StringComparison.Ordinal;
private readonly Dictionary<Pointer<BattleChara>, (Pointer<Character> character, bool petSet)> activePetDictionary = new();
Expand Down Expand Up @@ -97,18 +97,44 @@ public PetScale(DalamudPluginInterface _pluginInterface,
pluginInterface.UiBuilder.Draw += WindowSystem.Draw;
pluginInterface.UiBuilder.OpenConfigUi += ConfigWindow.Toggle;
clientState.TerritoryChanged += TerritoryChanged;
clientState.Login += SetStopwatch;
clientState.Logout += SetStopwatch;
framework.Update += OnFrameworkUpdate;
stopwatch.Start();

_ = Task.Run(InitSheet);
ConfigWindow.Save(save: false);
QueueOnlyExistingData();
}

private void SetStopwatch()
{
playerName = clientState.LocalPlayer?.Name.TextValue;
if (clientState.IsLoggedIn)
{
stopwatch.Start();
}
else
{
QueueOnlyExistingData();
stopwatch.Stop();
}
}

private void TerritoryChanged(ushort obj)
{
stopwatch.Restart();
}

private void QueueOnlyExistingData()
{
players.Clear();
foreach (var entry in config.PetData.Select(item => item.CharacterName).Distinct(StringComparer.OrdinalIgnoreCase))
{
players.Enqueue(entry);
}
}

private void InitSheet()
{
var petSheet = utilities.GetSheet<Pet>(clientState.ClientLanguage);
Expand Down Expand Up @@ -148,19 +174,6 @@ private unsafe void OnFrameworkUpdate(IFramework framework)
#endif
if (clientState is not { LocalPlayer: { } player })
{
if (playerName is not null)
{
playerName = null;
}
players.Clear();
foreach (var entry in config.PetData.Select(item => item.CharacterName).Distinct(StringComparer.OrdinalIgnoreCase))
{
players.Enqueue(entry);
}
if (stopwatch.IsRunning)
{
stopwatch.Stop();
}
return;
}
if (requestedCache)
Expand Down Expand Up @@ -350,7 +363,10 @@ public void Dispose()
{
stopwatch.Stop();
players.Clear();

framework.Update -= OnFrameworkUpdate;
clientState.Login -= SetStopwatch;
clientState.Logout -= SetStopwatch;
clientState.TerritoryChanged -= TerritoryChanged;
pluginInterface.UiBuilder.OpenConfigUi -= ConfigWindow.Toggle;
pluginInterface.UiBuilder.Draw -= WindowSystem.Draw;
Expand Down
15 changes: 14 additions & 1 deletion PetScale/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public override void OnClose()
public override void Draw()
{
ResizeIfNeeded();
ImGui.TextUnformatted("Amount of players: " + (players.Count > 2 ? players.Count - 1 : players.Count).ToString(CultureInfo.InvariantCulture));
ImGui.TextUnformatted("Amount of players: " + GetPlayerCount(players.Count, plugin.clientState.IsLoggedIn).ToString(CultureInfo.InvariantCulture));
var buttonPressed = false;
DrawComboBox("Characters", charaName, charaWidth, out charaName, players, filter: true);
ImGui.SameLine();
Expand Down Expand Up @@ -429,6 +429,19 @@ public void Save(bool save)
}
}

private static int GetPlayerCount(int queueCount, bool loggedIn)
{
if (!loggedIn)
{
return 0;
}
return queueCount switch
{
< 2 => 0,
>= 2 => queueCount - 1,
};
}

private async void QueueColumnWidthChange(IFontHandle handle, ILockedImFont lockedFont)
{
while (!handle.Available && !cToken.IsCancellationRequested)
Expand Down

0 comments on commit e57e44a

Please sign in to comment.