aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/baccarat.go
blob: 426127044da258a680b0d437a5df752848831af7 (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
package casino

import (
	"fmt"
	"math"
	"math/rand"
	"sync"
)

type baccaratPhase string

const (
	baccaratWaiting baccaratPhase = "waiting"
	baccaratBetting baccaratPhase = "betting"
)

// baccaratSpots are the outcomes a participant may wager on.
const (
	baccaratSpotPlayer = "player"
	baccaratSpotBanker = "banker"
	baccaratSpotTie    = "tie"
)

func baccaratSpotValid(spot string) bool {
	return spot == baccaratSpotPlayer || spot == baccaratSpotBanker || spot == baccaratSpotTie
}

type baccaratPlayer struct {
	Participant
	lastBet  int
	lastSpot string
}

type baccaratTable struct {
	mu sync.Mutex
	tableBase
	Rules        BaccaratConfig
	participants map[string]*baccaratPlayer
	order        []string
	phase        baccaratPhase
	timer        countdown
	shoe         *shoe
}

func newBaccaratTable(roomID int, cfg TableConfig, rng *rand.Rand) *baccaratTable {
	cfg = cfg.Normalize()
	rules := cfg.Baccarat.Normalize()
	return &baccaratTable{
		tableBase: tableBase{RoomID: roomID, Config: cfg}, Rules: rules,
		participants: make(map[string]*baccaratPlayer), phase: baccaratWaiting,
		shoe: newShoe(rules.Decks, rules.ShuffleAt, rng),
	}
}

func (t *baccaratTable) seats() []*Participant {
	seats := make([]*Participant, 0, len(t.order))
	for _, name := range t.order {
		if p := t.participants[name]; p != nil {
			seats = append(seats, &p.Participant)
		}
	}
	return seats
}

func (t *baccaratTable) menuEvent(name string) Event {
	e := t.private(name, t.menu())
	e.Color = "casino_menu"
	e.Menu = &MenuView{Kind: MenuBaccaratBet, Commands: []MenuCommand{
		{Command: "bet <amount>", Desc: "Place a wager (min/max accepted)"},
		{Command: "bet <amount> on player|banker|tie", Desc: "Wager on a specific outcome"},
		{Command: "stop", Desc: "Leave the table"},
	}}
	return e
}

func (t *baccaratTable) menu() string {
	return "New baccarat hand! Place your bet."
}

func (t *baccaratTable) join(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	if p := t.participants[name]; p != nil {
		p.Connected = true
		return []Event{t.menuEvent(name)}
	}
	if len(t.participants) >= t.Config.MaxPlayers {
		return []Event{t.private(name, "That baccarat table is full.")}
	}
	t.participants[name] = &baccaratPlayer{Participant: Participant{Name: name, Connected: true}}
	t.order = append(t.order, name)
	events := []Event{t.playerAction(name, fmt.Sprintf("%s sits down.", name), "You sit down.")}
	events = append(events, t.menuEvent(name))
	return events
}

func (t *baccaratTable) leave(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	p := t.participants[name]
	if p == nil {
		return nil
	}
	if t.phase != baccaratWaiting {
		return []Event{t.private(name, "You're in the middle of a baccarat round!")}
	}
	delete(t.participants, name)
	for i, n := range t.order {
		if n == name {
			t.order = append(t.order[:i], t.order[i+1:]...)
			break
		}
	}
	events := []Event{t.playerAction(name, fmt.Sprintf("%s stands up.", name), "You stand up.")}
	if len(t.participants) == 0 {
		events = append(events, t.event("The baccarat table closes.", true))
	}
	return events
}

func (t *baccaratTable) bet(name, spot string, amount int, wallet Wallet) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	p := t.participants[name]
	if p == nil {
		return []Event{t.private(name, "You aren't playing baccarat.")}
	}
	if spot == "" {
		spot = p.lastSpot
	}
	if spot == "" {
		return []Event{t.private(name, "Bet on what? Try \"bet <amount> on player\", \"bet <amount> on banker\", or \"bet <amount> on tie\".")}
	}
	if !baccaratSpotValid(spot) {
		return []Event{t.private(name, "Baccarat bets must be on player, banker, or tie.")}
	}
	if p.HasBet {
		return []Event{t.private(name, "You have already bet this round.")}
	}
	if amount < t.Config.MinBet || amount > t.Config.MaxBet {
		return []Event{t.private(name, fmt.Sprintf("Baccarat bets must be between %d and %d.", t.Config.MinBet, t.Config.MaxBet))}
	}
	wager, ok := wallet.Wager(amount)
	if !ok {
		return []Event{t.private(name, "You don't have enough chips and credits for that bet.")}
	}
	p.HasBet, p.Wager, p.Wallet, p.lastBet, p.lastSpot = true, wager, wallet, amount, spot
	events := []Event{t.playerAction(name,
		fmt.Sprintf("%s bets %d credits on %s.", name, amount, spot),
		fmt.Sprintf("You bet %d credits on %s.", amount, spot))}
	if len(t.participants) > 1 && t.phase == baccaratWaiting {
		t.phase = baccaratBetting
		t.timer.start(t.Config.BettingTicks)
		events = append(events, t.event(fmt.Sprintf("Baccarat betting is open for %d ticks.", t.Config.BettingTicks), true))
	}
	if allBet(t.seats()) {
		events = append(events, t.deal()...)
	}
	return events
}

func (t *baccaratTable) tick() []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	if t.phase != baccaratBetting {
		return nil
	}
	t.timer.tick()
	if allBet(t.seats()) {
		return t.deal()
	}
	if !t.timer.expired() {
		return nil
	}
	var events []Event
	for _, name := range sitOutNames(t.seats()) {
		events = append(events, t.event(fmt.Sprintf("%s sits out this baccarat round.", name), true))
	}
	if anyBet(t.seats()) {
		events = append(events, t.deal()...)
	} else {
		t.resetRound()
	}
	return events
}

func (t *baccaratTable) action(name, action string) []Event {
	return []Event{t.private(name, "That game has no player actions.")}
}

func (t *baccaratTable) rebet(name string) (string, int) {
	if p := t.participants[name]; p != nil && p.lastBet > 0 {
		return p.lastSpot, p.lastBet
	}
	return "", t.Config.MinBet
}

// deal plays out the hand by the punto banco tableau, settles every wager,
// and resets the table for the next round.
func (t *baccaratTable) deal() []Event {
	player, banker := playTableau(t.shoe)
	playerTotal, bankerTotal := baccaratTotal(player), baccaratTotal(banker)
	round := t.event("New baccarat hand!", true)
	round.Baccarat = t.snapshot(player, banker)
	events := []Event{round, t.event(baccaratOutcomeMessage(playerTotal, bankerTotal), true)}
	for _, name := range t.order {
		p := t.participants[name]
		if p == nil || !p.HasBet {
			continue
		}
		result, payout := settleBaccaratBet(p.lastSpot, p.Wager.Total, playerTotal, bankerTotal, t.Rules)
		if payout > 0 && p.Wallet != nil {
			p.Wallet.PayCredits(payout)
		}
		publicResult, privateResult := baccaratResultMessages(name, result, payout)
		settlement := t.playerAction(name, publicResult, privateResult)
		if result == "win" {
			if p.lastSpot == baccaratSpotTie {
				settlement.Color = "casino_jackpot_win"
			} else {
				settlement.Color = "casino_big_win"
			}
		}
		events = append(events, settlement)
	}
	t.resetRound()
	for _, name := range t.order {
		if p := t.participants[name]; p != nil && p.Connected {
			events = append(events, t.menuEvent(name))
		}
	}
	return events
}

func (t *baccaratTable) snapshot(player, banker []card) *BaccaratSnapshot {
	snapshot := &BaccaratSnapshot{
		PlayerScore: fmt.Sprintf("%d", baccaratTotal(player)),
		BankerScore: fmt.Sprintf("%d", baccaratTotal(banker)),
	}
	for _, c := range player {
		snapshot.Player = append(snapshot.Player, CardView{Rank: c.rank, Suit: c.suit})
	}
	for _, c := range banker {
		snapshot.Banker = append(snapshot.Banker, CardView{Rank: c.rank, Suit: c.suit})
	}
	for _, name := range t.order {
		p := t.participants[name]
		if p == nil || !p.HasBet {
			continue
		}
		snapshot.Bets = append(snapshot.Bets, BaccaratBetView{Name: name, Spot: p.lastSpot, Amount: p.Wager.Total})
	}
	return snapshot
}

func (t *baccaratTable) resetRound() {
	t.phase = baccaratWaiting
	t.timer.stop()
	clearBets(t.seats())
}

func (t *baccaratTable) markDisconnected(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	if p := t.participants[name]; p != nil {
		p.Connected = false
	}
	return nil
}

func (t *baccaratTable) markConnected(name string) {
	t.mu.Lock()
	defer t.mu.Unlock()
	if p := t.participants[name]; p != nil {
		p.Connected = true
	}
}

func (t *baccaratTable) hasParticipant(name string) bool {
	t.mu.Lock()
	defer t.mu.Unlock()
	return t.participants[name] != nil
}

// baccaratTotal scores a hand modulo 10.
func baccaratTotal(hand []card) int {
	total := 0
	for _, c := range hand {
		total += c.baccaratValue()
	}
	return total % 10
}

// playTableau deals the initial four cards and applies the punto banco
// drawing rules, returning the final player and banker hands.
func playTableau(sh *shoe) (player, banker []card) {
	player = []card{sh.draw(), sh.draw()}
	banker = []card{sh.draw(), sh.draw()}
	playerTotal, bankerTotal := baccaratTotal(player), baccaratTotal(banker)
	if playerTotal >= 8 || bankerTotal >= 8 {
		// A natural 8 or 9 ends the hand immediately.
		return player, banker
	}
	playerThird := -1
	if playerTotal <= 5 {
		c := sh.draw()
		playerThird = c.baccaratValue()
		player = append(player, c)
	}
	if playerThird < 0 {
		if bankerTotal <= 5 {
			banker = append(banker, sh.draw())
		}
		return player, banker
	}
	if bankerDraws(bankerTotal, playerThird) {
		banker = append(banker, sh.draw())
	}
	return player, banker
}

// bankerDraws applies the banker tableau when the player took a third card.
func bankerDraws(bankerTotal, playerThird int) bool {
	switch bankerTotal {
	case 0, 1, 2:
		return true
	case 3:
		return playerThird != 8
	case 4:
		return playerThird >= 2 && playerThird <= 7
	case 5:
		return playerThird >= 4 && playerThird <= 7
	case 6:
		return playerThird == 6 || playerThird == 7
	default: // 7
		return false
	}
}

// settleBaccaratBet returns the result label and total payout (stake plus
// winnings) for one wager. Player and banker bets push on a tie.
func settleBaccaratBet(spot string, bet int, playerTotal, bankerTotal int, rules BaccaratConfig) (string, int) {
	switch {
	case playerTotal > bankerTotal:
		if spot == baccaratSpotPlayer {
			return "win", bet * 2
		}
	case bankerTotal > playerTotal:
		if spot == baccaratSpotBanker {
			return "win", bet + int(math.Round(float64(bet)*rules.BankerPayout))
		}
	default:
		if spot == baccaratSpotTie {
			return "win", bet + int(math.Round(float64(bet)*rules.TiePayout))
		}
		return "push", bet
	}
	return "loss", 0
}

func baccaratOutcomeMessage(playerTotal, bankerTotal int) string {
	switch {
	case playerTotal > bankerTotal:
		return fmt.Sprintf("Player wins %d over %d.", playerTotal, bankerTotal)
	case bankerTotal > playerTotal:
		return fmt.Sprintf("Banker wins %d over %d.", bankerTotal, playerTotal)
	default:
		return fmt.Sprintf("Player and banker tie at %d.", playerTotal)
	}
}

func baccaratResultMessages(name, result string, payout int) (string, string) {
	switch result {
	case "win":
		return name + " wins!", fmt.Sprintf("You win! +%d credits", payout)
	case "push":
		return name + " pushes.", fmt.Sprintf("You push. +%d credits", payout)
	default:
		return name + " loses.", "You lose!"
	}
}