aboutsummaryrefslogtreecommitdiff
path: root/internal/game/casino_ux.go
blob: 249537fd818e377a88ffce24cafa82ac46a132d2 (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
package game

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"

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

type casinoChoice struct {
	address string
	names   []string
	table   bool
}

// Exact addresses beat aliases; prefixes must identify exactly one game.
func resolveCasinoChoice(choices []casinoChoice, input string) (casinoChoice, bool) {
	input = strings.ToLower(input)
	for _, c := range choices {
		if input != "" && strings.EqualFold(c.address, input) {
			return c, true
		}
	}
	if input != "" {
		var exact []casinoChoice
		for _, c := range choices {
			for _, name := range c.names {
				if strings.EqualFold(name, input) {
					exact = append(exact, c)
					break
				}
			}
		}
		if len(exact) == 1 {
			return exact[0], true
		}
		if len(exact) > 1 {
			return casinoChoice{}, false
		}
	}
	matches := []casinoChoice{}
	for _, c := range choices {
		matched := input == ""
		for _, name := range c.names {
			if strings.EqualFold(name, input) || (input != "" && strings.HasPrefix(strings.ToLower(name), input)) {
				matched = true
			}
		}
		if matched {
			matches = append(matches, c)
		}
	}
	if len(matches) == 1 {
		return matches[0], true
	}
	return casinoChoice{}, false
}

// casinoTableAliases lists the names the play, list and look commands accept
// for a multiplayer table, beyond its raw game name and fixture id.
func casinoTableAliases(cfg casino.TableConfig) []string {
	names := []string{string(cfg.Game)}
	switch cfg.Game {
	case casino.GameCashHunt:
		names = append(names, "cash hunt", "cashhunt", "cash-hunt")
	case casino.GameCoinFlip:
		names = append(names, "coin flip", "coinflip", "coin-flip", "coin")
	case casino.GamePachinko:
		names = append(names, "pachinko")
	case casino.GameBigSix:
		names = append(names, "big-six", "big_six", "big six", "big 6")
	case casino.GameCrazyTime:
		names = append(names, "crazy time", "crazytime", "crazy-time", "crazy")
	case casino.GameBlackjack:
		names = append(names, "bj")
	case casino.GameBaccarat:
		names = append(names, "bac")
	case casino.GamePaiGow:
		names = append(names, "pai", "pai gow", "pai gow poker")
	}
	return names
}

func (g *Game) casinoChoices(roomID int) []casinoChoice {
	room, err := g.World.LoadRoom(roomID)
	if err != nil {
		return nil
	}
	var choices []casinoChoice
	for _, c := range room.CasinoTables {
		if c.Game == casino.GameVideoPoker {
			names := append([]string{c.ID}, c.VideoPoker.CabinetAliases()...)
			choices = append(choices, casinoChoice{address: c.ID, names: names, table: true})
			continue
		}
		names := casinoTableAliases(c)
		choices = append(choices, casinoChoice{address: string(c.Game), names: names, table: true})
	}
	for _, c := range room.CasinoMachines {
		choices = append(choices, casinoChoice{address: c.ID, names: []string{c.ID, string(c.Game), c.Variant}})
	}
	return choices
}

func (g *Game) showCasinoChoices(sess *net.Session, choices []casinoChoice) {
	if len(choices) == 0 {
		sess.WriteLine("There are no casino games here.")
		return
	}
	commands := []casino.MenuCommand{}
	for _, c := range choices {
		commands = append(commands, casino.MenuCommand{Command: "play " + c.address, Desc: "Join this game"})
	}
	sess.WriteLine(g.renderCasinoCommands(sess, "", "Choose a game (use an unambiguous name or abbreviation):", commands))
}

// renderCasinoFelt draws a table's felt (layout, payouts, board) without the
// prose rule text. The table command shows this; rules opens the help page.
// Games that draw their felt through Board() (craps, roulette, Big Six,
// Plinko, Cash Hunt, video poker) return nothing here.
func (g *Game) renderCasinoFelt(sess *net.Session, cfg casino.TableConfig) string {
	switch cfg.Game {
	case casino.GameBlackjack:
		return g.renderCasinoSnapshot(sess, &casino.BlackjackRulesView{Config: cfg})
	case casino.GameBaccarat:
		return g.renderCasinoSnapshot(sess, &casino.BaccaratRulesView{Config: cfg})
	case casino.GamePaiGow:
		return renderPaiGowFelt(cfg, g.colorMode(sess), sess.Player.OptionBool("unicode"))
	}
	return ""
}

func (g *Game) executeTableStatus(sess *net.Session, args []string, _ string) {
	if sess.Player == nil {
		return
	}
	t, playing := g.Casino.Participation(sess.Player.Name)
	if !playing || t == nil {
		if len(args) > 0 && args[0] == "help" {
			// Machine players get their cabinet's guide; everyone else the
			// shared casino one.
			topic := "casino"
			if playing {
				topic = "slots"
			}
			g.doHelp(sess, topic)
			return
		}
		// A machine player has no table (Participation returns nil for
		// machines), but bare table/status must still describe the machine they
		// occupy instead of the room's "choose a game" list.
		if playing {
			if machine, ok := g.Casino.MachineFor(sess.Player.Name); ok {
				sess.WriteLine(g.renderCasinoCommands(sess, g.casinoBalance(sess), machine.Config.ID, machine.Hints()))
				return
			}
		}
		g.showCasinoChoices(sess, g.casinoChoices(sess.Player.RoomID))
		return
	}
	// Only "help" is special-cased here; any other argument is ignored and the
	// table/status command falls through to the felt below.
	if len(args) > 0 && args[0] == "help" {
		g.doHelp(sess, t.HelpTopic())
		if t.Config.Game != casino.GameVideoPoker {
			commands := []casino.MenuCommand{
				{Command: "rules", Desc: "Help page for this game variant"},
				{Command: "list", Desc: "Every bet available right now, with payouts"},
				{Command: "option casino_display compact", Desc: "Compact action hints (detailed for full command menus)"},
				{Command: "help casino", Desc: "Quick guides to all casino games"},
			}
			sess.WriteLine(g.renderCasinoCommands(sess, "", "Table controls", commands))
		}
		return
	}
	// Coin Flip has no felt or board: the spin and the coin are the whole
	// game, so the table command answers with a one-liner instead of nothing.
	if t.Config.Game == casino.GameCoinFlip {
		sess.WriteLine("Coin Flip has no table. Use bet N to spin and flip the coin.")
		return
	}
	// The table command shows only the table: the live board for roulette,
	// craps and video poker, or the printed felt for blackjack, baccarat and
	// Pai Gow. No balance, seat state, command menu, or reminder rides along.
	if board := t.Board(sess.Player.Name); board != nil {
		if rendered := g.renderCasinoSnapshot(sess, board); rendered != "" {
			sess.WriteLine(rendered)
		}
		return
	}
	if felt := g.renderCasinoFelt(sess, t.Config); felt != "" {
		sess.WriteLine(felt)
	}
}

func (g *Game) executeWatch(sess *net.Session, args []string, _ string) {
	if sess.Player == nil {
		return
	}
	if len(args) == 1 && args[0] == "off" {
		sess.CasinoWatchTable = ""
		sess.WriteLine(g.colorize(sess, "casino_action", "You stop watching table details."))
		return
	}
	if len(args) > 1 {
		sess.WriteLine("Use watch [game] or watch off.")
		return
	}
	choices := []casinoChoice{}
	for _, c := range g.casinoChoices(sess.Player.RoomID) {
		if c.table {
			choices = append(choices, c)
		}
	}
	input := ""
	if len(args) == 1 {
		input = args[0]
	}
	c, ok := resolveCasinoChoice(choices, input)
	if !ok {
		sess.WriteLine("Choose a table to watch: watch blackjack, watch baccarat, watch roulette, or watch craps.")
		return
	}
	t, ok := g.casinoTable(sess.Player.RoomID, c.address)
	if !ok {
		return
	}
	if t.Config.Game == casino.GameVideoPoker {
		sess.WriteLine("Poker hands are private. Use look " + t.Config.ID + " to inspect this cabinet.")
		return
	}
	sess.CasinoWatchRoom = sess.Player.RoomID
	sess.CasinoWatchTable = t.Config.ID
	sess.WriteLine(g.colorize(sess, "casino_action", "Watching "+t.Config.Label()+". Detailed public cards are visible; watch off stops watching."))
	g.emitCasinoEvents(t.Status(sess.Player.Name))
}

func (g *Game) seesCasinoDetails(sess *net.Session, event casino.Event) bool {
	if sess.Player == nil {
		return false
	}
	// Slot output is unchanged; watch controls multiplayer tables only.
	if g.Casino.Table(event.RoomID, event.TableID) == nil {
		return true
	}
	if sess.CasinoWatchRoom == event.RoomID && sess.CasinoWatchTable == event.TableID {
		return true
	}
	// Settlement may have removed a queued departure before these events
	// are delivered. Their own final cards must still be visible.
	switch s := event.Snapshot.(type) {
	case *casino.CrapsSnapshot:
		for _, name := range s.Queue {
			if name == sess.Player.Name {
				return true
			}
		}
	case *casino.BigSixSnapshot:
		for _, name := range s.Players {
			if name == sess.Player.Name {
				return true
			}
		}
	case *casino.RouletteSnapshot:
		for _, name := range s.Players {
			if name == sess.Player.Name {
				return true
			}
		}
	case *casino.BlackjackSnapshot:
		for _, p := range s.Players {
			if p.Name == sess.Player.Name {
				return true
			}
		}
	case *casino.BaccaratSnapshot:
		for _, p := range s.Bets {
			if p.Name == sess.Player.Name {
				return true
			}
		}
	}
	t, ok := g.Casino.Participation(sess.Player.Name)
	return ok && t != nil && t.RoomID == event.RoomID && t.Config.ID == event.TableID
}

var casinoTicksPattern = regexp.MustCompile(`([0-9]+) ticks`)

// Casino logic counts engine ticks; deadlines are presented in ticks too, so
// the configured tick cadence never rewrites a deadline the player can count.
func (g *Game) casinoTimeText(text string) string {
	return casinoTicksPattern.ReplaceAllString(text, "${1}t")
}

// betRangeWords renders the wager range in words for one-line hints.
func betRangeWords(minBet, maxBet int) string {
	if maxBet <= 0 || maxBet >= casino.DefaultMaxBet {
		return fmt.Sprintf("%d or more", minBet)
	}
	return fmt.Sprintf("%d-%d", minBet, maxBet)
}

func (g *Game) casinoBalance(sess *net.Session) string {
	if sess.Player == nil {
		return ""
	}
	p := sess.Player
	return fmt.Sprintf("Available %s (%s chips + %s credits)", g.colorize(sess, "casino_chips", strconv.Itoa(casinoAffordable(p))), g.colorize(sess, "casino_chips", strconv.Itoa(countChips(p))), g.colorize(sess, "casino_credits", strconv.Itoa(p.Credits)))
}

// executeRules is the seated table's own help entry point: a variant's page
// (Simplified Craps gets its own short page, not a section of the craps
// manual), slots help at a machine, and the room's own game while merely
// standing in a casino room. Explicit topics pass straight through to help.
func (g *Game) executeRules(sess *net.Session, args []string, rawInput string) {
	if sess.Player == nil {
		return
	}
	if len(args) > 0 {
		g.doHelp(sess, strings.Join(args, " "))
		return
	}
	if g.Casino == nil {
		g.doHelp(sess, "casino")
		return
	}
	if topic := g.casinoRulesTopic(sess.Player.RoomID, sess.Player.Name); topic != "" {
		g.doHelp(sess, topic)
		return
	}
	// Not in a casino game or room: the shared guide still answers.
	g.doHelp(sess, "casino")
}

// casinoRulesTopic picks the help page "rules" should open: the seated
// table's variant page first, then a machine's game, then the room's own
// casino games (so standing in the craps pit answers about craps). Mixed
// games, or no casino content at all, fall back to the shared guide.
func (g *Game) casinoRulesTopic(roomID int, name string) string {
	if table, playing := g.Casino.Participation(name); playing && table != nil {
		return table.HelpTopic()
	}
	if machine, ok := g.Casino.MachineFor(name); ok {
		return string(machine.Config.Game)
	}
	room, err := g.World.LoadRoom(roomID)
	if err != nil {
		return ""
	}
	topic := ""
	for _, cfg := range room.CasinoTables {
		page := cfg.HelpTopic()
		if topic == "" {
			topic = page
		} else if topic != page {
			return "casino"
		}
	}
	for _, m := range room.CasinoMachines {
		page := string(m.Game)
		if topic == "" {
			topic = page
		} else if topic != page {
			return "casino"
		}
	}
	return topic
}

// executeList shows the bets a casino game accepts right now — the game's
// own state-driven list, from the come-out pass line to the travelling come
// bets and one-roll props. Everywhere else (shops, plain rooms) "list"
// keeps its shop meaning.
func (g *Game) executeList(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	if p == nil {
		return
	}
	if len(args) > 0 || g.Casino == nil {
		g.executeShopList(sess, args, rawInput)
		return
	}
	if machine, ok := g.Casino.MachineFor(p.Name); ok {
		row := casinoListRow{bet: "bet N / MAX", odds: "paytable", win: "spin / autospin"}
		if _, enabled := g.casinoEdgePrecision(sess); enabled {
			row.edge = machine.Config.HouseEdge()
		}
		g.showCasinoListTable(sess, "Bet", []casinoListRow{row})
		return
	}
	if table, playing := g.Casino.Participation(p.Name); playing && table != nil {
		g.showCasinoWagerList(sess, table)
		return
	}
	g.executeShopList(sess, args, rawInput)
}

// showCasinoWagerList renders the live bets/actions as a compact table.
// An empty wager catalogue can still represent an ordinary amount-only bet.
func (g *Game) showCasinoWagerList(sess *net.Session, t *casino.Table) {
	if t.Config.Game == casino.GameVideoPoker {
		for _, h := range t.Hints(sess.Player.Name) {
			if h.Command == "draw" {
				sess.WriteLine("No wagers are available during this hand; finish with draw.")
				return
			}
		}
		rows := g.casinoListRows(sess, t, []casino.WagerRow{{Bet: "main", Payout: "paytable", Win: "deal"}})
		rows[0].bet = "bet [1-4|MAX]"
		g.showCasinoListTable(sess, "Bet", rows)
		return
	}
	rows := t.Wagers(sess.Player.Name)
	// Blackjack rows are actions, not targets for a new bet.
	if t.Config.Game == casino.GameBlackjack && len(rows) > 0 {
		g.showCasinoListTable(sess, "Action", g.casinoListRows(sess, t, rows))
		return
	}
	if len(rows) == 0 {
		canBet := false
		for _, h := range t.Hints(sess.Player.Name) {
			if strings.HasPrefix(h.Command, "bet ") {
				canBet = true
			}
		}
		if !canBet {
			sess.WriteLine(g.colorize(sess, "casino_action", "No new wagers are available right now."))
			return
		}
		wager := casino.WagerRow{Bet: "main", Win: betRangeWords(t.Config.MinBet, t.Config.MaxBet)}
		if t.Config.Game == casino.GameBlackjack {
			wager.Payout = fmt.Sprintf("1:1; BJ %g:1", t.Config.Blackjack.Normalize().BlackjackPayout)
		}
		list := g.casinoListRows(sess, t, []casino.WagerRow{wager})
		list[0].bet = "bet N"
		g.showCasinoListTable(sess, "Bet", list)
		return
	}
	g.showCasinoListTable(sess, "bet N on", g.casinoListRows(sess, t, rows))
}

// showCasinoGameLook appends the live game state and hint menu for the
// table the player is seated at, so look reads room first, then the table,
// then the commands that matter now. Machines are skipped: their menus
// arrive with every spin.
func (g *Game) showCasinoGameLook(sess *net.Session, p *player.Player, room *world.Room) {
	if g.Casino == nil || p == nil {
		return
	}
	table, playing := g.Casino.Participation(p.Name)
	if !playing || table == nil || table.RoomID != room.ID {
		return
	}
	sess.WriteLine("")
	g.emitCasinoEvents(table.Status(p.Name))
}