2023-01-10 20:07:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using CurrencyAlert.DataModels;
|
|
|
|
|
using CurrencyAlert.Localization;
|
|
|
|
|
using Dalamud.Logging;
|
|
|
|
|
using KamiLib.ChatCommands;
|
2023-01-18 14:14:09 +00:00
|
|
|
|
using KamiLib.GameState;
|
2023-01-10 20:07:57 +00:00
|
|
|
|
|
|
|
|
|
namespace CurrencyAlert.System.cs;
|
|
|
|
|
|
|
|
|
|
public class CurrencyTracker : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly CancellationTokenSource cancellationTokenSource = new();
|
|
|
|
|
private readonly Stopwatch timer = new();
|
|
|
|
|
|
|
|
|
|
public readonly List<TrackedCurrency> ActiveWarnings = new();
|
|
|
|
|
|
|
|
|
|
public CurrencyTracker()
|
|
|
|
|
{
|
|
|
|
|
UpdateCurrencies();
|
|
|
|
|
|
|
|
|
|
Service.ClientState.TerritoryChanged += OnZoneChange;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateCurrencies()
|
|
|
|
|
{
|
|
|
|
|
Service.Framework.RunOnTick(UpdateCurrencies, TimeSpan.FromMilliseconds(250), 0, cancellationTokenSource.Token);
|
|
|
|
|
|
|
|
|
|
ActiveWarnings.Clear();
|
|
|
|
|
if (!Service.ClientState.IsLoggedIn) return;
|
|
|
|
|
|
|
|
|
|
foreach (var currency in Service.Configuration.TrackedCurrencies)
|
|
|
|
|
{
|
|
|
|
|
if (currency.Enabled)
|
|
|
|
|
{
|
|
|
|
|
if (currency.CurrencyInfo().GetCurrentQuantity() >= currency.Threshold.Value)
|
|
|
|
|
{
|
|
|
|
|
ActiveWarnings.Add(currency);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnZoneChange(object? sender, ushort e)
|
|
|
|
|
{
|
|
|
|
|
if (!Service.Configuration.ChatNotification) return;
|
2023-01-18 14:14:09 +00:00
|
|
|
|
if (Condition.IsBoundByDuty()) return;
|
2023-01-10 20:07:57 +00:00
|
|
|
|
|
|
|
|
|
if (timer.Elapsed.Minutes >= 5 || timer.IsRunning == false)
|
|
|
|
|
{
|
|
|
|
|
timer.Restart();
|
|
|
|
|
foreach (var currency in ActiveWarnings)
|
|
|
|
|
{
|
|
|
|
|
Chat.Print($"{currency.CurrencyInfo().ItemName}", Strings.ChatWarningText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var lockoutRemaining = TimeSpan.FromMinutes(5) - timer.Elapsed;
|
|
|
|
|
PluginLog.Debug($"Zone Change Messages Suppressed, '{lockoutRemaining}' Remaining");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Service.ClientState.TerritoryChanged -= OnZoneChange;
|
|
|
|
|
|
|
|
|
|
cancellationTokenSource.Cancel();
|
|
|
|
|
cancellationTokenSource.Dispose();
|
|
|
|
|
}
|
2023-01-18 14:14:09 +00:00
|
|
|
|
}
|