Files
GladiatorIdle/Mono/Arena/LeaderboardScene.cs
2026-11-08 14:53:03 +01:00

173 lines
9.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using GladiatorIdle.Gfx;
using GladiatorIdle.Net;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using D = GladiatorIdle.Gfx.Draw;
namespace GladiatorIdle.Arena
{
/// <summary>
/// Bestenliste (aus dem Hauptmenü): Siegertreppchen für die drei Besten, darunter die Plätze 4–15.
/// Zwei Wertungen: stärkste Gladiatoren (beste Kampfkraft) und schnellste Freiheit. Der eigene
/// Eintrag leuchtet; steht er nicht unter den ersten 15, erscheint er unter der Liste.
/// </summary>
internal sealed class LeaderboardScene
{
enum Tab { Power, Fastest }
readonly Ui _ui = new();
readonly Particles _fx = new();
readonly Texture2D? _backdrop = Assets.Sprite("arena_colosseum", "jpg");
readonly string _me = Leaderboard.PlayerId;
List<LeaderEntry>? _all;
bool _loading = true, _offline, _dev;
Tab _tab;
double _t;
public bool Done { get; private set; }
public LeaderboardScene(GameState? game)
{
if (game != null) Leaderboard.Submit(game); // eigenen Stand zuerst aktualisieren
Reload();
}
void Reload()
{
_loading = true;
_ = Leaderboard.FetchAsync().ContinueWith(t =>
{
if (_dev) return;
_all = t.Result;
_offline = _all == null;
_loading = false;
});
}
/// Testläufe: Beispieleinträge statt der Datenbank (sendet und lädt nichts).
public void DevSample(GameState? game)
{
_dev = true;
var rnd = new Random(5);
string[] names = { "Spiculus", "Flamma", "Priscus", "Verus", "Carpophorus", "Tetraites", "Crixus", "Oenomaus", "Gannicus", "Varro", "Hilarus", "Celadus" };
_all = names.Select((n, i) => new LeaderEntry("dev" + i, n, ((GladiatorClass)(i % 4)).ToString(), 70 - i * 4, Math.Round(4e9 / Math.Pow(1.9, i)),
Math.Max(0, 6 - i / 2), 7200 + rnd.Next(9000), 7, "")).ToList();
if (game != null) _all.Add(Leaderboard.FromGame(game) with { Id = _me });
_loading = false; _offline = false;
}
public void Update(float dt, Input input, Matrix ui)
{
_t += dt;
_ui.Begin(input, ui);
_fx.Motes(AppSettings.ReduceMotion ? 0 : 40, Camera.W, Camera.H);
_fx.Update(dt);
if (input.Pressed(Keys.Escape)) Done = true;
}
List<LeaderEntry> Ranked() => _all == null ? new() : _tab == Tab.Power
? _all.Where(e => e.Power > 0).OrderByDescending(e => e.Power).ThenBy(e => e.Name).ToList()
: _all.Where(e => e.FastestFreedom > 0).OrderBy(e => e.FastestFreedom).ThenBy(e => e.Name).ToList();
string Value(LeaderEntry e) => _tab == Tab.Power ? NumberFormat.Format(e.Power) : NumberFormat.Duration(e.FastestFreedom);
string ClassName(LeaderEntry e) => Enum.TryParse<GladiatorClass>(e.Class, out var c) ? L.t(ClassDef.Of(c).Key) : e.Class;
// ── Zeichnen ────────────────────────────────────────────────
public void Draw(SpriteBatch sb, float scale, Vector2 offset)
{
D.UiScale = scale;
var ui = Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
if (_backdrop != null)
{
float k = 1.08f * MathF.Max(Camera.W / _backdrop.Width, Camera.H / _backdrop.Height);
sb.Draw(_backdrop, new Vector2(Camera.W / 2, Camera.H / 2), null, new Color(0.3f, 0.27f, 0.24f), 0,
new Vector2(_backdrop.Width / 2f, _backdrop.Height / 2f), k, SpriteEffects.None, 0);
}
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp, null, null, null, ui);
_fx.DrawAdd(sb);
D.Glow(sb, new Vector2(Camera.W / 2, 120), new Vector2(1600, 400), D.Gold * 0.1f);
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
D.Text(sb, Assets.Serif, 72, L.t("lb.title").ToUpperInvariant(), new Vector2(Camera.W / 2, 80), D.Gold, Align.Center);
float tw = 360;
if (_ui.Button(sb, new RectangleF(Camera.W / 2 - tw - 10, 140, tw, 56), L.t("lb.tab.power"), selected: _tab == Tab.Power, size: 22)) _tab = Tab.Power;
if (_ui.Button(sb, new RectangleF(Camera.W / 2 + 10, 140, tw, 56), L.t("lb.tab.fastest"), selected: _tab == Tab.Fastest, size: 22)) _tab = Tab.Fastest;
if (_loading) D.Text(sb, Assets.Serif, 30, L.t("lb.loading"), new Vector2(Camera.W / 2, 520), D.Sand, Align.Center);
else if (_offline)
{
D.Text(sb, Assets.Serif, 30, L.t("lb.offline"), new Vector2(Camera.W / 2, 500), D.Red, Align.Center);
if (_ui.Button(sb, new RectangleF(Camera.W / 2 - 160, 560, 320, 56), L.t("lb.retry"), size: 22)) Reload();
}
else
{
var list = Ranked();
if (list.Count == 0) D.Text(sb, Assets.Serif, 28, L.t(_tab == Tab.Power ? "lb.empty" : "lb.empty.fastest"), new Vector2(Camera.W / 2, 520), D.Sand, Align.Center);
else DrawRanking(sb, list);
}
if (_ui.Button(sb, new RectangleF(60, Camera.H - 100, 260, 60), L.t("lb.back"), size: 22)) Done = true;
sb.End();
}
void DrawRanking(SpriteBatch sb, List<LeaderEntry> list)
{
// Siegertreppchen: 2 – 1 – 3
Color[] medal = { D.Gold, new Color(205, 212, 225), new Color(205, 140, 90) };
int[] order = { 1, 0, 2 };
float[] heights = { 175, 215, 150 };
for (int s = 0; s < 3; s++)
{
int i = order[s];
if (i >= list.Count) continue;
var e = list[i];
float w = 360, x = Camera.W / 2 - 1.5f * w - 20 + s * (w + 20), h = heights[s], baseY = 480;
var r = new RectangleF(x, baseY - h, w, h);
D.Rect(sb, r, new Color(30, 22, 15) * 0.92f);
D.Frame(sb, r, e.Id == _me ? D.Purple : medal[i], e.Id == _me ? 4 : 3);
D.Text(sb, Assets.Serif, 44, (i + 1).ToString(NumberFormat.Culture), new Vector2(r.Center.X, r.Y + 30), medal[i], Align.Center);
D.Text(sb, Assets.Serif, 28, e.Name, new Vector2(r.Center.X, r.Y + 72), Color.White, Align.Center);
if (h >= 170) D.Text(sb, Assets.Sans, 17, $"{ClassName(e)} · {string.Format(L.t("ludus.level"), e.Level)}", new Vector2(r.Center.X, r.Y + 104), D.Sand, Align.Center); // i18n-ok
D.Text(sb, Assets.SansBold, 24, Value(e), new Vector2(r.Center.X, r.Bottom - 26), medal[i], Align.Center);
}
// Plätze 4–15
float x0 = Camera.W / 2 - 560, y = 512, rowH = 36, wAll = 1120;
D.Text(sb, Assets.Sans, 15, L.t("lb.col.rank"), new Vector2(x0 + 20, y), D.Dim);
D.Text(sb, Assets.Sans, 15, L.t("lb.col.name"), new Vector2(x0 + 90, y), D.Dim);
D.Text(sb, Assets.Sans, 15, L.t("lb.col.class"), new Vector2(x0 + 470, y), D.Dim);
D.Text(sb, Assets.Sans, 15, L.t("lb.col.freedoms"), new Vector2(x0 + 760, y), D.Dim, Align.Center);
D.Text(sb, Assets.Sans, 15, L.t(_tab == Tab.Power ? "lb.tab.power" : "lb.tab.fastest"), new Vector2(x0 + wAll - 20, y), D.Dim, Align.Right);
y += 26;
for (int i = 3; i < Math.Min(15, list.Count); i++) { Row(sb, list[i], i, x0, y, wAll, rowH); y += rowH; }
int mine = list.FindIndex(e => e.Id == _me);
if (mine >= 15) { y += 10; Row(sb, list[mine], mine, x0, y, wAll, rowH); }
if (mine >= 0)
D.Text(sb, Assets.Sans, 17, string.Format(L.t("lb.yourrank"), mine + 1, list.Count), new Vector2(Camera.W - 60, Camera.H - 70), D.Purple, Align.Right);
}
void Row(SpriteBatch sb, LeaderEntry e, int i, float x0, float y, float w, float h)
{
bool me = e.Id == _me;
var r = new RectangleF(x0, y, w, h - 4);
D.Rect(sb, r, (me ? new Color(60, 40, 80) : new Color(30, 22, 15)) * 0.9f);
if (me) D.Frame(sb, r, D.Purple, 2);
float cy = r.Center.Y;
D.Text(sb, Assets.SansBold, 19, (i + 1).ToString(NumberFormat.Culture), new Vector2(x0 + 20, cy), D.Sand);
D.Text(sb, Assets.SansBold, 19, me ? $"{e.Name} ({L.t("lb.you")})" : e.Name, new Vector2(x0 + 90, cy), me ? D.Purple : Color.White); // i18n-ok
D.Text(sb, Assets.Sans, 17, $"{ClassName(e)} · {string.Format(L.t("ludus.level"), e.Level)}", new Vector2(x0 + 470, cy), D.Sand); // i18n-ok
D.Text(sb, Assets.Sans, 17, e.Freedoms.ToString(NumberFormat.Culture), new Vector2(x0 + 760, cy), D.Sand, Align.Center);
D.Text(sb, Assets.SansBold, 19, Value(e), new Vector2(x0 + w - 20, cy), D.Gold, Align.Right);
}
}
}