aboutsummaryrefslogtreecommitdiff
path: root/internal/game/casino_rules_list_test.go
blob: f3a18e62ea0beb560585052481f11bc40ac02d95 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package game

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	"gopkg.in/yaml.v3"
	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/combat"
	"thehouseoficarus/internal/engine"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/object"
	"thehouseoficarus/internal/player"
	"thehouseoficarus/internal/world"
)

// casinoCrapsGame builds a one-room world with the named craps edition and
// points the help loader at the shipped data dir, so the rules command and
// the shipped-topic test exercise the real help pages.
func casinoCrapsGame(t *testing.T, roomYAML string) (*Game, *net.Session, *casinoTestConn) {
	t.Helper()
	dir := t.TempDir()
	if err := os.MkdirAll(filepath.Join(dir, "rooms"), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(dir, "rooms", "1.yaml"), []byte(roomYAML), 0644); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(filepath.Join(dir, "players", "characters"), 0o755); err != nil {
		t.Fatal(err)
	}
	p := player.New("alice")
	sess := &net.Session{Player: p, Conn: &casinoTestConn{}}
	g := &Game{
		Deps:          Deps{World: world.New(dir), ObjectStore: object.NewObjectStore(dir), MobStore: world.NewMobStore(dir), AccountStore: player.NewAccountStore(dir), Ticks: engine.New(), DataDir: "../../data"},
		Casino:        casino.NewManager(1),
		Combat:        combat.NewTracker(),
		safespot:      NewSafespotManager(),
		restTimers:    make(map[string]uint64),
		loggedInChars: map[string]*net.Session{"alice": sess},
	}
	p.Credits = 100
	p.RoomID = 1
	return g, sess, sess.Conn.(*casinoTestConn)
}

// TestRulesAtTableShowsVariantHelpPage pins the "rules" contract: seated at
// Simplified Craps, rules must open the variant's own short page — not the
// whole craps manual with the edition buried in a section.
func TestRulesAtTableShowsVariantHelpPage(t *testing.T) {
	room := "name: Simplified Salon\ncasino_tables:\n  - id: simple\n    game: craps\n    craps:\n      variant: simplified\n"
	g, sess, conn := casinoCrapsGame(t, room)
	g.executePlay(sess, []string{"craps"}, "play craps")
	conn.data = nil
	g.executeRules(sess, nil, "rules")
	text := stripANSI(string(conn.data))
	if !strings.Contains(text, "Simplified Craps") {
		t.Fatalf("rules missing the variant page: %q", text)
	}
	if strings.Contains(text, "HOUSE EDITIONS") || strings.Contains(text, "bet 10 on dontcome") {
		t.Fatalf("rules leaked the bank manual into the variant page: %q", text)
	}
}

// TestRulesStandingInCasinoRoomAnswersRoomGame checks the unseated fallback:
// standing in a one-table casino room answers that game's page.
func TestRulesStandingInCasinoRoomAnswersRoomGame(t *testing.T) {
	room := "name: Crapless Salon\ncasino_tables:\n  - id: cl\n    game: craps\n    craps:\n      variant: crapless\n"
	g, sess, conn := casinoCrapsGame(t, room)
	g.executeRules(sess, nil, "rules")
	if text := stripANSI(string(conn.data)); !strings.Contains(text, "Crapless Craps") {
		t.Fatalf("standing rules did not answer the room's game: %q", text)
	}
}

// TestRulesOutsideCasinoFallsBackToGuide checks rooms with no casino content.
func TestRulesOutsideCasinoFallsBackToGuide(t *testing.T) {
	g, sess, conn := casinoCrapsGame(t, "name: Void\n")
	g.executeRules(sess, nil, "rules")
	if text := stripANSI(string(conn.data)); !strings.Contains(text, "Casino quick guide") {
		t.Fatalf("rules outside a casino: %q", text)
	}
}

// TestListFollowsGameState covers the list contract end to end: come-out
// listTargets returns the bet names rendered in the first table column,
// working for both the unicode and ASCII table borders.
func listTargets(text string) map[string]bool {
	targets := map[string]bool{}
	for _, line := range strings.Split(text, "\n") {
		line = strings.TrimSpace(line)
		if !strings.HasPrefix(line, "║") && !strings.HasPrefix(line, "|") {
			continue
		}
		line = strings.TrimPrefix(strings.TrimPrefix(line, "║"), "|")
		for _, sep := range []string{"│", "|"} {
			if at := strings.Index(line, sep); at >= 0 {
				line = line[:at]
			}
		}
		if cell := strings.TrimSpace(line); cell != "" {
			targets[cell] = true
		}
	}
	return targets
}

// lists the line and one-roll bets, a point adds place/odds rows, bonus
// bets close after the first roll, and single-bet editions print one line.
func TestListFollowsGameState(t *testing.T) {
	room := "name: Bank Pit\ncasino_tables:\n  - id: bank\n    game: craps\n    craps:\n      variant: bank\n"
	g, sess, conn := casinoCrapsGame(t, room)
	g.executePlay(sess, []string{"craps"}, "play craps")
	g.executeBet(sess, []string{"10", "pass"}, "bet 10 pass")
	conn.data = nil
	g.executeList(sess, nil, "list")
	text := stripANSI(string(conn.data))
	targets := listTargets(text)
	for _, want := range []string{"pass", "dontpass", "field", "craps", "fire"} {
		if !targets[want] {
			t.Fatalf("come-out list missing %q: %q", want, text)
		}
	}
	for _, gone := range []string{"place 6", "odds pass"} {
		if targets[gone] {
			t.Fatalf("come-out list advertised %q: %q", gone, text)
		}
	}

	// Establish a point: place/odds appear, bonus bets close. The roll is
	// deliberately delayed, so advance the shared clock until it resolves.
	g.Casino.Action(1, "bank", "alice", "roll")
	for i := 0; i < 5; i++ {
		g.Casino.Tick()
	}
	conn.data = nil
	g.executeList(sess, nil, "list")
	text = stripANSI(string(conn.data))
	targets = listTargets(text)
	if !targets["place 6/8"] || !targets["odds pass / come"] {
		t.Fatalf("point list missing place/odds: %q", text)
	}
	if targets["fire"] {
		t.Fatalf("point list still advertising closed bets: %q", text)
	}
}

// TestListSingleBetGamesRenderOneRow: editions with one wager (Simplified)
// and one-wager games (blackjack) answer with a one-row table.
func TestListSingleBetGamesRenderOneRow(t *testing.T) {
	room := "name: Simple\ncasino_tables:\n  - id: simple\n    game: craps\n    craps:\n      variant: simplified\n"
	g, sess, conn := casinoCrapsGame(t, room)
	g.executePlay(sess, []string{"craps"}, "play craps")
	conn.data = nil
	g.executeList(sess, nil, "list")
	text := stripANSI(string(conn.data))
	if !strings.Contains(text, "Odds") {
		t.Fatalf("single-bet list did not render the table header: %q", text)
	}
	if targets := listTargets(text); !targets["pass"] {
		t.Fatalf("single-bet table missing the wager: %q", text)
	}

	// Blackjack: one wager, one row, and the list command still answers.
	room = "name: BJ\ncasino_tables:\n  - id: bj\n    game: blackjack\n"
	g, sess, conn = casinoCrapsGame(t, room)
	g.executePlay(sess, []string{"bj"}, "play bj")
	conn.data = nil
	g.executeList(sess, nil, "list")
	text = stripANSI(string(conn.data))
	if targets := listTargets(text); !targets["bet N"] {
		t.Fatalf("blackjack list: %q", text)
	}
}

// TestListAtShopKeepsShopMeaning guards the shop fallthrough: an explicit
// shopmob argument always browses the shop.
func TestListAtShopKeepsShopMeaning(t *testing.T) {
	g, sess, _ := casinoCrapsGame(t, "name: Void\n")
	g.executeList(sess, []string{"shopkeeper"}, "list shopkeeper")
	if text := stripANSI(string(sess.Conn.(*casinoTestConn).data)); !strings.Contains(text, "no shop") && !strings.Contains(text, "shop") {
		t.Fatalf("explicit list should reach the shop path: %q", text)
	}
}

// TestShippedCasinoHelpTopicsResolve walks every shipped casino table and
// machine and pins that its rules help topic actually resolves in data/help.
func TestShippedCasinoHelpTopicsResolve(t *testing.T) {
	dataDir := filepath.Join("..", "..", "data")
	helps, err := LoadHelp(dataDir)
	if err != nil {
		t.Fatal(err)
	}
	resolves := func(topic string) bool {
		for _, h := range helps {
			if matchesHelpTopic(h, topic) {
				return true
			}
		}
		return false
	}
	roomsDir := filepath.Join(dataDir, "rooms")
	err = filepath.WalkDir(roomsDir, func(path string, d os.DirEntry, err error) error {
		if err != nil || d.IsDir() || !strings.HasSuffix(path, ".yaml") {
			return err
		}
		raw, err := os.ReadFile(path)
		if err != nil {
			return err
		}
		var room world.Room
		if err := yaml.Unmarshal(raw, &room); err != nil {
			return err
		}
		for _, cfg := range room.CasinoTables {
			topic := cfg.HelpTopic()
			if !resolves(topic) {
				t.Errorf("%s: table %s (%s) help topic %q does not resolve", path, cfg.ID, cfg.Game, topic)
			}
		}
		for _, m := range room.CasinoMachines {
			if !resolves(string(m.Game)) {
				t.Errorf("%s: machine %s game %q does not resolve", path, m.ID, m.Game)
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(err)
	}
}

// TestRulesAtMachineShowsSlotsHelp: a slot machine player typing rules gets
// the cabinet's own guide, not a dead end.
func TestRulesAtMachineShowsSlotsHelp(t *testing.T) {
	room := "name: Slots Salon\ncasino_machines:\n  - id: puff\n    game: slots\n"
	g, sess, conn := casinoCrapsGame(t, room)
	g.executePlay(sess, []string{"slots"}, "play slots")
	conn.data = nil
	g.executeRules(sess, nil, "rules")
	if text := stripANSI(string(conn.data)); !strings.Contains(text, "Slots quick guide") {
		t.Fatalf("machine rules: %q", text)
	}
}

// TestListRouletteAndBaccarat: roulette lists the whole target catalogue and
// baccarat lists every outcome and side bet its variant offers.
func TestListRouletteAndBaccarat(t *testing.T) {
	room := "name: Wheel Salon\ncasino_tables:\n  - id: wheel\n    game: roulette\n    roulette:\n      racetrack: true\n"
	g, sess, conn := casinoCrapsGame(t, room)
	g.executePlay(sess, []string{"roulette"}, "play roulette")
	conn.data = nil
	g.executeList(sess, nil, "list")
	text := stripANSI(string(conn.data))
	targets := listTargets(text)
	for _, want := range []string{"NUMBER", "red / black", "voisins", "basket"} {
		if !targets[want] {
			t.Fatalf("roulette list missing %q: %q", want, text)
		}
	}

	room = "name: Crown Salon\ncasino_tables:\n  - id: crown\n    game: baccarat\n    baccarat:\n      variant: ez_baccarat\n      dragon_7: true\n      panda_8: true\n"
	g, sess, conn = casinoCrapsGame(t, room)
	g.executePlay(sess, []string{"bac"}, "play bac")
	conn.data = nil
	g.executeList(sess, nil, "list")
	text = stripANSI(string(conn.data))
	targets = listTargets(text)
	for _, want := range []string{"banker", "dragon_7", "panda_8"} {
		if !targets[want] {
			t.Fatalf("baccarat list missing %q: %q", want, text)
		}
	}
	if !strings.Contains(text, "1:1 (3-card 7 pushes)") {
		t.Fatalf("baccarat list dropped the EZ banker rule: %q", text)
	}
}

// The commission is implied by the 0.95:1 ratio and must not be repeated.
func TestListBaccaratDropsCommissionProse(t *testing.T) {
	g, sess, conn := casinoCrapsGame(t, "name: B\ncasino_tables:\n  - id: b\n    game: baccarat\n")
	g.executePlay(sess, []string{"b"}, "play b")
	conn.data = nil
	g.executeList(sess, nil, "list")
	text := stripANSI(string(conn.data))
	if strings.Contains(text, "commission") {
		t.Fatalf("list repeated the implied commission: %q", text)
	}
	if targets := listTargets(text); !targets["banker"] {
		t.Fatalf("baccarat list lost the banker row: %q", text)
	}
	if !strings.Contains(text, "0.95:1") {
		t.Fatalf("baccarat list lost the banker ratio: %q", text)
	}
}