Files
pokerfreak-shell/mac/Sources/CockpitKern/Ansicht.swift
T
mmintelandClaude Opus 5.5 f40ae277c0 Mac-App: Modus je Tisch - Turnier mit Spur zum Geld, K-Poker mit Tasten und Uhr
The data layer already sends `modus` on jetzt and on each decision row.
The app now decodes it (TischModus, lenient like the other models) and
shows it on every live tile and in the opened decision: tournament level,
blinds, a pulsing Bubble / green "im Geld" capsule and one tick per
remaining player (filled = paid place, a bar above 40 players); K-Poker
as key caps with sizes and the move clock; Cash as a quiet chip.

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

86 lines
3.3 KiB
Swift

import Foundation
/// Was eine Nachricht vom Server bewirkt hat.
public enum Nachricht: Equatable, Sendable {
case ansicht(Set<Bereich>) // diese Bereiche kamen neu (auch kaputte: sie sind jetzt nil)
case rueckmeldung(Rueckmeldung) // Antwort auf einen Knopf
case unbekannt
}
public struct Rueckmeldung: Equatable, Sendable {
public var aktion: String?
public var ok: Bool
public var text: String
public var raum: String?
public var bild: String?
public init(aktion: String?, ok: Bool, text: String, raum: String?, bild: String?) {
self.aktion = aktion; self.ok = ok; self.text = text; self.raum = raum; self.bild = bild
}
/// Aus der Antwort von POST /api/steuern oder einer WebSocket-Rueckmeldung.
public init?(json: [String: Any]) {
guard let ok = json["ok"] as? Bool else { return nil }
self.init(aktion: json["aktion"] as? String, ok: ok, text: json["text"] as? String ?? (ok ? "erledigt" : "fehlgeschlagen"),
raum: json["raum"] as? String, bild: json["bild"] as? String)
}
}
/// Der Stand der Ansicht, wie ihn der Server ueber /ws schickt: beim Verbinden alle Bereiche,
/// danach nur die geaenderten (sicht/ANSICHT.md).
public struct Ansicht: Equatable, Sendable {
public var kopf: Kopf?
public var jetzt: Jetzt?
public var wachstum: Wachstum?
public var entscheidungen: Entscheidungen?
public var geld: Geld?
public var sitzen: Sitzen?
public var stil: Stil?
public var ticker: Ticker?
public var stand: Double?
public init() {}
/// Eine Textnachricht einarbeiten. Wirft nie; Unlesbares ist `.unbekannt`, ein unlesbarer
/// Bereich wird nil (die App zeigt dann seinen Leerzustand), die anderen kommen trotzdem an.
public mutating func anwenden(_ text: String) -> Nachricht {
guard let daten = text.data(using: .utf8),
let json = (try? JSONSerialization.jsonObject(with: daten)) as? [String: Any] else { return .unbekannt }
switch json["typ"] as? String {
case "ansicht":
guard let teile = json["teile"] as? [String: Any] else { return .unbekannt }
var neu = Set<Bereich>()
for (name, wert) in teile {
guard let b = Bereich(rawValue: name) else { continue }
setzen(b, wert)
neu.insert(b)
}
if let s = json["stand"] as? Double { stand = s }
return .ansicht(neu)
case "rueckmeldung":
return Rueckmeldung(json: json).map(Nachricht.rueckmeldung) ?? .unbekannt
default:
return .unbekannt
}
}
private mutating func setzen(_ b: Bereich, _ wert: Any) {
switch b {
case .kopf: kopf = Self.lesen(wert)
case .jetzt: jetzt = Self.lesen(wert)
case .wachstum: wachstum = Self.lesen(wert)
case .entscheidungen: entscheidungen = Self.lesen(wert)
case .geld: geld = Self.lesen(wert)
case .sitzen: sitzen = Self.lesen(wert)
case .stil: stil = Self.lesen(wert)
case .ticker: ticker = Self.lesen(wert)
}
}
private static func lesen<T: Decodable>(_ wert: Any) -> T? {
guard wert is [String: Any],
let d = try? JSONSerialization.data(withJSONObject: wert) else { return nil }
return try? JSONDecoder().decode(T.self, from: d)
}
}