Skip to content

Commit

Permalink
[feat] Add Auto Archive & Bump Version
Browse files Browse the repository at this point in the history
  • Loading branch information
DueDine committed Dec 11, 2024
1 parent aac50da commit 000b0bf
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 21 deletions.
7 changes: 6 additions & 1 deletion ContactsTracker/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ContactsTracker/ContactsTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<Version>0.0.0.2</Version>
<Version>0.0.0.3</Version>
<Description>Help you remember previous contacts.</Description>
<PackageProjectUrl>https://github.com/DueDine/ContactsTracker</PackageProjectUrl>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
Expand Down
18 changes: 10 additions & 8 deletions ContactsTracker/ContactsTracker.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
2 changes: 1 addition & 1 deletion ContactsTracker/DataEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 59 additions & 7 deletions ContactsTracker/Database.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using CsvHelper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ContactsTracker;

Expand Down Expand Up @@ -35,6 +37,7 @@ public static void Load()
if (content != null)
{
Entries = content;
Plugin.Logger.Debug("Database Loaded");
}
else
{
Expand All @@ -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<List<DataEntry>>(File.ReadAllText(dataPath));
if (records == null)
{
Expand All @@ -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<List<DataEntry>>(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!");
}

}

}
2 changes: 2 additions & 0 deletions ContactsTracker/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 40 additions & 3 deletions ContactsTracker/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -156,20 +156,26 @@ private void DrawSettingsTab()
Plugin.Configuration.Save();
}

ImGui.Spacing();

var recordSolo = Plugin.Configuration.RecordSolo;
if (ImGui.Checkbox("Record Solo", ref recordSolo))
{
Plugin.Configuration.RecordSolo = recordSolo;
Plugin.Configuration.Save();
}

ImGui.Spacing();

var printToChat = Plugin.Configuration.PrintToChat;
if (ImGui.Checkbox("Print To Chat", ref printToChat))
{
Plugin.Configuration.PrintToChat = printToChat;
Plugin.Configuration.Save();
}

ImGui.Spacing();

var onlyDutyRoulette = Plugin.Configuration.OnlyDutyRoulette;
if (ImGui.Checkbox("Only Record Duty Roulette", ref onlyDutyRoulette))
{
Expand All @@ -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();
}

}
}
}

0 comments on commit 000b0bf

Please sign in to comment.