Load up to 1000 emotes

This commit is contained in:
Infi
2024-05-11 06:47:21 +02:00
parent fa41d22cb8
commit 050485fece
2 changed files with 47 additions and 20 deletions
+15 -4
View File
@@ -15,6 +15,9 @@ public static class EmoteCache
{
[JsonPropertyName("emote")]
public Emote Emote { get; set; }
[JsonPropertyName("id")]
public string Id { get; set; }
}
public struct Emote
@@ -29,7 +32,7 @@ public static class EmoteCache
public bool Animated { get; set; }
};
private enum LoadingState
public enum LoadingState
{
Unloaded,
Loading,
@@ -39,12 +42,13 @@ public static class EmoteCache
private const string BetterTTV = "https://api.betterttv.net/3";
private const string GlobalEmotes = $"{BetterTTV}/cached/emotes/global";
private const string Top100Emotes = $"{BetterTTV}/emotes/shared/top?limit=100";
private const string Top100BeforeEmotes = "{0}/emotes/shared/top?before={1}&limit=100";
private const string EmotePath = "https://cdn.betterttv.net/emote/{0}/3x";
private static readonly HttpClient Client = new();
// All of this data is uninitalized while State is not `LoadingState.Done`
private static LoadingState State = LoadingState.Unloaded;
public static LoadingState State = LoadingState.Unloaded;
private static Dictionary<string, IEmote> EmoteImages = new();
private static Dictionary<string, Emote> Cache = new();
@@ -65,12 +69,19 @@ public static class EmoteCache
foreach (var emote in JsonSerializer.Deserialize<Emote[]>(globalList)!)
Cache.TryAdd(emote.Code, emote);
var top = await Client.GetAsync(Top100Emotes);
var lastId = string.Empty;
for (var i = 0; i < 10; i++)
{
var top = await Client.GetAsync(lastId == string.Empty ? Top100Emotes : Top100BeforeEmotes.Format(BetterTTV, lastId));
var topList = await top.Content.ReadAsStringAsync();
foreach (var emote in JsonSerializer.Deserialize<List<Top100>>(topList)!)
var jsonList = JsonSerializer.Deserialize<List<Top100>>(topList)!;
foreach (var emote in jsonList)
Cache.TryAdd(emote.Emote.Code, emote.Emote);
lastId = jsonList.Last().Id;
}
EmoteCodeArray = Cache.Keys.ToArray();
State = LoadingState.Done;
}
+17 -1
View File
@@ -2,6 +2,7 @@ using System.Numerics;
using ChatTwo.Resources;
using ChatTwo.Util;
using Dalamud.Interface;
using Dalamud.Interface.Colors;
using Dalamud.Interface.Utility.Raii;
using ImGuiNET;
@@ -44,7 +45,8 @@ internal sealed class Emote : ISettingsTab
if (SearchSelector.SelectorPopup("WordAddPopup", out var newWord, WordPopupOptions))
Mutable.BlockedEmotes.Add(newWord);
using var table = ImRaii.Table("##BlockedWords", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInner);
using(var table = ImRaii.Table("##BlockedWords", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInner))
{
if (table)
{
ImGui.TableSetupColumn(Language.Options_Emote_EmoteTable);
@@ -63,6 +65,20 @@ internal sealed class Emote : ISettingsTab
Mutable.BlockedEmotes.Remove(word);
}
}
}
ImGui.Spacing();
ImGui.Separator();
ImGui.Spacing();
if (EmoteCache.State is EmoteCache.LoadingState.Done)
ImGui.TextColored(ImGuiColors.HealerGreen, "Ready");
else
ImGui.TextColored(ImGuiColors.DPSRed, "Not Ready");
ImGui.TextUnformatted($"Emotes loaded: {EmoteCache.EmoteCodeArray.Length}");
foreach (var word in EmoteCache.EmoteCodeArray)
ImGui.TextUnformatted(word);
ImGui.PopTextWrapPos();
}