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
|
package casino
import (
"strings"
"testing"
)
func uxBlackjack() *blackjackTable {
t := newBlackjackTable(1, TableConfig{ID: "bj", Game: GameBlackjack, Blackjack: BlackjackConfig{ActionTicks: 8}}, newTestRand())
t.phase = blackjackPlaying
t.dealer.cards = []card{{rank: "10", suit: "spades"}, {rank: "7", suit: "hearts"}}
t.players.add("alice", &blackjackPlayer{Participant: Participant{Name: "alice", Connected: true, HasBet: true, Wager: Wager{Total: 10}}, hands: []*blackjackHand{{bet: 10, cards: []card{{rank: "10"}, {rank: "6"}}}}})
t.prompt()
return t
}
func TestTableStatusDoesNotExtendAnyDeadlineOrRevealHoleCard(t *testing.T) {
table := uxBlackjack()
table.tick()
beforeTimer, beforeCeiling := table.timer, table.ceiling
for i := 0; i < 3; i++ {
events := table.status("alice")
snap := events[0].Snapshot.(*BlackjackSnapshot)
if !snap.Dealer[0].Hidden || snap.Dealer[0].Rank != "?" {
t.Fatal("status revealed hole card")
}
if !snap.Players[0].Active || snap.Players[0].Hand != 1 || snap.Players[0].Bet != 10 {
t.Fatalf("missing active hand: %+v", snap.Players[0])
}
}
if table.timer != beforeTimer || table.ceiling != beforeCeiling {
t.Fatal("status extended deadline")
}
}
func TestBlackjackWarnsOnceThenAutoStands(t *testing.T) {
for _, multi := range []bool{false, true} {
table := uxBlackjack()
if multi {
table.players.add("bob", &blackjackPlayer{Participant: Participant{Name: "bob", Connected: true, HasBet: true}, hands: []*blackjackHand{{bet: 10, stood: true, cards: []card{{rank: "10"}, {rank: "8"}}}}})
}
warnings := 0
timeout := false
for i := 0; i < 81; i++ {
for _, e := range table.tick() {
if e.PrivateTo == "alice" && strings.Contains(e.Message, "No response means stand") {
warnings++
if e.Color != "casino_warning" {
t.Fatal("warning not colored")
}
}
if strings.Contains(e.Message, "standing.") {
timeout = true
if e.Color != "casino_warning" {
t.Fatal("timeout not colored")
}
}
}
}
if multi {
if warnings != 1 || !timeout {
t.Fatalf("multi warnings=%d timeout=%v", warnings, timeout)
}
continue
}
// A lone player has no timer: no warning, no forced stand.
if warnings != 0 || timeout || table.phase != blackjackPlaying {
t.Fatalf("solo warnings=%d timeout=%v phase=%s", warnings, timeout, table.phase)
}
}
}
func TestInsuranceRequiresExplicitDecisionAndWarns(t *testing.T) {
table := uxBlackjack()
// A second seated player keeps the multi-player insurance timer running;
// they have already declined so only alice is still deciding.
table.players.add("bob", &blackjackPlayer{Participant: Participant{Name: "bob", Connected: true, HasBet: true, Wager: Wager{Total: 10}}, declinedInsurance: true})
table.phase = blackjackInsurance
table.timer.start(8)
before := len(table.players.get("alice").hands[0].cards)
events := table.action("alice", "hit")
if !hasMessage(events, "No card action") || table.players.get("alice").declinedInsurance || len(table.players.get("alice").hands[0].cards) != before {
t.Fatal("hit silently declined or took card")
}
warnings := 0
for i := 0; i < 8; i++ {
for _, e := range table.tick() {
if strings.Contains(e.Message, "Insurance closes") {
warnings++
if e.Color != "casino_warning" {
t.Fatal("insurance warning color")
}
}
}
}
if warnings != 1 || table.phase != blackjackPlaying {
t.Fatalf("warnings=%d phase=%s", warnings, table.phase)
}
}
func TestBettingWarningOnlyTargetsPlayersWhoNeedToBet(t *testing.T) {
table := newBaccaratTestTable(TableConfig{ID: "bac", Game: GameBaccarat, BettingTicks: 8})
for _, n := range []string{"alice", "bob", "carol"} {
table.join(n)
}
table.action("carol", "sitout")
table.bet("alice", "banker", 10, &testWallet{credits: 100})
warnings := 0
for i := 0; i < 8; i++ {
for _, e := range table.tick() {
if e.Color == "casino_warning" {
warnings++
if e.PrivateTo != "bob" {
t.Fatalf("warning sent to %s", e.PrivateTo)
}
}
}
}
if warnings != 1 {
t.Fatalf("warnings=%d", warnings)
}
}
func TestSitoutDoesNotBlockOthersAndBetRejoins(t *testing.T) {
table := newBaccaratTestTable(TableConfig{ID: "bac", Game: GameBaccarat})
table.join("alice")
table.join("bob")
table.bet("alice", "banker", 10, &testWallet{credits: 100})
events := table.action("bob", "sitout")
if !hasMessage(events, "New baccarat hand!") || !table.players.get("bob").SittingOut {
t.Fatal("sitout held up round")
}
table.action("alice", "sitout")
events = table.bet("bob", "player", 10, &testWallet{credits: 100})
if !hasMessage(events, "New baccarat hand!") || table.players.get("bob").SittingOut {
t.Fatal("bet failed to rejoin")
}
}
func TestQueuedLeaveAndSitoutPreserveCommittedBlackjackHands(t *testing.T) {
for _, action := range []string{"leave_after", "sitout"} {
table := uxBlackjack()
hand := table.players.get("alice").hands[0]
table.action("alice", action)
if !table.hasParticipant("alice") || hand.stood || hand.surrendered {
t.Fatalf("%s altered committed hand", action)
}
events := table.action("alice", "stand")
if !hasMessage(events, "{casino_payout}-10 credits{/}") {
t.Fatalf("missing settlement: %+v", events)
}
if action == "leave_after" && table.hasParticipant("alice") {
t.Fatal("queued leave retained seat")
}
if action == "sitout" && !table.players.get("alice").SittingOut {
t.Fatal("queued sitout not applied")
}
}
}
func TestSplitAndInsuranceRoundNetIncludesEveryStake(t *testing.T) {
table := uxBlackjack()
p := table.players.get("alice")
p.hands = []*blackjackHand{{bet: 20, stood: true, cards: []card{{rank: "10"}, {rank: "10"}}}, {bet: 10, bust: true, cards: []card{{rank: "10"}, {rank: "10"}, {rank: "5"}}}}
p.insurance = 5
events := table.settle()
for _, text := range []string{
"{casino_result_win}WIN{/}",
"{casino_payout}+5 credits{/}",
"Hand 1/2:{/} {casino_result_win}+20 credits{/}",
"Hand 2/2:{/} {casino_result_loss}-10 credits{/}",
"Insurance:{/} {casino_result_loss}-5 credits{/}",
} {
if !hasMessage(events, text) {
t.Fatalf("missing %q in %+v", text, events)
}
}
}
func TestSingleHandSettlementKeepsSummaryQuiet(t *testing.T) {
// A connected player with one hand and no insurance already saw the net in
// the per-hand result; the next bet menu carries the balance.
table := uxBlackjack()
table.dealer.cards = []card{{rank: "10", suit: "spades"}, {rank: "7", suit: "hearts"}}
events := table.settle()
if hasMessage(events, "Round complete") {
t.Fatalf("single-hand settlement repeated the net: %+v", events)
}
for _, e := range events {
if e.ShowBalance {
t.Fatalf("balance attached to a settlement whose next menu carries it: %+v", e)
}
}
}
func TestDepartingSettlementCarriesBalanceSummary(t *testing.T) {
table := uxBlackjack()
table.dealer.cards = []card{{rank: "10", suit: "spades"}, {rank: "7", suit: "hearts"}}
table.players.get("alice").LeaveAfter = true
events := table.settle()
found := false
for _, e := range events {
if strings.Contains(e.PrivateMessage, "LOSS") && e.ShowBalance {
found = true
}
}
if !found {
t.Fatalf("departing player lost the balance summary: %+v", events)
}
}
func TestRebetMenusAndMidRoundJoin(t *testing.T) {
table := uxBlackjack()
events := table.join("bob")
if !hasMessage(events, "Round in progress") {
t.Fatal("mid-round join offered betting")
}
bac := newBaccaratTestTable(TableConfig{ID: "bac", Game: GameBaccarat})
bac.join("alice")
first := bac.menuEvent("alice")
if first.Menu.Commands[0].Command == "bet <amount>" {
t.Fatal("first baccarat bet omitted target")
}
bac.bet("alice", "banker", 25, &testWallet{credits: 100})
menu := bac.menuEvent("alice").Menu
if !menu.Repeat || menu.Commands[0].Command != "bet" || !strings.Contains(menu.Commands[0].Desc, "Repeat") {
t.Fatalf("missing rebet details: %+v", menu)
}
}
|