From 000b0bffb82a0c6dfb9b6244320efb8a39cee25b Mon Sep 17 00:00:00 2001 From: DueDine Date: Wed, 11 Dec 2024 19:09:27 +0800 Subject: [PATCH] [feat] Add Auto Archive & Bump Version --- ContactsTracker/Configuration.cs | 7 ++- ContactsTracker/ContactsTracker.csproj | 2 +- ContactsTracker/ContactsTracker.json | 18 +++---- ContactsTracker/DataEntry.cs | 2 +- ContactsTracker/Database.cs | 66 +++++++++++++++++++++++--- ContactsTracker/Plugin.cs | 2 + ContactsTracker/Windows/MainWindow.cs | 43 +++++++++++++++-- 7 files changed, 119 insertions(+), 21 deletions(-) diff --git a/ContactsTracker/Configuration.cs b/ContactsTracker/Configuration.cs index efa7be5..a08cc86 100644 --- a/ContactsTracker/Configuration.cs +++ b/ContactsTracker/Configuration.cs @@ -16,7 +16,12 @@ public class Configuration : IPluginConfiguration public bool OnlyDutyRoulette { get; set; } = false; - // the below exist just to make saving less cumbersome + public bool ArchiveOldEntries { get; set; } = false; + + public int ArchiveWhenEntriesExceed { get; set; } = -1; // -1 means no limit + + public int ArchiveKeepEntries { get; set; } = 5; // Remove oldest ArchiveWhenEntriesExceed - ArchiveKeepEntries entries + public void Save() { Plugin.PluginInterface.SavePluginConfig(this); diff --git a/ContactsTracker/ContactsTracker.csproj b/ContactsTracker/ContactsTracker.csproj index cdf52af..9c050de 100644 --- a/ContactsTracker/ContactsTracker.csproj +++ b/ContactsTracker/ContactsTracker.csproj @@ -4,7 +4,7 @@ net8.0-windows - 0.0.0.2 + 0.0.0.3 Help you remember previous contacts. https://github.com/DueDine/ContactsTracker AGPL-3.0-or-later diff --git a/ContactsTracker/ContactsTracker.json b/ContactsTracker/ContactsTracker.json index f82e9da..0f92141 100644 --- a/ContactsTracker/ContactsTracker.json +++ b/ContactsTracker/ContactsTracker.json @@ -1,10 +1,12 @@ { - "Author": "Mistral", - "Name": "ContactsTracker", - "Punchline": "Help you remember previous contacts.", - "Description": "Remember to check Settings Tab.", - "ApplicableVersion": "any", - "Tags": [ - "plugin" - ] + "Author": "Due", + "Name": "ContactsTracker", + "Punchline": "Help you remember previous contacts.", + "Description": "Record Duty Completeion and Team Composition.\nRemember to check Settings Tab.", + "ApplicableVersion": "any", + "Tags": [ + "plugin", + "track", + "party" + ] } diff --git a/ContactsTracker/DataEntry.cs b/ContactsTracker/DataEntry.cs index 6c38409..06705e2 100644 --- a/ContactsTracker/DataEntry.cs +++ b/ContactsTracker/DataEntry.cs @@ -33,7 +33,7 @@ public static void finalize(Configuration configuration) return; } - Instance.jobName = Plugin.ClientState.LocalPlayer?.ClassJob.Value.Name.ToString(); // Some area allows job change + Instance.jobName = Plugin.ClientState.LocalPlayer?.ClassJob.Value.Name.ToString(); // Some area allows job change, take the final job Instance.endAt = DateTime.Now.ToString("T"); var numOfParty = Plugin.PartyList.Length; diff --git a/ContactsTracker/Database.cs b/ContactsTracker/Database.cs index 1451bae..3830b8b 100644 --- a/ContactsTracker/Database.cs +++ b/ContactsTracker/Database.cs @@ -1,7 +1,9 @@ using CsvHelper; using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.IO; +using System.Linq; namespace ContactsTracker; @@ -35,6 +37,7 @@ public static void Load() if (content != null) { Entries = content; + Plugin.Logger.Debug("Database Loaded"); } else { @@ -59,7 +62,8 @@ public static void SaveInProgressEntry(DataEntry entry) public static void Export() { - var exportPath = Path.Combine(Plugin.PluginInterface.ConfigDirectory.FullName, "export.csv"); + var fileName = $"export-{DateTime.Now:yyyy-MM-dd HH-mm-ss}.csv"; // Avoid duplicate + var exportPath = Path.Combine(Plugin.PluginInterface.ConfigDirectory.FullName, fileName); var records = JsonConvert.DeserializeObject>(File.ReadAllText(dataPath)); if (records == null) { @@ -73,14 +77,62 @@ public static void Export() } else { - using (var writer = new StreamWriter(exportPath)) - using (var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture)) - { - csv.WriteRecords(records); - } + using var writer = new StreamWriter(exportPath); + using var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture); + csv.WriteRecords(records); } - Plugin.ChatGui.Print($"Exported to {exportPath}"); + Plugin.ChatGui.Print($"Exported to {exportPath}"); // Maybe we can open explorer directly? + } + + public static void Archive(Configuration configuration) + { + if (configuration.ArchiveWhenEntriesExceed == -1) + { + Plugin.Logger.Debug("ArchiveWhenEntriesExceed is -1, skipping archive."); + return; + } + + if (Entries.Count < configuration.ArchiveWhenEntriesExceed) + { + Plugin.Logger.Debug("Entries count is less than ArchiveWhenEntriesExceed, skipping archive."); + return; + } + + var fileName = $"archive-{DateTime.Now:yyyy-MM-dd HH-mm-ss}.csv"; + var archivePath = Path.Combine(Plugin.PluginInterface.ConfigDirectory.FullName, fileName); + var records = JsonConvert.DeserializeObject>(File.ReadAllText(dataPath)); + + if (records == null) + { + Plugin.Logger.Debug("Failed to archive data.json!"); + return; + } + else if (records.Count == 0) + { + Plugin.Logger.Debug("No records to archive."); + return; + } + else + { + using var writer = new StreamWriter(archivePath); + using var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture); + csv.WriteRecords(records); + } + + Plugin.ChatGui.Print($"Archived to {archivePath}"); + + if (File.Exists(archivePath)) // Then keep newest ArchiveKeepEntries entries + { + var recordsToKeep = records.Skip(records.Count - configuration.ArchiveKeepEntries).ToList(); + File.WriteAllText(dataPath, JsonConvert.SerializeObject(recordsToKeep)); + Load(); // Update Entries + } + else + { + Plugin.Logger.Debug("Failed to archive data.json!"); + } + } } diff --git a/ContactsTracker/Plugin.cs b/ContactsTracker/Plugin.cs index cd4409a..b80bb2d 100644 --- a/ContactsTracker/Plugin.cs +++ b/ContactsTracker/Plugin.cs @@ -148,6 +148,8 @@ private void OnDutyCompleted(object? sender, ushort territoryID) { ChatGui.Print("ContactsTracker Record Completed"); } + + Database.Archive(Configuration); } private unsafe void OnCfPop(ContentFinderCondition condition) diff --git a/ContactsTracker/Windows/MainWindow.cs b/ContactsTracker/Windows/MainWindow.cs index ad9768e..c4086ec 100644 --- a/ContactsTracker/Windows/MainWindow.cs +++ b/ContactsTracker/Windows/MainWindow.cs @@ -64,12 +64,12 @@ private void DrawMainTab() return; } - if (ImGui.BeginCombo("Select Entry", $"{entries[selectedTab].TerritoryName} - {entries[selectedTab].Date} - {entries[selectedTab].beginAt}")) + if (ImGui.BeginCombo("Select Entry", $"{entries[selectedTab].TerritoryName} - {entries[selectedTab].Date} {entries[selectedTab].beginAt}")) { for (var i = 0; i < entries.Count; i++) { var isSelected = selectedTab == i; - if (ImGui.Selectable($"{entries[i].TerritoryName} - {entries[i].beginAt}", selectedTab == i)) + if (ImGui.Selectable($"{entries[i].TerritoryName} - {entries[i].Date} {entries[i].beginAt}", selectedTab == i)) { selectedTab = i; } @@ -156,6 +156,8 @@ private void DrawSettingsTab() Plugin.Configuration.Save(); } + ImGui.Spacing(); + var recordSolo = Plugin.Configuration.RecordSolo; if (ImGui.Checkbox("Record Solo", ref recordSolo)) { @@ -163,6 +165,8 @@ private void DrawSettingsTab() Plugin.Configuration.Save(); } + ImGui.Spacing(); + var printToChat = Plugin.Configuration.PrintToChat; if (ImGui.Checkbox("Print To Chat", ref printToChat)) { @@ -170,6 +174,8 @@ private void DrawSettingsTab() Plugin.Configuration.Save(); } + ImGui.Spacing(); + var onlyDutyRoulette = Plugin.Configuration.OnlyDutyRoulette; if (ImGui.Checkbox("Only Record Duty Roulette", ref onlyDutyRoulette)) { @@ -178,11 +184,42 @@ private void DrawSettingsTab() } } - private static void DrawDataTab() + private void DrawDataTab() { if (ImGui.Button("Export to CSV")) { Database.Export(); } + + ImGui.Spacing(); + + var autoArchive = Plugin.Configuration.ArchiveOldEntries; + if (ImGui.Checkbox("Enable Auto Archive", ref autoArchive)) + { + Plugin.Configuration.ArchiveOldEntries = autoArchive; + Plugin.Configuration.Save(); + } + + if (autoArchive) + { + ImGui.Spacing(); + + var archiveLimit = Plugin.Configuration.ArchiveWhenEntriesExceed; + if (ImGui.InputInt("Limit", ref archiveLimit)) + { + Plugin.Configuration.ArchiveWhenEntriesExceed = archiveLimit; + Plugin.Configuration.Save(); + } + + ImGui.Spacing(); + + var archiveKeep = Plugin.Configuration.ArchiveKeepEntries; + if (ImGui.InputInt("Keep", ref archiveKeep)) + { + Plugin.Configuration.ArchiveKeepEntries = archiveKeep; + Plugin.Configuration.Save(); + } + + } } }