aboutsummaryrefslogtreecommitdiff
path: root/internal/game/render_roulette_test.go
blob: c1065e50dc221613a6b168a2d60dca3cd336daec (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package game

import (
	"fmt"
	"regexp"
	"strings"
	"testing"

	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/color"
	"thehouseoficarus/internal/combat"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
	"thehouseoficarus/internal/world"
)

var rouletteSGR = regexp.MustCompile(`\x1b\[[0-9;]*m`)

func plainRoulette(s string) string { return rouletteSGR.ReplaceAllString(s, "") }

func TestRouletteArtModes(t *testing.T) {
	for _, mode := range []string{"none", "ansi", "xterm256"} {
		for _, unicode := range []bool{false, true} {
			for _, double := range []bool{false, true} {
				t.Run(fmt.Sprintf("%s/unicode=%v/double=%v", mode, unicode, double), func(t *testing.T) {
					wheelOrder := casino.WheelOrder(double)
					s := &casino.RouletteSnapshot{DoubleZero: double, Wheel: wheelOrder}
					for _, phase := range []string{"betting", "playing", "result"} {
						s.Phase = phase
						s.Pockets = [3]string{"26", "0", "32"}
						art := renderRouletteSnapshot(s, mode, unicode, 7)
						plain := plainRoulette(art)
						if mode == "xterm256" && unicode && double {
							t.Logf("%s preview (colors stripped):\n%s", phase, plain)
						}
						if strings.Contains(plain, "\x1b") {
							t.Fatal("malformed ANSI")
						}
						if mode == "none" && art != plain {
							t.Fatal("color disabled but ANSI present")
						}
						for _, r := range plain {
							if !unicode && r > 127 {
								t.Fatalf("non-ASCII rune %q in ASCII mode", r)
							}
						}
						for _, line := range strings.Split(art, "\n") {
							if color.VisibleLen(line) > 78 {
								t.Fatalf("line exceeds terminal width: %q", plainRoulette(line))
							}
						}
						if mode != "none" {
							for _, bg := range []int{0, 1, 2} {
								if !strings.Contains(art, color.BgCode(mode, bg)) {
									t.Fatalf("missing pocket background %d", bg)
								}
							}
							if !strings.Contains(art, color.FgCode(mode, 15)) || !strings.Contains(art, color.StyleCode("bold")) {
								t.Fatal("missing bold white text")
							}
							if !strings.HasSuffix(art, color.Reset) {
								t.Fatal("art leaves terminal colored")
							}
						}
					}

					layoutArt := renderRouletteLayout(double, mode, unicode)
					layout := plainRoulette(layoutArt)
					rows := strings.Split(layout, "\n")
					if double {
						if !strings.Contains(string([]rune(rows[2])[:7]), "0") || !strings.Contains(string([]rune(rows[6])[:7]), "00") {
							t.Fatal("double zero not left of grid", layout)
						}
					} else if !strings.Contains(string([]rune(rows[4])[:7]), "0") {
						t.Fatal("zero not left of grid", layout)
					}
					for _, target := range []string{"1to12", "13to24", "25to36", "row1", "row2", "row3", "red", "black", "odd", "even", "low", "high"} {
						if !strings.Contains(layout, target) {
							t.Fatal("missing bet", target)
						}
					}
					// The odds reminder belongs to "table", never the board.
					for _, reminder := range []string{"Numbers 35:1", "Low: 1-18", "bet 10 on"} {
						if strings.Contains(layout, reminder) {
							t.Fatal("reminder text leaked onto the betting board")
						}
					}
					if mode != "none" {
						// The whole 0/00 lane is green: blank cells above and below
						// the zeros fill the rectangle.
						wantGreen := 3
						if got := strings.Count(layoutArt, color.BgCode(mode, 2)); got != wantGreen {
							t.Fatalf("green cells on board = %d, want %d", got, wantGreen)
						}
					}

					wheelArt := renderRouletteWheel(wheelOrder, mode, unicode)
					plainWheel := plainRoulette(wheelArt)
					lines := strings.Split(plainWheel, "\n")
					if len(lines) != 23 {
						t.Fatalf("wheel height = %d, want 23", len(lines))
					}
					wantDoubleZeroLabel := 0
					if double {
						wantDoubleZeroLabel = 1
					}
					if got := strings.Count(plainWheel, "00"); got != wantDoubleZeroLabel {
						t.Fatalf("double-zero labels = %d, want %d", got, wantDoubleZeroLabel)
					}
					if mode != "none" {
						wantGreen := 1
						if double {
							wantGreen = 2
						}
						greens := strings.Count(wheelArt, color.BgCode(mode, 2))
						if greens != wantGreen {
							t.Fatalf("green pockets = %d, want %d", greens, wantGreen)
						}
						total := greens + strings.Count(wheelArt, color.BgCode(mode, 0)) + strings.Count(wheelArt, color.BgCode(mode, 1))
						if total != len(wheelOrder) {
							t.Fatalf("drawn pockets = %d, want %d", total, len(wheelOrder))
						}
					}

					result := plainRoulette(renderRouletteResult([3]string{"1", "00", "27"}, mode, unicode, 7))
					for _, banned := range []string{"PREV", "BALL", "NEXT", "WINNER"} {
						if strings.Contains(result, banned) {
							t.Fatalf("result prose %q should be removed:\n%s", banned, result)
						}
					}
				})
			}
		}
	}
}

// TestRouletteLayoutConnections guards the box-drawing joints: the dozen-row
// boxes must connect to the numbered grid above and be closed under, the
// row-bet cells must be boxed on the right, and the bottom rule must tee under
// each outside-bet divider.
func TestRouletteLayoutConnections(t *testing.T) {
	for _, double := range []bool{false, true} {
		art := plainRoulette(renderRouletteLayout(double, "xterm256", true))
		rows := strings.Split(art, "\n")
		// rows[7] is the numbered grid's bottom edge, directly above the dozen
		// labels. The dozen boundaries and the grid/row-lane joint cross; the
		// row-bet lane closes with a bottom-right corner.
		gridBottom := []rune(rows[7])
		for _, col := range []int{6, 26, 46, 66} {
			if gridBottom[col] != '┼' {
				t.Fatalf("double=%v: grid bottom col %d = %q, want ┼:\n%s", double, col, gridBottom[col], art)
			}
		}
		if gridBottom[73] != '┘' {
			t.Fatalf("double=%v: grid bottom row-lane corner = %q, want ┘:\n%s", double, gridBottom[73], art)
		}
		// The row-bet lane is boxed: its separators cross at the grid edge and
		// close on the right.
		for _, idx := range []int{3, 5} {
			sep := []rune(rows[idx])
			if sep[66] != '┼' || sep[73] != '┤' {
				t.Fatalf("double=%v: row lane separator %q lacks box joints:\n%s", double, rows[idx], art)
			}
		}
		// rows[9] is the dozen boxes' bottom edge: tees point down where the
		// even-money dividers sit and crosses pass the dozen dividers through.
		dozenBottom := []rune(rows[9])
		wantDozen := map[int]rune{6: '├', 16: '┬', 26: '┼', 36: '┬', 46: '┼', 56: '┬', 66: '┤'}
		for col, want := range wantDozen {
			if dozenBottom[col] != want {
				t.Fatalf("double=%v: dozen bottom col %d = %q, want %q:\n%s", double, col, dozenBottom[col], want, art)
			}
		}
		// The last line is the table's bottom rule; it tees upward under each
		// outside-bet divider.
		bottom := []rune(rows[len(rows)-1])
		for _, col := range []int{16, 26, 36, 46, 56} {
			if bottom[col] != '┴' {
				t.Fatalf("double=%v: table bottom col %d = %q, want ┴:\n%s", double, col, bottom[col], art)
			}
		}
	}
}

func TestRouletteResultSizesAndNeighbors(t *testing.T) {
	for _, unicode := range []bool{false, true} {
		for _, size := range []int{1, 5, 7, 9} {
			art := plainRoulette(renderRouletteResult([3]string{"1", "00", "27"}, "xterm256", unicode, size))
			lines := strings.Split(art, "\n")
			height := max(5, cardRenderSize(unicode, size))
			width := height + 2
			if len(lines) != height {
				t.Fatalf("size=%d, height=%d want %d", size, len(lines), height)
			}
			for _, line := range lines {
				if color.VisibleLen(line) != 3*width+2 {
					t.Fatalf("unequal pocket widths: %q", line)
				}
			}
			center := []rune(lines[height/2])
			if !strings.Contains(string(center[:width]), "1") || !strings.Contains(string(center[width+1:2*width+1]), "00") || !strings.Contains(string(center[2*width+2:]), "27") {
				t.Fatal("wrong pocket order", art)
			}
			ball := "o"
			if unicode {
				ball = "●"
			}
			if strings.Count(art, ball) != 1 {
				t.Fatal("missing or duplicate ball", art)
			}
		}
	}
}

func TestRouletteEventColorsDoNotWrapArt(t *testing.T) {
	for _, public := range []bool{false, true} {
		for _, mode := range []string{"none", "ansi", "xterm256"} {
			g := &Game{Casino: casino.NewManager(1), Hub: net.NewHub()}
			p := player.New("alice")
			p.RoomID = 1
			p.Options = map[string]any{"color": mode, "unicode": true}
			conn := &casinoTestConn{}
			sess := &net.Session{Player: p, Conn: conn}
			g.loggedInChars = map[string]*net.Session{"alice": sess}
			g.Hub.EnterRoom(sess, 1)
			// Use an emphatic blue background to detect whole-message color wrapping.
			sess.Account = &player.Account{Colors: map[string]string{"casino_action": "0F bg:04 bold"}}
			g.Casino.Join(1, casino.TableConfig{ID: "r", Game: casino.GameRoulette}, p.Name)
			e := casino.Event{RoomID: 1, TableID: "r", Message: "Spinning now", Color: "casino_action", Public: public, Snapshot: &casino.RouletteSnapshot{Phase: "playing", Players: []string{p.Name}}}
			if !public {
				e.PrivateTo = p.Name
			}
			g.emitCasinoEvents([]casino.Event{e})
			output := strings.ReplaceAll(string(conn.data), "\r\n", "\n")
			expected := g.colorize(sess, "casino_action", e.Message) + "\n" + g.renderCasinoSnapshot(sess, e.Snapshot) + "\n"
			if output != expected {
				t.Fatalf("public=%v mode=%s: prose color leaked into art or snapshot missing\n%q\nwant\n%q", public, mode, output, expected)
			}
		}
	}
}

func TestRouletteSettlementShowsBalanceOnce(t *testing.T) {
	for _, action := range []string{"", "sitout", "leave_after"} {
		g := &Game{Casino: casino.NewManager(7), Combat: combat.NewTracker(), Deps: Deps{World: world.New(t.TempDir())}}
		p := player.New("alice")
		p.Credits = 1000
		p.Options = map[string]any{"color": "none"}
		conn := &casinoTestConn{}
		sess := &net.Session{Player: p, Conn: conn}
		g.loggedInChars = map[string]*net.Session{"alice": sess}
		cfg := casino.TableConfig{ID: "r", Game: casino.GameRoulette, BettingTicks: 1, Roulette: casino.RouletteConfig{SpinTicks: 1}}
		g.Casino.Join(1, cfg, p.Name)
		g.Casino.Bet(1, "r", p.Name, "dozen2", 10, casinoWallet{g: g, p: p, saveCharacter: func(*player.Player) error { return nil }})
		if action != "" {
			g.Casino.Action(1, "r", p.Name, action)
		}
		g.Casino.Tick()
		g.emitCasinoEvents(g.Casino.Tick())
		if got := strings.Count(string(conn.data), "Available "); got != 1 {
			t.Fatalf("action=%q: balance printed %d times\n%s", action, got, conn.data)
		}
	}
}