aboutsummaryrefslogtreecommitdiff
path: root/internal/game/casino_ux.go
blob: 6f7964ff600f2ca4244da8f9ec5a1f87d08652de (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
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
}

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 := []string{string(c.Game)}
		switch c.Game {
		case casino.GameBlackjack:
			names = append(names, "bj")
		case casino.GameBaccarat:
			names = append(names, "bac")
		case casino.GamePaiGow:
			names = append(names, "paigow", "pai", "pai gow", "pai gow poker")
		}
		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))
}

func (g *Game) executeTableStatus(sess *net.Session, args []string, _ string) {
	if sess.Player == nil {
		return
	}
	if len(args) > 0 && args[0] != "rules" && args[0] != "help" {
		sess.WriteLine("Use table, table rules, or table help.")
		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)
		} else {
			g.showCasinoChoices(sess, g.casinoChoices(sess.Player.RoomID))
		}
		return
	}
	if len(args) > 0 && args[0] == "rules" {
		// The felt and the exact, per-table wording stay under table rules;
		// bare rules opens the game's own help page.
		if t.Config.Game == casino.GameVideoPoker {
			g.executeTableAction(sess, "paytable")
		} else {
			sess.WriteLine(g.renderCasinoRules(sess, t.Config))
		}
		return
	}
	if len(args) > 0 && args[0] == "help" {
		g.doHelp(sess, t.HelpTopic())
	} else {
		g.emitCasinoEvents(t.Status(sess.Player.Name))
		if t.Config.Game == casino.GameRoulette {
			sess.WriteLine(g.rouletteReminder(sess, t.Config))
		} else {
			sess.WriteLine(g.colorize(sess, "command", "Learn to play: rules. Exact payouts: table rules."))
		}
	}
	if len(args) > 0 {
		// Reuse the game's current-state menu instead of advertising actions
		// (insurance, split, rebet, etc.) that may not be legal right now.
		g.emitCasinoEvents(t.Status(sess.Player.Name))
		if t.Config.Game == casino.GameVideoPoker {
			return
		}
		commands := []casino.MenuCommand{
			{Command: "rules", Desc: "Help page for this game variant"},
			{Command: "list", Desc: "Every bet available right now, with payouts"},
			{Command: "table rules", Desc: "This table’s exact rules and 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))
	}
}

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.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; present deadlines in actual configured seconds.
func (g *Game) casinoTimeText(text string) string {
	ms := 600
	if g.Ticks != nil {
		ms = g.Ticks.TickLengthMilliseconds()
	}
	return casinoTicksPattern.ReplaceAllStringFunc(text, func(s string) string {
		n, _ := strconv.Atoi(strings.Fields(s)[0])
		return fmt.Sprintf("%g seconds", float64(n)*float64(ms)/1000)
	})
}

// 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 {
		sess.WriteLine(g.colorize(sess, "casino_action", "A "+casinoGameLabel(machine.Config.Game)+" takes one wager: bet <amount> (or bet max), then spin or autospin."))
		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 game's live wager list. Two or more rows
// become a brief table; a single row is one line; an empty list means the
// only bet is the ordinary amount-only wager.
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
			}
		}
		sess.WriteLine("This cabinet has one wager: load coins with bet [1-4|max], then deal. Use paytable for payouts.")
		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 {
		commands := make([]casino.MenuCommand, 0, len(rows))
		for _, r := range rows {
			commands = append(commands, casino.MenuCommand{Command: r.Bet, Desc: strings.Trim(r.Payout+" | "+r.Win, " |")})
		}
		sess.WriteLine(g.renderCasinoCommands(sess, g.casinoBalance(sess), "Available actions", commands))
		return
	}
	switch {
	case len(rows) == 1:
		r := rows[0]
		sess.WriteLine(g.colorize(sess, "casino_action", "Your only wager: bet <amount> on "+r.Bet+" — "+r.Payout+" — "+r.Win+"."))
	case 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
		}
		sess.WriteLine(g.colorize(sess, "casino_action", "The only wager here is bet <amount>") + " (" + betRangeWords(t.Config.MinBet, t.Config.MaxBet) + ").")
	default:
		commands := make([]casino.MenuCommand, 0, len(rows))
		for _, r := range rows {
			commands = append(commands, casino.MenuCommand{Command: "bet <amount> on " + r.Bet, Desc: r.Payout + " | " + r.Win})
		}
		sess.WriteLine(g.renderCasinoCommands(sess, g.casinoBalance(sess), t.Config.Label()+" — available bets", commands))
	}
}

// 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))
}