using System.Text.Json.Nodes; namespace Pokerfreak.Cockpit.Kern; /// Was an einem Tisch gespielt wird (lesen/formate.py) - nicht der Modus (Uebung/Cash). public enum SpielFormat { Cash, CashPlo, Aof, Bombpot, Turnier, Kpoker, Vip, Freeroll } public static class Formate { public static readonly SpielFormat[] Alle = Enum.GetValues(); public static string Roh(this SpielFormat f) => f switch { SpielFormat.Cash => "cash", SpielFormat.CashPlo => "cash_plo", SpielFormat.Aof => "aof", SpielFormat.Bombpot => "bombpot", SpielFormat.Turnier => "turnier", SpielFormat.Kpoker => "kpoker", SpielFormat.Vip => "vip", _ => "freeroll", }; public static SpielFormat? AusRoh(string? s) => Alle.Cast().FirstOrDefault(f => f!.Value.Roh() == s); public static string Name(this SpielFormat f) => f switch { SpielFormat.Cash => "Cash NLH", SpielFormat.CashPlo => "Cash PLO", SpielFormat.Aof => "All-In or Fold", SpielFormat.Bombpot => "Bomb Pot", SpielFormat.Turnier => "Turnier", SpielFormat.Kpoker => "K-Poker", SpielFormat.Vip => "VIP Arena", _ => "Freerolls", }; /// Segoe Fluent Icons je Format. public static string Zeichen(this SpielFormat f) => f switch { SpielFormat.Cash => "", // Kreis SpielFormat.CashPlo => "", // Liste SpielFormat.Aof => "", // Blitz SpielFormat.Bombpot => "", // Energie SpielFormat.Turnier => "", // Fahne SpielFormat.Kpoker => "", // Tastatur SpielFormat.Vip => "", // Stern _ => "", // Geschenk }; } public enum Modus { Uebung, Echt } public enum Tischform { Multiway, Hu, Beide } /// /// Der Spielplan, wie das Cockpit ihn einstellt und vom Server liest (cockpit/src/spielplan.rs, /// betrieb/spielplan.py): Uebung oder Cash, Tische je Format, Budget, Ziel (Ausweichen), Fuellfolge. /// Zeit- und Geldplan liegen als JSON und werden typisiert gelesen/geschrieben (Kern/Plan.cs). /// public sealed class Spielplan { /// DIE Tisch-Obergrenze - keine eigene Zahl: der Server schickt sie (kopf.tische_hoechstens, /// lesen/geld.py HOECHSTENS_TISCHE). Bis dahin kappt die App nicht; der Server kappt jeden Plan. public static int Hoechstens { get; private set; } = int.MaxValue; /// Aus der Ansicht uebernehmen (Cockpit.Abgleichen). public static void HoechstensSetzen(double? n) { if (n is double d && d >= 1) Hoechstens = (int)Math.Min(d, int.MaxValue); } public Modus Modus = Modus.Uebung; private readonly SortedDictionary _formate = new() { [SpielFormat.Cash] = 1 }; public HashSet Gemessen = new() { SpielFormat.Cash }; public HashSet Spielbar = new() { SpielFormat.Cash, SpielFormat.Aof }; private int? _ziel; private List _reihenfolge = new(); public double? Budget; private int? _tischeBis; public bool Zeiger, Lehrer, MicroOnly; public Tischform Tischform = Tischform.Multiway; public JsonObject? Zeitplan, Geld, Turnier; public static Spielplan Neu(Modus m, int tische, double? budget = null) { var p = new Spielplan { Modus = m, Budget = budget > 0 ? budget : null }; p._formate[SpielFormat.Cash] = Klemmen(tische); return p; } public Spielplan Kopie() => Aus(Daten(), mitListe: false) is { } p ? p.Mit(x => { x.Modus = Modus; x.Gemessen = new(Gemessen); x.Spielbar = new(Spielbar); x.Budget = Budget; }) : Neu(Modus, 1); private Spielplan Mit(Action a) { a(this); return this; } private static int Klemmen(int n) => Math.Clamp(n, 1, Hoechstens); public bool Cash => Modus == Modus.Echt; public int Anzahl(SpielFormat f) => _formate.TryGetValue(f, out var n) ? n : 0; public int Tische { get { var summe = _formate.Values.Sum(); return _ziel is int z ? Math.Min(z, summe) : summe; } } public void TischeSetzen(int n) { if (_ziel != null) { ZielSetzen(n); return; } var rest = Tische - Anzahl(SpielFormat.Cash); Setzen(SpielFormat.Cash, n - rest); } public int? Ziel => _ziel; public void ZielSetzen(int? z) { _ziel = z is int v ? Klemmen(v) : null; _tischeBis = Spanne(Tische, _tischeBis); } public int? TischeBis => _tischeBis; public void TischeBisSetzen(int? bis) => _tischeBis = Spanne(Tische, bis); private static int? Spanne(int von, int? bis) => bis is int b && b > von ? Math.Min(b, Hoechstens) : null; /// Die Fuellfolge: genannte zuerst, dann der Rest in fester Reihenfolge. public List Folge() { var genannt = _reihenfolge.Where(f => Anzahl(f) > 0).ToList(); var folge = new List(genannt); folge.AddRange(Formate.Alle.Where(f => Anzahl(f) > 0 && !genannt.Contains(f))); return folge; } public void Setzen(SpielFormat f, int n) { var grenze = _ziel != null ? Hoechstens : Hoechstens - (Tische - Anzahl(f)); var neu = Math.Max(0, Math.Min(n, grenze)); var davor = Folge(); if (neu > 0) _formate[f] = neu; else _formate.Remove(f); var reihe = davor.Where(x => Anzahl(x) > 0).ToList(); reihe.AddRange(Folge().Where(x => !davor.Contains(x))); Ordnen(reihe); _tischeBis = Spanne(Tische, _tischeBis); } public void Ausweichen(bool an) { if (an) { ZielSetzen(Math.Max(1, Tische)); return; } var reihe = Folge(); _ziel = null; var rest = Hoechstens; foreach (var f in reihe) { var n = Math.Min(Anzahl(f), rest); if (n > 0) _formate[f] = n; else _formate.Remove(f); rest -= n; } _tischeBis = Spanne(Tische, _tischeBis); } public void Vorziehen(SpielFormat f) { var reihe = Folge(); var i = reihe.IndexOf(f); if (i <= 0) return; (reihe[i], reihe[i - 1]) = (reihe[i - 1], reihe[i]); Ordnen(reihe); } private void Ordnen(List reihe) { var vorgabe = Formate.Alle.Where(reihe.Contains).ToList(); _reihenfolge = reihe.SequenceEqual(vorgabe) ? new() : reihe; } /// kopf.spielplan des Servers -> Spielplan; null, wenn unlesbar. public static Spielplan? Aus(JsonObject? o, bool mitListe = true) { if (o == null) return null; var modus = o.Text("modus") switch { "uebung" => Modus.Uebung, "cash" => Modus.Echt, _ => (Modus?)null }; if (modus == null || o.Zahl("tische") is not double tische) return null; var p = Neu(modus.Value, (int)tische, o.Zahl("budget")); p.ZielSetzen(o.Zahl("ziel") is double z ? (int)z : null); p.TischeBisSetzen(o.Zahl("tische_bis") is double b ? (int)b : null); p.Zeiger = o.Wahr("zeiger"); p.Lehrer = o.Wahr("lehrer"); p.MicroOnly = o.Wahr("micro_only"); p.Tischform = o.Text("tischform") switch { "hu" => Tischform.Hu, "beide" => Tischform.Beide, _ => Tischform.Multiway, }; p.Zeitplan = o.Obj("zeitplan")?.DeepClone() as JsonObject; p.Geld = o.Obj("geld")?.DeepClone() as JsonObject; p.Turnier = o.Obj("turnier")?.DeepClone() as JsonObject; if (o.Obj("formate") is JsonObject roh) { p._formate.Clear(); foreach (var f in Formate.Alle) if (roh.Zahl(f.Roh()) is double n) p.Setzen(f, (int)n); // Alt-Plaene mit freeroll/vip: EINE Turnier-Kachel, der Typ steckt in turnier.kategorie var extra = p.Anzahl(SpielFormat.Freeroll) + p.Anzahl(SpielFormat.Vip); if (extra > 0) { var neu = p.Anzahl(SpielFormat.Turnier) > 0 ? p.Anzahl(SpielFormat.Turnier) : extra; p._formate.Remove(SpielFormat.Freeroll); p._formate.Remove(SpielFormat.Vip); if (neu > 0) p._formate[SpielFormat.Turnier] = neu; } p._tischeBis = Spanne(p.Tische, o.Zahl("tische_bis") is double b2 ? (int)b2 : null); } if (o["reihenfolge"] is JsonArray namen) { var genannt = namen.Select(Json.TextVon).Select(Formate.AusRoh).Where(f => f != null).Select(f => f!.Value).ToList(); var reihe = genannt.Where(x => p.Anzahl(x) > 0).ToList(); reihe.AddRange(p.Folge().Where(x => !genannt.Contains(x))); p.Ordnen(reihe); } if (mitListe && o["formate_liste"] is JsonArray liste) { var eintraege = liste.OfType() .Select(e => (F: Formate.AusRoh(e.Text("format")), G: e.WahrOderNull("gemessen"), S: e.WahrOderNull("spielbar"))) .Where(e => e.F != null && e.G != null).ToList(); p.Gemessen = eintraege.Where(e => e.G == true).Select(e => e.F!.Value).ToHashSet(); if (eintraege.Any(e => e.S != null)) p.Spielbar = eintraege.Where(e => e.S == true).Select(e => e.F!.Value).ToHashSet(); } return p; } /// Die Daten fuer spielplan_setzen (betrieb/spielplan.py). public JsonObject Daten() { var d = new JsonObject { ["modus"] = Cash ? "cash" : "uebung", ["tische"] = Math.Max(1, Tische), }; if (!(_formate.Count == 1 && _formate.ContainsKey(SpielFormat.Cash))) { var m = new JsonObject(); foreach (var (k, v) in _formate) m[k.Roh()] = v; d["formate"] = m; } if (_ziel is int z) d["ziel"] = z; if (_reihenfolge.Count > 0) d["reihenfolge"] = new JsonArray(_reihenfolge.Select(f => (JsonNode)f.Roh()).ToArray()); if (Cash && Budget is double b) d["budget"] = b == Math.Truncate(b) ? (JsonNode)(long)b : b; if (_tischeBis is int bis) d["tische_bis"] = bis; if (Zeiger) d["zeiger"] = true; if (Lehrer) d["lehrer"] = true; if (MicroOnly) d["micro_only"] = true; if (Turnier != null) d["turnier"] = Turnier.DeepClone(); d["tischform"] = Tischform switch { Tischform.Hu => "hu", Tischform.Beide => "beide", _ => "multiway" }; if (Zeitplan is { Count: > 0 }) d["zeitplan"] = Zeitplan.DeepClone(); if (Geld is { Count: > 0 }) d["geld"] = Geld.DeepClone(); return d; } /// Titel und Fuss der Plan-Kachel: eine Vorlage (Zeit- UND Geldplan gleich) oder "Eigener" /// mit der Kurzform des Zeitplans (Spielplan.swift `PlanKachel`). public (string Titel, string Fuss) PlanText() => PlanVorlagen.Erkennen(this) is PlanVorlage v ? (v.Titel(), v.Untertitel()) : ("Eigener", Kern.Zeitplan.Aus(Zeitplan).Kurz); public bool Unendlich => Kern.Zeitplan.Aus(Zeitplan).Sitzung == null; public int ReserveBuyins => Geldplan.Aus(Geld).ReserveBuyins; public bool KaufMax => Geldplan.Aus(Geld).KaufMax; }