import Foundation // Der Reiter Features (GET /api/features, sicht/features.py + sicht/featurewirkung.py) - wie // winui/Seiten/FeaturesSeite.cs ihn zeigt. Alles nachsichtig gelesen: jedes Feld darf fehlen, eine Zahl darf // als Text kommen, ein kaputter Listeneintrag faellt weg. Die Texte stehen hier als reine Funktionen // (`FeaturesText`), die Ansicht malt sie nur. private extension KeyedDecodingContainer { /// Eine ganze Zahl; was nicht endlich oder absurd gross ist, ist unbekannt (Int(x) wuerde sonst abstuerzen). func ganz(_ k: Key) -> Int? { guard let x = zahl(k), x.isFinite, abs(x) < 1e15 else { return nil } return Int(x) } func einzeln(_ k: Key) -> T? { (try? decodeIfPresent(T.self, forKey: k)) ?? nil } func vorhanden(_ k: Key) -> Bool { contains(k) && !((try? decodeNil(forKey: k)) ?? true) } } /// Mittel mit 95-%-Halbbreite und Summe (featurewirkung._mittel; bei Sitzungen ohne Summe). public struct FeaturesMass: Decodable, Equatable, Sendable { public var mittel: Double? public var halb95: Double? public var summe: Double? public var n: Int? enum CodingKeys: String, CodingKey { case mittel, halb95, summe, n } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) mittel = c.zahl(.mittel); halb95 = c.zahl(.halb95); summe = c.zahl(.summe); n = c.ganz(.n) } } /// Ein Schalter, der in der Sitzung anders war als in der vorigen (name, vorher, nachher; nil = nicht gesetzt). public struct FeaturesUnterschied: Decodable, Equatable, Sendable { public var name: String public var vorher: String? public var nachher: String? enum CodingKeys: String, CodingKey { case name, vorher, nachher } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; vorher = c.text(.vorher); nachher = c.text(.nachher) } } /// Ein Schalter, der seit dem Laeuferstart aus der Datei verschwand (jetzt nil = wieder Standard). public struct FeaturesWeg: Decodable, Equatable, Sendable { public var name: String public var vorher: String public var jetzt: String? enum CodingKeys: String, CodingKey { case name, vorher, jetzt } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; vorher = c.text(.vorher) ?? ""; jetzt = c.text(.jetzt) } } public struct FeaturesLaeufer: Decodable, Equatable, Sendable { public var seit: Double? public var markeVon: Double? public var geaendert: Int public var weg: [FeaturesWeg] enum CodingKeys: String, CodingKey { case seit, markeVon = "marke_von", geaendert, weg } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) seit = c.zahl(.seit); markeVon = c.zahl(.markeVon); geaendert = c.ganz(.geaendert) ?? 0; weg = c.objekte(.weg) } public init() { seit = nil; markeVon = nil; geaendert = 0; weg = [] } } public struct FeaturesSchalter: Decodable, Equatable, Sendable { public var name: String public var wert: String public var zeile: Int? public var datum: String? public var kommentar: String? public var block: [String] public var sharingan: Bool public var pfad: Bool /// null (kein Laeufer, keine Marke) zaehlt als nicht neu. public var neuSeitStart: Bool enum CodingKeys: String, CodingKey { case name, wert, zeile, datum, kommentar, block, sharingan, pfad, neuSeitStart = "neu_seit_start" } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; wert = c.text(.wert) ?? ""; zeile = c.ganz(.zeile) datum = c.text(.datum); kommentar = c.text(.kommentar); block = c.liste(.block) sharingan = c.wahr(.sharingan); pfad = c.wahr(.pfad); neuSeitStart = c.wahr(.neuSeitStart) } } public struct FeaturesGruppe: Decodable, Equatable, Sendable, Identifiable { public var id: String public var titel: String public var schalter: [FeaturesSchalter] enum CodingKeys: String, CodingKey { case id, titel, schalter } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) id = c.text(.id) ?? ""; titel = c.text(.titel) ?? id; schalter = c.objekte(.schalter) } } /// Eine Zeile aus "aus" oder "ruht" (ruht bringt `elter` mit). public struct FeaturesEinstellung: Decodable, Equatable, Sendable { public var name: String public var wert: String public var datum: String? public var kommentar: String? public var elter: String enum CodingKeys: String, CodingKey { case name, wert, datum, kommentar, elter } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; wert = c.text(.wert) ?? ""; datum = c.text(.datum) kommentar = c.text(.kommentar); elter = c.text(.elter) ?? "" } } /// Eine leere Zeile der Datei; `gilt` = was trotzdem weiter gilt. public struct FeaturesLeerZeile: Decodable, Equatable, Sendable { public var name: String public var zeile: Int? public var gilt: String? enum CodingKeys: String, CodingKey { case name, zeile, gilt } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; zeile = c.ganz(.zeile); gilt = c.text(.gilt) } } public struct FeaturesUnbekannt: Decodable, Equatable, Sendable { public var name: String public var wert: String public var zeile: Int? enum CodingKeys: String, CodingKey { case name, wert, zeile } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; wert = c.text(.wert) ?? ""; zeile = c.ganz(.zeile) } } public struct FeaturesExe: Decodable, Equatable, Sendable { public var rolle: String public var name: String public var pfad: String public var an: Bool public var da: Bool public var laeuft: Bool public var md5: String? public var datum: Double? enum CodingKeys: String, CodingKey { case rolle, name, pfad, an, da, laeuft, md5, datum } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) rolle = c.text(.rolle) ?? ""; name = c.text(.name) ?? ""; pfad = c.text(.pfad) ?? "" an = c.wahr(.an); da = c.wahr(.da); laeuft = c.wahr(.laeuft); md5 = c.text(.md5); datum = c.zahl(.datum) } } /// Eine Sitzung (gleiche Schalter-Marke, keine Pause ueber 60 min): AIVAT bb/100 mit 95 %. public struct FeaturesSitzung: Decodable, Equatable, Sendable { public var von: Double? public var bis: Double? public var haende: Int public var roh: FeaturesMass? public var aivat: FeaturesMass? public var nettoBB: Double? public var anders: [FeaturesUnterschied] enum CodingKeys: String, CodingKey { case von, bis, haende, roh, aivat, nettoBB = "netto_bb", anders } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) von = c.zahl(.von); bis = c.zahl(.bis); haende = c.ganz(.haende) ?? 0 roh = c.einzeln(.roh); aivat = c.einzeln(.aivat); nettoBB = c.zahl(.nettoBB); anders = c.objekte(.anders) } } public struct FeaturesVergleich: Decodable, Equatable, Sendable { public var diff: Double? public var halb95: Double? public var urteil: String public var anders: [FeaturesUnterschied] enum CodingKeys: String, CodingKey { case diff, halb95, urteil, anders } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) diff = c.zahl(.diff); halb95 = c.zahl(.halb95); urteil = c.text(.urteil) ?? ""; anders = c.objekte(.anders) } } public struct FeaturesSitzungen: Decodable, Equatable, Sendable { /// Neueste zuerst. public var liste: [FeaturesSitzung] public var vergleich: FeaturesVergleich? public var klein: Int public var mindest: Int? enum CodingKeys: String, CodingKey { case liste, vergleich, klein, mindest } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) liste = c.objekte(.liste); vergleich = c.einzeln(.vergleich); klein = c.ganz(.klein) ?? 0; mindest = c.ganz(.mindest) } } // MARK: Wirkung je Feature public struct FeaturesZug: Decodable, Equatable, Sendable { public var street: String public var aktion: String public var groesse: Double? public var grund: String /// Die Features, die diese Entscheidung zaehlt (ids). public var wer: [String] enum CodingKeys: String, CodingKey { case street, aktion, groesse, grund, wer } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) street = c.text(.street) ?? ""; aktion = c.text(.aktion) ?? ""; groesse = c.zahl(.groesse) grund = c.text(.grund) ?? ""; wer = c.liste(.wer) } } public struct FeaturesHand: Decodable, Equatable, Sendable { public var hand: String public var t: Double? public var raum: String? public var roh: Double? public var aivat: Double? /// Die Features, bei denen diese Hand zaehlt, weil sie das meiste Geld setzten. public var haupt: [String] public var alle: [FeaturesZug] enum CodingKeys: String, CodingKey { case hand, t, raum, roh, aivat, haupt, alle } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) hand = c.text(.hand) ?? ""; t = c.zahl(.t); raum = c.text(.raum); roh = c.zahl(.roh); aivat = c.zahl(.aivat) haupt = c.liste(.haupt); alle = c.objekte(.alle) } } public struct FeaturesWechsel: Decodable, Equatable, Sendable { public var name: String public var vorher: String? public var nachher: String? public var kommentar: String? enum CodingKeys: String, CodingKey { case name, vorher, nachher, kommentar } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name) ?? ""; vorher = c.text(.vorher); nachher = c.text(.nachher); kommentar = c.text(.kommentar) } } /// Die Haende, in denen das Feature das meiste Geld setzte. public struct FeaturesHaupt: Decodable, Equatable, Sendable { public var haende: Int public var aivat: FeaturesMass? public var roh: FeaturesMass? enum CodingKeys: String, CodingKey { case haende, aivat, roh } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) haende = c.ganz(.haende) ?? 0; aivat = c.einzeln(.aivat); roh = c.einzeln(.roh) } } public struct FeaturesVorher: Decodable, Equatable, Sendable { public var haupt: FeaturesHaupt enum CodingKeys: String, CodingKey { case haupt } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) haupt = c.einzeln(.haupt) ?? FeaturesHaupt.leer } } extension FeaturesHaupt { static let leer: FeaturesHaupt = { // Ein leeres Objekt liest sich immer; try! ist hier ausgeschlossen. (try? JSONDecoder().decode(FeaturesHaupt.self, from: Data("{}".utf8))) ?? FeaturesHaupt(haende: 0, aivat: nil, roh: nil) }() init(haende: Int, aivat: FeaturesMass?, roh: FeaturesMass?) { self.haende = haende; self.aivat = aivat; self.roh = roh } } public struct FeaturesWirkVergleich: Decodable, Equatable, Sendable { public var diff: Double? public var halb95: Double? public var urteil: String enum CodingKeys: String, CodingKey { case diff, halb95, urteil } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) diff = c.zahl(.diff); halb95 = c.zahl(.halb95); urteil = c.text(.urteil) ?? "" } } public struct FeaturesWirkFeature: Decodable, Equatable, Sendable, Identifiable { public var id: String public var titel: String public var erklaerung: String /// "rot", "gruen" oder "grau". public var ton: String public var satz: String public var urteil: String public var entscheidungen: Int public var ueberstimmt: Int public var haende: Int public var aivat: FeaturesMass? public var roh: FeaturesMass? public var haupt: FeaturesHaupt public var wechsel: [FeaturesWechsel] public var vorher: FeaturesVorher? public var vergleich: FeaturesWirkVergleich? public var verlierer: [FeaturesHand] public var gewinner: [FeaturesHand] /// Wert der zugehoerigen Schalter in dieser Sitzung (Name -> Wert). public var zustand: [String: String] enum CodingKeys: String, CodingKey { case id, titel, erklaerung, ton, satz, urteil, entscheidungen, ueberstimmt, haende, aivat, roh, haupt case wechsel, vorher, vergleich, verlierer, gewinner, zustand } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) id = c.text(.id) ?? ""; titel = c.text(.titel) ?? id; erklaerung = c.text(.erklaerung) ?? "" ton = c.text(.ton) ?? "grau"; satz = c.text(.satz) ?? ""; urteil = c.text(.urteil) ?? "" entscheidungen = c.ganz(.entscheidungen) ?? 0; ueberstimmt = c.ganz(.ueberstimmt) ?? 0; haende = c.ganz(.haende) ?? 0 aivat = c.einzeln(.aivat); roh = c.einzeln(.roh); haupt = c.einzeln(.haupt) ?? FeaturesHaupt.leer wechsel = c.objekte(.wechsel); vorher = c.einzeln(.vorher); vergleich = c.einzeln(.vergleich) verlierer = c.objekte(.verlierer); gewinner = c.objekte(.gewinner); zustand = c.woerter(.zustand) } } public struct FeaturesWirkSitzung: Decodable, Equatable, Sendable { public var von: Double? public var bis: Double? public var haende: Int public var aivatBB: Double? enum CodingKeys: String, CodingKey { case von, bis, haende, aivatBB = "aivat_bb" } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) von = c.zahl(.von); bis = c.zahl(.bis); haende = c.ganz(.haende) ?? 0; aivatBB = c.zahl(.aivatBB) } } /// Die Regeln der Ampel (featurewirkung): fehlen sie, gelten die Werte des Servers. public struct FeaturesRegeln: Decodable, Equatable, Sendable { public var minHaende: Int public var klumpenBB: Double public var klumpenAnteil: Double enum CodingKeys: String, CodingKey { case minHaende = "min_haende", klumpenBB = "klumpen_bb", klumpenAnteil = "klumpen_anteil" } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) minHaende = c.ganz(.minHaende) ?? 10; klumpenBB = c.zahl(.klumpenBB) ?? 15; klumpenAnteil = c.zahl(.klumpenAnteil) ?? 0.5 } public init() { minHaende = 10; klumpenBB = 15; klumpenAnteil = 0.5 } } public struct FeaturesWirkung: Decodable, Equatable, Sendable { public var features: [FeaturesWirkFeature] public var sitzung: FeaturesWirkSitzung? public var hinweis: String? public var fehler: String? public var regeln: FeaturesRegeln enum CodingKeys: String, CodingKey { case features, sitzung, hinweis, fehler, regeln } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) features = c.objekte(.features); sitzung = c.einzeln(.sitzung); hinweis = c.text(.hinweis) fehler = c.text(.fehler); regeln = c.einzeln(.regeln) ?? FeaturesRegeln() } } // MARK: Die ganze Nutzlast public struct Features: Decodable, Equatable, Sendable { public var stand: Double? /// Der Server rechnet noch ({rechnet: true, leer: "..."}). public var rechnet: Bool /// `leer` als Text (solange der Server rechnet); als Liste steht es in `leerZeilen`. public var leerText: String? public var fehler: String? public var hatGruppen: Bool public var laeufer: FeaturesLaeufer public var an: Int public var gruppen: [FeaturesGruppe] public var aus: [FeaturesEinstellung] public var ruht: [FeaturesEinstellung] public var unbekannt: [FeaturesUnbekannt] public var leerZeilen: [FeaturesLeerZeile] public var exes: [FeaturesExe] public var sitzungen: FeaturesSitzungen? public var wirkung: FeaturesWirkung? enum CodingKeys: String, CodingKey { case stand, rechnet, leer, fehler, laeufer, an, gruppen, aus, ruht, unbekannt, exes, sitzungen, wirkung } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) stand = c.zahl(.stand); rechnet = c.wahr(.rechnet); leerText = c.text(.leer); fehler = c.text(.fehler) hatGruppen = c.vorhanden(.gruppen) laeufer = c.einzeln(.laeufer) ?? FeaturesLaeufer(); an = c.ganz(.an) ?? 0 gruppen = c.objekte(.gruppen); aus = c.objekte(.aus); ruht = c.objekte(.ruht) unbekannt = c.objekte(.unbekannt); leerZeilen = c.objekte(.leer); exes = c.objekte(.exes) sitzungen = c.einzeln(.sitzungen); wirkung = c.einzeln(.wirkung) } /// Die Antwort des Servers lesen; ein Text, der kein Objekt ist, ergibt nil. public static func lesen(text: String) -> Features? { lesen(Data(text.utf8)) } public static func lesen(_ daten: Data) -> Features? { try? JSONDecoder().decode(Features.self, from: daten) } } // MARK: Texte /// Die Texte des Reiters als reine Funktionen (wie FeaturesSeite.cs). Zeiten in der Ortszeit; /// `zoneSekunden` (Abstand zu UTC) ist fuer die Pruefung. public enum FeaturesText { public static let nichtGesetzt = "nicht gesetzt" public struct Zeile: Equatable, Sendable { public var name: String public var wert: String public var unter: String public init(name: String, wert: String, unter: String) { self.name = name; self.wert = wert; self.unter = unter } } public struct LeerSeite: Equatable, Sendable { public var titel: String public var text: String public init(titel: String, text: String) { self.titel = titel; self.text = text } } // ---- Zeit private static func zeit(_ t: Double?, _ muster: String, _ zone: Int?) -> String? { guard let t, t.isFinite else { return nil } let f = DateFormatter() f.locale = Locale(identifier: "de_DE") f.dateFormat = muster if let zone, let z = TimeZone(secondsFromGMT: zone) { f.timeZone = z } return f.string(from: Date(timeIntervalSince1970: t)) } /// "30.11. 12:16" (ohne Zeit: "–"). public static func uhrtag(_ t: Double?, zoneSekunden: Int? = nil) -> String { zeit(t, "dd.MM. HH:mm", zoneSekunden) ?? "–" } /// "12:16" (ohne Zeit: leer). public static func uhr(_ t: Double?, zoneSekunden: Int? = nil) -> String { zeit(t, "HH:mm", zoneSekunden) ?? "" } // ---- Zahlen /// "+12,5 ±4": Wert mit Vorzeichen, dahinter die Halbbreite der 95-%-Spanne. public static func mitSpanne(_ m: Double?, _ h: Double?) -> String { guard let m else { return "–" } guard let h else { return AuswertungText.vz(m, stellen: 1) } return "\(AuswertungText.vz(m, stellen: 1)) ±\(Format.zahl(h, stellen: 1))" } private static func vz(_ x: Double?, _ stellen: Int = 1) -> String { AuswertungText.vz(x, stellen: stellen) } /// Eine Groesse ohne Nachkommanullen, mit Punkt (2.5, 0.125, 3). public static func groesse(_ x: Double) -> String { let f = NumberFormatter() f.locale = Locale(identifier: "en_US_POSIX") f.numberStyle = .decimal f.usesGroupingSeparator = false f.minimumFractionDigits = 0 f.maximumFractionDigits = 3 return f.string(from: x as NSNumber) ?? Format.roh(x) } // ---- Leerseite public static let nichtErreicht = "Server ohne /api/features (älterer Stand?) oder nicht erreicht." /// Was statt der Seite steht, solange es keine Gruppen gibt. `nil` = noch nie geantwortet. public static func leerSeite(_ f: Features?) -> LeerSeite { guard let f else { return LeerSeite(titel: "Keine Features", text: nichtErreicht) } return LeerSeite(titel: f.rechnet ? "Wird gelesen" : "Keine Features", text: f.leerText ?? "Verbindet mit dem Cockpit-Server …") } // ---- Kopf public struct Marke: Equatable, Sendable { public var text: String public var ton: String } public struct Kopf: Equatable, Sendable { public var titel: String public var unter: String public var laeuft: Bool public var abzeichen: [Marke] /// Schalter, die seit dem Laeuferstart anders sind (gelten erst nach einem Neustart). public var geaendert: [String] } public static func kopf(_ f: Features, zoneSekunden: Int? = nil) -> Kopf { let l = f.laeufer let laeuft = l.seit != nil let titel = laeuft ? "Läufer spielt seit \(uhrtag(l.seit, zoneSekunden: zoneSekunden))" : "Läufer steht" let unter = laeuft ? "mit diesen Schaltern" : l.markeVon.map { "letzte Hand \(uhrtag($0, zoneSekunden: zoneSekunden)) · die Schalter gelten beim nächsten Start" } ?? "noch keine Hand mit Schalter-Marke" var a = [Marke(text: "\(f.an) \(f.an == 1 ? "Feature" : "Features") an", ton: "blau")] let feats = f.wirkung?.features ?? [] let rot = feats.filter { $0.ton == "rot" }.count let gruen = feats.filter { $0.ton == "gruen" }.count if rot > 0 { a.append(Marke(text: rot == 1 ? "1 Feature ansehen" : "\(rot) Features ansehen", ton: "rot")) } if gruen > 0 { a.append(Marke(text: "\(gruen) bringen Geld", ton: "gruen")) } let n = l.geaendert if n > 0 { a.append(Marke(text: n == 1 ? "1 Änderung wartet auf Neustart" : "\(n) Änderungen warten auf Neustart", ton: "orange")) } else { a.append(Marke(text: laeuft ? "Schalter wie beim Start" : "Schalter wie im letzten Lauf", ton: "grau")) } let fehlt = f.exes.filter { $0.an && !$0.da }.count if fehlt > 0 { a.append(Marke(text: fehlt == 1 ? "1 Programm fehlt" : "\(fehlt) Programme fehlen", ton: "rot")) } var zeilen = f.gruppen.flatMap(\.schalter).filter(\.neuSeitStart).map { "\($0.name) = \(wert($0.wert, pfad: $0.pfad))" } zeilen += l.weg.map { "\($0.name): \($0.vorher) → \($0.jetzt ?? "entfernt (wieder Standard)")" } return Kopf(titel: titel, unter: unter, laeuft: laeuft, abzeichen: a, geaendert: zeilen) } // ---- Sitzung gegen Sitzung public static func urteilTon(_ urteil: String) -> String { urteil == "besser" ? "gruen" : urteil == "schlechter" ? "rot" : "gelb" } public static func urteilSatz(_ urteil: String) -> String { switch urteil { case "besser": "Die letzte Sitzung war besser als die vorige – und das ist kein Zufall mehr." case "schlechter": "Die letzte Sitzung war schlechter als die vorige – und das ist kein Zufall mehr." default: "Noch kein Urteil: der Unterschied kann Zufall sein. Mehr Hände abwarten." } } public struct SitzungKachelText: Equatable, Sendable { public var kopf: String public var gross: String public var ton: String? public var mitte: String public var unter: String } public static func sitzungKachel(_ titel: String, _ s: FeaturesSitzung, zoneSekunden: Int? = nil) -> SitzungKachelText { let m = s.aivat?.mittel return SitzungKachelText( kopf: "\(titel) · \(uhrtag(s.von, zoneSekunden: zoneSekunden))–\(uhr(s.bis, zoneSekunden: zoneSekunden))", gross: vz(m), ton: AuswertungText.ton(m), mitte: "± \(Format.zahl(s.aivat?.halb95, stellen: 1)) bb/100 · \(s.haende) Hände", unter: "roh \(vz(s.roh?.mittel)) · netto \(Format.vorzeichen(s.nettoBB, stellen: 0)) bb") } public static func vergleichZeile(_ v: FeaturesVergleich) -> String { mitSpanne(v.diff, v.halb95) + " bb/100" } /// "MOTOR_EINSATZ nicht gesetzt → 1" public static func wechsel(_ a: FeaturesUnterschied) -> String { "\(a.name) \(a.vorher ?? nichtGesetzt) → \(a.nachher ?? nichtGesetzt)" } public static func andersText(_ namen: [String]) -> String { namen.isEmpty ? "Schalter wie davor" : "anders: " + namen.prefix(4).joined(separator: ", ") + (namen.count > 4 ? " +\(namen.count - 4)" : "") } public struct SitzungZeileText: Equatable, Sendable { public var kopf: String public var unter: String public var wert: String public var ton: String? } public static func sitzungZeile(_ s: FeaturesSitzung, zoneSekunden: Int? = nil) -> SitzungZeileText { SitzungZeileText( kopf: "\(uhrtag(s.von, zoneSekunden: zoneSekunden))–\(uhr(s.bis, zoneSekunden: zoneSekunden)) · \(s.haende) Hände", unter: andersText(s.anders.map(\.name)), wert: mitSpanne(s.aivat?.mittel, s.aivat?.halb95), ton: AuswertungText.ton(s.aivat?.mittel)) } public static func sitzungFuss(_ s: FeaturesSitzungen) -> String { var t = "Sitzung = Hände mit derselben Schalter-Marke ohne Pause über 60 min, nur Cash, AIVAT (lesen/aivat.py), ±95 %" if s.klein > 0 { t += " · \(s.klein) kleine \(s.klein == 1 ? "Sitzung" : "Sitzungen") unter \(s.mindest.map(String.init) ?? "–") Händen ausgelassen" } return t } // ---- Wirkung public static func wirkungKopf(_ s: FeaturesWirkSitzung?, zoneSekunden: Int? = nil) -> String? { s.map { "diese Sitzung seit \(uhrtag($0.von, zoneSekunden: zoneSekunden)) · \($0.haende) Hände · \(vz($0.aivatBB)) bb" } } public static func wirkungFuss(_ r: FeaturesRegeln) -> String { "So wird gerechnet: Eine Hand gehört zu dem Feature, dessen Entscheidungen in ihr das meiste Geld gesetzt haben. " + "bb sind glücksbereinigt (AIVAT), nur Cash. " + "Rot = sicher im Minus, oder höchstens 3 Hände tragen mindestens \(Format.zahl(r.klumpenAnteil * 100, stellen: 0)) % eines Verlusts ab \(Format.zahl(r.klumpenBB, stellen: 0)) bb. " + "Grau = unter \(r.minHaende) Händen oder unauffällig." } /// Der Satz, wenn kein Feature zu zeigen ist; nil, wenn es Features gibt oder der Server einen Fehler meldet. public static func wirkungLeer(_ w: FeaturesWirkung?) -> String? { guard let w, w.features.isEmpty, w.fehler == nil else { return nil } return w.hinweis == "keine Cash-Sitzung" ? "Noch keine Cash-Sitzung." : "Noch keine Entscheidung eines Features in dieser Sitzung." } public struct WirkZahl: Equatable, Sendable { public var wert: String public var unter: String public var ton: String? } /// Rechts im Kopf der Karte: bb der Haende, in denen das Feature das meiste Geld setzte. public static func wirkungZahl(_ f: FeaturesWirkFeature) -> WirkZahl { let n = f.haupt.haende let s = f.haupt.aivat?.summe return WirkZahl(wert: n > 0 ? vz(s) : "–", unter: n > 0 ? "bb · \(n) Hände" : "keine Hand", ton: AuswertungText.ton(s)) } /// Rot steht offen. public static func standardOffen(_ f: FeaturesWirkFeature) -> Bool { f.ton == "rot" } public static func wirkungKacheln(_ f: FeaturesWirkFeature) -> [KennzahlKachel] { let n = f.haupt.haende let s = f.haupt.aivat?.summe let diese = KennzahlKachel( titel: "Diese Sitzung", wert: n > 0 ? "\(vz(s)) bb" : "–", ton: AuswertungText.ton(s), unter: "\(n) Hände · je Hand \(mitSpanne(f.haupt.aivat?.mittel, f.haupt.aivat?.halb95))", hilfe: nil) let vh = f.vorher?.haupt let vs = vh?.aivat?.summe let vorige = KennzahlKachel( titel: "Vorige Sitzung", wert: vh == nil ? "–" : (vh?.haende ?? 0) > 0 ? "\(vz(vs)) bb" : "nicht aktiv", ton: AuswertungText.ton(vs), unter: vh == nil ? "keine vorige Sitzung" : "\(vh?.haende ?? 0) Hände", hilfe: nil) let dd = f.vergleich?.diff let unterschied = KennzahlKachel( titel: "Unterschied je Hand", wert: dd.map { "\(vz($0, 2)) bb" } ?? "–", ton: AuswertungText.ton(dd), unter: f.vergleich.map { $0.urteil == "noch offen" ? "kann Zufall sein" : $0.urteil } ?? "", hilfe: nil) return [diese, vorige, unterschied] } public static func beteiligt(_ f: FeaturesWirkFeature) -> String { "Insgesamt beteiligt: \(f.entscheidungen) Entscheidungen in \(f.haende) Händen · \(vz(f.aivat?.summe)) bb bereinigt, \(vz(f.roh?.summe)) bb roh" + (f.ueberstimmt > 0 ? " · \(f.ueberstimmt)× von einer Regel überstimmt" : "") } public static func neuInSitzung(_ w: FeaturesWechsel) -> String { "Neu in dieser Sitzung: \(w.name) \(w.vorher ?? nichtGesetzt) → \(w.nachher ?? nichtGesetzt)" + (w.kommentar.map { $0.isEmpty ? "" : " · " + $0 } ?? "") } // ---- Hand private static let strassen = ["PREFLOP": "PF", "FLOP": "F", "TURN": "T", "RIVER": "R"] public static func kurzstrasse(_ s: String) -> String { strassen[s] ?? s } /// Feature-Kennung -> kurzer Name fuer den Ablauf einer Hand. public static func kurzname(_ id: String) -> String { switch id { case "blueprint": "SHARINGAN"; case "suche_flop": "Flop-Suche"; case "suche_turn_river": "T/R-Suche" case "motor_preis": "Preis-Rückfall"; case "motor_genau": "Motor genau"; case "motor_baum": "Motor" case "nl2_daten": "NL2-Daten"; case "nl2_typ": "Gegnertyp"; case "nl2_einsatz": "Bluff/Wert"; case "nl2_iso": "Iso" case "nl2_raise": "Raise-Bremse"; case "nl2_groesse": "Wertgröße"; case "vertrauen": "Vertrauen" case "splash": "Splash"; case "fold_rundung": "Fold-Rundung"; case "kern": "Kern" default: id } } public struct KettenTeil: Equatable, Sendable { public var text: String /// Entschieden vom Feature dieser Karte (farbig), sonst grau. public var eigen: Bool /// Bei fremden Entscheidungen: wer sie entschied. public var wer: String? } public struct GrundZeile: Equatable, Sendable { public var text: String public var eigen: Bool } public struct HandText: Equatable, Sendable { public var kennung: String public var mitte: String public var ergebnis: String public var ton: String? public var hilfe: String public var kette: [KettenTeil] public var gruende: [GrundZeile] } public static func handZeile(_ h: FeaturesHand, feature fid: String, zoneSekunden: Int? = nil) -> HandText { let haupt = h.haupt.contains(fid) let kette = h.alle.map { z -> KettenTeil in let eigen = z.wer.contains(fid) let g = z.groesse.map { " " + groesse($0) } ?? "" return KettenTeil(text: "\(kurzstrasse(z.street)) \(z.aktion)\(g)", eigen: eigen, wer: !eigen && !z.wer.isEmpty ? z.wer.map(kurzname).joined(separator: ", ") : nil) } let gruende = h.alle.map { GrundZeile(text: "\(kurzstrasse($0.street)) \($0.aktion): \($0.grund)", eigen: $0.wer.contains(fid)) } return HandText( kennung: h.hand, mitte: uhr(h.t, zoneSekunden: zoneSekunden) + (haupt ? " · hier entschied es das meiste Geld" : " · beteiligt"), ergebnis: "\(vz(h.aivat)) bb", ton: AuswertungText.ton(h.aivat), hilfe: "roh \(vz(h.roh)) bb · \(h.raum ?? "")", kette: kette, gruende: gruende) } // ---- Schalter und Programme /// Pfade kurz (nur der Dateiname), der Rest hinter dem ersten Komma bleibt; voll im Tipp. public static func wert(_ w: String, pfad: Bool = false) -> String { guard pfad || w.contains(":\\") || w.contains(":/") else { return w } let teile = w.split(separator: ",", maxSplits: 1, omittingEmptySubsequences: false) var name = String(teile.first ?? "") while let l = name.last, l == "\\" || l == "/" { name.removeLast() } if let i = name.lastIndex(where: { $0 == "\\" || $0 == "/" }) { name = "…" + name[i...] } return teile.count > 1 ? name + "," + teile[1] : name } /// "22.11. · Grund": das Datum steht nur einmal vorn. public static func grund(datum: String?, kommentar: String?) -> String { var k = kommentar ?? "" if let datum, k.hasPrefix(datum) { k = String(k.dropFirst(datum.count).drop { $0 == " " || $0 == ":" }) } return datum.map { "\($0) · \(k)" } ?? k } public static func gruppeWert(_ g: FeaturesGruppe) -> String { let neu = g.schalter.filter(\.neuSeitStart).count return neu > 0 ? "\(g.schalter.count) an · \(neu) neu" : "\(g.schalter.count) an" } public static func gruppeHinweis(_ g: FeaturesGruppe) -> String? { g.id == "kern" ? "Spielt nur, wenn der Motor ausfällt (Ante, Splash, Nicht-NLHE, Ausfall)." : nil } /// Das Fenster am Schalter: Zeile in der Datei und der Kommentarblock. public static func schalterTipp(_ s: FeaturesSchalter) -> String { "Zeile \(s.zeile.map(String.init) ?? "–")\n" + s.block.filter { !$0.isEmpty }.joined(separator: "\n") } public static func exeZustand(_ e: FeaturesExe) -> (ton: String, text: String) { !e.da ? ("rot", "Datei fehlt!") : e.laeuft ? ("gruen", "läuft gerade") : e.an ? ("grau", "liegt bereit") : ("grau", "ausgeschaltet") } public static func exesWert(_ exes: [FeaturesExe]) -> String { "\(exes.filter(\.laeuft).count) von \(exes.count) laufen" } public static func exeUnter(_ e: FeaturesExe, zoneSekunden: Int? = nil) -> String { (e.datum.map { uhrtag($0, zoneSekunden: zoneSekunden) } ?? "") + " · " + exeZustand(e).text } public static func zeilenAus(_ l: [FeaturesEinstellung]) -> [Zeile] { l.map { Zeile(name: $0.name, wert: "= " + wert($0.wert), unter: grund(datum: $0.datum, kommentar: $0.kommentar)) } } public static func zeilenRuht(_ l: [FeaturesEinstellung]) -> [Zeile] { l.map { Zeile(name: $0.name, wert: "= " + wert($0.wert), unter: "wirkt nicht: \($0.elter) ist aus") } } public static func zeilenLeerUnbekannt(leer: [FeaturesLeerZeile], unbekannt: [FeaturesUnbekannt]) -> [Zeile] { func zeile(_ n: Int?) -> String { "Zeile \(n.map(String.init) ?? "–")" } return leer.map { Zeile(name: $0.name + "=", wert: zeile($0.zeile), unter: $0.gilt.map { "leer überschreibt nicht – es gilt weiter \($0)" } ?? "leer – der Schalter ist nicht gesetzt") } + unbekannt.map { Zeile(name: $0.name, wert: "= " + wert($0.wert), unter: "\(zeile($0.zeile)): kein bekannter Schalter (entscheiden/decide.py) – wirkt nicht") } } }