OrangeGuidanceTomestone/client/Ui/MainWindowTabs/MessageList.cs

88 lines
2.7 KiB
C#
Raw Normal View History

2022-09-04 07:12:51 +00:00
using Dalamud.Utility;
2022-09-04 05:03:59 +00:00
using ImGuiNET;
2022-09-04 07:12:51 +00:00
using Lumina.Excel.GeneratedSheets;
2022-09-04 05:03:59 +00:00
using Newtonsoft.Json;
using OrangeGuidanceTomestone.Helpers;
namespace OrangeGuidanceTomestone.Ui.MainWindowTabs;
internal class MessageList : ITab {
public string Name => "Your messages";
private Plugin Plugin { get; }
private SemaphoreSlim MessagesMutex { get; } = new(1, 1);
private List<MessageWithTerritory> Messages { get; } = new();
internal MessageList(Plugin plugin) {
this.Plugin = plugin;
}
2022-09-06 07:37:03 +00:00
public void Dispose() {
}
2022-09-04 05:03:59 +00:00
public void Draw() {
if (ImGui.Button("Refresh")) {
this.Refresh();
}
2022-09-04 05:10:48 +00:00
2022-09-04 05:03:59 +00:00
this.MessagesMutex.Wait();
2022-09-05 08:21:45 +00:00
ImGui.TextUnformatted($"Messages: {this.Messages.Count:N0} / {10 + this.Plugin.Ui.MainWindow.ExtraMessages:N0}");
ImGui.Separator();
2022-09-04 05:03:59 +00:00
foreach (var message in this.Messages) {
2022-09-04 07:12:51 +00:00
var territory = this.Plugin.DataManager.GetExcelSheet<TerritoryType>()?.GetRow(message.Territory);
var territoryName = territory?.PlaceName.Value?.Name?.ToDalamudString().TextValue ?? "???";
2022-09-04 05:03:59 +00:00
ImGui.TextUnformatted(message.Text);
2022-09-04 05:15:27 +00:00
ImGui.TreePush();
2022-09-04 07:12:51 +00:00
ImGui.TextUnformatted($"Location: {territoryName}");
var appraisals = Math.Max(0, message.PositiveVotes - message.NegativeVotes);
2022-09-04 07:19:48 +00:00
ImGui.TextUnformatted($"Appraisals: {appraisals:N0} ({message.PositiveVotes:N0} - {message.NegativeVotes:N0})");
2022-09-04 05:10:48 +00:00
if (ImGui.Button($"Delete##{message.Id}")) {
this.Delete(message.Id);
}
2022-09-04 05:15:27 +00:00
ImGui.TreePop();
ImGui.Separator();
2022-09-04 05:03:59 +00:00
}
this.MessagesMutex.Release();
}
private void Refresh() {
Task.Run(async () => {
var resp = await ServerHelper.SendRequest(
this.Plugin.Config.ApiKey,
HttpMethod.Get,
2022-09-05 08:21:45 +00:00
"/messages?v=2"
2022-09-04 05:03:59 +00:00
);
var json = await resp.Content.ReadAsStringAsync();
2022-09-05 08:21:45 +00:00
var messages = JsonConvert.DeserializeObject<MyMessages>(json)!;
2022-09-04 05:03:59 +00:00
await this.MessagesMutex.WaitAsync();
2022-09-05 08:21:45 +00:00
this.Plugin.Ui.MainWindow.ExtraMessages = messages.Extra;
2022-09-04 05:03:59 +00:00
this.Messages.Clear();
2022-09-05 08:21:45 +00:00
this.Messages.AddRange(messages.Messages);
2022-09-04 05:03:59 +00:00
this.MessagesMutex.Release();
});
}
2022-09-04 05:10:48 +00:00
private void Delete(Guid id) {
Task.Run(async () => {
var resp = await ServerHelper.SendRequest(
this.Plugin.Config.ApiKey,
HttpMethod.Delete,
$"/messages/{id}"
);
if (resp.IsSuccessStatusCode) {
this.Refresh();
2022-09-04 05:15:27 +00:00
this.Plugin.Vfx.RemoveStatic(id);
2022-09-04 05:19:30 +00:00
this.Plugin.Messages.Remove(id);
2022-09-04 05:10:48 +00:00
}
});
}
2022-09-04 05:03:59 +00:00
}