// VoteScript for .NET 6+. Server side only. using System; using System.Collections.Generic; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; public sealed class VoteScriptClient { private static readonly HashSet Types = new("vip group permission title teleport storage until unlock xp boost effect outfit mount spell badge run custom".Split(' ')); public sealed record Item(string key, string name, long amount) { /// "vip" and ["gold"] for vip/gold; "item" and the key for a plain key. Check the parts before you use them. public (string type, string[] args) Parse() { string[] p = key.Split('/'); if (p.Length < 2 || !Types.Contains(p[0])) return ("item", new[] { key }); return (p[0], p[0] == "custom" ? new[] { key[7..] } : p[1..]); } } public sealed record Answer(bool ok, string? error, int votes, List? items, int held = 0); private static readonly HttpClient Http = new(new HttpClientHandler { AllowAutoRedirect = false }) { Timeout = TimeSpan.FromSeconds(8) }; private readonly string _base, _key; private readonly string _types; /// What you can give, such as "item", "vip/gold". Rewards with anything else stay waiting. None: everything is handed out. public VoteScriptClient(string url, string key, params string[] types) { _base = url.TrimEnd('/') + "/index.php?r=api/v1/"; _key = key; _types = string.Join(",", types); } /// Claims everything waiting. Store reference before calling and reuse it on retry. public Task ClaimAsync(string player, string? reference = null) => PostAsync("claim", new() { ["player"] = player, ["ref"] = reference ?? Guid.NewGuid().ToString() }); public Task ClaimCodeAsync(string player, string code, string? reference = null) => PostAsync("claim-code", new() { ["player"] = player, ["code"] = code, ["ref"] = reference ?? Guid.NewGuid().ToString() }); private async Task PostAsync(string method, Dictionary form) { Exception? last = null; if (_types != "") form["types"] = _types; for (int i = 0; i < 3; i++) { if (i > 0) await Task.Delay(700 * i); try { using var req = new HttpRequestMessage(HttpMethod.Post, _base + method) { Content = new FormUrlEncodedContent(form) }; req.Headers.Add("X-Api-Key", _key); using var res = await Http.SendAsync(req); string text = await res.Content.ReadAsStringAsync(); if ((int)res.StatusCode >= 500) { last = new HttpRequestException("VoteScript answered " + (int)res.StatusCode); continue; } var answer = JsonSerializer.Deserialize(text)!; if (!res.IsSuccessStatusCode) throw new InvalidOperationException(answer.error); return answer; } catch (Exception e) when (e is HttpRequestException or TaskCanceledException) { last = e; } } throw last!; } }