Files
pokerfreak/winui/Kern/Ansicht.cs
T
mmintelandClaude Opus 5.5 0452484efa WinUI: Sammeln mit Zuschauzeit, Pause, stillen Tischen, Must-Show und Knopf-Zwischenstand
- SammlerTeil: "Höchstens je Tisch" (5er-Schritte, 0 = ohne Grenze), "Danach Pause für den Tisch",
  "Stille Tische zu nach", Must-Show zuerst|nur|egal; gilt sofort, auch im Lauf (sammler_wahl_setzen).
- Hauptknopf: "Sammeln stoppen · 5/16", "Sammeln startet …", "Sammeln stoppt … noch 7 offen" (ohne Klick).
  Sammelt bleibt wahr, solange er noch schliesst - vorher zeigte der Knopf dann schon "Sammeln starten".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 17:38:17 +01:00

164 lines
7.0 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.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace Pokerfreak.Cockpit.Kern;
/// <summary>
/// Die Ansicht des Servers (sicht/ANSICHT.md), nachsichtig gelesen: fehlt ein Feld oder hat es den
/// falschen Typ, ist es leer - nie ein Fehler. Die Bereiche bleiben JSON; die App liest, was sie zeigt.
/// </summary>
public sealed class Ansicht
{
public JsonObject Teile { get; }
public Ansicht(JsonObject teile) => Teile = teile;
public static Ansicht Leer { get; } = new(new JsonObject());
public JsonObject? Kopf => Teile.Obj("kopf");
public JsonObject? Jetzt => Teile.Obj("jetzt");
public JsonObject? Wachstum => Teile.Obj("wachstum");
public JsonObject? Entscheidungen => Teile.Obj("entscheidungen");
public JsonObject? Geld => Teile.Obj("geld");
public JsonObject? Sitzen => Teile.Obj("sitzen");
public JsonObject? Stil => Teile.Obj("stil");
public JsonObject? Ticker => Teile.Obj("ticker");
public JsonObject? Zuschauer => Teile.Obj("zuschauer");
/// <summary>Der Trainer an der Buehne (sicht/ANSICHT.md **trainer**).</summary>
public JsonObject? Trainer => Teile.Obj("trainer");
public bool Laeuft => Kopf.Wahr("laeuft");
/// <summary>Der Sammler (sicht/ansicht.py `sammler_`): sieht fremden Tischen zu, setzt sich nie.</summary>
public JsonObject? Sammler => Zuschauer.Obj("sammler");
/// <summary>startet | laeuft | stoppt | aus (sicht/steuern.py `sammler_zustand`); aelterer Server: aus "laeuft".</summary>
public string SammlerPhase => Sammler.Text("phase") ?? (Sammler.Wahr("laeuft") ? "laeuft" : "aus");
/// <summary>Er sammelt - auch solange er startet oder beim Stopp noch seine Tische schliesst (sonst
/// zeigte der Knopf schon "Sammeln starten", waehrend noch Tische offen sind).</summary>
public bool Sammelt => Sammler.Wahr("laeuft") || SammlerPhase is "startet" or "stoppt";
/// <summary>Der Zuschauer-Reiter ist an: der Log-Leser laeuft oder der Sammler sammelt (Owner 20.11.:
/// Zuschauer und Sammeln sind eins).</summary>
public bool Zuschauen => Zuschauer.Wahr("laeuft") || Sammelt;
}
public static class Json
{
public static JsonObject? Obj(this JsonObject? o, string k) => o?[k] as JsonObject;
public static JsonArray Liste(this JsonObject? o, string k) => o?[k] as JsonArray ?? new JsonArray();
public static IEnumerable<JsonObject> Objekte(this JsonObject? o, string k) =>
o.Liste(k).OfType<JsonObject>();
public static IEnumerable<string> Texte(this JsonObject? o, string k) =>
o.Liste(k).Select(TextVon).Where(t => t != null)!;
public static string? Text(this JsonObject? o, string k) => TextVon(o?[k]);
public static string T(this JsonObject? o, string k) => o.Text(k) ?? "";
public static string? TextVon(JsonNode? n) => n is JsonValue v
? v.GetValueKind() switch
{
JsonValueKind.String => v.GetValue<string>(),
JsonValueKind.Number => ZahlVon(v)?.ToString(CultureInfo.InvariantCulture),
JsonValueKind.True => "true",
JsonValueKind.False => "false",
_ => null,
}
: null;
/// <summary>Zahl aus einem Knoten. Vom Server gelesene Werte sind JsonElement; selbst gesetzte
/// (JsonValue.Create(long/int)) lassen sich nicht als double holen, daher alle Arten probieren.</summary>
public static double? ZahlVon(JsonNode? n)
{
if (n is not JsonValue v || v.GetValueKind() != JsonValueKind.Number) return null;
if (v.TryGetValue<double>(out var d)) return d;
if (v.TryGetValue<long>(out var l)) return l;
if (v.TryGetValue<int>(out var i)) return i;
if (v.TryGetValue<decimal>(out var m)) return (double)m;
if (v.TryGetValue<float>(out var f)) return f;
return double.TryParse(v.ToJsonString(), NumberStyles.Float, CultureInfo.InvariantCulture, out var t) ? t : null;
}
public static double? Zahl(this JsonObject? o, string k) => ZahlVon(o?[k]);
public static bool Wahr(this JsonObject? o, string k) =>
o?[k] is JsonValue v && v.GetValueKind() == JsonValueKind.True;
/// <summary>true/false, null wenn das Feld fehlt oder kein Wahrheitswert ist.</summary>
public static bool? WahrOderNull(this JsonObject? o, string k) =>
o?[k] is JsonValue v ? v.GetValueKind() switch
{
JsonValueKind.True => true,
JsonValueKind.False => false,
_ => null,
} : null;
/// <summary>Gleicher Inhalt? (Kacheln nur neu bauen, wenn sich ihr JSON aendert.)</summary>
public static string Abdruck(this JsonNode? n) => n?.ToJsonString() ?? "null";
}
/// <summary>Zahlen und Zeiten, wie die App sie zeigt: deutsch, Strich fuer unbekannt (format.rs).</summary>
public static class Format
{
private static readonly CultureInfo De = CultureInfo.GetCultureInfo("de-DE");
public static string Zahl(double? x, int stellen = 0)
{
if (x is not double v || !double.IsFinite(v)) return "–";
var s = Math.Round(v, stellen, MidpointRounding.AwayFromZero).ToString("#,0." + new string('#', stellen), De);
return s == "-0" ? "0" : s;
}
public static string Auto(double? x) => x is double v
? Zahl(v, Math.Abs(v) >= 100 ? 0 : Math.Abs(v) >= 10 ? 1 : 2)
: "–";
/// <summary>Achsenzahl: 1200 -> "1,2k", sonst bis eine Nachkommastelle.</summary>
public static string Kurzzahl(double v) => Math.Abs(v) >= 1000
? Zahl(v / 1000, 1) + "k"
: Zahl(v, Math.Abs(v) >= 10 ? 0 : 1);
public static string Prozent(double? x) => x is double v ? $"{Zahl(v * 100)} %" : "–";
public static string Vorzeichen(double? x, int stellen = 1)
{
if (x is not double v) return "–";
var t = Zahl(Math.Abs(v), stellen);
return v > 0 ? "+" + t : v < 0 ? "−" + t : t;
}
public static string Dauer(double s)
{
var g = (long)Math.Max(0, s);
return g >= 3600 ? $"{g / 3600} h {g % 3600 / 60} min" : g >= 60 ? $"{g / 60} min" : $"{g} s";
}
public static DateTime Ortszeit(double epoche) =>
DateTimeOffset.FromUnixTimeMilliseconds((long)(epoche * 1000)).LocalDateTime;
public static string Uhr(double? t) => t is double v ? Ortszeit(v).ToString("HH:mm:ss") : "–";
public static string Kurz(double? t) => t is double v ? Ortszeit(v).ToString("HH:mm") : "";
public static double Jetzt() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0;
/// <summary>"Qh" -> ("Q♥", rot); "Th" -> "10♥". Unlesbares: null.</summary>
public static (string Text, bool Rot)? Karte(string? s)
{
if (s is not { Length: 2 }) return null;
var (symbol, rot) = char.ToLowerInvariant(s[1]) switch
{
'h' => ("♥", true),
'd' => ("♦", true),
's' => ("♠", false),
'c' => ("♣", false),
_ => ("", false),
};
var r = char.ToUpperInvariant(s[0]);
if (symbol == "" || !"23456789TJQKA".Contains(r)) return null;
return ((r == 'T' ? "10" : r.ToString()) + symbol, rot);
}
}