aboutsummaryrefslogtreecommitdiff
path: root/internal/game/roulette_animation_test.go
blob: 910e7812eb23f3f4dd9dba6f7c5501ce212bd6a8 (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
package game

import (
	"strings"
	"testing"
	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/color"
	"thehouseoficarus/internal/net"
)

func TestRouletteSpinOutputHasNoPromptOrBlankLines(t *testing.T) {
	g, sess, conn := uxCasinoGame(t)
	g.Hub = net.NewHub()
	g.Hub.EnterRoom(sess, 1)
	sess.Player.Options = map[string]any{"color": "none", "unicode": true}
	g.emitCasinoEvents(g.Casino.Join(1, casino.TableConfig{ID: "roulette", Game: casino.GameRoulette}, sess.Player.Name))
	sess.ClearPrompt()
	conn.data = nil
	g.executeBet(sess, []string{"2", "on", "2"}, "bet 2 on 2")
	g.writePrompt(sess) // the command queue's post-command prompt
	text := strings.ReplaceAll(string(conn.data), "\r\n", "\n")
	if !strings.HasSuffix(text, "The roulette wheel spins...\n") {
		t.Fatalf("prompt after spin announcement: %q", text)
	}
	before := len(conn.data)
	g.reprompt(sess)
	if len(conn.data) != before {
		t.Fatal("reprompt interrupted animation")
	}
	for tick := 1; tick <= 5; tick++ {
		conn.data = nil
		g.CasinoTick()
		text = strings.ReplaceAll(string(conn.data), "\r\n", "\n")
		if strings.Count(text, "\n") != 1 || !strings.HasSuffix(text, "\n") || color.VisibleLen(strings.TrimSuffix(text, "\n")) != 10 {
			t.Fatalf("tick %d: expected exactly one strip: %q", tick, text)
		}
		if tick == 5 && strings.Count(text, ".") != 1 {
			t.Fatal("final strip missing ball")
		}
	}
	for tick := 6; tick <= 7; tick++ {
		conn.data = nil
		g.CasinoTick()
		if len(conn.data) != 0 {
			t.Fatalf("tick %d: expected silence: %q", tick, conn.data)
		}
	}
	g.CasinoTick()
	result := strings.ReplaceAll(string(conn.data), "\r\n", "\n")
	if !strings.HasPrefix(result, "\nThe ball lands in") || g.casinoAnimationBusy(sess) {
		t.Fatalf("result did not start on a blank line or end animation: %q", result)
	}
	// A resolved spin shows the result and the next-spin menu, but not the
	// tracker, racetrack, and history again.
	if strings.Contains(result, "History: last") {
		t.Fatalf("settlement redrew the table board: %q", result)
	}
	sess.ClearPrompt()
	conn.data = nil
	g.writePrompt(sess)
	if len(conn.data) == 0 {
		t.Fatal("prompt did not resume")
	}
}

func TestRouletteSpinStrip(t *testing.T) {
	for _, mode := range []string{"none", "ansi", "xterm256"} {
		for _, unicode := range []bool{false, true} {
			for _, ball := range []int{-1, 5} {
				s := &casino.RouletteSnapshot{Phase: "playing", SpinFrame: 1, BallPosition: ball, SpinPockets: [10]string{"1", "2", "1", "2", "1", "26", "0", "32", "15", "19"}}
				art := renderRouletteSnapshot(s, mode, unicode, 7)
				plain := plainRoulette(art)
				dots := 0
				if ball >= 0 {
					dots = 1
				}
				if color.VisibleLen(art) != 10 || strings.Count(plain, ".") != dots || strings.Contains(plain, "\n") {
					t.Fatalf("bad strip: %q", art)
				}
				block := "█"
				if !unicode {
					block = "#"
				}
				if strings.Count(plain, block) != 10-dots {
					t.Fatalf("bad blocks: %q", plain)
				}
				if mode != "none" && ball >= 0 && !strings.Contains(art, roulettePaint(mode, ".", 8)) {
					t.Fatalf("26 ball is not black: %q", art)
				}
			}
			for _, pocket := range []string{"0", "00", "26", "32"} {
				s := &casino.RouletteSnapshot{Phase: "playing", SpinFrame: 5, BallPosition: 4}
				s.SpinPockets[4] = pocket
				art := renderRouletteSnapshot(s, mode, unicode, 7)
				fg, _ := roulettePocketColor(pocket)
				if fg == 0 {
					fg = 8
				}
				if !strings.Contains(art, roulettePaint(mode, ".", fg)) {
					t.Fatalf("wrong ball color for %s: %q", pocket, art)
				}
			}
		}
	}
	if got := renderRouletteSnapshot(&casino.RouletteSnapshot{Phase: "playing"}, "none", true, 7); got != "" {
		t.Fatalf("old wheel still shown: %q", got)
	}
}