Files
pokerfreak/mac/Sources/CockpitKern/Spielplan.swift
T
mmintelandClaude Opus 5.5 f382095ba1 Mac cockpit: Features tab, cash/tournament switches, mini tables (WinUI parity)
- Features tab from GET /api/features (sicht/features.py): runner since,
  features on, changes pending a restart, missing programs, what the
  features bring, session vs session, details; polls every 10 s while open
- Cash and tournaments switched on/off separately in real money
  (spielplan "aus", always sent like WinUI); main button "Cash oder
  Turniere einschalten" when both rest
- Raster tiles become mini tables: felt, own cards, pot, coloured board or
  last own action, draining move clock

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

328 lines
16 KiB
Swift

import Foundation
/// Übung oder , wie viele Tische, (nur ) ein Budget, Zeitplan und Geldmodell - wie
/// betrieb/spielplan.py.
/// Das Cockpit schickt ihn mit `spielplan_setzen`, der Läufer füllt danach selbst auf.
public struct Spielplan: Equatable, Sendable, Decodable {
public enum Modus: String, Sendable, Decodable { case uebung, echt = "cash" }
/// Tischform: Multiway (Ring, Engine-Stärke) vs. Heads-Up (2 Plätze). Wie betrieb/spielplan.tischform.
public enum Tischform: String, Sendable, Decodable, CaseIterable {
case multiway, hu, beide
public var name: String { self == .multiway ? "Multiway" : self == .hu ? "Heads-Up" : "Beide" }
}
/// 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.
nonisolated(unsafe) public private(set) static var hoechstens = Int.max
/// Aus der Ansicht uebernehmen (Cockpit.abgleichen).
public static func hoechstensSetzen(_ n: Int?) {
if let n, n >= 1 { hoechstens = n }
}
public static let standard = Spielplan(modus: .uebung, tische: 1, budget: nil)
public var modus: Modus
/// Tische je Format (betrieb/spielplan.py `formate`); ohne Eintrag 0.
public private(set) var formate: [SpielFormat: Int]
/// Welche Formate der Läufer öffnen kann (kopf.spielplan.formate_liste) - nur vom Server, nie gesendet.
public var gemessen: Set<SpielFormat> = [.cash]
/// Welche Formate die Engine SPIELEN kann - MODUSABHÄNGIG (Bomb Pot/PLO5 nur zur Übung). Nur vom Server.
public var spielbar: Set<SpielFormat> = [.cash, .aof]
/// Tische insgesamt (Ausweichen): die Zahl je Format ist dann die Obergrenze. Ohne: die Summe zählt.
public var ziel: Int? {
didSet {
ziel = ziel.map { max(1, min(Self.hoechstens, $0)) }
tischeBis = Self.spanne(tische, tischeBis)
}
}
/// Füllfolge, wenn sie von der Vorgabe abweicht (betrieb/spielplan.py `reihenfolge`).
private var reihenfolge: [SpielFormat] = []
/// Alle Tische zusammen. Setzen ändert das Ziel oder sonst Cash (die anderen Formate bleiben).
public var tische: Int {
get {
let summe = formate.values.reduce(0, +)
return ziel.map { min($0, summe) } ?? summe
}
set {
if ziel != nil { ziel = newValue } else { setzen(.cash, newValue - (tische - anzahl(.cash))) }
}
}
/// Die gewählten Formate in Füllfolge.
public var folge: [SpielFormat] {
let genannt = reihenfolge.filter { anzahl($0) > 0 }
return genannt + SpielFormat.allCases.filter { anzahl($0) > 0 && !genannt.contains($0) }
}
public var budget: Double?
/// Tischspanne tische..tischeBis, je Sitzung gewürfelt; nil oder nicht über tische: keine Spanne.
public var tischeBis: Int? { didSet { tischeBis = Self.spanne(tische, tischeBis) } }
/// Der lebende Zeiger zwischen den Zügen (hand/zeiger.py) - kann nerven: Vorgabe aus.
public var zeiger = false
/// Die Lehrer-Stimme erklärt jeden Zug laut (betrieb/regie.py+sprecher.py) - UNABHÄNGIG von
/// Maus/Zeiger und Twitch (Owner 8.11.). Vorgabe aus.
public var lehrer = false
/// Multiway (Ring) oder Heads-Up (2 Plätze); Vorgabe Multiway (betrieb/spielplan.tischform).
public var tischform: Tischform = .multiway
/// Die gewaehlten Stufen je Modus ("cash"/"uebung"), wichtigste zuerst; leer: automatisch nach Bankroll.
/// Big Blinds > 0 sind Cash, -1 Freeroll bis -4 Turnier-Buy-ins (betrieb/stufenwahl.py). Ersetzt "Micro only".
private var stufenJe: [String: [Double]] = [:]
/// Tische je Stufe (Owner 26.11.: "10 NL2 + 4 NL5"); fehlt eine: frei.
private var stufenTischeJe: [String: [Double: Int]] = [:]
public var zeitplan = Zeitplan.ohne
public var geld = Geldplan()
/// Turnier-Auswahl: Buy-in-Band, Typ-Filter, Late-Reg (betrieb/turnierplan.py).
public var turnier = Turnierplan()
public var cash: Bool { modus == .echt }
/// Was im Echtgeld gerade ruht: "cash" und/oder "turniere" (betrieb/spielplan.ruht). Die Stufenwahl bleibt
/// stehen; der Laeufer liest es live (Owner 30.11.: Cash und Turniere getrennt steuern).
public var ruhend: Set<String> = []
public static let ruhendeArten: Set<String> = ["cash", "turniere"]
public func ruht(_ art: String) -> Bool { cash && ruhend.contains(art) }
public mutating func umschalten(_ art: String) {
guard Self.ruhendeArten.contains(art) else { return }
if ruhend.remove(art) == nil { ruhend.insert(art) }
}
/// Stehen Turniere in der Vorrang-Reihe (negative Schluessel, betrieb/stufenwahl.TURNIERE)?
public var hatTurniere: Bool { stufen.contains { $0 < 0 } }
private var stufenSchluessel: String { cash ? "cash" : "uebung" }
/// Die Stufen des jetzigen Modus nach Vorrang.
public var stufen: [Double] { stufenJe[stufenSchluessel] ?? [] }
public mutating func stufenSetzen(_ reihe: [Double]) {
var gesehen = Set<Double>()
stufenJe[stufenSchluessel] = reihe.filter { gesehen.insert($0).inserted }
}
public func tischeJeStufe(_ bb: Double) -> Int? { stufenTischeJe[stufenSchluessel]?[bb] }
public mutating func tischeJeStufeSetzen(_ bb: Double, _ n: Int?) {
var je = stufenTischeJe[stufenSchluessel] ?? [:]
je[bb] = n.map { max(0, min($0, Self.hoechstens)) }
stufenTischeJe[stufenSchluessel] = je
}
/// Ordnung "Höchste zuerst": Cash nach Big Blind, danach Turniere nach Buy-in (-1 Freeroll bis -4).
/// Als Vergleich, nicht als eine Zahl: 1e15 + bb (WinUI StufenPrio.Wert) kann NL2 und NL5 nicht mehr trennen.
public static func hoeherImVorrang(_ a: Double, _ b: Double) -> Bool {
if (a > 0) != (b > 0) { return a > 0 }
return a > 0 ? a > b : -a > -b
}
public static func hoechsteZuerst(_ reihe: [Double]) -> [Double] { reihe.sorted(by: hoeherImVorrang) }
public init(modus: Modus, tische: Int, budget: Double?) {
self.modus = modus
self.formate = [.cash: Self.klemmen(tische)]
self.budget = budget.flatMap { $0 > 0 ? $0 : nil }
}
private static func klemmen(_ n: Int) -> Int { max(1, min(hoechstens, n)) }
public func anzahl(_ f: SpielFormat) -> Int { formate[f] ?? 0 }
/// Tische eines Formats: nicht unter 0, die Summe nicht über `hoechstens`.
public mutating func setzen(_ f: SpielFormat, _ n: Int) {
let grenze = ziel != nil ? Self.hoechstens : Self.hoechstens - (tische - anzahl(f))
let neu = max(0, min(n, grenze))
let davor = folge
formate[f] = neu > 0 ? neu : nil
ordnen(davor.filter { anzahl($0) > 0 } + folge.filter { !davor.contains($0) })
tischeBis = Self.spanne(tische, tischeBis)
}
/// Ausweichen an: das Ziel ist die jetzige Tischzahl. Aus: die Summe zählt wieder, die Formate
/// hinten in der Füllfolge werden unter `hoechstens` gekürzt.
public mutating func ausweichen(an: Bool) {
if an {
ziel = max(1, tische)
return
}
let reihe = folge
ziel = nil
var rest = Self.hoechstens
for f in reihe {
let n = min(anzahl(f), rest)
formate[f] = n > 0 ? n : nil
rest -= n
}
tischeBis = Self.spanne(tische, tischeBis)
}
/// Ein Format in der Füllfolge einen Platz nach vorn.
public mutating func vorziehen(_ f: SpielFormat) {
var reihe = folge
guard let i = reihe.firstIndex(of: f), i > 0 else { return }
reihe.swapAt(i, i - 1)
ordnen(reihe)
}
/// Die Vorgabe (Folge wie `allCases`) bleibt leer - so vergleichen sich Pläne und die Datei bleibt schlank.
private mutating func ordnen(_ reihe: [SpielFormat]) {
reihenfolge = reihe == SpielFormat.allCases.filter(reihe.contains) ? [] : reihe
}
private static func spanne(_ von: Int, _ bis: Int?) -> Int? { bis.flatMap { $0 > von ? min($0, hoechstens) : nil } }
enum CodingKeys: String, CodingKey {
case modus, tische, budget, ziel, reihenfolge, zeiger, lehrer, tischform, tischeBis = "tische_bis", zeitplan, geld, formate
case stufen, stufenTische = "stufen_tische"
case turnier, aus
case formateListe = "formate_liste"
}
private struct FormatInfo: Decodable { var format: String; var gemessen: Bool; var spielbar: Bool? }
public init(from d: Decoder) throws {
let c = try d.container(keyedBy: CodingKeys.self)
guard let m = try? c.decode(Modus.self, forKey: .modus), let n = c.zahl(.tische) else {
throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "kein Spielplan"))
}
self.init(modus: m, tische: Int(n), budget: c.zahl(.budget))
ziel = c.zahl(.ziel).map { Int($0) }
tischeBis = Self.spanne(tische, c.zahl(.tischeBis).map { Int($0) })
zeiger = c.wahr(.zeiger)
lehrer = c.wahr(.lehrer)
if let roh = (try? c.decodeIfPresent([String: [Double]].self, forKey: .stufen)) ?? nil {
for m in ["cash", "uebung"] {
if let r = roh[m] { stufenJe[m] = r.filter { $0 > 0 || (-4 ... -1).contains($0) } }
}
}
if let roh = (try? c.decodeIfPresent([String: [String: Double]].self, forKey: .stufenTische)) ?? nil {
for m in ["cash", "uebung"] {
guard let je = roh[m] else { continue }
stufenTischeJe[m] = Dictionary(uniqueKeysWithValues: je.compactMap { k, v in
Double(k).flatMap { $0 > 0 ? ($0, Int(v)) : nil }
})
}
}
if let t = c.text(.tischform).flatMap(Tischform.init(rawValue:)) { tischform = t }
ruhend = Set(c.liste(.aus)).intersection(Self.ruhendeArten)
zeitplan = ((try? c.decodeIfPresent(Zeitplan.self, forKey: .zeitplan)) ?? nil) ?? .ohne
geld = ((try? c.decodeIfPresent(Geldplan.self, forKey: .geld)) ?? nil) ?? Geldplan()
turnier = ((try? c.decodeIfPresent(Turnierplan.self, forKey: .turnier)) ?? nil) ?? Turnierplan()
if let roh = (try? c.decodeIfPresent([String: Double].self, forKey: .formate)) ?? nil {
formate = [:]
for f in SpielFormat.allCases { if let n = roh[f.rawValue] { setzen(f, Int(n)) } }
// Alt-Pläne mit eigenen freeroll/vip-Formaten ins neue Modell falten: EINE Turnier-Kachel,
// der Typ steckt in turnier.kategorie (Owner 9.11.). Gesetzte Turnier-Zahl bleibt maßgeblich.
let extra = anzahl(.freeroll) + anzahl(.vip)
if extra > 0 {
let turnierNeu = anzahl(.turnier) > 0 ? anzahl(.turnier) : extra
formate[.freeroll] = nil
formate[.vip] = nil
formate[.turnier] = turnierNeu > 0 ? turnierNeu : nil
}
tischeBis = Self.spanne(tische, c.zahl(.tischeBis).map { Int($0) })
}
if let namen = (try? c.decodeIfPresent([String].self, forKey: .reihenfolge)) ?? nil {
let genannt = namen.compactMap(SpielFormat.init(rawValue:))
ordnen(genannt.filter { anzahl($0) > 0 } + folge.filter { !genannt.contains($0) })
}
// Echtgeld-Turniere stehen in der Vorrang-Reihe (25.11., wie WinUI): das Alt-Format faellt weg, sein
// Typ wird Stufe; ohne Cash-Wahl bliebe die Reihe sonst leer und die Cash-Automatik liefe nicht mehr.
if cash && anzahl(.turnier) > 0 {
let alt: [Double] = turnier.kategorie?.lowercased() == "freeroll" ? [-1] : [-2, -3, -4]
formate[.turnier] = nil
reihenfolge.removeAll { $0 == .turnier }
let nurTurniere = formate.isEmpty
if nurTurniere { formate[.cash] = 1 }
if nurTurniere || !stufen.isEmpty { stufenSetzen(stufen + alt) }
turnier = Turnierplan()
}
if let liste = (try? c.decodeIfPresent([FormatInfo].self, forKey: .formateListe)) ?? nil {
gemessen = Set(liste.filter(\.gemessen).compactMap { SpielFormat(rawValue: $0.format) })
if liste.contains(where: { $0.spielbar != nil }) { // nur, wenn der Server es schickt
spielbar = Set(liste.filter { $0.spielbar == true }.compactMap { SpielFormat(rawValue: $0.format) })
}
}
}
/// Für POST /api/steuern {aktion: "spielplan_setzen", daten}. Budget nur im Modus.
public var daten: [String: Any] {
var d: [String: Any] = ["modus": modus.rawValue, "tische": max(1, tische)]
if Set(formate.keys) != [.cash] { // nur Cash ist die Vorgabe und bleibt weg
d["formate"] = Dictionary(uniqueKeysWithValues: formate.map { ($0.key.rawValue, $0.value) })
}
if let ziel { d["ziel"] = ziel }
if !reihenfolge.isEmpty { d["reihenfolge"] = reihenfolge.map(\.rawValue) }
if cash, let budget { d["budget"] = budget }
if let tischeBis { d["tische_bis"] = tischeBis }
if zeiger { d["zeiger"] = true }
if lehrer { d["lehrer"] = true }
// immer, auch leer: fehlt es, behaelt der Server die alte Reihe bzw. die alten Zahlen
d["stufen"] = stufenJe
d["stufen_tische"] = stufenTischeJe.mapValues { je in
Dictionary(uniqueKeysWithValues: je.map { (Format.roh($0.key), $0.value) })
}
if turnier.gesetzt { d["turnier"] = turnier.daten }
d["aus"] = ruhend.sorted() // immer, auch leer: leer heisst beides an
d["tischform"] = tischform.rawValue // immer mitschicken (robuster Round-Trip, wie geld.reserve)
if zeitplan.aktiv { d["zeitplan"] = zeitplan.daten }
if geld != Geldplan() { d["geld"] = geld.daten }
return d
}
public var datenText: String {
(try? JSONSerialization.data(withJSONObject: daten, options: [.sortedKeys]))
.flatMap { String(data: $0, encoding: .utf8) } ?? "{}"
}
}
public struct GeldRegeln: Decodable, Equatable, Sendable {
public var buyinUebung: String?
public var buyinEcht: String?
public var nachkaufUnterBB: Double?
public var aufstehenUnterBB: Double?
public var stopTischBB: Double?
public var stopSitzungBB: Double?
public var anteilJeTisch: Double?
public var hoechstensTische: Int?
public var ratholing = false
public var modell: String?
public var kauf: String?
public var stakesBB: Double?
public var stops: [String] = []
enum CodingKeys: String, CodingKey {
case modell, kauf, stops, stakesBB = "stakes_bb"
case buyinUebung = "buyin_uebung", buyinEcht = "buyin_echt", nachkaufUnterBB = "nachkauf_unter_bb"
case aufstehenUnterBB = "aufstehen_unter_bb", stopTischBB = "stop_tisch_bb", stopSitzungBB = "stop_sitzung_bb"
case anteilJeTisch = "anteil_je_tisch", hoechstensTische = "hoechstens_tische", ratholing
}
public init(from d: Decoder) throws {
let c = try d.container(keyedBy: CodingKeys.self)
buyinUebung = c.text(.buyinUebung); buyinEcht = c.text(.buyinEcht); nachkaufUnterBB = c.zahl(.nachkaufUnterBB)
aufstehenUnterBB = c.zahl(.aufstehenUnterBB); stopTischBB = c.zahl(.stopTischBB); stopSitzungBB = c.zahl(.stopSitzungBB)
anteilJeTisch = c.zahl(.anteilJeTisch); hoechstensTische = c.zahl(.hoechstensTische).map { Int($0) }
ratholing = c.wahr(.ratholing)
modell = c.text(.modell); kauf = c.text(.kauf); stakesBB = c.zahl(.stakesBB); stops = c.liste(.stops)
}
}
public struct GeldStand: Decodable, Equatable, Sendable {
public struct Tisch: Decodable, Equatable, Sendable, Identifiable {
public var raum: String
public var nettoBB: Double?
public var id: String { raum }
enum CodingKeys: String, CodingKey { case raum, nettoBB = "netto_bb" }
public init(from d: Decoder) throws {
let c = try d.container(keyedBy: CodingKeys.self)
raum = c.text(.raum) ?? "?"; nettoBB = c.zahl(.nettoBB)
}
}
public var sitzungBB: Double?
public var seit: Double?
public var tische: [Tisch] = []
enum CodingKeys: String, CodingKey { case sitzungBB = "sitzung_bb", seit, tische }
public init(from d: Decoder) throws {
let c = try d.container(keyedBy: CodingKeys.self)
sitzungBB = c.zahl(.sitzungBB); seit = c.zahl(.seit); tische = c.objekte(.tische)
}
}