Skip to content

Commit

Permalink
feat: add history log
Browse files Browse the repository at this point in the history
  • Loading branch information
kalilistic committed May 28, 2021
1 parent f05784d commit 4f52431
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/SillyChat.Test/Mocks/FunSpeakPluginMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public SillyChatPluginMock()
PluginName = "SillyChat";
Configuration = new SillyChatConfigMock();
TranslationService = new TranslationService(this);
HistoryService = new HistoryService(this);
}

/// <inheritdoc />
Expand All @@ -23,6 +24,9 @@ public SillyChatPluginMock()
/// <inheritdoc />
public TranslationService TranslationService { get; }

/// <inheritdoc />
public HistoryService HistoryService { get; }

/// <inheritdoc />
public void SaveConfig()
{
Expand Down
2 changes: 2 additions & 0 deletions src/SillyChat.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=rotf/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=roxx/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=scallywags/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sillyhistory/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=smat/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=swoggler/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=t_0027gether/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Uaeio/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Urianger/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=we_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=yarr/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=yass/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
4 changes: 0 additions & 4 deletions src/SillyChat/SillyChat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,4 @@
<EmbeddedResource Include="SillyChat\Resource\translation\zh.json" />
</ItemGroup>

<ItemGroup>
<Folder Include="SillyChat\Service" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/SillyChat/SillyChat.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sillychat_005Cmodel/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sillychat_005Cplugin/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sillychat_005Cservice/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sillychat_005Cservice_005Chistoryservice/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sillychat_005Cservice_005Csampleservice/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sillychat_005Cservice_005Ctranslationservice/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sillychat_005Cservice_005Ctranslationservice_005Ctranslator/@EntryIndexedValue">True</s:Boolean>
Expand Down
10 changes: 10 additions & 0 deletions src/SillyChat/SillyChat/Configuration/SillyChatConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@ public abstract class SillyChatConfig
/// Gets or sets translation mode.
/// </summary>
public int TranslationMode { get; set; } = 4;

/// <summary>
/// Gets or sets timer to process intervals for history view.
/// </summary>
public int ProcessTranslationInterval { get; set; } = 300000;

/// <summary>
/// Gets or sets max number of translations to keep in history.
/// </summary>
public int TranslationHistoryMax { get; set; } = 30;
}
}
29 changes: 29 additions & 0 deletions src/SillyChat/SillyChat/Model/Translation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace SillyChat
{
/// <summary>
/// Translation records.
/// </summary>
public class Translation
{
/// <summary>
/// Initializes a new instance of the <see cref="Translation"/> class.
/// </summary>
/// <param name="input">Input text to be translated.</param>
/// <param name="output">Output text from translation.</param>
public Translation(string input, string output)
{
this.Input = input;
this.Output = output;
}

/// <summary>
/// Gets or sets input text to be translated.
/// </summary>
public string Input { get; set; }

/// <summary>
/// Gets or sets output text from translation.
/// </summary>
public string Output { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/SillyChat/SillyChat/Plugin/ISillyChatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public interface ISillyChatPlugin
/// </summary>
TranslationService TranslationService { get; }

/// <summary>
/// Gets history service.
/// </summary>
HistoryService HistoryService { get; }

/// <summary>
/// Save plugin configuration.
/// </summary>
Expand Down
20 changes: 17 additions & 3 deletions src/SillyChat/SillyChat/Plugin/SillyChatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public SillyChatPlugin(string pluginName, DalamudPluginInterface pluginInterface

// setup translator
this.TranslationService = new TranslationService(this);
this.HistoryService = new HistoryService(this);
pluginInterface.Framework.Gui.Chat.OnChatMessage += this.OnChatMessage;
this.common.Functions.BattleTalk.OnBattleTalk += this.OnBattleTalk;
this.common.Functions.Talk.OnTalk += this.OnTalk;
Expand All @@ -81,6 +82,11 @@ public SillyChatPlugin(string pluginName, DalamudPluginInterface pluginInterface
/// </summary>
public TranslationService TranslationService { get; private set; } = null!;

/// <summary>
/// Gets historyService.
/// </summary>
public HistoryService HistoryService { get; private set; } = null!;

/// <inheritdoc/>
public new string PluginName { get; set; } = null!;

Expand All @@ -103,6 +109,7 @@ public void SaveConfig()
/// </summary>
public new void Dispose()
{
this.HistoryService.Dispose();
this.common.Dispose();
base.Dispose();
this.WindowManager.Dispose();
Expand Down Expand Up @@ -156,8 +163,15 @@ private void Translate(SeString message)
{
if (payload is TextPayload textPayload)
{
if (string.IsNullOrEmpty(textPayload.Text) || textPayload.Text.Contains('\uE0BB')) continue;
textPayload.Text = this.TranslationService.Translate(textPayload.Text);
var input = textPayload.Text;
if (string.IsNullOrEmpty(input) || input.Contains('\uE0BB')) continue;
var output = this.TranslationService.Translate(input);
if (!input.Equals(output))
{
textPayload.Text = output;
Logger.LogDebug($"{input}|{output}");
this.HistoryService.AddTranslation(new Translation(input, output));
}
}
}
}
Expand All @@ -177,7 +191,7 @@ private void HandleFreshInstall()
this.Chat.PrintNotice(Loc.Localize("InstallThankYou", "Thank you for installing SillyChat!"));
Thread.Sleep(500);
this.Chat.PrintNotice(
Loc.Localize("Instructions", "You can use /silly to toggle the plugin and /sillyconfig to view settings."));
Loc.Localize("Instructions", "You can use /silly to toggle the plugin, /sillyconfig to view settings, and /sillyhistory to see previous translations."));
this.Configuration.FreshInstall = false;
this.SaveConfig();
this.WindowManager.ConfigWindow!.IsOpen = true;
Expand Down
24 changes: 22 additions & 2 deletions src/SillyChat/SillyChat/Resource/source/SillyChat_Localizable.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"description": "SillyChatPlugin.HandleFreshInstall"
},
"Instructions": {
"message": "You can use /silly to toggle the plugin and /sillyconfig to view settings.",
"message": "You can use /silly to toggle the plugin, /sillyconfig to view settings, and /sillyhistory to see previous translations.",
"description": "SillyChatPlugin.HandleFreshInstall"
},
"ToggleCommandHelp": {
"message": "Toggle SillyChat.",
"description": "<>c__DisplayClass1_0.<.ctor>b__0"
"description": "<>c__DisplayClass2_0.<.ctor>b__0"
},
"SettingsTitle": {
"message": "SillyChat Settings",
Expand All @@ -27,8 +27,28 @@
"message": "Translator",
"description": "ConfigWindow.Draw"
},
"HistoryTitle": {
"message": "SillyChat History",
"description": "HistoryWindow..ctor"
},
"SourceMessage": {
"message": "Source",
"description": "HistoryWindow.Draw"
},
"TranslationMessage": {
"message": "Translation",
"description": "HistoryWindow.Draw"
},
"NoTranslations": {
"message": "Nothing has been translated yet.",
"description": "HistoryWindow.Draw"
},
"ConfigCommandHelp": {
"message": "Show SillyChat config window.",
"description": "WindowManager..ctor"
},
"HistoryCommandHelp": {
"message": "Show SillyChat history window.",
"description": "WindowManager..ctor"
}
}
94 changes: 94 additions & 0 deletions src/SillyChat/SillyChat/Service/HistoryService/HistoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Timers;

using DalamudPluginCommon;

namespace SillyChat
{
/// <summary>
/// Manage history of translations.
/// </summary>
public class HistoryService
{
private readonly ISillyChatPlugin plugin;
private readonly Timer processTranslationsTimer;

/// <summary>
/// Initializes a new instance of the <see cref="HistoryService"/> class.
/// </summary>
/// <param name="plugin">SillyChat plugin.</param>
public HistoryService(ISillyChatPlugin plugin)
{
this.plugin = plugin;
this.Translations = new ConcurrentQueue<Translation>();
this.TranslationsDisplay = new List<Translation>();
this.processTranslationsTimer = new Timer
{
Interval = plugin.Configuration.ProcessTranslationInterval,
Enabled = true,
};
this.processTranslationsTimer.Elapsed += this.ProcessTranslationsTimerOnElapsed;
this.processTranslationsTimer.Start();
}

/// <summary>
/// Gets a value indicating whether gets indicator if history is being processed.
/// </summary>
public bool IsProcessing { get; private set; }

/// <summary>
/// Gets list of current historical records for display.
/// </summary>
public List<Translation> TranslationsDisplay { get; private set; }

/// <summary>
/// Gets queue for historical translations.
/// </summary>
public ConcurrentQueue<Translation> Translations { get; }

/// <summary>
/// Add translation to queue.
/// </summary>
/// <param name="translation">translation.</param>
public void AddTranslation(Translation translation)
{
this.Translations.Enqueue(translation);
if (!this.IsProcessing)
{
this.TranslationsDisplay.Add(translation);
}
}

/// <summary>
/// Dispose history service.
/// </summary>
public void Dispose()
{
this.processTranslationsTimer.Stop();
this.processTranslationsTimer.Elapsed -= this.ProcessTranslationsTimerOnElapsed;
}

private void ProcessTranslationsTimerOnElapsed(object sender, ElapsedEventArgs e)
{
try
{
this.IsProcessing = true;
while (this.Translations.Count > this.plugin.Configuration.TranslationHistoryMax)
{
this.Translations.TryDequeue(out _);
}

this.TranslationsDisplay = this.Translations.ToList();
this.IsProcessing = false;
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to process translations.");
this.IsProcessing = false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace SillyChat
{
/// <summary>
/// Sample service.
/// Orchestrate translation process.
/// </summary>
public class TranslationService
{
Expand Down
61 changes: 61 additions & 0 deletions src/SillyChat/SillyChat/UserInterface/HistoryWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Linq;

using CheapLoc;
using Dalamud.Interface.Colors;
using ImGuiNET;

namespace SillyChat
{
/// <summary>
/// History window for the plugin.
/// </summary>
public class HistoryWindow : PluginWindow
{
private readonly ISillyChatPlugin plugin;

/// <summary>
/// Initializes a new instance of the <see cref="HistoryWindow"/> class.
/// </summary>
/// <param name="plugin">SillyChat plugin.</param>
public HistoryWindow(ISillyChatPlugin plugin)
: base(plugin, Loc.Localize("HistoryTitle", "SillyChat History"))
{
this.plugin = plugin;
}

/// <inheritdoc/>
public override void Draw()
{
try
{
if (this.plugin.HistoryService.IsProcessing) return;
var translations = this.plugin.HistoryService.Translations.ToList();
if (translations.Count > 0)
{
ImGui.Columns(2);
ImGui.TextColored(ImGuiColors.HealerGreen, Loc.Localize("SourceMessage", "Source"));
ImGui.NextColumn();
ImGui.TextColored(ImGuiColors.DPSRed, Loc.Localize("TranslationMessage", "Translation"));
ImGui.NextColumn();
ImGui.Separator();
foreach (var translation in translations)
{
ImGui.TextWrapped(translation.Input);
ImGui.NextColumn();
ImGui.TextWrapped(translation.Output);
ImGui.NextColumn();
ImGui.Separator();
}
}
else
{
ImGui.Text(Loc.Localize("NoTranslations", "Nothing has been translated yet."));
}
}
catch
{
// ignored
}
}
}
}
Loading

0 comments on commit 4f52431

Please sign in to comment.