Files
pokerfreak/winui/Kern/Ansicht.cs
T
mmintelandClaude Opus 5.5 d2f8f1d9c3 Trainer im Cockpit: Datenschicht (Knöpfe, Ansicht trainer) und Reiter in WinUI, Rust, Swift
Plan 2026-11-19 trainer, Stufe 1 Schritte 5 und 6.

- sicht/trainer_einstellung.py: Vorgaben, erlaubte Werte, Prüfung mit deutschem Text,
  <treffpunkt>/trainer.json atomar und ohne BOM; eine Stelle für Cockpit, Ansicht und Trainer-Lauf
- sicht/steuern.py: trainer_starten (betrieb/trainer_lauf.py als Kind, PID in sicht/scratch/trainer.pid,
  TRAINER_JSON in der Umgebung), trainer_stoppen (sanft, dann hart), trainer_einstellen(daten)
- sicht/ansicht.py + quellen.py + server.py: Bereich trainer {laeuft, leer, text, einstellungen,
  optionen, abgleich neueste zuerst, sitzung}; ohne Trainer leerer Zustand, nie ein Fehler; ANSICHT.md
- WinUI: Seiten/TrainerSeite.cs + Reiter; Rust: src/trainer.rs + ui/trainer.slint + 5. Reiter,
  Test aus der Fixture; Swift: CockpitKern/TrainerModell.swift + Cockpit/Trainer.swift (ungebaut)
- Fixture ansicht.json (Rust = Swift, bytegleich) um den Block trainer erweitert

Specs: tests/trainer_steuern_spec.py (16), tests/trainer_ansicht_spec.py (12), cargo test (158),
dotnet build 0 Fehler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:52:28 +01:00

155 lines
6.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.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");
public bool Zuschauen => Zuschauer.Wahr("laeuft");
}
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);
}
}