Files
pokerfreak/mac/Sources/Cockpit/Diagramme.swift
T
mmintel 9c79d8236b Acceptance 5.11.: channel play without AFK, quiet focus, working Cockpit, stronger play
- Practice tables: 20-min session 54/54 turns at the server, control session 28 turns,
  0 timeouts; stage: 3 tables, two rounds each, clean teardown.
- Never let the clock run: an engine abstention becomes check or fold (nie_verstreichen).
- Mittler join in a fixed order (kill the duplicate, then announce), a mittler ends with its
  game, the laufwache opens no tables, stop waits for the runner to stand up.
- Focus: no lobby connections from HUD, probes or layout; "Focus Window on Turn" off.
- Tables: disconnected or sitting-out opponents do not count; a table without channel is
  closed and avoided after 45 s; the join event keeps the writer's pid (strict invariants).
- Geldwache: same room list as the runner, the table's own report, stand up before closing.
- Engine: the small blind completes the top 65 % against limpers (price per fair share);
  the calling range of a bet is as wide as the villain calls - value bets into stations.
- Cockpit: the source tree never comes from SHELL; one operating set for laufwache and
  Cockpit (sicht/start.py --client, ServerStart).
- Project lives in ~/Projects/pokerfreak (kern was a detour).
2026-11-05 16:10:43 +01:00

292 lines
13 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
import Charts
import CockpitKern
import SwiftUI
/// Ergebnis und Stil unter einem Reiter.
struct StatistikAnsicht: View {
enum Teil: String, CaseIterable { case ergebnis = "Ergebnis", stil = "Stil" }
@AppStorage("statistik.teil") private var teil: Teil = .ergebnis
var body: some View {
VStack(spacing: 0) {
Picker("", selection: $teil) { ForEach(Teil.allCases, id: \.self) { Text($0.rawValue) } }
.pickerStyle(.segmented).labelsHidden().padding(.horizontal, 12).padding(.top, 10)
switch teil {
case .ergebnis: ErgebnisAnsicht()
case .stil: StilAnsicht()
}
}
}
}
struct ErgebnisAnsicht: View {
/// Übung und getrennt (je ab ihrem Nullpunkt), Archiv: alle Sitze im Bestand.
enum Grundlage: String, CaseIterable { case uebung = "Übung", cash = "Cash", archiv = "Archiv" }
enum Teil: String, CaseIterable { case kurve = "Kurve", verteilung = "Verteilung", stakes = "Stakes", stunden = "Stunden", stapel = "Stapel", sitzungen = "Sitzungen" }
@Environment(Cockpit.self) private var cockpit
@State private var grundlage: Grundlage?
@State private var fragtReset = false
@AppStorage("ergebnis.teil") private var teil: Teil = .kurve
private var gewaehlt: Grundlage { grundlage ?? (cockpit.wahl.cash ? .cash : .uebung) }
var body: some View {
let w = cockpit.ansicht.wachstum
let k: Kurven? = switch gewaehlt {
case .uebung: w?.uebung
case .cash: w?.echt
case .archiv: w?.bestand
}
ScrollView {
VStack(alignment: .leading, spacing: 12) {
Picker("", selection: Binding(get: { gewaehlt }, set: { grundlage = $0; fragtReset = false })) {
ForEach(Grundlage.allCases, id: \.self) { Text($0.rawValue) }
}
.pickerStyle(.segmented).labelsHidden()
HStack(alignment: .firstTextBaseline) {
if let g = k?.grundlage { Text(g).font(.caption).foregroundStyle(.secondary) }
Spacer()
if gewaehlt != .archiv { resetKnoepfe(k) }
}
if fragtReset { resetFrage }
if gewaehlt == .archiv { Text("Summe über alle Sitze im Archiv, nicht der eigene Verlauf.").font(.caption2).foregroundStyle(.tertiary) }
if let u = w?.unklar, u > 0, gewaehlt != .archiv {
Text("\(u) Hände ohne bekannten Modus zählen hier nicht mit.").font(.caption2).foregroundStyle(.tertiary)
}
kennzahlen(k)
if gewaehlt != .archiv { FormatVergleich(kurven: k) }
Picker("", selection: $teil) {
ForEach(Teil.allCases.filter { gewaehlt != .archiv || ($0 != .stapel && $0 != .sitzungen) }, id: \.self) { Text($0.rawValue) }
}
.pickerStyle(.menu).labelsHidden().fixedSize()
diagramm(k, w).frame(minHeight: 220)
}
.padding(12)
}
.overlay {
if k == nil || (k?.summe?.haende ?? 0) == 0 {
Leer(titel: k?.rechnet == true ? "Wird gerechnet" : "Noch keine Hände",
text: k?.leer ?? w?.leer ?? "Kurven erscheinen mit den ersten eigenen Händen.")
.background(.regularMaterial)
.allowsHitTesting(false)
}
}
}
private var modusDaten: String { #"{"modus":"\#(gewaehlt == .cash ? "cash" : "uebung")"}"# }
@ViewBuilder private func resetKnoepfe(_ k: Kurven?) -> some View {
HStack(spacing: 8) {
if k?.nullpunkt != nil {
Button("Ab Anfang") { cockpit.steuern("statistik_ab_anfang", daten: modusDaten) }
}
Button("Zurücksetzen") { fragtReset = true }
}
.buttonStyle(.borderless).font(.caption)
}
private var resetFrage: some View {
HStack {
Text("Statistik \(gewaehlt.rawValue) ab jetzt neu zählen? Das Archiv bleibt.").font(.caption)
Spacer()
Button("Abbrechen") { fragtReset = false }
Button("Zurücksetzen") {
fragtReset = false
cockpit.steuern("statistik_zuruecksetzen", daten: modusDaten)
}
.buttonStyle(.borderedProminent).tint(.orange)
}
.controlSize(.small)
.padding(8)
.background(.orange.opacity(0.12), in: RoundedRectangle(cornerRadius: 10))
}
private func kennzahlen(_ k: Kurven?) -> some View {
let s = k?.summe
let bb100 = BB100.gesamt(k?.bb100 ?? [])
return Grid(horizontalSpacing: 8, verticalSpacing: 8) {
GridRow {
Kennzahl(titel: "Hände", wert: s?.haende.map(String.init) ?? "–")
Kennzahl(titel: "Netto", wert: Format.vorzeichen(s?.nettoBB) + " bb", farbe: Farben.ergebnis(s?.nettoBB))
}
GridRow {
Kennzahl(titel: "bb/100", wert: Format.vorzeichen(bb100, stellen: 1), farbe: Farben.ergebnis(bb100))
Kennzahl(titel: "Gewonnen / verloren", wert: "\(s?.gewonnen ?? 0) / \(s?.verloren ?? 0)")
}
}
}
@ViewBuilder private func diagramm(_ k: Kurven?, _ w: Wachstum?) -> some View {
switch teil {
case .kurve:
Chart(Array((k?.ergebnisKurve ?? []).enumerated()), id: \.offset) { _, p in
if let t = p.t, let bb = p.bb {
LineMark(x: .value("Zeit", t.datum), y: .value("bb", bb)).interpolationMethod(.monotone)
AreaMark(x: .value("Zeit", t.datum), y: .value("bb", bb)).foregroundStyle(.tint.opacity(0.12))
PointMark(x: .value("Zeit", t.datum), y: .value("bb", bb)).symbolSize((k?.ergebnisKurve.count ?? 0) > 40 ? 0 : 30)
}
}
.chartYAxisLabel("bb")
case .verteilung:
Chart(Array((k?.verteilung ?? []).enumerated()), id: \.offset) { _, c in
BarMark(x: .value("Klasse", c.beschriftung), y: .value("Hände", c.anzahl ?? 0))
.foregroundStyle((c.bis ?? 1) <= 0 ? Color.red.opacity(0.7) : Color.green.opacity(0.7))
}
.chartXAxis { AxisMarks { _ in AxisValueLabel(orientation: .verticalReversed).font(.caption2) } }
case .stakes:
Chart(k?.bb100 ?? []) { z in
if let b = z.bb100 { PointMark(x: .value("bb/100", b), y: .value("Stake", z.stake)).foregroundStyle(Farben.ergebnis(b)) }
if let u = z.unten, let o = z.oben {
RuleMark(xStart: .value("unten", u), xEnd: .value("oben", o), y: .value("Stake", z.stake)).opacity(0.5)
}
}
.chartXAxisLabel("bb/100 mit 95 %-Intervall")
case .stunden:
Chart(Array((k?.jeStunde ?? []).enumerated()), id: \.offset) { _, s in
if let t = s.stunde { BarMark(x: .value("Stunde", t.datum, unit: .hour), y: .value("Hände", s.haende ?? 0)) }
}
case .stapel:
Chart(w?.stapel ?? []) { r in
ForEach(Array(r.punkte.enumerated()), id: \.offset) { _, p in
if let t = p.t, let s = p.stack {
LineMark(x: .value("Zeit", t.datum), y: .value("Stack", s)).foregroundStyle(by: .value("Tisch", r.raum))
}
}
}
.chartLegend(position: .bottom)
case .sitzungen:
VStack(alignment: .leading, spacing: 8) {
ForEach(w?.sitzungen ?? []) { s in
HStack {
VStack(alignment: .leading) {
Text(s.raum ?? "–").font(.callout.weight(.semibold))
Text("\(Zeit.kurz(s.von))–\(Zeit.kurz(s.bis)) · \(s.haende ?? 0) Hände").font(.caption).foregroundStyle(.secondary)
}
Spacer()
Text(Format.vorzeichen(s.nettoBB) + " bb").foregroundStyle(Farben.ergebnis(s.nettoBB)).monospacedDigit()
}
Divider()
}
}
}
}
}
/// bb/100 ueber alle Stakes, gewichtet nach Haenden.
enum BB100 {
static func gesamt(_ z: [StakeZeile]) -> Double? {
let n = z.reduce(0) { $0 + ($1.haende ?? 0) }
guard n > 0 else { return nil }
return z.reduce(0.0) { $0 + ($1.bb100 ?? 0) * Double($1.haende ?? 0) } / Double(n)
}
}
struct StilAnsicht: View {
enum Teil: String, CaseIterable { case anteile = "Anteile", verlauf = "Verlauf", groessen = "Größen", denkzeit = "Denkzeit" }
@Environment(Cockpit.self) private var cockpit
@AppStorage("stil.teil") private var teil: Teil = .anteile
private static let aktionen: [(String, KeyPath<Anteile, Double>, Color)] = [
("Fold", \.fold, .gray), ("Check", \.check, .teal), ("Call", \.call, .blue), ("Raise", \.raise, .orange), ("All-in", \.allin, .red),
]
var body: some View {
let s = cockpit.ansicht.stil
ScrollView {
VStack(alignment: .leading, spacing: 12) {
Picker("", selection: $teil) { ForEach(Teil.allCases, id: \.self) { Text($0.rawValue) } }
.pickerStyle(.segmented).labelsHidden()
switch teil {
case .anteile: anteile(s?.anteile)
case .verlauf: verlauf(s?.verlauf ?? [])
case .groessen: groessen(s?.groessen ?? [])
case .denkzeit: denkzeit(s)
}
}
.padding(12)
}
.overlay {
if s == nil || (s?.anteile?.n ?? 0) == 0 {
Leer(titel: "Noch kein Stil", text: s?.leer ?? "Erst nach den ersten Entscheidungen.").background(.regularMaterial)
}
}
}
private func anteile(_ a: Anteile?) -> some View {
VStack(alignment: .leading, spacing: 8) {
ForEach(Self.aktionen, id: \.0) { name, pfad, farbe in
let v = a?[keyPath: pfad] ?? 0
HStack {
Text(name).frame(width: 52, alignment: .leading).font(.callout)
Balken(anteil: v, farbe: farbe)
Text(Format.prozent(v)).font(.caption.monospacedDigit()).frame(width: 40, alignment: .trailing)
}
}
if let n = a?.n { Text("aus \(n) Entscheidungen").font(.caption).foregroundStyle(.secondary) }
}
}
private func verlauf(_ v: [Anteile]) -> some View {
Chart {
ForEach(Array(v.enumerated()), id: \.offset) { i, b in
ForEach(Self.aktionen, id: \.0) { name, pfad, _ in
AreaMark(x: .value("Block", i + 1), y: .value("Anteil", b[keyPath: pfad]), stacking: .normalized)
.foregroundStyle(by: .value("Aktion", name))
}
}
}
.chartForegroundStyleScale(domain: Self.aktionen.map(\.0), range: Self.aktionen.map(\.2))
.chartXAxisLabel("Blöcke zu 20 Entscheidungen")
.frame(height: 240)
}
private func groessen(_ g: [Stil.Groesse]) -> some View {
Chart(Array(g.enumerated()), id: \.offset) { _, x in
if let a = x.topfAnteil, let b = x.betragBB {
PointMark(x: .value("Topfanteil", a), y: .value("bb", b)).symbolSize(18).opacity(0.7)
}
}
.chartXAxisLabel("Anteil am Topf")
.chartYAxisLabel("bb")
.frame(height: 240)
}
/// Haeufigkeit je voller Sekunde.
private static func sekunden(_ d: [Double]) -> [(Int, Int)] {
Dictionary(grouping: d.map { Int($0.rounded(.down)) }, by: { $0 }).map { ($0.key, $0.value.count) }.sorted { $0.0 < $1.0 }
}
private func denkzeit(_ s: Stil?) -> some View {
VStack(alignment: .leading, spacing: 10) {
HStack(spacing: 8) {
Kennzahl(titel: "Median", wert: (s?.denkzeit?.median).map { Format.zahl($0, stellen: 1) + " s" } ?? "–")
Kennzahl(titel: "Streuung", wert: (s?.denkzeit?.streuung).map { Format.zahl($0, stellen: 1) + " s" } ?? "–")
Kennzahl(titel: "VK", wert: Format.zahl(s?.denkzeit?.vk))
}
Chart(Self.sekunden(s?.denkzeiten ?? []), id: \.0) { sekunde, anzahl in
BarMark(x: .value("Sekunden", sekunde), y: .value("Anzahl", anzahl))
}
.chartXAxisLabel("Gedenkzeit, volle Sekunden")
.frame(height: 200)
Text("Ein VK deutlich über 0 heißt: nicht maschinell gleichmäßig.").font(.caption).foregroundStyle(.secondary)
}
}
}
struct Balken: View {
var anteil: Double
var farbe: Color
var body: some View {
GeometryReader { g in
ZStack(alignment: .leading) {
Capsule().fill(.quaternary)
Capsule().fill(farbe).frame(width: g.size.width * min(1, max(0, anteil)))
}
}
.frame(height: 8)
}
}