OrangeGuidanceTomestone/client/Ui/MainWindowTabs/MessageList.cs

168 lines
5.4 KiB
C#
Raw Normal View History

2022-09-07 05:46:30 +00:00
using Dalamud.Game.Text.SeStringHandling.Payloads;
2022-09-07 06:05:13 +00:00
using Dalamud.Interface;
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; }
2022-09-07 05:46:30 +00:00
private SortMode Sort { get; set; }
2022-09-04 05:03:59 +00:00
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-07 05:46:30 +00:00
ImGui.SameLine();
if (ImGui.BeginCombo("Sort", $"{this.Sort}")) {
foreach (var mode in Enum.GetValues<SortMode>()) {
if (ImGui.Selectable($"{mode}", mode == this.Sort)) {
this.Sort = mode;
}
}
ImGui.EndCombo();
}
2022-09-04 05:03:59 +00:00
this.MessagesMutex.Wait();
2023-01-22 03:42:03 +00:00
try {
this.ShowList();
} finally {
this.MessagesMutex.Release();
}
}
2022-09-04 05:03:59 +00:00
2023-01-22 03:42:03 +00:00
private void ShowList() {
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-07 05:46:30 +00:00
if (ImGui.BeginChild("##messages-list")) {
var messages = this.Messages;
if (this.Sort != SortMode.Date) {
messages = messages.ToList();
messages.Sort((a, b) => {
return this.Sort switch {
SortMode.Date => 0,
2022-09-07 05:47:31 +00:00
SortMode.Appraisals => Math.Max(b.PositiveVotes - b.NegativeVotes, 0)
.CompareTo(Math.Max(a.PositiveVotes - a.NegativeVotes, 0)),
SortMode.Likes => b.PositiveVotes.CompareTo(a.PositiveVotes),
SortMode.Dislikes => b.NegativeVotes.CompareTo(a.NegativeVotes),
2022-09-07 05:58:55 +00:00
SortMode.Location => a.Territory.CompareTo(b.Territory),
2022-09-07 05:46:30 +00:00
_ => throw new ArgumentOutOfRangeException(),
};
});
2022-09-04 05:10:48 +00:00
}
2022-09-04 05:15:27 +00:00
2022-09-07 05:46:30 +00:00
foreach (var message in messages) {
var territory = this.Plugin.DataManager.GetExcelSheet<TerritoryType>()?.GetRow(message.Territory);
var territoryName = territory?.PlaceName.Value?.Name?.ToDalamudString().TextValue ?? "???";
ImGui.TextUnformatted(message.Text);
ImGui.TreePush();
ImGui.TextUnformatted($"Location: {territoryName}");
ImGui.SameLine();
2022-09-09 10:19:14 +00:00
if (ImGuiHelper.SmallIconButton(FontAwesomeIcon.MapMarkerAlt, $"{message.Id}") && territory != null) {
2022-09-10 14:47:37 +00:00
this.Plugin.GameGui.OpenMapWithMapLink(new MapLinkPayload(
territory.RowId,
territory.Map.Row,
(int) (message.X * 1_000),
(int) (message.Z * 1_000)
));
2022-09-07 05:46:30 +00:00
}
2022-09-09 10:19:14 +00:00
if (message.IsHidden) {
2022-09-09 10:27:17 +00:00
ImGuiHelper.WarningText("This message will not be shown to other players due to its low score.");
2022-09-09 10:19:14 +00:00
}
2022-09-07 06:05:13 +00:00
var ctrl = ImGui.GetIO().KeyCtrl;
2022-09-07 05:46:30 +00:00
var appraisals = Math.Max(0, message.PositiveVotes - message.NegativeVotes);
ImGui.TextUnformatted($"Appraisals: {appraisals:N0} ({message.PositiveVotes:N0} - {message.NegativeVotes:N0})");
2022-09-07 06:05:13 +00:00
if (!ctrl) {
ImGui.BeginDisabled();
}
2022-09-07 05:46:30 +00:00
if (ImGui.Button($"Delete##{message.Id}")) {
this.Delete(message.Id);
}
2022-09-07 06:05:13 +00:00
if (!ctrl) {
ImGui.EndDisabled();
}
ImGui.SameLine();
2022-09-09 10:19:14 +00:00
ImGuiHelper.HelpIcon("Hold Ctrl to enable the delete button.");
2022-09-07 06:05:13 +00:00
2022-09-07 05:46:30 +00:00
ImGui.TreePop();
ImGui.Separator();
}
2022-09-04 05:03:59 +00:00
}
2022-09-07 05:46:30 +00:00
ImGui.EndChild();
2022-09-04 05:03:59 +00:00
}
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();
2023-01-22 03:42:03 +00:00
try {
this.Plugin.Ui.MainWindow.ExtraMessages = messages.Extra;
this.Messages.Clear();
this.Messages.AddRange(messages.Messages);
} finally {
this.MessagesMutex.Release();
}
2022-09-04 05:03:59 +00:00
});
}
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-07 05:46:30 +00:00
private enum SortMode {
Date,
Appraisals,
Likes,
Dislikes,
Location,
}
2022-09-04 05:03:59 +00:00
}