Files
pokerfreak/winui/Kern/Spielplan.cs
T

331 lines
14 KiB
C#

using System.Text.Json.Nodes;
namespace Pokerfreak.Cockpit.Kern;
/// <summary>Was an einem Tisch gespielt wird (lesen/formate.py) - nicht der Modus (Uebung/Cash).</summary>
public enum SpielFormat { Cash, CashPlo, Aof, Bombpot, Turnier, Kpoker, Vip, Freeroll }
public static class Formate
{
public static readonly SpielFormat[] Alle = Enum.GetValues<SpielFormat>();
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<SpielFormat?>().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",
};
/// <summary>Segoe Fluent Icons je Format.</summary>
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 }
/// <summary>
/// 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).
/// </summary>
public sealed class Spielplan
{
/// <summary>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.</summary>
public static int Hoechstens { get; private set; } = int.MaxValue;
/// <summary>Aus der Ansicht uebernehmen (Cockpit.Abgleichen).</summary>
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<SpielFormat, int> _formate = new() { [SpielFormat.Cash] = 1 };
public HashSet<SpielFormat> Gemessen = new() { SpielFormat.Cash };
public HashSet<SpielFormat> Spielbar = new() { SpielFormat.Cash, SpielFormat.Aof };
private int? _ziel;
private List<SpielFormat> _reihenfolge = new();
public double? Budget;
private int? _tischeBis;
public bool Zeiger, Lehrer;
/// <summary>Die gewaehlten Stufen (Big Blinds, wichtigste zuerst) je Modus; leer: automatisch nach Bankroll.</summary>
private Dictionary<string, List<double>> _stufen = new();
public IReadOnlyList<double> Stufen => _stufen.TryGetValue(Cash ? "cash" : "uebung", out var r) ? r : Array.Empty<double>();
public void StufenSetzen(List<double> reihe) => _stufen[Cash ? "cash" : "uebung"] = reihe.Distinct().ToList();
/// <summary>Tische je Stufe (Owner 26.11.: "10 NL2 + 4 NL5" statt die Stufe nur an/aus); fehlt: frei.</summary>
private Dictionary<string, Dictionary<double, int>> _stufenTische = new();
public int? TischeJeStufe(double bb) =>
_stufenTische.TryGetValue(Cash ? "cash" : "uebung", out var je) && je.TryGetValue(bb, out var n) ? n : null;
public void TischeJeStufeSetzen(double bb, int? n)
{
var m = Cash ? "cash" : "uebung";
if (!_stufenTische.TryGetValue(m, out var je)) _stufenTische[m] = je = new();
if (n is int z) je[bb] = Math.Max(0, Math.Min(z, Hoechstens));
else je.Remove(bb);
}
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<Spielplan> 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;
/// <summary>Die Fuellfolge: genannte zuerst, dann der Rest in fester Reihenfolge.</summary>
public List<SpielFormat> Folge()
{
var genannt = _reihenfolge.Where(f => Anzahl(f) > 0).ToList();
var folge = new List<SpielFormat>(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<SpielFormat> reihe)
{
var vorgabe = Formate.Alle.Where(reihe.Contains).ToList();
_reihenfolge = reihe.SequenceEqual(vorgabe) ? new() : reihe;
}
/// <summary>kopf.spielplan des Servers -> Spielplan; null, wenn unlesbar.</summary>
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");
foreach (var m in new[] { "cash", "uebung" })
if (o.Obj("stufen")?[m] is JsonArray reihe)
p._stufen[m] = reihe.Select(x => x is JsonValue v && v.TryGetValue<double>(out var b) ? b : 0).Where(b => b > 0 || b is >= -4 and <= -1).ToList();
foreach (var m in new[] { "cash", "uebung" })
if (o.Obj("stufen_tische")?[m] is JsonObject je)
p._stufenTische[m] = je.Where(kv => double.TryParse(kv.Key, System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture, out var b) && b > 0
&& kv.Value is JsonValue v && v.TryGetValue<double>(out _))
.ToDictionary(kv => double.Parse(kv.Key, System.Globalization.CultureInfo.InvariantCulture),
kv => (int)kv.Value!.GetValue<double>());
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);
}
// Echtgeld-Turniere stehen in der Vorrang-Reihe (25.11.): das Alt-Format faellt weg, sein Typ wird Stufe
if (p.Cash && p.Anzahl(SpielFormat.Turnier) > 0)
{
var alt = p.Turnier?.Text("kategorie")?.ToLowerInvariant() == "freeroll" ? new[] { -1.0 } : new[] { -2.0, -3.0, -4.0 };
p._formate.Remove(SpielFormat.Turnier);
p._reihenfolge.Remove(SpielFormat.Turnier);
var nurTurniere = p._formate.Count == 0;
if (nurTurniere) p._formate[SpielFormat.Cash] = 1;
// ohne Cash-Wahl bliebe die Reihe sonst leer und die Cash-Automatik liefe nicht mehr
if (nurTurniere || p.Stufen.Count > 0) p.StufenSetzen(p.Stufen.Concat(alt).ToList());
p.Turnier = null;
}
if (mitListe && o["formate_liste"] is JsonArray liste)
{
var eintraege = liste.OfType<JsonObject>()
.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;
}
/// <summary>Die Daten fuer spielplan_setzen (betrieb/spielplan.py).</summary>
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;
// immer, auch leer: fehlt es, behaelt der Server die alte Reihe (aeltere Oberflaechen kennen sie nicht)
var st = new JsonObject();
foreach (var (m, r) in _stufen) st[m] = new JsonArray(r.Select(b => (JsonNode?)JsonValue.Create(b)).ToArray());
d["stufen"] = st;
// ebenso immer: leer heisst "keine Stufe begrenzt" (fehlt es, behaelt der Server die alten Zahlen)
var stt = new JsonObject();
foreach (var (m, je) in _stufenTische)
{
var o = new JsonObject();
foreach (var (sb, n) in je) o[sb.ToString(System.Globalization.CultureInfo.InvariantCulture)] = n;
stt[m] = o;
}
d["stufen_tische"] = stt;
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;
}
/// <summary>Titel und Fuss der Plan-Kachel: eine Vorlage (Zeit- UND Geldplan gleich) oder "Eigener"
/// mit der Kurzform des Zeitplans (Spielplan.swift `PlanKachel`).</summary>
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;
}