diff --git a/lesen/profil.py b/lesen/profil.py index 6f327c7..2c56780 100644 --- a/lesen/profil.py +++ b/lesen/profil.py @@ -132,6 +132,17 @@ def profile_je_sitz(text): return ergebnis +def aus_sitzliste(liste, werte): + """{sitz: profil} aus einer Sitzliste (seatResponseDataList einer Bohne) und den Statistiken + je Kennung. Je Raum gerufen (lesen/spot_from_log.py): Platz 2 an zwei Tischen sind zwei Leute + (live main.6 - ein Profil für das ganze Log gab beiden Tischen dieselben Werte).""" + profile = {} + for sitz in liste or (): + if isinstance(sitz, dict) and sitz.get("seatId") is not None and str(sitz.get("userId")) in werte: + profile[int(sitz["seatId"])] = werte[str(sitz["userId"])] + return profile + + def mit_gezeigten(profile, aufgedeckt, raum=None, ohne=None, maximal=10): """Haengt aufgedeckte Karten an die Profile: `gezeigt` = die letzten `maximal` ganz gezeigten Haende des Sitzes (Liste von Kartenlisten, z. B. [["Ah", "Kd"]]), alt -> neu. diff --git a/lesen/spot_from_log.py b/lesen/spot_from_log.py index b4f9114..996d3b8 100644 --- a/lesen/spot_from_log.py +++ b/lesen/spot_from_log.py @@ -442,7 +442,7 @@ def spots(text, beobachter=None): `beobachter(cmd, daten, tisch)` (nur für Prüfungen) wird nach jeder Bohne mit dem Zustand gerufen, den sie hinterlassen hat.""" tische = {} - profile = profil.profile_je_sitz(text) + werte = profil.statistiken(text) # je Kennung; das Profil je Sitz kommt aus der Sitzliste des Raums hole = {} ergebnis = [] beitrittsanfrage = None @@ -471,7 +471,9 @@ def spots(text, beobachter=None): if not isinstance(d, dict): continue t = tische.setdefault(raum, Tisch(raum)) - t.profile = profile + liste = d.get("seatResponseDataList") or (d.get("seatInfoRsponseData") or {}).get("seatResponseDataList") + if liste: # Platz 2 an zwei Tischen sind zwei Leute (live main.6) + t.profile = profil.aus_sitzliste(liste, werte) if cmd == "game.game_alldata": g = d.get("gameInitResponseData") or {} diff --git a/mac/Sources/Cockpit/Bausteine.swift b/mac/Sources/Cockpit/Bausteine.swift index 41dfe19..c85a650 100644 --- a/mac/Sources/Cockpit/Bausteine.swift +++ b/mac/Sources/Cockpit/Bausteine.swift @@ -203,4 +203,17 @@ enum Aktionstext { guard let bb, !t.localizedCaseInsensitiveContains("bb") else { return t } return t + " \(Format.zahl(bb)) bb" } + + static func kurz(_ aktion: String, _ bb: Double?) -> String { Aktionskuerzel.kurz(aktion, bb) } + + /// Fold grau, passiv grün, aggressiv orange, All-in rot. + static func farbe(_ aktion: String) -> Color { + switch aktion { + case "FOLD": .gray + case "CHECK", "CALL": .green + case "RAISE", "BET": .orange + case "ALL_IN": .red + default: .white.opacity(0.7) + } + } } diff --git a/mac/Sources/Cockpit/LiveTische.swift b/mac/Sources/Cockpit/LiveTische.swift index 941906c..a695bee 100644 --- a/mac/Sources/Cockpit/LiveTische.swift +++ b/mac/Sources/Cockpit/LiveTische.swift @@ -127,7 +127,7 @@ struct KachelAnsicht: View { symbolKnopf("camera", "Foto") { cockpit.steuern("foto", raum: k.raum) } symbolKnopf("xmark", "Tisch schließen") { cockpit.steuern("tische_schliessen", raum: k.raum) } } - MiniTisch(plaetze: k.raumInfo?.groesse ?? 6, board: k.board, topf: topf, zug: k.zug) + MiniTisch(plaetze: k.raumInfo?.groesse ?? 6, board: k.board, topf: topf, zug: k.zug, tisch: k.raumInfo?.tisch) if !k.karten.isEmpty { HStack(spacing: 4) { ForEach(Array(k.karten.enumerated()), id: \.offset) { KarteAnsicht(code: $0.element, gross: true) } } } @@ -144,6 +144,14 @@ struct KachelAnsicht: View { } else { Text(k.hinweis ?? "noch keine Entscheidung an diesem Tisch").font(.caption).foregroundStyle(.secondary) } + if let t = k.raumInfo?.tisch, t.spieler.contains(where: { !$0.ich }) { + HStack { + Text("Gegner").font(.caption2.weight(.semibold)).foregroundStyle(.secondary) + Spacer() + Text("VPIP/PFR/Fold to Bet").font(.system(size: 9)).foregroundStyle(.tertiary) + } + SpielerListe(tisch: t) + } if let r = k.raumInfo, r.platz != nil || r.stack != nil { Text("Platz \(r.platz ?? "–") · Stack \(Format.auto(r.stack))" + (r.bb.map { " · BB \($0)" } ?? "")) .font(.caption2).foregroundStyle(.tertiary) diff --git a/mac/Sources/Cockpit/Tischbild.swift b/mac/Sources/Cockpit/Tischbild.swift index 1c6861b..90b4e88 100644 --- a/mac/Sources/Cockpit/Tischbild.swift +++ b/mac/Sources/Cockpit/Tischbild.swift @@ -8,6 +8,8 @@ struct MiniTisch: View { var board: [String] var topf: Double? var zug: Zug? + /// Mit Plätzen aus dem Log: jeder Sitz als Schild (Name, Stack, Rolle, Aktion). + var tisch: TischAnsicht? = nil private static let filzHell = Color(red: 0.08, green: 0.45, blue: 0.30) private static let filzDunkel = Color(red: 0.03, green: 0.25, blue: 0.17) @@ -19,8 +21,8 @@ struct MiniTisch: View { let w: Double = g.size.width let h: Double = g.size.height let mitte = CGPoint(x: w / 2, y: h / 2 - 6) - let rx: Double = w / 2 - 22 - let ry: Double = h / 2 - 20 + let rx: Double = w / 2 - (tisch == nil ? 22 : 40) + let ry: Double = h / 2 - (tisch == nil ? 20 : 26) ZStack { filz(breite: rx * 2 + 16, hoehe: ry * 2 + 14, radius: w / 2) .position(x: mitte.x, y: mitte.y) @@ -30,7 +32,13 @@ struct MiniTisch: View { } } } - .frame(height: 118) + .frame(height: tisch == nil ? 118 : 176) + } + + /// Der Spieler an Stelle i (0 = unten, dein Platz). + private func spieler(an i: Int) -> Spieler? { + guard let t = tisch else { return nil } + return t.spieler.first { Sitzlage.stelle(sitz: $0.sitz, ich: t.ich, plaetze: sitzzahl) == i } } private var sitzzahl: Int { max(2, plaetze) } @@ -63,7 +71,11 @@ struct MiniTisch: View { } @ViewBuilder private func sitz(_ i: Int) -> some View { - if i == 0 { + if let s = spieler(an: i) { + SitzSchild(spieler: s, zug: s.ich ? zug : nil) + } else if tisch != nil { + Circle().fill(Self.sitzFarbe.opacity(0.5)).frame(width: 12, height: 12) // frei + } else if i == 0 { EigenerPlatz(zug: zug) } else { Circle().fill(Self.sitzFarbe).frame(width: 16, height: 16) @@ -72,6 +84,104 @@ struct MiniTisch: View { } } +/// Ein Platz am Tisch: Name, Stack in BB, Rolle (BTN/SB/BB) und die letzte Aktion dieser Hand. +/// Wer aussitzt, ist blass; dein Platz trägt den Countdown-Ring. +struct SitzSchild: View { + var spieler: Spieler + var zug: Zug? + + var body: some View { + VStack(spacing: 1) { + HStack(spacing: 2) { + if spieler.ich { EigenerPlatz(zug: zug).scaleEffect(0.62).frame(width: 16, height: 16) } + if let r = spieler.rolle { RollenChip(rolle: r) } + Text(spieler.name).font(.system(size: 9, weight: .semibold)).lineLimit(1).truncationMode(.tail) + } + Text(spieler.stackBB.map { "\(Format.auto($0)) bb" } ?? "–") + .font(.system(size: 8.5, weight: .medium).monospacedDigit()).foregroundStyle(.white.opacity(0.75)) + if let a = spieler.aktion { + Text(Aktionstext.kurz(a, spieler.betragBB)) + .font(.system(size: 8, weight: .bold)).foregroundStyle(Aktionstext.farbe(a)) + } + } + .padding(.horizontal, 4).padding(.vertical, 2) + .frame(maxWidth: 74) + .background(spieler.ich ? Color.accentColor.opacity(0.35) : Color.black.opacity(0.45), + in: RoundedRectangle(cornerRadius: 6)) + .opacity(spieler.draussen ? 0.45 : 1) + } +} + +struct RollenChip: View { + var rolle: String + var body: some View { + Text(rolle == "BTN" ? "D" : rolle).font(.system(size: 7, weight: .heavy)) + .padding(.horizontal, 2.5).padding(.vertical, 0.5) + .foregroundStyle(rolle == "BTN" ? Color.black : Color.white) + .background(rolle == "BTN" ? Color.white : Color.blue.opacity(0.8), in: Capsule()) + } +} + +/// Die Gegner als Liste: Rolle, Name, Stack, Aktion - und was der Bot über sie weiss (VPIP, PFR, +/// Fold to Bet aus hud_stats) mit einer Einordnung in einem Wort. +struct SpielerListe: View { + var tisch: TischAnsicht + + var body: some View { + VStack(alignment: .leading, spacing: 3) { + ForEach(tisch.spieler.filter { !$0.ich }) { s in zeile(s) } + } + } + + private func zeile(_ s: Spieler) -> some View { + HStack(spacing: 5) { + Group { + if let r = s.rolle { RollenChip(rolle: r) } else { Color.clear } + } + .frame(width: 18) + Text(s.name).font(.caption2.weight(.semibold)).lineLimit(1).frame(maxWidth: 96, alignment: .leading) + Text(s.stackBB.map { "\(Format.auto($0)) bb" } ?? "–").font(.caption2.monospacedDigit()) + .foregroundStyle(.secondary).frame(width: 52, alignment: .trailing) + if let a = s.aktion { + Text(Aktionstext.kurz(a, s.betragBB)).font(.caption2.weight(.bold)).foregroundStyle(Aktionstext.farbe(a)) + } + Spacer(minLength: 2) + profil(s.profil) + } + .opacity(s.draussen ? 0.5 : 1) + } + + @ViewBuilder private func profil(_ p: Spieler.Profil?) -> some View { + if let p { + HStack(spacing: 4) { + if let t = p.typ { Abzeichen(text: t.kurz, farbe: Self.farbe(t.kurz)).help(t.lang) } + Text(werte(p)).font(.system(size: 9).monospacedDigit()).foregroundStyle(.secondary) + if let d = Spielertyp.druck(p.foldToBet) { + Text(d).font(.system(size: 9, weight: .medium)).foregroundStyle(.orange) + } + } + } else { + Text("noch keine Werte").font(.system(size: 9)).foregroundStyle(.tertiary) + } + } + + private func werte(_ p: Spieler.Profil) -> String { + let z = { (x: Double?) in x.map { "\(Int(($0 * 100).rounded()))" } ?? "–" } + return "\(z(p.vpip))/\(z(p.pfr))/\(z(p.foldToBet))" + } + + static func farbe(_ typ: String) -> Color { + switch typ { + case "Fisch": .green + case "Maniac": .red + case "Loose": .orange + case "Nit": .gray + case "TAG": .blue + default: .purple + } + } +} + /// Der eigene Platz: pulsiert, solange ein Zug offen ist; der Ring ist die Restzeit. struct EigenerPlatz: View { var zug: Zug? diff --git a/mac/Sources/CockpitKern/LiveKachel.swift b/mac/Sources/CockpitKern/LiveKachel.swift index 689204d..75335f0 100644 --- a/mac/Sources/CockpitKern/LiveKachel.swift +++ b/mac/Sources/CockpitKern/LiveKachel.swift @@ -24,7 +24,9 @@ public struct LiveKachel: Equatable, Sendable, Identifiable { var raeume: [String] = [] func dazu(_ r: String?) { if let r, !r.isEmpty, !raeume.contains(r) { raeume.append(r) } } if laeuft { dazu(a.jetzt?.raum) } - for r in a.sitzen?.raeume ?? [] where r.platz != nil || besetzt.contains(r.zustand ?? "") { dazu(r.raum) } + for r in a.sitzen?.raeume ?? [] where r.platz != nil || besetzt.contains(r.zustand ?? "") || (r.fenster ?? 0) > 0 { + dazu(r.raum) // offenes Fenster: immer sehen, was er am Tisch erkennt + } if laeuft { (a.kopf?.tische ?? []).forEach(dazu) } return raeume.map { kachel($0, a) } } @@ -48,6 +50,12 @@ public struct LiveKachel: Equatable, Sendable, Identifiable { k.aktion = d.aktionText ?? d.aktion; k.betragBB = d.betragBB; k.equity = d.equity; k.klartext = d.klartext k.klick = Klick(status: d.klick, warum: d.klickWarum, beleg: d.beleg) } + if let t = info?.tisch, t.ich == nil, !(a.jetzt?.raum == raum && a.jetzt?.zug?.offen == true) { + // Kein eigener Platz an diesem Tisch: eigene Karten und Entscheidung wären von früher + k.karten = []; k.aktion = nil; k.betragBB = nil; k.equity = nil; k.klartext = nil; k.klick = nil + k.hinweis = "du sitzt an diesem Tisch nicht" + return k + } if k.aktion == nil, let h = info?.handlung, let art = h.art { k.hinweis = art + (h.grund.map { ": " + $0 } ?? "") } diff --git a/mac/Sources/CockpitKern/Modelle.swift b/mac/Sources/CockpitKern/Modelle.swift index 44113d4..533f245 100644 --- a/mac/Sources/CockpitKern/Modelle.swift +++ b/mac/Sources/CockpitKern/Modelle.swift @@ -327,6 +327,8 @@ public struct Raum: Decodable, Equatable, Sendable, Identifiable { public var fenster: Int? /// Die laufende Hand aus dem Client-Log (sicht/ansicht._haende_im_log) - auch ohne Läufer. public var hand: LogHand? + /// Alle Plätze mit Namen, Stack, Rolle, Aktion und Gegnerwerten (sicht/ansicht._tisch_ansicht). + public var tisch: TischAnsicht? public var id: String { raum } public struct LogHand: Decodable, Equatable, Sendable { @@ -341,7 +343,7 @@ public struct Raum: Decodable, Equatable, Sendable, Identifiable { } } - enum CodingKeys: String, CodingKey { case raum, zustand, platz, stack, groesse, bb, modus, handlung, fenster, hand } + enum CodingKeys: String, CodingKey { case raum, zustand, platz, stack, groesse, bb, modus, handlung, fenster, hand, tisch } public init(from d: Decoder) throws { let c = try d.container(keyedBy: CodingKeys.self) @@ -349,6 +351,7 @@ public struct Raum: Decodable, Equatable, Sendable, Identifiable { groesse = c.zahl(.groesse).map { Int($0) }; bb = c.text(.bb); modus = c.text(.modus) handlung = try? c.decodeIfPresent(Handlung.self, forKey: .handlung); fenster = c.zahl(.fenster).map { Int($0) } hand = (try? c.decodeIfPresent(LogHand.self, forKey: .hand)) ?? nil + tisch = (try? c.decodeIfPresent(TischAnsicht.self, forKey: .tisch)) ?? nil } } diff --git a/mac/Sources/CockpitKern/Spieler.swift b/mac/Sources/CockpitKern/Spieler.swift new file mode 100644 index 0000000..3955fa9 --- /dev/null +++ b/mac/Sources/CockpitKern/Spieler.swift @@ -0,0 +1,101 @@ +import Foundation + +/// Ein Tisch für die Vorschau (sicht/ansicht._tisch_ansicht): jeder Platz aus dem Log, auch ohne Läufer. +public struct TischAnsicht: Decodable, Equatable, Sendable { + public var hand: String? + public var dealer: Int? + public var bb: Double? + public var strasse: String? + public var spieler: [Spieler] = [] + + enum CodingKeys: String, CodingKey { case hand, dealer, bb, strasse, spieler } + public init(from d: Decoder) throws { + let c = try d.container(keyedBy: CodingKeys.self) + hand = c.text(.hand); dealer = c.zahl(.dealer).map { Int($0) }; bb = c.zahl(.bb) + strasse = c.text(.strasse); spieler = c.objekte(.spieler) + } + + /// Der eigene Platz - unten in der Vorschau. + public var ich: Int? { spieler.first(where: \.ich)?.sitz } +} + +public struct Spieler: Decodable, Equatable, Sendable, Identifiable { + public struct Profil: Decodable, Equatable, Sendable { + public var vpip: Double? + public var pfr: Double? + public var foldToBet: Double? + enum CodingKeys: String, CodingKey { case vpip, pfr, foldToBet = "fold_to_bet" } + public init(from d: Decoder) throws { + let c = try d.container(keyedBy: CodingKeys.self) + vpip = c.zahl(.vpip); pfr = c.zahl(.pfr); foldToBet = c.zahl(.foldToBet) + } + public var typ: Spielertyp? { Spielertyp(vpip: vpip, pfr: pfr) } + } + + public var sitz: Int + public var name: String + public var ich: Bool + public var stackBB: Double? + public var rolle: String? + public var aktion: String? + public var betragBB: Double? + public var einsatzBB: Double? + public var status: String? + public var draussen: Bool + public var profil: Profil? + public var id: Int { sitz } + + enum CodingKeys: String, CodingKey { + case sitz, name, ich, rolle, aktion, status, draussen, profil + case stackBB = "stack_bb", betragBB = "betrag_bb", einsatzBB = "einsatz_bb" + } + public init(from d: Decoder) throws { + let c = try d.container(keyedBy: CodingKeys.self) + sitz = c.zahl(.sitz).map { Int($0) } ?? 0; name = c.text(.name) ?? "?"; ich = c.wahr(.ich) + stackBB = c.zahl(.stackBB); rolle = c.text(.rolle); aktion = c.text(.aktion); betragBB = c.zahl(.betragBB) + einsatzBB = c.zahl(.einsatzBB); status = c.text(.status); draussen = c.wahr(.draussen) + profil = (try? c.decodeIfPresent(Profil.self, forKey: .profil)) ?? nil + } +} + +/// Wo ein Platz in der Vorschau liegt: dein Platz unten (Stelle 0), die anderen im Uhrzeigersinn. +public enum Sitzlage { + public static func stelle(sitz: Int, ich: Int?, plaetze: Int) -> Int { + let n = max(2, plaetze) + return (((sitz - (ich ?? 1)) % n) + n) % n + } +} + +/// Der Gegner in einem Wort aus VPIP und PFR (die üblichen HUD-Schwellen), dazu Fold to Bet. +public struct Spielertyp: Equatable, Sendable { + public var kurz: String + public var lang: String + + public init?(vpip: Double?, pfr: Double?) { + guard let v = vpip, let p = pfr else { return nil } + switch (v, p) { + case _ where v >= 0.40 && p < 0.15: (kurz, lang) = ("Fisch", "spielt viel, erhöht selten") + case _ where v >= 0.40: (kurz, lang) = ("Maniac", "spielt viel und aggressiv") + case _ where v >= 0.30: (kurz, lang) = ("Loose", "spielt viele Hände") + case _ where v < 0.16: (kurz, lang) = ("Nit", "spielt kaum") + case _ where p >= v * 0.7: (kurz, lang) = ("TAG", "eng und aggressiv") + default: (kurz, lang) = ("Reg", "solide") + } + } + + public static func druck(_ foldToBet: Double?) -> String? { + guard let f = foldToBet else { return nil } + return f >= 0.6 ? "foldet viel" : f <= 0.35 ? "zahlt viel" : nil + } +} + +/// Die letzte Aktion eines Platzes in wenigen Zeichen, Beträge in BB. +public enum Aktionskuerzel { + public static func kurz(_ aktion: String, _ bb: Double?) -> String { + let namen = ["FOLD": "Fold", "CHECK": "Check", "CALL": "Call", "RAISE": "Raise", "BET": "Bet", + "ALL_IN": "All-in", "POST_SB": "SB", "POST_BB": "BB", "POST_ANTE": "Ante"] + let name = namen[aktion] ?? aktion.capitalized + guard let bb, bb > 0, aktion != "FOLD", aktion != "CHECK" else { return name } + return "\(name) \(Format.zahl(bb, stellen: bb.rounded() == bb ? 0 : 1))" + } +} diff --git a/mac/Tests/Pruefung/NachrichtSpec.swift b/mac/Tests/Pruefung/NachrichtSpec.swift index e4a8255..5292deb 100644 --- a/mac/Tests/Pruefung/NachrichtSpec.swift +++ b/mac/Tests/Pruefung/NachrichtSpec.swift @@ -107,6 +107,16 @@ struct LiveSpec { #expect(LiveKachel.alle(a).map(\.raum) == ["A"]) } + @Test("jeder Tisch mit offenem Fenster hat eine Kachel - auch ohne Laeufer und ohne Sitz (live 30.10.)") + func offeneFenster() { + let a = ansicht(#""" + {"kopf":{"laeuft":false}, + "sitzen":{"raeume":[{"raum":"A","zustand":"frei","fenster":1},{"raum":"X","zustand":"frei","fenster":0}, + {"raum":"S","zustand":"ausgesetzt"}]}} + """#) + #expect(LiveKachel.alle(a).map(\.raum) == ["A", "S"]) + } + @Test("die Kachel des Zugs nimmt jetzt, andere die letzte Entscheidung ihres Raums") func inhalt() { let a = ansicht(#""" diff --git a/mac/Tests/Pruefung/SpielerSpec.swift b/mac/Tests/Pruefung/SpielerSpec.swift new file mode 100644 index 0000000..5f7086a --- /dev/null +++ b/mac/Tests/Pruefung/SpielerSpec.swift @@ -0,0 +1,92 @@ +import CockpitKern +import Testing + +private func ansicht(_ json: String) -> Ansicht { + var a = Ansicht() + _ = a.anwenden(#"{"typ":"ansicht","teile":"# + json + "}") + return a +} + +@Suite("Vorschau: jeder Platz mit Name, Stack, Rolle, Aktion und Gegnerwerten") +struct SpielerSpec { + @Test("aus sitzen.raeume[].tisch gelesen") + func gelesen() { + let a = ansicht(#""" + {"sitzen":{"raeume":[{"raum":"A","zustand":"gesessen","platz":3, + "tisch":{"hand":"h1","dealer":2,"bb":2000,"strasse":"PREFLOP","spieler":[ + {"sitz":2,"name":"Thoughtknox","ich":false,"stack":2973358,"stack_bb":1486.7,"rolle":"BTN", + "aktion":"RAISE","betrag_bb":37.5,"einsatz_bb":37.5,"status":"Raise","draussen":false, + "profil":{"vpip":0.87,"pfr":0.45,"fold_to_bet":0.3}}, + {"sitz":3,"name":"mmintel","ich":true,"stack_bb":38.8,"rolle":null,"aktion":null,"draussen":true,"profil":null}]}}]}} + """#) + let t = a.sitzen?.raeume.first?.tisch + #expect(t?.dealer == 2) + #expect(t?.spieler.count == 2) + let s = t?.spieler.first + #expect(s?.name == "Thoughtknox") + #expect(s?.rolle == "BTN") + #expect(s?.betragBB == 37.5) + #expect(s?.profil?.vpip == 0.87) + #expect(t?.spieler.last?.ich == true) + #expect(t?.spieler.last?.draussen == true) + } + + @Test("dein Platz liegt unten, die anderen im Uhrzeigersinn") + func lage() { + #expect(Sitzlage.stelle(sitz: 3, ich: 3, plaetze: 6) == 0) + #expect(Sitzlage.stelle(sitz: 4, ich: 3, plaetze: 6) == 1) + #expect(Sitzlage.stelle(sitz: 1, ich: 3, plaetze: 6) == 4) + #expect(Sitzlage.stelle(sitz: 2, ich: nil, plaetze: 6) == 1) // ohne eigenen Platz: Platz 1 unten + } + + @Test("Spielertyp aus VPIP und PFR") + func typ() { + #expect(Spielertyp(vpip: 0.55, pfr: 0.08)?.kurz == "Fisch") + #expect(Spielertyp(vpip: 0.50, pfr: 0.38)?.kurz == "Maniac") + #expect(Spielertyp(vpip: 0.34, pfr: 0.20)?.kurz == "Loose") + #expect(Spielertyp(vpip: 0.12, pfr: 0.08)?.kurz == "Nit") + #expect(Spielertyp(vpip: 0.22, pfr: 0.18)?.kurz == "TAG") + #expect(Spielertyp(vpip: 0.26, pfr: 0.12)?.kurz == "Reg") + #expect(Spielertyp(vpip: nil, pfr: 0.2) == nil) + } + + @Test("Fold to Bet als Hinweis") + func druck() { + #expect(Spielertyp.druck(0.65) == "foldet viel") + #expect(Spielertyp.druck(0.30) == "zahlt viel") + #expect(Spielertyp.druck(0.5) == nil) + #expect(Spielertyp.druck(nil) == nil) + } +} + +@Suite("Aktion am Platz, kurz") +struct AktionKurzSpec { + @Test func kurz() { + #expect(Aktionskuerzel.kurz("RAISE", 37.5) == "Raise 37,5") + #expect(Aktionskuerzel.kurz("CALL", 2) == "Call 2") + #expect(Aktionskuerzel.kurz("FOLD", nil) == "Fold") + #expect(Aktionskuerzel.kurz("ALL_IN", 40) == "All-in 40") + #expect(Aktionskuerzel.kurz("POST_BB", 1) == "BB 1") + #expect(Aktionskuerzel.kurz("CHECK", nil) == "Check") + #expect(Aktionskuerzel.kurz("BET", 0) == "Bet") // ohne Betrag kein "0" + } +} + + +@Suite("Kachel ohne eigenen Platz") +struct OhnePlatzSpec { + @Test("sitzt du nicht am Tisch, zeigt die Kachel keine eigenen Karten und keine alte Entscheidung (live 30.10.)") + func ohnePlatz() { + let a = ansicht(#""" + {"kopf":{"laeuft":false}, + "entscheidungen":{"zeilen":[{"raum":"A","hand":"h1","aktion":"FOLD","karten":["9s","3s"]}]}, + "sitzen":{"raeume":[{"raum":"A","zustand":"voll","fenster":1, + "hand":{"hand":"h1","karten":["9s","3s"],"board":[],"topf":null}, + "tisch":{"dealer":2,"spieler":[{"sitz":2,"name":"X","ich":false}]}}]}} + """#) + let k = LiveKachel.alle(a)[0] + #expect(k.karten.isEmpty) + #expect(k.aktion == nil) + #expect(k.hinweis == "du sitzt an diesem Tisch nicht") + } +} diff --git a/sicht/ansicht.py b/sicht/ansicht.py index 50e54b9..e60ebe6 100644 --- a/sicht/ansicht.py +++ b/sicht/ansicht.py @@ -504,11 +504,7 @@ def _offener_zug(q, jetzt, entschieden): text, t_log = q.get("log") or "", _zahl(q.get("log_zeit")) if not text or t_log is None: return None - try: - import spot_from_log - spots = spot_from_log.spots(text) - except Exception: - return None + spots = _log_auswertung(text)[0] if not spots: return None zeile, kurz, raum, hand, takt = spots[-1] @@ -915,6 +911,60 @@ def _haende_im_log(text): return haende +_LOG_CACHE = {"schluessel": None, "wert": ([], {})} + + +def _log_auswertung(text): + """Einmal je Logstand: die eigenen Züge und der Zustand je Tisch (lesen/spot_from_log.py) - + der Server baut alle 0,5 s, das Log ändert sich seltener.""" + schluessel = (len(text or ""), (text or "")[-4096:]) + if _LOG_CACHE["schluessel"] == schluessel: + return _LOG_CACHE["wert"] + tische = {} + try: + import spot_from_log + spots = spot_from_log.spots(text or "", beobachter=lambda cmd, d, t: tische.__setitem__(t.room, t)) + except Exception: + spots, tische = [], {} + _LOG_CACHE.update(schluessel=schluessel, wert=(spots, tische)) + return spots, tische + + +def _bb(wert, bb): + return round(wert / bb, 1) if _zahl(wert) is not None and bb else None + + +def _tisch_ansicht(t, ich_sitz): + """Alle Plätze eines Tisches für die Vorschau: Name, Stack, Rolle (BTN/SB/BB), letzte Aktion + dieser Hand mit Betrag, Einsatz auf der laufenden Straße, Status und Gegnerwerte.""" + if t is None or not t.namen: + return None + bb = t.bb + strasse = t.verlauf[-1]["street"] if t.verlauf else None + letzte, einsatz, blinds = {}, {}, {} + for v in t.verlauf: + sitz, art = v.get("seat"), v.get("action") + if art in ("POST_SB", "POST_BB"): + blinds[sitz] = art[-2:] + if art != "POST_ANTE": + letzte[sitz] = v + if v.get("street") == strasse and _zahl(v.get("amount")) is not None: + einsatz[sitz] = v["amount"] + spieler = [] + for sitz in sorted(t.namen): + name = t.namen[sitz] + ich = sitz == ich_sitz or (ich_sitz is None and name == t.ich) + a = letzte.get(sitz) or {} + p = None if ich else t.profile.get(sitz) + spieler.append({ + "sitz": sitz, "name": name, "ich": ich, "stack": _zahl(t.chips.get(sitz)), "stack_bb": _bb(t.chips.get(sitz), bb), + "rolle": "BTN" if sitz == t.button else blinds.get(sitz), + "aktion": a.get("action"), "betrag_bb": _bb(a.get("amount"), bb), "einsatz_bb": _bb(einsatz.get(sitz), bb), + "status": t.status.get(sitz), "draussen": bool(t.draussen(sitz)), + "profil": None if not p else {"vpip": p.get("vpip"), "pfr": p.get("pfr"), "fold_to_bet": p.get("foldToBet")}}) + return {"hand": t.hand, "dealer": t.button, "bb": bb, "strasse": strasse, "spieler": spieler} + + def sitzen_(q, jetzt, lagen=None): lagen = lagen if lagen is not None else _lagen(q)[0] handlungen = {} @@ -930,6 +980,7 @@ def sitzen_(q, jetzt, lagen=None): je_nummer[nr] = je_nummer.get(nr, 0) + 1 raeume = [] haende = _haende_im_log(q.get("log")) + tische = _log_auswertung(q.get("log") or "")[1] if q.get("log") else {} for raum in sorted(set(lagen) | set(handlungen)): l = lagen.get(raum) if l is None: @@ -950,6 +1001,7 @@ def sitzen_(q, jetzt, lagen=None): "groesse": l.groesse if l else None, "bb": l.bb if l else None, "modus": l.modus if l else None, "handlung": handlungen.get(raum), "hand": haende.get(raum), + "tisch": _tisch_ansicht(tische.get(raum), l.held_sitz if l else None), "fenster": None if fenster is None else je_nummer.get(_nummer(raum) or "", 0)}) ueber = [] if fenster: diff --git a/tests/profil_spec.py b/tests/profil_spec.py index 8ac2906..4cc284d 100644 --- a/tests/profil_spec.py +++ b/tests/profil_spec.py @@ -94,5 +94,20 @@ class Reihenfolge(unittest.TestCase): self.assertAlmostEqual(s["1701711"]["vpip"], 0.40, places=4) +class AusSitzliste(unittest.TestCase): + """Live 30.10. (main.6): zwei Tische, an beiden ein Platz 2 - einmal du, einmal Thoughtknox. + Ein Profil je Sitz für das ganze Log gab beiden dieselben Werte. Das Profil kommt aus der + Sitzliste des Raums (lesen/spot_from_log.py ruft es je Bohne).""" + + def test_platz_nach_der_sitzliste_des_raums(self): + werte = profil.statistiken(KOMPLETT) + a = [{"seatId": 2, "userName": "SneS77", "userId": 1662615}] + b = [{"seatId": 2, "userName": "Chicago111", "userId": 1701711}, {"seatId": 3, "userId": 5}] + self.assertAlmostEqual(profil.aus_sitzliste(a, werte)[2]["vpip"], 0.29, places=4) + self.assertAlmostEqual(profil.aus_sitzliste(b, werte)[2]["vpip"], 0.26, places=4) + self.assertNotIn(3, profil.aus_sitzliste(b, werte)) # ohne Statistik kein Profil + self.assertEqual(profil.aus_sitzliste(None, werte), {}) + + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/tests/sicht_ansicht_spec.py b/tests/sicht_ansicht_spec.py index 48828b6..831ee22 100644 --- a/tests/sicht_ansicht_spec.py +++ b/tests/sicht_ansicht_spec.py @@ -161,7 +161,7 @@ class Jetzt(unittest.TestCase): "board": [{"rank": "TWO", "suit": "HEARTS"}], "pot": 1.0, "toCall": 0.0, "heroStack": 10.0} alt = sys.modules.get("spot_from_log") sys.modules["spot_from_log"] = types.SimpleNamespace( - spots=lambda text: [(json.dumps(anfrage), "9 FLOP toCall 0 Pot 1.0 Gegner 1", "Big Ben 123460", "9", 16)]) + spots=lambda text, beobachter=None: [(json.dumps(anfrage), "9 FLOP toCall 0 Pot 1.0 Gegner 1", "Big Ben 123460", "9", 16)]) try: strom = [{"art": "vorhaben", "raum": "Big Ben 123460", "aktion": "CHECK", "t": JETZT - 1, "warten_s": 0.7, "gedenkzeit_s": 1.2}] @@ -297,6 +297,45 @@ class TischVorschau(unittest.TestCase): self.assertEqual((h["hand"], h["board"], h["topf"]), ("h2", [], None)) +class Spieler(unittest.TestCase): + """Die Vorschau zeigt jeden Platz: Name, Stack, Rolle, Aktion, Einsatz und was der Bot über + den Gegner weiss (hud_stats: VPIP, PFR, Fold to Bet) - aus dem Log, auch ohne Läufer.""" + + @classmethod + def setUpClass(cls): + import gzip + pfad = os.path.join(_w, "archiv", "echt", "main.6.log.gz") + cls.text = gzip.open(pfad, "rt", encoding="utf-8", errors="replace").read() + + def raum(self, name): + r = ansicht.bauen(quelle(log=self.text, log_zeit=JETZT), JETZT)["sitzen"]["raeume"] + return next(x for x in r if x["raum"] == name) + + def test_jeder_platz_mit_namen_stack_und_rolle(self): + t = self.raum("Big Ben 1664488")["tisch"] + sp = {p["sitz"]: p for p in t["spieler"]} + self.assertEqual(t["dealer"], 2) + self.assertEqual(sp[2]["name"], "Thoughtknox") + self.assertEqual(sp[2]["rolle"], "BTN") + self.assertEqual(sp[6]["rolle"], "BB") + self.assertEqual(sp[2]["stack_bb"], round(2973358.0 / 2000.0, 1)) + self.assertEqual((sp[2]["aktion"], sp[2]["betrag_bb"]), ("RAISE", round(75024.0 / 2000.0, 1))) + self.assertTrue(sp[3]["ich"]) + self.assertFalse(sp[2]["ich"]) + + def test_gegnerwerte_je_raum(self): + sp = {p["sitz"]: p for p in self.raum("Big Ben 1664488")["tisch"]["spieler"]} + self.assertEqual(sp[2]["profil"], {"vpip": 0.87, "pfr": sp[2]["profil"]["pfr"], "fold_to_bet": sp[2]["profil"]["fold_to_bet"]}) + self.assertIsNone(sp[3]["profil"]) # über sich selbst nichts + andere = {p["sitz"]: p for p in self.raum("Big Ben 1664319")["tisch"]["spieler"]} + self.assertEqual(andere[2]["profil"]["vpip"], 0.89) # Platz 2 dort ist jemand anderes + + def test_ohne_log_kein_tisch(self): + z = {"gedaechtnis": {"Big Ben 1": {"groesse": 6}}} + r = ansicht.bauen(quelle(zustand=z), JETZT)["sitzen"]["raeume"][0] + self.assertIsNone(r["tisch"]) + + class StilUndTicker(unittest.TestCase): def test_anteile_groessen_denkzeit(self): s = [entscheidung(100 - i, hand=str(i), aktion="FOLD", gedenkzeit=0.5 + i * 0.1) for i in range(15)]