Files
pokerfreak/mac/Sources/CockpitKern/Spielplan.swift
T

97 lines
4.0 KiB
Swift

import Foundation
/// Übung oder Live, wie viele Tische, und (nur Live) ein Budget - 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
public var tische: Int { didSet { tische = Self.klemmen(tische) } }
public var budget: Double?
public var live: Bool { modus == .echt }
public init(modus: Modus, tische: Int, budget: Double?) {
self.modus = modus
self.tische = Self.klemmen(tische)
self.budget = budget.flatMap { $0 > 0 ? $0 : nil }
}
private static func klemmen(_ n: Int) -> Int { max(1, min(hoechstens, n)) }
enum CodingKeys: String, CodingKey { case modus, tische, budget }
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))
}
/// 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": tische]
if live, let budget { d["budget"] = budget }
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
enum CodingKeys: String, CodingKey {
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)
}
}
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)
}
}