// RÆGROK — THE HÆRTH leaderboard sync for Streamer.bot (Execute C# Code sub-action) // Paste into: Action → Sub-Actions → Core → C# → Execute C# Code // References tab: make sure System.Net.Http.dll is listed (right-click → Add reference from file if missing). // Triggers: Core → Timed Actions (600s, enabled) AND Twitch → Stream → Stream Offline. // Full guide: https://raegrok.com/guides/obs-streamerbot-subathon-architecture/ using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; public class CPHInline { // ===== EDIT THESE ===================================================== private const string Endpoint = "https://raegrok.com/api/leaderboard"; // your deployed URL private const string BearerToken = "PASTE_YOUR_LEADERBOARD_SECRET_HERE"; // or set global var "hearthLeaderboardSecret" private const string PointsVar = "points"; // persisted Twitch user variable that holds currency private const string WatchTimeVar = ""; // optional persisted user var in MINUTES (leave "" to skip) private const string CurrencyName = "Gold"; private const int TopN = 100; // API caps at 500 // ===================================================================== private static readonly HttpClient Http = new HttpClient { Timeout = TimeSpan.FromSeconds(15) }; public bool Execute() { string token = CPH.GetGlobalVar("hearthLeaderboardSecret", true); if (string.IsNullOrWhiteSpace(token)) token = BearerToken; var points = CPH.GetTwitchUsersVar(PointsVar, true); if (points == null || points.Count == 0) { CPH.LogInfo("[HÆRTH] No users with points yet."); return true; } var watch = new Dictionary(StringComparer.OrdinalIgnoreCase); if (!string.IsNullOrEmpty(WatchTimeVar)) { foreach (var w in CPH.GetTwitchUsersVar(WatchTimeVar, true) ?? new List>()) watch[w.UserLogin] = w.Value; } var top = points .Where(p => p.Value > 0 && !string.IsNullOrEmpty(p.UserName)) .OrderByDescending(p => p.Value) .Take(TopN) .ToList(); var sb = new StringBuilder(); sb.Append("{\"currencyName\":").Append(Json(CurrencyName)) .Append(",\"updatedAt\":").Append(Json(DateTime.UtcNow.ToString("o"))) .Append(",\"entries\":["); for (int i = 0; i < top.Count; i++) { var u = top[i]; if (i > 0) sb.Append(','); sb.Append("{\"rank\":").Append(i + 1) .Append(",\"username\":").Append(Json(u.UserName)) .Append(",\"points\":").Append(u.Value); long mins; if (watch.TryGetValue(u.UserLogin, out mins)) sb.Append(",\"watchTime\":").Append(Json(string.Format("{0}h {1}m", mins / 60, mins % 60))); sb.Append('}'); } sb.Append("]}"); try { var req = new HttpRequestMessage(HttpMethod.Post, Endpoint); req.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token); req.Content = new StringContent(sb.ToString(), Encoding.UTF8, "application/json"); var res = Http.SendAsync(req).GetAwaiter().GetResult(); string body = res.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (!res.IsSuccessStatusCode) { CPH.LogWarn("[HÆRTH] Sync failed " + (int)res.StatusCode + ": " + body); return false; } CPH.LogInfo("[HÆRTH] Synced " + top.Count + " viewers. " + body); return true; } catch (Exception ex) { CPH.LogError("[HÆRTH] Sync error: " + ex.Message); return false; } } private static string Json(string s) { if (s == null) return "null"; var b = new StringBuilder("\""); foreach (char c in s) { switch (c) { case '"': b.Append("\\\""); break; case '\\': b.Append("\\\\"); break; case '\n': b.Append("\\n"); break; case '\r': b.Append("\\r"); break; case '\t': b.Append("\\t"); break; default: if (c < 0x20) b.AppendFormat("\\u{0:x4}", (int)c); else b.Append(c); break; } } return b.Append('"').ToString(); } }