import Foundation /// Übung oder Live, wie viele Tische, (nur Live) 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 } public static let hoechstens = 12 // wie betrieb/spielplan.HOECHSTENS 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 = [.cash] /// Alle Tische zusammen. Setzen ändert Cash (die anderen Formate bleiben). public var tische: Int { get { formate.values.reduce(0, +) } set { setzen(.cash, newValue - (tische - anzahl(.cash))) } } public var budget: Double? /// Eingabe einheitlich für Lobby und Tisch: mit Maus oder ohne (hand/eingabe.py). public var maus: Bool /// 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) - nur mit Maus, kann nerven: Vorgabe aus. public var zeiger = false public var zeitplan = Zeitplan.ohne public var geld = Geldplan() public var live: Bool { modus == .echt } public init(modus: Modus, tische: Int, budget: Double?, maus: Bool = true) { self.modus = modus self.formate = [.cash: Self.klemmen(tische)] self.budget = budget.flatMap { $0 > 0 ? $0 : nil } self.maus = maus } 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 andere = tische - anzahl(f) let neu = max(0, min(n, Self.hoechstens - andere)) formate[f] = neu > 0 ? neu : nil tischeBis = Self.spanne(tische, tischeBis) } 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, maus, zeiger, tischeBis = "tische_bis", zeitplan, geld, formate case formateListe = "formate_liste" } private struct FormatInfo: Decodable { var format: String; var gemessen: 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), maus: ((try? c.decodeIfPresent(Bool.self, forKey: .maus)) ?? nil) ?? true) tischeBis = Self.spanne(tische, c.zahl(.tischeBis).map { Int($0) }) zeiger = c.wahr(.zeiger) zeitplan = ((try? c.decodeIfPresent(Zeitplan.self, forKey: .zeitplan)) ?? nil) ?? .ohne geld = ((try? c.decodeIfPresent(Geldplan.self, forKey: .geld)) ?? nil) ?? Geldplan() 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)) } } } if let liste = (try? c.decodeIfPresent([FormatInfo].self, forKey: .formateListe)) ?? nil { gemessen = Set(liste.filter(\.gemessen).compactMap { SpielFormat(rawValue: $0.format) }) } } /// Für POST /api/steuern {aktion: "spielplan_setzen", daten}. Budget nur im Live-Modus. public var daten: [String: Any] { var d: [String: Any] = ["modus": modus.rawValue, "tische": max(1, tische), "maus": maus] 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 live, let budget { d["budget"] = budget } if let tischeBis { d["tische_bis"] = tischeBis } if zeiger { d["zeiger"] = true } 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) } }