2026-11-08 14:53:03 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace GladiatorIdle.Net
|
|
|
|
|
{
|
|
|
|
|
/// Ein Spieler in der Bestenliste: die besten Werte aus allen seinen Leben.
|
|
|
|
|
internal sealed record LeaderEntry(string Id, string Name, string Class, int Level, double Power, int Freedoms,
|
|
|
|
|
double FastestFreedom, int Arena, string Date);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-11-08 14:59:52 +01:00
|
|
|
/// Online-Bestenliste über die eigene Firebase-Datenbank (gladiatoridle), Bereich
|
|
|
|
|
/// <c>players</c>: ein Eintrag je Spielprofil (zufällige ID in
|
2026-11-08 14:53:03 +01:00
|
|
|
/// %AppData%\GladiatorIdle\player.txt), der bei Fortschritt überschrieben wird. Ohne Netz oder ohne
|
|
|
|
|
/// Freigabe in den Datenbank-Regeln bleibt die Liste leer ("offline"). Testläufe senden nie.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static class Leaderboard
|
|
|
|
|
{
|
2026-11-08 14:59:52 +01:00
|
|
|
const string BASE = "https://gladiatoridle-default-rtdb.europe-west1.firebasedatabase.app/players";
|
2026-11-08 14:53:03 +01:00
|
|
|
static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(12) };
|
|
|
|
|
static string? _lastSent;
|
|
|
|
|
|
|
|
|
|
public static string PlayerId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (DevShot.Active || DevShot.Playtest) return "dev"; // Testläufe legen nichts an
|
|
|
|
|
string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GladiatorIdle");
|
|
|
|
|
string file = Path.Combine(dir, "player.txt");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(file) && File.ReadAllText(file).Trim() is { Length: >= 8 } id) return id;
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
string fresh = Guid.NewGuid().ToString("N");
|
|
|
|
|
File.WriteAllText(file, fresh);
|
|
|
|
|
return fresh;
|
|
|
|
|
}
|
|
|
|
|
catch { return "local"; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Eintrag aus dem Spielstand: bestes aus laufendem Leben und allen Legenden.
|
|
|
|
|
public static LeaderEntry FromGame(GameState g)
|
|
|
|
|
{
|
|
|
|
|
double power = g.Legends.Select(l => l.Power).Append(g.Power()).Max();
|
|
|
|
|
int level = g.Legends.Select(l => l.Level).Append(g.CharLevel).Max();
|
|
|
|
|
double fastest = g.Legends.Count > 0 ? g.Legends.Min(l => l.RunSeconds) : 0;
|
|
|
|
|
int arena = g.Legends.Count > 0 ? ArenaDef.All.Length : g.Arena + 1;
|
|
|
|
|
string name = (g.Name ?? "").Trim();
|
|
|
|
|
if (name.Length > 24) name = name[..24];
|
|
|
|
|
return new LeaderEntry(PlayerId, name, g.Class.ToString(), level, Math.Round(power), g.Freedoms, Math.Round(fastest), arena,
|
|
|
|
|
DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Eigenen Eintrag hochladen (im Hintergrund, Fehler still). Nichts bei Testläufen oder ohne Änderung.
|
|
|
|
|
public static void Submit(GameState g)
|
|
|
|
|
{
|
|
|
|
|
if (DevShot.Active || DevShot.Playtest || string.IsNullOrWhiteSpace(g.Name)) return;
|
|
|
|
|
var e = FromGame(g);
|
|
|
|
|
string json = JsonSerializer.Serialize(new
|
|
|
|
|
{
|
|
|
|
|
name = e.Name, @class = e.Class, level = e.Level, power = e.Power, freedoms = e.Freedoms,
|
|
|
|
|
fastest = e.FastestFreedom, arena = e.Arena, date = e.Date
|
|
|
|
|
});
|
|
|
|
|
string key = json[..json.LastIndexOf("\"date\"", StringComparison.Ordinal)];
|
|
|
|
|
if (key == _lastSent) return;
|
|
|
|
|
_lastSent = key;
|
|
|
|
|
_ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try { await Http.PutAsync($"{BASE}/{e.Id}.json", new StringContent(json, Encoding.UTF8, "application/json")); }
|
|
|
|
|
catch { /* offline */ }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Alle Einträge laden; null bei Fehler (offline oder keine Freigabe).
|
|
|
|
|
public static async Task<List<LeaderEntry>?> FetchAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var resp = await Http.GetAsync(BASE + ".json");
|
|
|
|
|
if (!resp.IsSuccessStatusCode) return null;
|
|
|
|
|
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
|
|
|
|
|
var list = new List<LeaderEntry>();
|
|
|
|
|
if (doc.RootElement.ValueKind != JsonValueKind.Object) return list;
|
|
|
|
|
foreach (var p in doc.RootElement.EnumerateObject())
|
|
|
|
|
{
|
|
|
|
|
var v = p.Value;
|
|
|
|
|
if (v.ValueKind != JsonValueKind.Object) continue;
|
|
|
|
|
string S(string k) => v.TryGetProperty(k, out var x) && x.ValueKind == JsonValueKind.String ? x.GetString() ?? "" : "";
|
|
|
|
|
double N(string k) => v.TryGetProperty(k, out var x) && x.ValueKind == JsonValueKind.Number ? x.GetDouble() : 0;
|
|
|
|
|
string name = S("name");
|
|
|
|
|
if (name.Length == 0) continue;
|
|
|
|
|
list.Add(new LeaderEntry(p.Name, name.Length > 24 ? name[..24] : name, S("class"), (int)N("level"), N("power"),
|
|
|
|
|
(int)N("freedoms"), N("fastest"), (int)N("arena"), S("date")));
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
catch { return null; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|