import Foundation // Der Zustand der Sendung, wie ihn der Server als `sendung` schickt (sicht/sendung_knoepfe.ansicht): // {twitch: {...}, chat: {leitung, an, faelle, fragen, hinweis}}. Nachsichtig gelesen - nie ein // Schluessel, fehlende Felder sind leer. Spec: mac/Tests/Pruefung/SendungSpec.swift. public struct Sendung: Decodable, Equatable, Sendable { public var twitch = Twitch() public var chat = Chat() /// Eine reine Ansichtsmeldung (kein Knopf): der Chatfaden meldet von sich aus. public static let lesen = "sendung_lesen" public var live: Bool { chat.an } enum CodingKeys: String, CodingKey { case twitch, chat } public init() {} public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) twitch = (try? c.decode(Twitch.self, forKey: .twitch)) ?? Twitch() chat = (try? c.decode(Chat.self, forKey: .chat)) ?? Chat() } /// Aus dem `sendung`-Feld einer Rueckmeldung (JSONSerialization-Objekt). public static func aus(_ roh: Any?) -> Sendung? { guard let roh, JSONSerialization.isValidJSONObject(roh), // kein Objekt -> nil, nie ein Wurf let data = try? JSONSerialization.data(withJSONObject: roh) else { return nil } return try? JSONDecoder().decode(Sendung.self, from: data) } public struct Twitch: Decodable, Equatable, Sendable { public var clientId: String? public var angemeldet = false public var login: String? public var anmeldung: Anmeldung? enum CodingKeys: String, CodingKey { case clientId = "client_id", angemeldet, login, anmeldung } public init() {} public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) clientId = c.text(.clientId) angemeldet = c.wahr(.angemeldet) login = c.text(.login) anmeldung = (try? c.decodeIfPresent(Anmeldung.self, forKey: .anmeldung)) ?? nil } } /// Eine laufende Geraetecode-Anmeldung: der Nutzer tippt `code` auf `adresse`, gueltig bis `bis`. public struct Anmeldung: Decodable, Equatable, Sendable { public var code: String? public var adresse: String? public var bis: Double? enum CodingKeys: String, CodingKey { case code, adresse, bis } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) code = c.text(.code); adresse = c.text(.adresse); bis = c.zahl(.bis) } } public struct Chat: Decodable, Equatable, Sendable { public var leitung: String? // "aus" | "an" | ... (Zustand der Chatleitung) public var an = false public var hinweis: String? public var faelle: [Fall] = [] public var fragen: [Frage] = [] enum CodingKeys: String, CodingKey { case leitung, an, hinweis, faelle, fragen } public init() {} public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) leitung = c.text(.leitung) an = c.wahr(.an) hinweis = c.text(.hinweis) faelle = c.objekte(.faelle) fragen = c.objekte(.fragen) } } /// Ein Moderationsfall (sendung/moderation.Fall) - nur fuers Cockpit, mit "Zuruecknehmen". public struct Fall: Decodable, Equatable, Sendable, Identifiable { public var nummer = 0 public var nutzer: String? public var kennung: String? public var text: String? public var grund: String? public var strafe: String? // "timeout" | "bann" public var zeit: Double? public var zurueckgenommen = false public var id: Int { nummer } enum CodingKeys: String, CodingKey { case nummer, nutzer, kennung, text, grund, strafe, zeit, zurueckgenommen } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) nummer = Int(c.zahl(.nummer) ?? 0) nutzer = c.text(.nutzer); kennung = c.text(.kennung); text = c.text(.text) grund = c.text(.grund); strafe = c.text(.strafe); zeit = c.zahl(.zeit) zurueckgenommen = c.wahr(.zurueckgenommen) } } /// Eine beantwortete Frage (sendung/moderator): Name, Frage, Antwort. public struct Frage: Decodable, Equatable, Sendable, Identifiable { public var name: String? public var frage: String? public var antwort: String? public var zeit: Double? public var id: String { "\(zeit ?? 0)-\(name ?? "")" } enum CodingKeys: String, CodingKey { case name, frage, antwort, zeit } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) name = c.text(.name); frage = c.text(.frage); antwort = c.text(.antwort); zeit = c.zahl(.zeit) } } }