import Foundation // Die fertig gerechneten Anzeigen der Datenschicht (sicht/abgeleitet.py, wie in der WinUI): die App // zeichnet sie nur. Texte kommen fertig formatiert, Farben als Namen ("gruen", "rot", ... "" = Text). // Nur den Countdown rechnet die App selbst aus `zug.bis`. /// `sitzen.live`: Tisch-Chips im Raster, je Tisch eine Kachel, ohne Tisch der Weg zum Spielen, die Suche. public struct LiveTische: Decodable, Equatable, Sendable { public var anzahl = 0 /// Tische je Bereich (Schalter Cash / Turniere). public var cashTische = 0 public var turnierTische = 0 /// Erst ab zwei Tischen (sonst leer); `spalte_nr`/`zeile_nr` ordnen das Raster. public var chips: [LiveChip] = [] public var kacheln: [LiveKachelDaten] = [] public var bereit: LiveBereit? public var schliessenFrage: String? public var such = 0 public var suchTitel = "" public var suchText = "" enum CodingKeys: String, CodingKey { case anzahl, chips, kacheln, bereit, schliessenFrage = "schliessen_frage", such case cashTische = "cash_tische", turnierTische = "turnier_tische" case suchTitel = "such_titel", suchText = "such_text" } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) anzahl = c.zahl(.anzahl).map { Int($0) } ?? 0 cashTische = c.zahl(.cashTische).map { Int($0) } ?? 0 turnierTische = c.zahl(.turnierTische).map { Int($0) } ?? 0 chips = c.objekte(.chips); kacheln = c.objekte(.kacheln) bereit = try? c.decodeIfPresent(LiveBereit.self, forKey: .bereit) schliessenFrage = c.text(.schliessenFrage) such = c.zahl(.such).map { Int($0) } ?? 0 suchTitel = c.text(.suchTitel) ?? ""; suchText = c.text(.suchText) ?? "" } public func kachel(_ raum: String) -> LiveKachelDaten? { kacheln.first { $0.raum == raum } } } public struct LiveChip: Decodable, Equatable, Sendable, Identifiable { public var raum = "" public var titel = "" public var zeile = "" public var zeileTon = "" public var punktTon = "" public var amZug = false public var stufe = "" public var spalte = 0 public var reihe = 0 public var id: String { raum } enum CodingKeys: String, CodingKey { case raum, titel, zeile, zeileTon = "zeile_ton", punktTon = "punkt_ton", amZug = "am_zug", stufe case spalte = "spalte_nr", reihe = "zeile_nr" } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) raum = c.text(.raum) ?? ""; titel = c.text(.titel) ?? ""; zeile = c.text(.zeile) ?? "" zeileTon = c.text(.zeileTon) ?? ""; punktTon = c.text(.punktTon) ?? ""; amZug = c.wahr(.amZug) stufe = c.text(.stufe) ?? "" spalte = c.zahl(.spalte).map { Int($0) } ?? 0; reihe = c.zahl(.reihe).map { Int($0) } ?? 0 } } /// Eine Tischkachel: Kopf, Modus, Minitisch, deine Karten mit der Entscheidung, Urteil und Gegner. public struct LiveKachelDaten: Decodable, Equatable, Sendable, Identifiable { public var raum = "" public var titel = "" public var kurzTitel = "" public var zustand = "" public var zustandTon = "" public var netto = "" public var nettoTon = "" public var modus: LiveModus? public var plaetze = 6 public var mitTisch = false public var sitze: [LiveSitz] = [] public var karten: [String] = [] public var board: [String] = [] public var topf = "" public var zug: Zug? public var aktion = "" public var equity = "" public var klick = "" public var klickTon = "" public var klickWarum = "" public var klartext = "" public var hinweis = "" public var urteil: LiveUrteil? public var gegner: [LiveGegner] = [] public var platz = "" public var zugOffen = false public var id: String { raum } enum CodingKeys: String, CodingKey { case raum, titel, kurzTitel = "kurz_titel", zustand, zustandTon = "zustand_ton", netto, nettoTon = "netto_ton" case modus, plaetze, mitTisch = "mit_tisch", sitze, karten, board, topf, zug, aktion, equity, klick case klickTon = "klick_ton", klickWarum = "klick_warum", klartext, hinweis, urteil, gegner, platz case zugOffen = "zug_offen" } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) raum = c.text(.raum) ?? ""; titel = c.text(.titel) ?? raum; kurzTitel = c.text(.kurzTitel) ?? titel zustand = c.text(.zustand) ?? ""; zustandTon = c.text(.zustandTon) ?? "" netto = c.text(.netto) ?? ""; nettoTon = c.text(.nettoTon) ?? "" modus = try? c.decodeIfPresent(LiveModus.self, forKey: .modus) plaetze = c.zahl(.plaetze).map { Int($0) } ?? 6; mitTisch = c.wahr(.mitTisch) sitze = c.objekte(.sitze); karten = c.liste(.karten); board = c.liste(.board); topf = c.text(.topf) ?? "" zug = try? c.decodeIfPresent(Zug.self, forKey: .zug) aktion = c.text(.aktion) ?? ""; equity = c.text(.equity) ?? ""; klick = c.text(.klick) ?? "" klickTon = c.text(.klickTon) ?? ""; klickWarum = c.text(.klickWarum) ?? ""; klartext = c.text(.klartext) ?? "" hinweis = c.text(.hinweis) ?? "" urteil = try? c.decodeIfPresent(LiveUrteil.self, forKey: .urteil) gegner = c.objekte(.gegner); platz = c.text(.platz) ?? ""; zugOffen = c.wahr(.zugOffen) } } /// Der Modus des Tisches: Chip, Text, Lage zum Geld und die Spur (Turnier), Tasten und Uhr (K-Poker). public struct LiveModus: Decodable, Equatable, Sendable { public struct Spur: Decodable, Equatable, Sendable { /// Ein Strich je Spieler bis 40, gefuellt = bezahlt; sonst nur ein Anteil. public var striche: [Bool]? public var anteil: Double? public var text = "" enum CodingKeys: String, CodingKey { case striche, anteil, text } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) striche = c.contains(.striche) ? c.liste(.striche).map { $0 == "ja" } : nil anteil = c.zahl(.anteil); text = c.text(.text) ?? "" } } public var art = "" public var chip = "" public var chipTon = "" public var text = "" public var lage = "" public var lageTon = "" public var spur: Spur? public var tasten: [String] = [] public var uhr = "" enum CodingKeys: String, CodingKey { case art, chip, chipTon = "chip_ton", text, lage, lageTon = "lage_ton", spur, tasten, uhr } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) art = c.text(.art) ?? ""; chip = c.text(.chip) ?? ""; chipTon = c.text(.chipTon) ?? "" text = c.text(.text) ?? ""; lage = c.text(.lage) ?? ""; lageTon = c.text(.lageTon) ?? "" spur = try? c.decodeIfPresent(Spur.self, forKey: .spur) tasten = c.liste(.tasten); uhr = c.text(.uhr) ?? "" } } /// Ein Platz am Minitisch, auf dem Oval an (ux, uy) mit Stelle 0 unten (dein Platz). public struct LiveSitz: Decodable, Equatable, Sendable, Identifiable { public var stelle = 0 public var ux = 0.0 public var uy = 0.0 public var frei = true public var ich = false public var name = "" public var stack = "" public var stackBB = "" public var rolle = "" public var aktion = "" public var aktionTon = "" public var stilTon = "" public var stilKuerzel = "" public var draussen = false public var id: Int { stelle } enum CodingKeys: String, CodingKey { case stelle, ux, uy, frei, ich, name, stack, stackBB = "stack_bb", rolle, aktion, aktionTon = "aktion_ton" case stilTon = "stil_ton", stilKuerzel = "stil_kuerzel", draussen } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) stelle = c.zahl(.stelle).map { Int($0) } ?? 0; ux = c.zahl(.ux) ?? 0; uy = c.zahl(.uy) ?? 0 frei = c.contains(.frei) ? c.wahr(.frei) : true; ich = c.wahr(.ich) name = c.text(.name) ?? ""; stack = c.text(.stack) ?? ""; stackBB = c.text(.stackBB) ?? "" rolle = c.text(.rolle) ?? ""; aktion = c.text(.aktion) ?? ""; aktionTon = c.text(.aktionTon) ?? "" stilTon = c.text(.stilTon) ?? ""; stilKuerzel = c.text(.stilKuerzel) ?? ""; draussen = c.wahr(.draussen) } } public struct LiveUrteil: Decodable, Equatable, Sendable { public var titel = "" public var grund = "" public var ton = "" public var setzen = false enum CodingKeys: String, CodingKey { case titel, grund, ton, setzen } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) titel = c.text(.titel) ?? ""; grund = c.text(.grund) ?? ""; ton = c.text(.ton) ?? ""; setzen = c.wahr(.setzen) } } /// Was der Kern ueber einen Gegner denkt: Stil, Fold-Chance, Vertrauen, sein Satz, angetippt alle Werte. public struct LiveGegner: Decodable, Equatable, Sendable, Identifiable { public struct Wert: Decodable, Equatable, Sendable { public var name = "" public var wert = "" enum CodingKeys: String, CodingKey { case name, wert } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; wert = c.text(.wert) ?? "" } } public var sitz: Int? public var name = "" public var rolle = "" public var stack = "" public var stil = "" public var stilTon = "" public var geschaetzt = false public var fold = "" public var vertrauen = 0.0 public var vertrauenText = "" public var vertrauenTon = "" public var satz = "" public var satzLeise = false public var werte: [Wert] = [] public var stichprobe = "" public var draussen = false public var id: String { "\(sitz ?? -1)|\(name)" } enum CodingKeys: String, CodingKey { case sitz, name, rolle, stack, stil, stilTon = "stil_ton", geschaetzt, fold, vertrauen case vertrauenText = "vertrauen_text", vertrauenTon = "vertrauen_ton", satz, satzLeise = "satz_leise" case werte, stichprobe, draussen } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) sitz = c.zahl(.sitz).map { Int($0) }; name = c.text(.name) ?? "?"; rolle = c.text(.rolle) ?? "" stack = c.text(.stack) ?? ""; stil = c.text(.stil) ?? ""; stilTon = c.text(.stilTon) ?? "" geschaetzt = c.wahr(.geschaetzt); fold = c.text(.fold) ?? "" vertrauen = min(1, max(0, c.zahl(.vertrauen) ?? 0)) vertrauenText = c.text(.vertrauenText) ?? ""; vertrauenTon = c.text(.vertrauenTon) ?? "" satz = c.text(.satz) ?? ""; satzLeise = c.wahr(.satzLeise); werte = c.objekte(.werte) stichprobe = c.text(.stichprobe) ?? ""; draussen = c.wahr(.draussen) } } /// Sitzt der Laeufer nirgends: fuenf Schritte bis zum Spielen, "Als Naechstes", die letzte Sitzung. public struct LiveBereit: Decodable, Equatable, Sendable { public struct Schritt: Decodable, Equatable, Sendable { public var kurz = "" public var erledigt = false public var aktuell = false public init(kurz: String, erledigt: Bool, aktuell: Bool) { self.kurz = kurz; self.erledigt = erledigt; self.aktuell = aktuell } enum CodingKeys: String, CodingKey { case kurz, erledigt, aktuell } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) kurz = c.text(.kurz) ?? ""; erledigt = c.wahr(.erledigt); aktuell = c.wahr(.aktuell) } } public var schritte: [Schritt] = [] public var naechst = "" public var tun = "" enum CodingKeys: String, CodingKey { case schritte, naechst, tun } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) schritte = c.objekte(.schritte); naechst = c.text(.naechst) ?? ""; tun = c.text(.tun) ?? "" } /// Der Server kennt die macOS-Freigaben nicht (er rechnet sie als erteilt): Schritt 0 kommt von hier. public func mitFreigabe(_ lesbar: Bool, naechstText: String, tunText: String) -> LiveBereit { guard !lesbar, !schritte.isEmpty else { return self } var b = self b.schritte = schritte.enumerated().map { i, s in Schritt(kurz: s.kurz, erledigt: i == 0 ? false : s.erledigt, aktuell: i == 0) } b.naechst = naechstText; b.tun = tunText return b } /// Erster offener Schritt (nil: alles erledigt). public var offenBei: Int? { schritte.firstIndex { !$0.erledigt } } } /// `entscheidungen.feed`: die letzten Haende in Bloecken je Sitzung (neueste zuerst). public struct FeedBlock: Decodable, Equatable, Sendable { public struct Zeile: Decodable, Equatable, Sendable { public struct Schritt: Decodable, Equatable, Sendable { public var text = "" public var ton = "" enum CodingKeys: String, CodingKey { case text, ton } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) text = c.text(.text) ?? ""; ton = c.text(.ton) ?? "" } } public var zeit = "" public var raum = "" public var karten: [String] = [] public var schritte: [Schritt] = [] public var ergebnis = "" public var ton = "" enum CodingKeys: String, CodingKey { case zeit, raum, karten, schritte, ergebnis, ton } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) zeit = c.text(.zeit) ?? ""; raum = c.text(.raum) ?? ""; karten = c.liste(.karten) schritte = c.objekte(.schritte); ergebnis = c.text(.ergebnis) ?? ""; ton = c.text(.ton) ?? "" } } public var beginn = "" public var summe = "" public var summeTon = "" public var zeilen: [Zeile] = [] enum CodingKeys: String, CodingKey { case beginn, summe, summeTon = "summe_ton", zeilen } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) beginn = c.text(.beginn) ?? ""; summe = c.text(.summe) ?? ""; summeTon = c.text(.summeTon) ?? "" zeilen = c.objekte(.zeilen) } } /// `geld.anzeige`: Sitzung und Tische gegen die Stops als Skalen (Mitte = 0), die Regeln als Zeilen. public struct GeldAnzeige: Decodable, Equatable, Sendable { public struct Skala: Decodable, Equatable, Sendable { public var titel = "" public var netto = "" public var nettoTon = "" public var anteil = 0.0 public var minus = false public var ton = "" public var gross = false public var stopp = "" public var mitte = "" public var plus = "" enum CodingKeys: String, CodingKey { case titel, netto, nettoTon = "netto_ton", anteil, minus, ton, gross, stopp, mitte, plus } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) titel = c.text(.titel) ?? ""; netto = c.text(.netto) ?? ""; nettoTon = c.text(.nettoTon) ?? "" anteil = min(1, max(0, c.zahl(.anteil) ?? 0)); minus = c.wahr(.minus); ton = c.text(.ton) ?? "" gross = c.wahr(.gross); stopp = c.text(.stopp) ?? ""; mitte = c.text(.mitte) ?? ""; plus = c.text(.plus) ?? "" } } public var hinweis = "" public var skalen: [Skala] = [] public var regeln: [String] = [] enum CodingKeys: String, CodingKey { case hinweis, skalen, regeln } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) hinweis = c.text(.hinweis) ?? ""; skalen = c.objekte(.skalen); regeln = c.liste(.regeln) } } // ---- Minitisch (Rasterkachel als kleiner Tisch, wie WinUI LiveTeil.Minitisch) ---- extension LiveKachelDaten { /// Die letzte eigene Aktion dieser Hand aus den Plaetzen ("Fold", "Raise 0,06", ...). public var ichAktion: String { sitze.first(where: \.ich)?.aktion ?? "" } /// Nach dem Fold sind die eigenen Karten blass. public var ichRaus: Bool { ichAktion.hasPrefix("Fold") } } public enum LiveAktion { public static func ton(_ aktion: String) -> String { if aktion.hasPrefix("Fold") { return "grau" } if aktion.hasPrefix("Call") || aktion.hasPrefix("Check") { return "gruen" } if ["Raise", "Bet", "All"].contains(where: aktion.hasPrefix) { return "orange" } return "" } } extension Zug { /// Rest der Frist als Anteil 0...1 (ohne Frist: 1); nil, wenn keine Uhr laeuft. public func restAnteil(jetzt: Double) -> Double? { guard offen, let bis else { return nil } let rest = max(0, bis - jetzt) let ganz = fristS.flatMap { $0 > 0 ? $0 : nil } ?? max(rest, 1) return min(1, max(0, rest / ganz)) } public static func uhrTon(_ anteil: Double) -> String { anteil < 0.25 ? "rot" : anteil < 0.5 ? "orange" : "gruen" } }