aboutsummaryrefslogtreecommitdiff
path: root/internal/game/render_roulette_tracker.go
blob: 3602224c13fbe69ec5e09cfc2b23e147900220c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package game

import (
	"fmt"
	"sort"
	"strconv"
	"strings"

	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/color"
)

// The tracker is descriptive, not predictive. All statistics share the same
// bounded history window; zero pockets count in the percentage denominator.
func renderRouletteTracker(s *casino.RouletteSnapshot, mode string, unicode bool) string {
	const width = 72 // same 74-column footprint as the betting board
	b := rouletteBorders(unicode)
	ink := func(text string) string { return rouletteInk(mode, text) }
	pocket := func(n string) string {
		bg, name := roulettePocketColor(n)
		if mode == "none" || mode == "" {
			n += name[:1]
		}
		return roulettePaint(mode, rouletteCenter(n, 4), bg)
	}
	accent := func(text string, fg int) string {
		return color.Render(mode, color.ColorSpec{Fg: fg, Bg: -1, Bold: true}, text)
	}
	counts := map[string]int{}
	colors := [3]int{} // black, red, green
	for _, n := range s.History {
		counts[n]++
		bg, _ := roulettePocketColor(n)
		colors[bg]++
	}
	var recent []string
	for i := len(s.History) - 1; i >= 0 && len(recent) < 8; i-- {
		recent = append(recent, pocket(s.History[i]))
	}
	recentText := "-- awaiting the first spin --"
	if len(recent) > 0 {
		recentText = strings.Join(recent, " ")
	}
	// Numeric tie-breaking keeps the display stable. Cold includes pockets
	// not yet hit, but we never label an unhit pocket as hot.
	numbers := []string{"0"}
	if s.DoubleZero {
		numbers = append(numbers, "00")
	}
	for n := 1; n <= 36; n++ {
		numbers = append(numbers, strconv.Itoa(n))
	}
	ranked := func(hot bool) string {
		if len(s.History) == 0 {
			return ink("--")
		}
		order := append([]string(nil), numbers...)
		sort.SliceStable(order, func(i, j int) bool {
			if hot {
				return counts[order[i]] > counts[order[j]]
			}
			return counts[order[i]] < counts[order[j]]
		})
		var cells []string
		for _, n := range order {
			if len(cells) == 3 || hot && counts[n] == 0 {
				break
			}
			cells = append(cells, pocket(n)+ink(fmt.Sprintf("(%d)", counts[n])))
		}
		return strings.Join(cells, " ")
	}
	percentage := func(label string, bg int) string {
		value := "--"
		if len(s.History) > 0 {
			value = fmt.Sprintf("%.1f%%", 100*float64(colors[bg])/float64(len(s.History)))
		}
		return roulettePaint(mode, " "+label+" "+value+" ", bg)
	}
	hot := accent("Hot: ", 9) + ranked(true)
	cold := accent("Cold: ", 14) + ranked(false)
	ranking := hot + ink(strings.Repeat(" ", 34-color.VisibleLen(hot))+" | ") + cold
	spinLabel := "spins"
	if len(s.History) == 1 {
		spinLabel = "spin"
	}
	title := fmt.Sprintf(" History: last %d %s ", len(s.History), spinLabel)
	row := func(text string) string {
		return ink(b.v+" ") + text + ink(strings.Repeat(" ", width-1-color.VisibleLen(text))+b.v)
	}
	return strings.Join([]string{
		ink(b.tl+b.h) + accent(title, 14) + ink(strings.Repeat(b.h, width-1-len(title))+b.tr),
		row(ink("Recent: ") + recentText),
		row(ranking),
		row(percentage("Red", 1) + ink(" | ") + percentage("Black", 0) + ink(" | ") + percentage("Zero", 2)),
		ink(b.bl + strings.Repeat(b.h, width) + b.br),
	}, "\n")
}