Files
pokerfreak/winui/Kern/Ansicht.cs
T
mmintelandClaude Opus 5.5 b5d997a12a WinUI-Cockpit: Einstellungen wie die Mac-App, Leiste am Rand, --nur-spiel, Windows-Stimme
- Einstellungen: Lehrer & Stimme (Anbieter-Formulare), Plan-Editor Zeiten & Geld (Vorlagen,
  Feineinstellung, Geld-Stopps), Streamer (Twitch/Chat/Moderation); Plan-Kachel oeffnet ihn
- Absturz beim Klick auf eine Vorlage behoben (Json.ZahlVon las selbst gesetzte long nicht)
- Leiste wie das Mac-Panel: volle Hoehe am Rand, ueber allem, schnappt nach dem Ziehen an den
  naechsten Rand, Restbreite neben den Kacheln (cockpit_ziel.json), Ausweichen fuer Laeufer-Klicks;
  Cockpit-Seite in Allgemein; cockpit.json mit form leiste/seite/mindestbreite/zeit
- Tisch-Desktop an: das Cockpit zieht mit hinueber und dockt nur dort (statt an alle Desktops)
- Zahnrad als vierte Systemtaste buendig vor Minimieren/Maximieren/Schliessen
- --nur-spiel / POKERFREAK_NUR_SPIEL=1: nur Cash Game und Turnier (ohne Zuschauer,
  Sonderformate, Lehrer & Stimme, Streamer, Maus-Schalter, Klon-Abzeichen)
- Stimme: unter Windows "Windows" (System.Speech) statt macOS, MP3 ueber MediaPlayer;
  Specs plattformneutral (anbieter, anbieter_knoepfe, sprecher gruen)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:22:27 +01:00

153 lines
6.2 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");
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);
}
}