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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
package casino
import (
"fmt"
"math"
"math/rand"
"strings"
"sync"
)
type blackjackPhase string
const (
blackjackWaiting blackjackPhase = "waiting"
blackjackBetting blackjackPhase = "betting"
blackjackInsurance blackjackPhase = "insurance"
blackjackPlaying blackjackPhase = "playing"
)
type blackjackHand struct {
cards []card
bet int
fromSplit bool
doubled bool
stood bool
bust bool
surrendered bool
}
func (h *blackjackHand) total() (int, bool) {
total, aces := 0, 0
for _, c := range h.cards {
total += c.blackjackValue()
if c.rank == "A" {
aces++
}
}
for total > 21 && aces > 0 {
total -= 10
aces--
}
soft := aces > 0
return total, soft
}
func (h *blackjackHand) blackjack(rules BlackjackConfig) bool {
total, _ := h.total()
return len(h.cards) == 2 && !h.fromSplit && total == 21 || rules.CountsDoubleSplitBlackjack() && h.fromSplit && h.doubled && total == 21
}
type blackjackPlayer struct {
Participant
hands []*blackjackHand
insurance int
declinedInsurance bool
lastBet int
}
func (p *blackjackPlayer) participantPtr() *Participant { return &p.Participant }
type blackjackTable struct {
mu sync.Mutex
tableBase
Rules BlackjackConfig
players seatSet[*blackjackPlayer]
phase blackjackPhase
window bettingWindow
timer countdown
activePlayer int
activeHand int
dealer blackjackHand
shoe *shoe
}
func newBlackjackTable(roomID int, cfg TableConfig, rng *rand.Rand) *blackjackTable {
cfg = cfg.Normalize()
rules := cfg.Blackjack.Normalize()
return &blackjackTable{
tableBase: tableBase{RoomID: roomID, Config: cfg}, Rules: rules,
players: newSeatSet[*blackjackPlayer](), phase: blackjackWaiting,
shoe: newShoe(rules.Decks, rules.ShuffleAt, rng),
}
}
func (t *blackjackTable) menuEvent(name string) Event {
e := t.private(name, t.menu())
e.Color = "casino_menu"
e.Menu = &MenuView{Kind: MenuBlackjackBet, Commands: []MenuCommand{
{Command: "bet", Desc: "Place a wager (min/max accepted)"},
{Command: "bet <amount>", Desc: "Place a specific wager"},
{Command: "stop", Desc: "Leave the table"},
}}
return e
}
func (t *blackjackTable) join(name string) []Event {
t.mu.Lock()
defer t.mu.Unlock()
if t.players.has(name) {
return []Event{t.menuEvent(name)}
}
if t.players.count() >= t.Config.MaxPlayers {
return []Event{t.private(name, "That blackjack table is full.")}
}
t.players.add(name, &blackjackPlayer{Participant: Participant{Name: name, Connected: true}})
events := []Event{t.playerAction(name, fmt.Sprintf("%s sits down.", name), "You sit down.")}
events = append(events, t.menuEvent(name))
return events
}
func (t *blackjackTable) leave(name string) []Event {
t.mu.Lock()
defer t.mu.Unlock()
if !t.players.has(name) {
return nil
}
if t.phase != blackjackWaiting {
return []Event{t.private(name, "You're in the middle of a blackjack round!")}
}
t.players.remove(name)
events := []Event{t.playerAction(name, fmt.Sprintf("%s stands up.", name), "You stand up.")}
if t.players.count() == 0 {
events = append(events, t.event("The blackjack table closes.", true))
}
return events
}
func (t *blackjackTable) bet(name, spot string, amount int, wallet Wallet) []Event {
t.mu.Lock()
defer t.mu.Unlock()
p := t.players.get(name)
if p == nil {
return []Event{t.private(name, "You aren't playing blackjack.")}
}
if spot != "" {
return []Event{t.private(name, "Blackjack bets don't take a bet target.")}
}
if t.phase != blackjackWaiting && t.phase != blackjackBetting {
return []Event{t.private(name, "That blackjack table isn't accepting bets.")}
}
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("Blackjack 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 = true, wager, wallet, amount
events := []Event{t.playerAction(name, fmt.Sprintf("%s bets %d credits.", name, amount), fmt.Sprintf("You bet %d credits.", amount))}
if t.players.count() > 1 && t.phase == blackjackWaiting {
t.phase = blackjackBetting
t.window.open(t.Config.BettingTicks)
events = append(events, t.event(fmt.Sprintf("Blackjack betting is open for %d ticks.", t.Config.BettingTicks), true))
}
if allBet(t.players.participants()) {
events = append(events, t.startRound()...)
}
return events
}
func (t *blackjackTable) tick() []Event {
t.mu.Lock()
defer t.mu.Unlock()
switch t.phase {
case blackjackBetting:
sitOuts, resolve, pending := t.window.advance(t.players.participants())
if pending {
return nil
}
var events []Event
for _, name := range sitOuts {
events = append(events, t.event(fmt.Sprintf("%s sits out this blackjack round.", name), true))
}
if resolve {
events = append(events, t.startRound()...)
} else {
t.resetRound()
}
return events
case blackjackInsurance:
if t.insuranceSettled() {
return t.finishInsurance()
}
t.timer.tick()
if !t.timer.expired() {
return nil
}
return t.finishInsurance()
case blackjackPlaying:
name := t.activeName()
if name == "" {
return nil
}
active := t.players.get(name)
if active == nil {
// Unreachable while mid-round leaves are blocked; keep the game moving.
t.activePlayer++
return t.finishHand()
}
if active.Connected && t.bettorCount() <= 1 {
// A lone connected player is never timed out. A disconnected
// active player always is, so the table can never stall.
return nil
}
t.timer.tick()
if !t.timer.expired() {
return nil
}
active.hands[t.activeHand].stood = true
events := []Event{t.private(name, "Your action timed out; standing.")}
events = append(events, t.finishHand()...)
return events
default:
return nil
}
}
func (t *blackjackTable) action(name, action string) []Event {
t.mu.Lock()
defer t.mu.Unlock()
if t.phase == blackjackInsurance {
return t.insuranceAction(name, action)
}
if t.phase != blackjackPlaying || t.activeName() != name {
return []Event{t.private(name, "It's not your turn.")}
}
p := t.players.get(name)
hand := p.hands[t.activeHand]
t.timer.start(t.Rules.ActionTicks)
switch action {
case "hit":
if fromSplitAces(hand) {
return []Event{t.private(name, "Split aces cannot be hit.")}
}
hand.cards = append(hand.cards, t.shoe.draw())
total, _ := hand.total()
if total > 21 {
hand.bust = true
}
e := t.playerAction(name, fmt.Sprintf("%s hits and receives %s.", name, hand.cards[len(hand.cards)-1]), fmt.Sprintf("You hit and receive %s.", hand.cards[len(hand.cards)-1]))
e.Blackjack = t.snapshot(false)
events := []Event{e}
if total >= 21 {
events = append(events, t.finishHand()...)
} else {
events = append(events, t.prompt()...)
}
return events
case "stand":
hand.stood = true
return t.finishHand()
case "double":
if len(hand.cards) != 2 || hand.doubled || hand.surrendered || fromSplitAces(hand) || (hand.fromSplit && !t.Rules.AllowsDoubleAfterSplit()) {
return []Event{t.private(name, "You can only double a qualifying hand.")}
}
if _, ok := p.Wallet.Wager(hand.bet); !ok {
return []Event{t.private(name, "You don't have enough chips and credits to double that hand.")}
}
hand.bet *= 2
hand.doubled = true
hand.stood = true
hand.cards = append(hand.cards, t.shoe.draw())
if total, _ := hand.total(); total > 21 {
hand.bust = true
}
e := t.playerAction(name, fmt.Sprintf("%s doubles down.", name), "You double down.")
e.Blackjack = t.snapshot(false)
return append([]Event{e}, t.finishHand()...)
case "split":
return t.splitHand(name, p, hand)
case "surrender":
if !t.Rules.AllowsSurrender() || hand.fromSplit || len(hand.cards) != 2 {
return []Event{t.private(name, "Surrender is not available for this hand.")}
}
hand.surrendered = true
return t.finishHand()
default:
return []Event{t.private(name, "Unknown blackjack action.")}
}
}
// insuranceAction handles commands during the insurance offer window:
// "insurance" buys cover for half the base bet; any other action declines.
func (t *blackjackTable) insuranceAction(name, action string) []Event {
p := t.players.get(name)
if p == nil || !p.HasBet {
return []Event{t.private(name, "Insurance is only offered to players with a bet this round.")}
}
if p.insurance > 0 || p.declinedInsurance {
return []Event{t.private(name, "You have already decided about insurance.")}
}
var events []Event
if action == "insurance" {
amount := p.hands[0].bet / 2
if amount <= 0 {
return []Event{t.private(name, "Your bet is too small to insure.")}
}
if _, ok := p.Wallet.Wager(amount); !ok {
return []Event{t.private(name, "You don't have enough chips and credits for insurance.")}
}
p.insurance = amount
events = append(events, t.playerAction(name, fmt.Sprintf("%s takes insurance.", name), fmt.Sprintf("You place %d credits on insurance.", amount)))
} else {
p.declinedInsurance = true
events = append(events, t.private(name, "You decline insurance."))
}
if t.insuranceSettled() {
events = append(events, t.finishInsurance()...)
}
return events
}
// insuranceSettled reports whether every connected bettor has taken or
// declined insurance.
func (t *blackjackTable) insuranceSettled() bool {
for _, p := range t.players.ordered() {
if !p.HasBet || !p.Connected {
continue
}
if p.insurance == 0 && !p.declinedInsurance {
return false
}
}
return true
}
// finishInsurance resolves the insurance window: a dealer blackjack settles
// the round immediately, otherwise play begins.
func (t *blackjackTable) finishInsurance() []Event {
if t.dealerBlackjack() {
return t.settle()
}
t.beginPlay()
return t.finishHand()
}
func (t *blackjackTable) splitHand(name string, p *blackjackPlayer, hand *blackjackHand) []Event {
if len(hand.cards) != 2 || len(p.hands) >= t.Rules.MaxSplitHands || !splitCompatible(hand.cards[0], hand.cards[1], t.Rules.AllowsUnlikeTenSplit()) {
return []Event{t.private(name, "That hand cannot be split.")}
}
if hand.cards[0].rank == "A" && hand.fromSplit && !t.Rules.AllowsResplitAces() {
return []Event{t.private(name, "Aces cannot be re-split at this table.")}
}
if _, ok := p.Wallet.Wager(hand.bet); !ok {
return []Event{t.private(name, "You don't have enough chips and credits to split that hand.")}
}
left := &blackjackHand{cards: []card{hand.cards[0]}, bet: hand.bet, fromSplit: true}
right := &blackjackHand{cards: []card{hand.cards[1]}, bet: hand.bet, fromSplit: true}
left.cards = append(left.cards, t.shoe.draw())
right.cards = append(right.cards, t.shoe.draw())
index := t.activeHand
p.hands = append(p.hands[:index], append([]*blackjackHand{left, right}, p.hands[index+1:]...)...)
e := t.playerAction(name, fmt.Sprintf("%s splits the hand.", name), "You split the hand.")
e.Blackjack = t.snapshot(false)
events := []Event{e}
if left.cards[0].rank == "A" {
left.stood = !(t.Rules.AllowsResplitAces() && left.cards[1].rank == "A")
right.stood = !(t.Rules.AllowsResplitAces() && right.cards[1].rank == "A")
events = append(events, t.finishHand()...)
} else {
events = append(events, t.prompt()...)
}
return events
}
func splitCompatible(a, b card, unlikeTens bool) bool {
if a.rank == b.rank {
return true
}
return unlikeTens && a.blackjackValue() == 10 && b.blackjackValue() == 10
}
func (t *blackjackTable) startRound() []Event {
t.dealer = blackjackHand{cards: []card{t.shoe.draw(), t.shoe.draw()}}
for _, p := range t.players.ordered() {
if !p.HasBet {
continue
}
p.hands = []*blackjackHand{{cards: []card{t.shoe.draw(), t.shoe.draw()}, bet: p.Wager.Total}}
if p.hands[0].bet/2 <= 0 {
// A half-bet that rounds to zero cannot be insured.
p.declinedInsurance = true
}
}
roundEvent := t.event("", true)
roundEvent.Blackjack = t.snapshot(false)
events := []Event{roundEvent}
if t.dealer.cards[1].rank == "A" {
t.phase = blackjackInsurance
t.timer.start(t.Rules.ActionTicks)
events = append(events, t.event("The dealer shows an ace. Insurance is on offer.", true))
for _, p := range t.players.ordered() {
if !p.HasBet || !p.Connected || p.declinedInsurance {
continue
}
events = append(events, t.private(p.Name, fmt.Sprintf("Insure your %d-credit bet for %d credits? Type \"insurance\" to buy, or any other action to decline.", p.hands[0].bet, p.hands[0].bet/2)))
}
if t.insuranceSettled() {
events = append(events, t.finishInsurance()...)
}
return events
}
if t.dealerBlackjack() {
events = append(events, t.settle()...)
return events
}
t.beginPlay()
return append(events, t.finishHand()...)
}
func (t *blackjackTable) beginPlay() {
t.phase = blackjackPlaying
t.timer.start(t.Rules.ActionTicks)
t.activePlayer, t.activeHand = 0, 0
}
func (t *blackjackTable) prompt() []Event {
name := t.activeName()
if name == "" {
return t.settle()
}
p := t.players.get(name)
h := p.hands[t.activeHand]
ranks := make([]string, len(h.cards))
for i, card := range h.cards {
ranks[i] = card.rank
}
actions := t.availableActions(p, h)
e := t.private(name, fmt.Sprintf("Your turn (%s).\nActions: %s", strings.Join(ranks, ","), strings.Join(actions, ", ")))
e.Color = "casino_menu"
e.Menu = &MenuView{Kind: MenuBlackjackTurn, Title: fmt.Sprintf("Your turn (%s)", strings.Join(ranks, ",")), Actions: actions, Commands: blackjackActionCommands(actions)}
return []Event{e}
}
func blackjackActionCommands(actions []string) []MenuCommand {
descriptions := map[string]string{
"hit": "Take another card",
"stand": "Hold your total",
"double": "Double your bet and take one card",
"split": "Split your pair into two hands",
"surrender": "Forfeit half your bet",
}
commands := make([]MenuCommand, 0, len(actions))
for _, action := range actions {
commands = append(commands, MenuCommand{Command: action, Desc: descriptions[action]})
}
return commands
}
func (t *blackjackTable) availableActions(p *blackjackPlayer, hand *blackjackHand) []string {
if fromSplitAces(hand) {
// Split aces receive one card each and may only be re-split.
actions := []string{"stand"}
if len(hand.cards) == 2 && len(p.hands) < t.Rules.MaxSplitHands && t.Rules.AllowsResplitAces() &&
splitCompatible(hand.cards[0], hand.cards[1], t.Rules.AllowsUnlikeTenSplit()) {
actions = append(actions, "split")
}
return actions
}
actions := []string{"hit", "stand"}
if len(hand.cards) == 2 && !hand.doubled && (!hand.fromSplit || t.Rules.AllowsDoubleAfterSplit()) {
actions = append(actions, "double")
}
if len(hand.cards) == 2 && len(p.hands) < t.Rules.MaxSplitHands && splitCompatible(hand.cards[0], hand.cards[1], t.Rules.AllowsUnlikeTenSplit()) &&
!(hand.cards[0].rank == "A" && hand.fromSplit && !t.Rules.AllowsResplitAces()) {
actions = append(actions, "split")
}
if t.Rules.AllowsSurrender() && !hand.fromSplit && len(hand.cards) == 2 {
actions = append(actions, "surrender")
}
return actions
}
func fromSplitAces(hand *blackjackHand) bool {
return hand.fromSplit && len(hand.cards) > 0 && hand.cards[0].rank == "A"
}
func (t *blackjackTable) finishHand() []Event {
t.skipCompletedHands()
if t.activeName() == "" {
return t.settle()
}
return t.prompt()
}
func (t *blackjackTable) skipCompletedHands() {
for t.activePlayer < t.players.count() {
p := t.players.get(t.players.nameAt(t.activePlayer))
if p == nil || !p.HasBet {
t.activePlayer++
t.activeHand = 0
continue
}
for t.activeHand < len(p.hands) {
h := p.hands[t.activeHand]
total, _ := h.total()
if h.stood || h.bust || h.surrendered || total >= 21 || h.blackjack(t.Rules) {
t.activeHand++
continue
}
return
}
t.activePlayer++
t.activeHand = 0
}
}
func (t *blackjackTable) activeName() string {
return t.players.nameAt(t.activePlayer)
}
func (t *blackjackTable) dealerBlackjack() bool {
return t.dealer.blackjack(t.Rules)
}
func (t *blackjackTable) settle() []Event {
for {
total, soft := t.dealer.total()
if total > 17 || total == 17 && (!soft || !t.Rules.DealerHitsSoft17()) {
break
}
t.dealer.cards = append(t.dealer.cards, t.shoe.draw())
}
dealerTotal, _ := t.dealer.total()
dealerEvent := t.event("", true)
dealerEvent.Blackjack = t.snapshot(true)
events := []Event{dealerEvent}
dealerBlackjack := t.dealerBlackjack()
for _, p := range t.players.ordered() {
if !p.HasBet {
continue
}
for _, hand := range p.hands {
result, payout := settleHand(hand, dealerTotal, dealerBlackjack, t.Rules)
if payout > 0 && p.Wallet != nil {
p.Wallet.PayCredits(payout)
}
publicResult, privateResult := blackjackResultMessages(p.Name, result, payout)
settlement := t.playerAction(p.Name, publicResult, privateResult)
if result == "blackjack" {
settlement.Color = "casino_jackpot_win"
} else if result == "win" {
settlement.Color = "casino_big_win"
}
events = append(events, settlement)
}
if p.insurance > 0 {
insurancePayout := 0
if dealerBlackjack {
insurancePayout = int(math.Floor(float64(p.insurance) * (1 + t.Rules.InsurancePayout)))
}
if insurancePayout > 0 {
p.Wallet.PayCredits(insurancePayout)
}
events = append(events, t.private(p.Name, fmt.Sprintf("Insurance pays %d credits.", insurancePayout)))
}
}
t.resetRound()
for _, p := range t.players.ordered() {
if p.Connected {
events = append(events, t.menuEvent(p.Name))
}
}
return events
}
func settleHand(hand *blackjackHand, dealerTotal int, dealerBlackjack bool, rules BlackjackConfig) (string, int) {
total, _ := hand.total()
if hand.surrendered {
return "surrender", hand.bet / 2
}
if hand.bust {
return "bust", 0
}
playerBlackjack := hand.blackjack(rules)
if playerBlackjack && dealerBlackjack {
return "push", hand.bet
}
if dealerBlackjack {
return "dealer blackjack", 0
}
if playerBlackjack {
// Winnings round down to whole credits; never bet less than two.
return "blackjack", hand.bet + int(math.Floor(float64(hand.bet)*rules.BlackjackPayout))
}
if total > 21 || dealerTotal > total && dealerTotal <= 21 {
return "loss", 0
}
if dealerTotal == total {
return "push", hand.bet
}
if dealerTotal > 21 {
return "win", hand.bet * 2
}
return "win", hand.bet * 2
}
func (t *blackjackTable) resetRound() {
t.phase = blackjackWaiting
t.window.close()
t.timer.stop()
for _, p := range t.players.ordered() {
p.HasBet, p.Wager, p.hands, p.insurance, p.declinedInsurance = false, Wager{}, nil, 0, false
}
}
func (t *blackjackTable) rebet(name string) (string, int) {
if p := t.players.get(name); p != nil && p.lastBet > 0 {
return "", p.lastBet
}
return "", t.Config.MinBet
}
func (t *blackjackTable) bettorCount() int {
count := 0
for _, p := range t.players.ordered() {
if p.HasBet {
count++
}
}
return count
}
func (t *blackjackTable) snapshot(revealDealer bool) *BlackjackSnapshot {
snapshot := &BlackjackSnapshot{}
for i, card := range t.dealer.cards {
if i == 0 && !revealDealer {
snapshot.Dealer = append(snapshot.Dealer, CardView{Rank: "?", Hidden: true})
continue
}
snapshot.Dealer = append(snapshot.Dealer, CardView{Rank: card.rank, Suit: card.suit})
}
for _, p := range t.players.ordered() {
if !p.HasBet {
continue
}
for _, hand := range p.hands {
view := BlackjackPlayerView{Name: p.Name, Score: handScore(hand)}
for _, card := range hand.cards {
view.Cards = append(view.Cards, CardView{Rank: card.rank, Suit: card.suit})
}
snapshot.Players = append(snapshot.Players, view)
}
}
if len(t.dealer.cards) > 1 {
snapshot.DealerShownScore = handScore(&blackjackHand{cards: []card{t.dealer.cards[1]}})
}
snapshot.DealerScore = handScore(&t.dealer)
snapshot.DealerRevealed = revealDealer
return snapshot
}
func (t *blackjackTable) menu() string {
return "New blackjack hand! Place your bet."
}
func handScore(hand *blackjackHand) string {
total, soft := hand.total()
if !soft || total == 21 {
return fmt.Sprintf("%d", total)
}
return fmt.Sprintf("%d or %d", total-10, total)
}
func blackjackResultMessages(name, result string, payout int) (string, string) {
switch result {
case "win", "blackjack":
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!"
}
}
// markDisconnected removes the player from the table, forfeiting any wager
// they committed (settle only pays seated bettors). Removing a seat shifts
// the seat indices the active turn points into, so mid-round play is fixed
// up and handed to the next bettor (or settled) immediately.
func (t *blackjackTable) markDisconnected(name string) []Event {
t.mu.Lock()
defer t.mu.Unlock()
index := t.players.indexOf(name)
if index < 0 {
return nil
}
t.players.remove(name)
events := []Event{t.event(fmt.Sprintf("%s stands up.", name), true)}
if t.players.count() == 0 {
events = append(events, t.event("The blackjack table closes.", true))
}
if t.phase != blackjackPlaying {
// Betting and insurance resolve against the remaining seats on the
// next tick; waiting has no round in flight.
return events
}
switch {
case index < t.activePlayer:
// The same player stays active, one seat earlier.
t.activePlayer--
case index == t.activePlayer:
// The next bettor slides into the active seat.
t.activeHand = 0
}
t.timer.start(t.Rules.ActionTicks)
return append(events, t.finishHand()...)
}
func (t *blackjackTable) hasParticipant(name string) bool {
t.mu.Lock()
defer t.mu.Unlock()
return t.players.has(name)
}
|