aboutsummaryrefslogtreecommitdiff
path: root/internal/game/casino.go
blob: 2d8024c1dafa0cb54e50e0b42f82f0d478d85883 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
package game

import (
	"fmt"
	"strconv"
	"strings"

	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

// leaveCasinoGames departs every casino game the player participates in
// using the graceful leave policy each game defines for player-initiated
// exits (the same path the "stop" command takes): a table with a wager
// committed to the round in flight refuses and emits its own refusal
// message, machines stop auto-bet or are left outright. It reports whether
// the player is now clear of all casino games; false means a departure is
// blocked mid-round and the caller (quit) must abort. Nil-safe for test
// scaffolding without a Casino manager.
func (g *Game) leaveCasinoGames(p *player.Player) bool {
	if g.Casino == nil {
		return true
	}
	if table, playing := g.Casino.Participation(p.Name); playing {
		if table != nil {
			g.emitCasinoEvents(g.Casino.Leave(table.RoomID, table.Config.ID, p.Name))
		} else if machine, ok := g.Casino.MachineFor(p.Name); ok {
			// Stop autobet first, then take the machine's own leave path: a
			// machine between spins has no committed stake, so one quit must
			// free it (and must not warn about a forfeit that cannot happen).
			// A machine mid-spin refuses the leave, the player stays seated,
			// and the caller warns about the stake that is really committed.
			if events, stopped := g.Casino.StopMachineAutoBet(machine.RoomID, machine.Config.ID, p.Name); stopped {
				g.emitCasinoEvents(events)
			}
			g.emitCasinoEvents(g.Casino.LeaveMachine(machine.RoomID, machine.Config.ID, p.Name))
		}
		_, stillPlaying := g.Casino.Participation(p.Name)
		return !stillPlaying
	}
	return true
}

// inCasinoGame reports whether the player is currently seated at any casino
// table or machine. Nil-safe for test scaffolding without a Casino manager.
func (g *Game) inCasinoGame(name string) bool {
	if g.Casino == nil {
		return false
	}
	_, playing := g.Casino.Participation(name)
	return playing
}

// casinoTable returns the room's table for the named game. A room addresses
// at most one table (or machine) per game name — "play blackjack" carries no
// table id — so a second table for the same game would be unreachable
// content; ValidateRoomConfigs rejects that configuration instead of silently
// dropping it.
func (g *Game) casinoTable(roomID int, gameName string) (*casino.Table, bool) {
	room, err := g.World.LoadRoom(roomID)
	if err != nil {
		return nil, false
	}
	if casino.ValidateRoomConfigs(room.CasinoTables, room.CasinoMachines) != nil {
		return nil, false
	}
	for _, cfg := range room.CasinoTables {
		if (cfg.Game == casino.GameVideoPoker && strings.EqualFold(cfg.ID, gameName)) || (cfg.Game != casino.GameVideoPoker && strings.EqualFold(string(cfg.Game), gameName)) {
			table := g.Casino.EnsureTable(roomID, cfg)
			if table == nil {
				return nil, false
			}
			return table, true
		}
	}
	return nil, false
}

// casinoMachine returns the room's machine for the named game or variant.
// Like casinoTable, only the first match is reachable, which is why
// ValidateRoomConfigs rejects a room with two entries addressing the same
// game name or variant.
func (g *Game) casinoMachine(roomID int, gameName string) (*casino.Machine, bool) {
	room, err := g.World.LoadRoom(roomID)
	if err != nil {
		return nil, false
	}
	if casino.ValidateRoomConfigs(room.CasinoTables, room.CasinoMachines) != nil {
		return nil, false
	}
	// An explicit machine id is the most specific address, so match it first:
	// the shipped room's machine `huff_puff_01` must be reachable by name even
	// though it also declares a variant.
	for _, cfg := range room.CasinoMachines {
		if strings.EqualFold(cfg.ID, gameName) {
			machine := g.Casino.EnsureMachine(roomID, cfg)
			return machine, machine != nil
		}
	}
	// Prefer an exact variant match first: a room may host several variants of
	// one game, where a bare game name is ambiguous.
	for _, cfg := range room.CasinoMachines {
		if cfg.Variant != "" && strings.EqualFold(cfg.Variant, gameName) {
			machine := g.Casino.EnsureMachine(roomID, cfg)
			return machine, machine != nil
		}
	}
	// Then a bare game name: prefer a variant-less machine so a machine that
	// declares its own variant is reached by that variant, and a sole machine
	// of a game is still reachable by the game name.
	for _, cfg := range room.CasinoMachines {
		if cfg.Variant == "" && strings.EqualFold(string(cfg.Game), gameName) {
			machine := g.Casino.EnsureMachine(roomID, cfg)
			return machine, machine != nil
		}
	}
	for _, cfg := range room.CasinoMachines {
		if strings.EqualFold(string(cfg.Game), gameName) {
			machine := g.Casino.EnsureMachine(roomID, cfg)
			return machine, machine != nil
		}
	}
	return nil, false
}

func (g *Game) executePlay(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	if p == nil {
		return
	}
	if _, playing := g.Casino.Participation(p.Name); playing {
		sess.WriteLine("You're already playing a game. Type \"stop\" between rounds to leave it.")
		return
	}
	choices := g.casinoChoices(p.RoomID)
	input := ""
	if len(args) > 0 {
		input = args[0]
		// Multi-word names (Cash Hunt, Pai Gow, Big Six) precede any
		// optional wager. Resolve the longest matching name first.
		for n := len(args); n > 1; n-- {
			name := strings.Join(args[:n], " ")
			if _, ok := resolveCasinoChoice(choices, name); ok {
				input = name
				args = append([]string{name}, args[n:]...)
				break
			}
		}
	}
	choice, ok := resolveCasinoChoice(choices, input)
	if !ok {
		g.showCasinoChoices(sess, choices)
		return
	}
	g.startCasinoGame(sess, p, choice.address)
	if len(args) > 1 {
		// Let executeBet parse the wager/target tokens. Pre-validating here
		// rejected the advertised "play GAME N on BET" form (len > 3) and any
		// machine amount (choice.table is false for machines); executeBet's own
		// parser already produces precise errors for each game's syntax.
		g.executeBet(sess, args[1:], rawInput)
	}
}

func (g *Game) startCasinoGame(sess *net.Session, p *player.Player, gameName string) {
	if p == nil || sess == nil {
		return
	}
	if _, playing := g.Casino.Participation(p.Name); playing {
		sess.WriteLine("You're already playing a game. Type \"stop\" between rounds to leave it.")
		return
	}
	if machine, ok := g.casinoMachine(p.RoomID, gameName); ok {
		g.emitCasinoEvents(g.Casino.JoinMachine(machine.RoomID, machine.Config, p.Name))
		return
	}
	table, ok := g.casinoTable(p.RoomID, gameName)
	if !ok {
		sess.WriteLine(fmt.Sprintf("There is no %s table here.", gameName))
		return
	}
	events := g.Casino.Join(table.RoomID, table.Config, p.Name)
	if table.Config.Game == casino.GameVideoPoker {
		if joined, playing := g.Casino.Participation(p.Name); playing && joined == table {
			events = g.Casino.Bet(table.RoomID, table.Config.ID, p.Name, "view", 0, casinoWallet{g: g, p: p})
		}
	}
	g.emitCasinoEvents(events)
}

// Keep slots' spin-as-bet behavior, while Big 6 spins already committed wagers.
func (g *Game) executeSpin(sess *net.Session, args []string, rawInput string) {
	if sess.Player != nil {
		if table, playing := g.Casino.Participation(sess.Player.Name); playing && table != nil && table.Config.Game == casino.GameBigSix {
			if len(args) != 0 {
				sess.WriteLine("Use spin after placing your bets.")
				return
			}
			g.executeTableAction(sess, "spin")
			return
		}
	}
	g.executeBet(sess, args, rawInput)
}

func (g *Game) executeBet(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	if p == nil {
		sess.WriteLine("Bet how much?")
		return
	}
	if machine, ok := g.Casino.MachineFor(p.Name); ok {
		if len(args) == 0 {
			g.emitCasinoEvents(g.Casino.StartMachine(machine.RoomID, machine.Config.ID, p.Name, casinoWallet{g: g, p: p}))
			return
		}
		if len(args) == 1 {
			if args[0] == "max" {
				amount := min(machine.Config.MaxBet, casinoAffordable(p))
				if amount < 1 {
					sess.WriteLine("You don't have any chips or credits to bet.")
					return
				}
				g.emitCasinoEvents(g.Casino.SetMachineBet(machine.RoomID, machine.Config.ID, p.Name, amount, casinoWallet{g: g, p: p}))
				return
			}
			if amount, err := strconv.Atoi(args[0]); err == nil && amount > 0 {
				g.emitCasinoEvents(g.Casino.SetMachineBet(machine.RoomID, machine.Config.ID, p.Name, amount, casinoWallet{g: g, p: p}))
				return
			}
		}
		sess.WriteLine("Use bet, bet N, or bet max.")
		return
	}
	table, playing := g.Casino.Participation(p.Name)
	if !playing || table == nil {
		sess.WriteLine("You're not playing a game.")
		return
	}
	if table.Config.Game == casino.GameVideoPoker {
		g.emitCasinoEvents(g.Casino.Action(table.RoomID, table.Config.ID, p.Name, strings.TrimSpace("coins "+strings.Join(args, " "))))
		return
	}
	// Baccarat repeats every remembered side bet and the main bet on a bare
	// "bet"; games with one wager keep the generic single-rebet path below.
	if table.Config.Game == casino.GameBaccarat && len(args) == 0 {
		g.emitCasinoEvents(g.Casino.RebetAll(table.RoomID, table.Config.ID, p.Name, casinoWallet{g: g, p: p}))
		return
	}
	// Table bet syntax: bet | bet N [on BET] | bet min|max [on BET].
	// BEFORE an "on" keyword, a token that parses as an amount (or min/max)
	// sets the wager and any other token is the bet target; AFTER "on", the
	// next token is always the target. That makes numeric targets reachable
	// ("bet 5 on 17"), which roulette, craps, and keno need.
	// Roulette combinations and craps bets need multi-token targets. Once
	// an amount and target keyword exist, subsequent numbers belong to the
	// target even without "on" (bet 10 split 1 2 / bet 10 buy 4).
	// Other games keep the stricter one-token target.
	joinTarget := table.Config.Game == casino.GameRoulette || table.Config.Game == casino.GameCraps
	amount, spot, hasAmount, sawOn := 0, "", false, false
	for i, arg := range args {
		if strings.EqualFold(arg, "on") {
			if sawOn || spot != "" || i == len(args)-1 {
				sess.WriteLine("Use bet N on BET. No wager was placed.")
				return
			}
			sawOn = true
			continue
		}
		// Once an amount is set, every remaining token belongs to the target
		// ("bet 2 2" and "bet 2 on 2" are identical). Before that, a numeric
		// token sets the amount and anything else starts the target.
		if !sawOn && !hasAmount {
			if strings.EqualFold(arg, "min") {
				amount, hasAmount = table.Config.MinBet, true
				continue
			}
			if strings.EqualFold(arg, "max") {
				amount = min(table.Config.MaxBet, casinoAffordable(p)/table.StakeMultiple())
				if amount < 1 {
					sess.WriteLine("You don't have any chips or credits to bet.")
					return
				}
				hasAmount = true
				continue
			}
			if value, err := strconv.Atoi(arg); err == nil && value > 0 {
				amount, hasAmount = value, true
				continue
			}
		}
		if spot == "" {
			spot = strings.ToLower(arg)
			continue
		}
		if joinTarget {
			spot += " " + strings.ToLower(arg)
			continue
		}
		sess.WriteLine("Use bet, bet N [on BET], bet min, or bet max.")
		return
	}
	if table.Config.Game == casino.GameBaccarat {
		switch spot {
		case "b":
			spot = "banker"
		case "p":
			spot = "player"
		case "t":
			spot = "tie"
		}
	}
	if !hasAmount {
		var lastSpot string
		lastSpot, amount = g.Casino.Rebet(table.RoomID, table.Config.ID, p.Name)
		if spot == "" {
			spot = lastSpot
		}
		if amount <= 0 {
			sess.WriteLine("Bet how much?")
			return
		}
	}
	g.emitCasinoEvents(g.Casino.Bet(table.RoomID, table.Config.ID, p.Name, spot, amount, casinoWallet{g: g, p: p}))
}

func (g *Game) executeAutoBet(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	if p == nil {
		return
	}
	if machine, ok := g.Casino.MachineFor(p.Name); ok {
		g.emitCasinoEvents(g.Casino.EnableMachineAutoBet(machine.RoomID, machine.Config.ID, p.Name, casinoWallet{g: g, p: p}))
		return
	}
	sess.WriteLine("Autobet is only available at games without player choices.")
}

func (g *Game) executeTableAction(sess *net.Session, action string) {
	p := sess.Player
	if p == nil {
		return
	}
	table, playing := g.Casino.Participation(p.Name)
	if !playing || table == nil {
		sess.WriteLine("You're not playing a multiplayer casino game.")
		return
	}
	if table.Config.Game == casino.GameVideoPoker && action == "deal" {
		g.emitCasinoEvents(g.Casino.Bet(table.RoomID, table.Config.ID, p.Name, "deal", 0, casinoWallet{g: g, p: p}))
		return
	}
	g.emitCasinoEvents(g.Casino.Action(table.RoomID, table.Config.ID, p.Name, action))
}

// casinoAnimationBusy reports whether a seated player's game is mid-animation:
// a spinning roulette wheel or Big 6 wheel, or a falling Plinko or Pachinko
// ball. The automatic prompt stays out of that output and resumes when the
// table settles.
func (g *Game) casinoAnimationBusy(sess *net.Session) bool {
	if g.Casino == nil || sess == nil || sess.Player == nil {
		return false
	}
	table, playing := g.Casino.Participation(sess.Player.Name)
	if !playing || table == nil {
		return false
	}
	if table.Busy(sess.Player.Name) {
		return true
	}
	if table.Config.Game != casino.GameRoulette {
		return false
	}
	s, ok := table.Board(sess.Player.Name).(*casino.RouletteSnapshot)
	return ok && s.Phase == "playing"
}

func (g *Game) CasinoTick() {
	if g.Casino != nil {
		g.emitCasinoEvents(g.Casino.Tick())
	}
}

func (g *Game) emitCasinoEvents(events []casino.Event) {
	for _, event := range events {
		if event.Message == "" && event.PrivateMessage == "" && event.Menu == nil && event.Snapshot == nil && len(event.Settlement) == 0 {
			continue
		}
		category := event.Color
		if category == "" {
			category = "casino_action"
		}
		if event.Public && g.Hub != nil {
			for _, sess := range g.Hub.PlayersInRoom(event.RoomID) {
				if event.PrivateTo != "" && sess.Player != nil && sess.Player.Name == event.PrivateTo {
					continue
				}
				details := g.seesCasinoDetails(sess, event)
				if event.Detail && !details {
					continue
				}
				// Style prose before composing it with independently styled art.
				// Wrapping the finished message leaks casino_action into borders
				// and plain spaces between a snapshot's own color resets.
				message := g.casinoProse(sess, category, g.casinoTimeText(event.Message))
				if details {
					if rendered := g.renderCasinoSnapshot(sess, event.Snapshot); rendered != "" {
						if message != "" {
							message += "\n"
						}
						message += rendered
					}
				}
				if message != "" {
					sess.WriteLine(message)
				}
			}
		}
		if event.PrivateTo == "" {
			continue
		}
		text := event.PrivateMessage
		if text == "" {
			text = event.Message
		}
		if text == "" && event.Menu == nil && event.Snapshot == nil && len(event.Settlement) == 0 {
			continue
		}
		g.charsMu.Lock()
		sess := g.loggedInChars[event.PrivateTo]
		g.charsMu.Unlock()
		if sess == nil {
			continue
		}
		privateMessage := g.casinoPrivateMessage(sess, category, event.Menu, g.casinoTimeText(text))
		privateMessage = g.renderCasinoMessage(sess, category, event.Menu, privateMessage)
		rendered := g.renderCasinoSnapshot(sess, event.Snapshot)
		if len(event.Settlement) > 0 {
			receipt := renderCasinoSettlement(event.Settlement, sess.Player.OptionBool("unicode"), func(tag, text string) string { return g.colorize(sess, tag, text) })
			if event.SettlementNote != "" {
				receipt += "\n" + g.casinoProse(sess, "casino_warning", event.SettlementNote)
			}
			// Reveal first, then the receipt, then the next-action menu. A
			// settlement's legacy prose is only a non-UI fallback, not a second payout.
			rendered = composeTableThenReminder(rendered, receipt)
			if event.Menu == nil {
				privateMessage = ""
			}
		}
		if reminder, ok := g.casinoBoardReminder(sess, category, event.Menu, event.Snapshot, g.casinoTimeText(text)); ok {
			privateMessage = composeTableThenReminder(rendered, reminder)
		} else if rendered != "" {
			if privateMessage != "" {
				privateMessage += "\n"
			}
			privateMessage += rendered
		}
		if event.ShowBalance {
			privateMessage += "\n" + g.casinoBalance(sess)
		}
		sess.WriteLine(privateMessage)
		if category == "casino_menu" {
			g.writePrompt(sess)
		}
	}
}

// casinoMenuParts resolves a structured menu into the shared header/balance
// heading/commands used by both menu render paths: the balance line, the
// heading (falling back to the event message), and a copy of the commands.
// Unaffordable commands are filtered at render time by casinoCommandColumns.
func (g *Game) casinoMenuParts(sess *net.Session, menu *casino.MenuView, message string) (balance, heading string, commands []casino.MenuCommand) {
	balance = ""
	if menu.ShowBalance {
		balance = g.casinoBalance(sess)
	}
	heading = g.casinoTimeText(menu.Heading)
	if heading == "" {
		heading = message
	}
	commands = append([]casino.MenuCommand(nil), menu.Commands...)
	if !g.casinoHintsVisible(sess, menu) {
		commands = nil
	}
	// Unaffordable commands are hidden by casinoCommandColumns; an
	// "unavailable: insufficient funds" annotation here would be dead, since
	// the renderer drops exactly those commands with the same test.
	return balance, heading, commands
}

// casinoHintsVisible decides whether a menu's command list prints. "on"
// always shows hints (the historical behavior); "off" never does; the default
// "once" shows them only on the menu a player gets when sitting down. Players
// can always ask for the live list with "what" or "verbs".
func (g *Game) casinoHintsVisible(sess *net.Session, menu *casino.MenuView) bool {
	if sess == nil || sess.Player == nil || menu == nil {
		return true
	}
	switch sess.Player.OptionString("casino_hints") {
	case "off":
		return false
	case "once":
		return menu.ShowHints
	default:
		return true
	}
}

// casinoBoardReminder splits a persistent-board menu (craps, roulette, Big 6,
// video poker, Pai Gow) so its instructions print under the board they describe.
// Every other game, and compact displays, keep their menu in one block.
func (g *Game) casinoBoardReminder(sess *net.Session, category string, menu *casino.MenuView, snapshot casino.Snapshot, message string) (reminder string, ok bool) {
	if category != "casino_menu" || menu == nil || sess.Player == nil {
		return "", false
	}
	_, isCraps := snapshot.(*casino.CrapsSnapshot)
	_, isPoker := snapshot.(*casino.VideoPokerSnapshot)
	_, isPlinko := snapshot.(*casino.PlinkoSnapshot)
	_, isPachinko := snapshot.(*casino.PachinkoSnapshot)
	_, isCashHunt := snapshot.(*casino.CashHuntSnapshot)
	_, isCoinFlip := snapshot.(*casino.CoinFlipSnapshot)
	_, isPaiGow := snapshot.(*casino.PaiGowSnapshot)
	b, isBigSix := snapshot.(*casino.BigSixSnapshot)
	isBigSix = isBigSix && b.Phase != "playing" && b.Phase != "result"
	s, isRoulette := snapshot.(*casino.RouletteSnapshot)
	if !isCashHunt && !isCoinFlip && !isPlinko && !isPachinko && !isPoker && !isCraps && !isPaiGow && !isBigSix && (!isRoulette || s.Phase == "playing" || s.Phase == "result") {
		return "", false
	}
	if isRoulette && sess.Player.OptionString("casino_display") == "compact" && (menu.Repeat || !menu.ShowBalance) {
		// The compact reminder is already a short block; leave it intact.
		return "", false
	}
	balance, heading, commands := g.casinoMenuParts(sess, menu, message)
	// The board replaces the game-name heading, and the balance sits with the
	// commands underneath it rather than above the graphic.
	return g.renderCasinoCommands(sess, balance, heading, commands), true
}

// composeTableThenReminder joins a board graphic and the wager reminder. The
// board snapshot already leads with a blank line, and the reminder keeps
// exactly one blank line below the board's last visible row.
func composeTableThenReminder(table, reminder string) string {
	out := strings.TrimRight(table, "\n")
	if reminder != "" {
		if out != "" {
			out += "\n\n"
		}
		out += reminder
	}
	return out
}

// renderCasinoMessage colorizes a casino message unless it is an
// already-composed structured menu.
func (g *Game) renderCasinoMessage(sess *net.Session, category string, menu *casino.MenuView, message string) string {
	if message == "" {
		return ""
	}
	if category == "casino_menu" && menu != nil {
		return message
	}
	return g.casinoProse(sess, category, message)
}

// casinoProse colorizes casino event text, expanding inline color tags such as
// {casino_result_win}WIN{/} against the session's palette. The event's color
// category is the default for untagged text.
func (g *Game) casinoProse(sess *net.Session, category, text string) string {
	return g.expandTagsDefault(sess, g.resolveColor(sess, category), text)
}

func (g *Game) casinoPrivateMessage(sess *net.Session, category string, menu *casino.MenuView, message string) string {
	if category != "casino_menu" || menu == nil {
		return message
	}
	balance, heading, commands := g.casinoMenuParts(sess, menu, message)
	if sess.Player.OptionString("casino_display") == "compact" && !menu.ShowBalance {
		var hints []casino.MenuCommand
		for _, c := range commands {
			label := c.Command
			switch c.Command {
			case "hit":
				label = "h:hit"
			case "stand":
				label = "st:stand"
			case "double":
				label = "dd:double"
			case "split":
				label = "sp:split"
			}
			if c.Cost > 0 {
				label += fmt.Sprintf(" (+%d)", c.Cost)
			}
			hints = append(hints, casino.MenuCommand{Command: label, Cost: c.Cost})
		}
		return g.renderCasinoCommands(sess, balance, heading, hints)
	}
	return g.renderCasinoCommands(sess, balance, heading, commands)
}

// renderCasinoSnapshot renders a game's structured view for one session. It is
// the only per-game switch in the presentation layer: adding a game means
// adding its snapshot type here, and the Snapshot interface guarantees the
// event carries it rather than being silently dropped by a field filter.
func (g *Game) renderCasinoSnapshot(sess *net.Session, snapshot casino.Snapshot) string {
	if snapshot == nil || sess.Player == nil {
		return ""
	}
	mode := g.colorMode(sess)
	unicode := sess.Player.OptionBool("unicode")
	// Card art is governed by card_size alone; casino_display only controls
	// how verbose the command menus are.
	size := sess.Player.OptionInt("card_size")
	switch s := snapshot.(type) {
	case *casino.CashHuntSnapshot:
		rtp := ""
		if precision, ok := g.casinoEdgePrecision(sess); ok {
			rtp = casinoRTP(s.Edge, precision)
		}
		return renderCashHunt(s, mode, unicode, rtp)
	case *casino.CoinFlipSnapshot:
		return renderCoinFlip(s, mode, unicode)
	case *casino.PlinkoSnapshot:
		return renderPlinko(s, mode, unicode)
	case *casino.PachinkoSnapshot:
		precision, showOdds := g.casinoEdgePrecision(sess)
		return renderPachinko(s, mode, unicode, showOdds, precision)
	case *casino.PaiGowSnapshot:
		return renderPaiGow(s, mode, unicode, size, sess.Player.OptionString("card_style"), g.CardArt)
	case *casino.VideoPokerSnapshot:
		return renderVideoPoker(s, mode, unicode, size, sess.Player.OptionString("card_style"), g.CardArt)
	case *casino.CrapsSnapshot:
		return renderCrapsSnapshot(s, mode, unicode, sess.Player.OptionString("card_style"))
	case *casino.BigSixSnapshot:
		return renderBigSixSnapshot(s, mode, unicode)
	case *casino.RouletteSnapshot:
		return renderRouletteSnapshot(s, mode, unicode, size)
	case *casino.SlotSnapshot:
		return renderSlotSnapshot(s, mode, unicode)
	case *casino.BlackjackSnapshot:
		return renderBlackjackSnapshotFor(s, mode, unicode, size, sess.Player.OptionString("card_style"), g.CardArt, sess.Player.Name)
	case *casino.BlackjackRulesView:
		return renderBlackjackRulesFelt(s.Config, mode, unicode)
	case *casino.BaccaratRulesView:
		return renderBaccaratRulesFelt(s.Config, mode, unicode)
	case *casino.BaccaratSnapshot:
		return renderBaccaratSnapshot(s, mode, unicode, size, sess.Player.OptionString("card_style"), g.CardArt)
	}
	return ""
}

// renderCasinoCommands lays every casino menu out in the same style: an
// optional balance line, a heading, and an aligned command list with each
// command in the "command" color and its description in plain text.
func (g *Game) renderCasinoCommands(sess *net.Session, balance, heading string, commands []casino.MenuCommand) string {
	head := g.casinoMenuHeader(sess, balance, heading)
	body := g.casinoMenuCommands(sess, commands)
	switch {
	case head == "":
		return body
	case body == "":
		return head
	default:
		return head + "\n\n" + body
	}
}

// casinoMenuHeader formats the balance and heading that introduce a casino
// menu. The heading (if any) comes first so the balance always sits directly
// above the command list. It is split from the command list so the roulette
// board can be drawn between them.
func (g *Game) casinoMenuHeader(sess *net.Session, balance, heading string) string {
	var sb strings.Builder
	if heading != "" {
		sb.WriteString(g.colorize(sess, "casino_action", heading))
		sb.WriteString("\n\n")
	}
	if balance != "" {
		sb.WriteString(balance)
		sb.WriteString("\n\n")
	}
	return strings.TrimRight(sb.String(), "\n")
}

// casinoMenuCommands formats an aligned, colored command list.
func (g *Game) casinoMenuCommands(sess *net.Session, commands []casino.MenuCommand) string {
	return g.casinoCommandColumns(sess, commands)
}