aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/table_ux.go
blob: 48ea46ef1a0bcc5e24c49d50361d4209e0036308 (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
package casino

import (
	"fmt"
	"strings"
)

// RulesText is the shared rules view for table commands and room fixtures.
func (c TableConfig) RulesText() string {
	switch c.Game {
	case GameBlackjack:
		return c.BlackjackRulesText()
	case GameBaccarat:
		return c.BaccaratRulesText()
	case GameRoulette:
		s := &RouletteSnapshot{DoubleZero: c.Roulette.DoubleZero, Phase: "betting"}
		return c.Label() + "\n" + s.ASCII() + "\nStraight numbers pay 35:1; dozens/columns 2:1; other outside bets 1:1.\nZero pockets lose all outside bets. One wager per spin. Use help roulette."
	default:
		return c.Label()
	}
}

func (c TableConfig) Label() string {
	if c.Game == GameRoulette {
		if c.Roulette.DoubleZero {
			return "Roulette / Double zero"
		}
		return "Roulette / Single zero"
	}
	if c.Game != GameBaccarat {
		return gameTitle(c.Game)
	}
	switch c.Baccarat.Normalize().Variant {
	case BaccaratSuper6:
		return "Baccarat / Super 6"
	case BaccaratEZ:
		return "Baccarat / EZ"
	default:
		return "Baccarat / Commission"
	}
}

func (c *tableCore[P]) overview() string {
	c.mu.Lock()
	defer c.mu.Unlock()
	return c.overviewLocked()
}

func (c *tableCore[P]) overviewLocked() string {
	state := "Betting open"
	switch c.phase {
	case phaseBetting:
		state = fmt.Sprintf("Betting closes in %d ticks", c.window.timer.remaining())
	case phasePlaying:
		state = "Round in progress"
	case phaseInsurance:
		state = "Insurance decisions"
	}
	return fmt.Sprintf("%s | Limits %s | %d/%d seated | %s", c.Config.Label(), betRangeText(c.Config.MinBet, c.Config.MaxBet), c.players.count(), c.Config.MaxPlayers, state)
}

func (c *tableCore[P]) warning(name, text string) Event {
	e := c.private(name, text)
	e.Color = "casino_warning"
	return e
}

// controlLocked queues departures without changing committed hands or stakes.
func (c *tableCore[P]) controlLocked(name, action string) ([]Event, bool) {
	if action != "leave_after" && action != "sitout" {
		return nil, false
	}
	for _, p := range c.players.participants() {
		if p.Name != name {
			continue
		}
		if action == "leave_after" {
			p.LeaveAfter = true
			return []Event{c.private(name, "You’ll leave after this round. Your current hand still needs your actions.")}, true
		}
		if p.HasBet {
			p.SitOutAfter = true
			return []Event{c.private(name, "You’ll sit out after this round. Your current wager and actions are unchanged.")}, true
		}
		p.SittingOut = true
		return []Event{c.private(name, "You’re sitting out. Place a bet to rejoin; stop to leave.")}, true
	}
	return []Event{c.private(name, "You aren’t seated at this table.")}, true
}

// Called only after resetRound, when seat removal cannot disturb active indices.
func (c *tableCore[P]) finishControlsLocked() []Event {
	var events []Event
	for _, p := range c.players.participants() {
		if p.LeaveAfter {
			c.removeSeatLocked(p.Name)
			events = append(events, c.playerAction(p.Name, p.Name+" stands up.", "Round complete. You stand up."))
		} else if p.SitOutAfter {
			p.SitOutAfter = false
			p.SittingOut = true
			events = append(events, c.private(p.Name, "You’re now sitting out. Place a bet to rejoin."))
		}
	}
	return c.closeIfEmptyLocked(events)
}

func (c *tableCore[P]) seatStatusLocked(name string) string {
	for _, p := range c.players.participants() {
		if p.Name != name {
			continue
		}
		if p.LeaveAfter {
			return "Leaving after this round"
		}
		if p.SitOutAfter {
			return "Sitting out after this round"
		}
		if p.SittingOut {
			return "Sitting out | Bet to rejoin"
		}
		if p.HasBet {
			return fmt.Sprintf("Wager committed: %d", p.Wager.Total)
		}
	}
	var waiting []string
	for _, p := range c.players.participants() {
		if p.Connected && !p.SittingOut && !p.HasBet {
			waiting = append(waiting, p.Name)
		}
	}
	if len(waiting) > 0 {
		return "Waiting for bets: " + strings.Join(waiting, ", ")
	}
	return ""
}

func (t *blackjackTable) status(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	e := t.private(name, t.overviewLocked())
	if text := t.seatStatusLocked(name); text != "" {
		e.Message += "\n" + text
	}
	events := []Event{e}
	switch t.phase {
	case blackjackPlaying:
		events[0].Snapshot = t.snapshot(false)
		if t.activeName() == name {
			events = append(events, t.turnMenuLocked(name))
		} else {
			events[0].Message += "\n" + t.activeName() + "’s turn | You are waiting"
		}
	case blackjackInsurance:
		events[0].Snapshot = t.snapshot(false)
		if p := t.players.get(name); p != nil && p.HasBet && !p.declinedInsurance && p.insurance == 0 {
			events = append(events, t.insuranceMenuLocked(p))
		}
	default:
		if p := t.players.get(name); p != nil && !p.HasBet {
			events = append(events, t.menuEvent(name))
		}
	}
	return events
}

func (t *baccaratTable) status(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	e := t.private(name, t.overviewLocked())
	if text := t.seatStatusLocked(name); text != "" {
		e.Message += "\n" + text
	}
	events := []Event{e}
	if p := t.players.get(name); p != nil && !p.HasBet {
		events = append(events, t.menuEvent(name))
	}
	return events
}

func (t *blackjackTable) insuranceMenuLocked(p *blackjackPlayer) Event {
	cost := p.hands[0].bet / 2
	e := t.private(p.Name, "Insurance decision")
	e.Color = "casino_menu"
	e.Menu = &MenuView{Heading: fmt.Sprintf("Insurance | Costs %d | Pays %g:1 | %d ticks remaining", cost, t.Rules.InsurancePayout, t.timer.remaining()), Commands: []MenuCommand{{Command: "insurance", Desc: "Buy insurance", Cost: cost}, {Command: "decline", Desc: "Continue without insurance (also the timeout default)"}}}
	if cost == 0 {
		e.Menu.Commands = e.Menu.Commands[1:]
		e.Menu.Heading = "Bet too small to insure | decline to continue"
	}
	return e
}

func resultText(result string, stake, returned int) string {
	label := strings.ToUpper(result)
	switch result {
	case "blackjack":
		label = "BLACKJACK"
	case "bust", "loss", "dealer blackjack":
		label = "LOSS"
	}
	return fmt.Sprintf("%s | Net %+d credits | %d returned (stake included)", label, returned-stake, returned)
}