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
|
package casino
import (
"strings"
"testing"
"time"
)
func crapsEventText(events []Event) string {
var s strings.Builder
for _, e := range events {
s.WriteString(e.Message)
s.WriteByte('\n')
s.WriteString(e.PrivateMessage)
}
return s.String()
}
func TestCrapsMultiplayerClockAndLateBets(t *testing.T) {
tb := crapsFixture()
tb.join("alice")
tb.join("bob")
w := &testWallet{chips: 1000}
tb.bet("alice", "pass", 10, w)
if tb.view().ShootIn != 10 || tb.view().DealerIn != 70 {
t.Fatal(tb.view())
}
if text := crapsEventText(tb.tickElapsed(6 * time.Second)); strings.Contains(text, "Last call") {
t.Fatal("early warning")
}
events := tb.tickElapsed(time.Second)
if !strings.Contains(crapsEventText(events), "Last call for bets! 3 seconds until alice can shoot!") {
t.Fatal(events)
}
if !events[0].Public {
t.Fatal("warning not broadcast")
}
tb.action("alice", "roll")
if tb.dice != [2]int{} {
t.Fatal("early shoot")
}
tb.tickElapsed(3 * time.Second)
if tb.view().ShootIn != 0 || tb.view().DealerIn != 60 {
t.Fatal(tb.view())
}
tb.bet("alice", "field", 10, w)
if len(w.open) != 2 {
t.Fatal("late bet rejected")
}
tb.action("alice", "unbet field")
if len(tb.players.get("alice").bets) != 1 {
t.Fatal("late removal rejected")
}
if strings.Contains(crapsEventText(tb.tickElapsed(59*time.Second)), "Last call") {
t.Fatal("warning repeated")
}
if tb.dice != [2]int{} {
t.Fatal("early forced roll")
}
tb.tickElapsed(time.Second)
if tb.dice == [2]int{} {
t.Fatal("60 second dealer fallback missing")
}
}
func TestCrapsSoloAndRosterTransitions(t *testing.T) {
tb := crapsFixture()
tb.join("alice")
w := &testWallet{chips: 1000}
tb.bet("alice", "fire", 1, w)
tb.tickElapsed(500 * time.Second)
if tb.dice != [2]int{} || tb.view().DealerIn != 0 || tb.view().ShootIn != 0 {
t.Fatal("solo deadline", tb.view())
}
tb.action("alice", "roll")
if tb.dice == [2]int{} {
t.Fatal("solo could not roll")
}
tb.join("bob")
if tb.view().ShootIn != 10 {
t.Fatal("join did not give bettors a window")
}
tb.tickElapsed(4 * time.Second)
tb.bet("alice", "field", 10, w)
if tb.view().ShootIn != 6 {
t.Fatal("bet extended timer")
}
tb.leave("bob")
if tb.view().ShootIn != 0 || tb.view().DealerIn != 0 {
t.Fatal("departure left forced timer")
}
tb.join("bob")
tb.action("bob", "sitout")
if tb.view().ShootIn != 0 {
t.Fatal("sit-out counts as playing")
}
tb.refundAll()
if tb.hasLiveBets() {
t.Fatal("refund retained wagers")
}
}
func TestCrapsAutorollPaceAndStop(t *testing.T) {
tb := crapsFixture()
tb.join("alice")
w := &testWallet{chips: 1000}
tb.bet("alice", "fire", 1, w)
tb.action("alice", "autoroll")
tb.tickElapsed(2999 * time.Millisecond)
if tb.dice != [2]int{} {
t.Fatal("autoroll fired early")
}
tb.tickElapsed(time.Millisecond)
if tb.dice == [2]int{} {
t.Fatal("autoroll did not fire")
}
if p := tb.players.get("alice"); p.HasBet && p.autoElapsed != 0 {
t.Fatal("pace not reset")
}
handle := &Table{tableBase: tb.tableBase, game: tb}
tb.players.get("alice").autoRoll = true
if _, stopped := handle.StopAutoRoll("alice"); !stopped {
t.Fatal("stop failed")
}
before := tb.dice
tb.tickElapsed(500 * time.Second)
if tb.dice != before || !tb.hasParticipant("alice") {
t.Fatal("stop rolled or departed")
}
if _, stopped := handle.StopAutoRoll("alice"); stopped {
t.Fatal("manual stop incorrectly consumed")
}
// A one-roll bet ends autoroll immediately, regardless of the outcome.
tb = crapsFixture()
tb.join("alice")
w = &testWallet{chips: 100}
tb.bet("alice", "field", 10, w)
tb.action("alice", "autoroll")
tb.tickElapsed(3 * time.Second)
if tb.players.get("alice").autoRoll || tb.players.get("alice").HasBet {
t.Fatal("autoroll survived last bet")
}
before = tb.dice
tb.tickElapsed(3 * time.Second)
if tb.dice != before {
t.Fatal("extra roll after completion")
}
}
func TestCrapsAutorollRespectsOtherPlayers(t *testing.T) {
tb := crapsFixture()
tb.join("alice")
tb.join("bob")
w := &testWallet{chips: 100}
tb.bet("alice", "field", 10, w)
tb.action("alice", "autoroll")
tb.tickElapsed(3 * time.Second)
if tb.dice != [2]int{} {
t.Fatal("autoroll stole multiplayer betting time")
}
tb.tickElapsed(7 * time.Second)
if tb.dice == [2]int{} {
t.Fatal("autoroll failed when ready")
}
// A non-shooter's opt-in never takes the dice away from the current shooter.
tb = crapsFixture()
tb.join("alice")
tb.join("bob")
w = &testWallet{chips: 100}
tb.bet("bob", "field", 10, w)
tb.action("bob", "autoroll")
tb.tickElapsed(11 * time.Second)
if tb.dice != [2]int{} {
t.Fatal("non-shooter autorolled")
}
}
func TestCrapsManagerUsesElapsedCadence(t *testing.T) {
for _, step := range []time.Duration{100 * time.Millisecond, 600 * time.Millisecond, time.Second} {
m := NewManager(4)
cfg := TableConfig{ID: "c", Game: GameCraps}
m.Join(1, cfg, "alice")
w := &testWallet{chips: 100}
m.Bet(1, "c", "alice", "field", 10, w)
m.Action(1, "c", "alice", "autoroll")
tb := m.Table(1, "c").game.(*crapsTable)
for elapsed := step; elapsed < 3*time.Second; elapsed += step {
m.TickElapsed(step)
if tb.dice != [2]int{} {
t.Fatal("early", step)
}
}
m.TickElapsed(step)
if tb.dice == [2]int{} {
t.Fatal("wrong cadence", step)
}
}
}
func TestCrapsConsolidatedResultsAndBetNumbers(t *testing.T) {
tb := crapsFixture()
tb.join("alice")
w := &testWallet{chips: 1000}
events := tb.bet("alice", "pass", 10, w)
if !strings.Contains(crapsEventText(events), "ROLL RESULTS ON YOUR BETS") {
t.Fatal("no post-bet roll table")
}
tb.bet("alice", "dontpass", 10, w)
tb.resolveRoll(2, 3)
events = tb.resolveRoll(3, 4)
balances, settlements := 0, 0
for _, e := range events {
if e.ShowBalance {
balances++
}
if strings.Contains(e.Message, "Pass [5]:") || strings.Contains(e.Message, "Pass [5]") {
settlements++
if !strings.Contains(e.Message, "Don't pass [5]") {
t.Fatal("not combined", e.Message)
}
}
}
if balances != 1 || settlements != 1 {
t.Fatal("duplicate balance or separate results", balances, settlements, crapsEventText(events))
}
text := crapsEventText(events)
for _, ugly := range []string{"returned (stake", "Craps: bets open", "Phase: waiting"} {
if strings.Contains(text, ugly) {
t.Fatal(ugly)
}
}
if !strings.Contains(text, "{casino_result_loss}") || !strings.Contains(text, "{casino_result_win}") {
t.Fatal("missing outcome styles")
}
}
func TestCrapsUnbetAllAndRollTableFormat(t *testing.T) {
tb := crapsFixture()
tb.join("alice")
w := &testWallet{chips: 1000}
tb.bet("alice", "fire", 1, w)
tb.bet("alice", "pass", 10, w)
tb.resolveRoll(3, 3)
tb.bet("alice", "odds pass", 20, w)
tb.bet("alice", "place 4", 10, w)
tb.bet("alice", "field", 10, w)
before := w.paid
events := tb.action("alice", "unbet all")
text := crapsEventText(events)
if !strings.Contains(text, "take down {casino_chips}40{/}") || !strings.Contains(text, "contracts stay up") {
t.Fatal(text)
}
if w.paid != before+40 {
t.Fatal("refund amount", w.paid-before)
}
bets := tb.players.get("alice").bets
if len(bets) != 2 || bets[0].kind != "fire" || bets[1].kind != "pass" || bets[1].odds.Total != 0 {
t.Fatal("contracts or odds mishandled", bets)
}
if tb.hasLiveBets() != true || len(w.open) != 2 {
t.Fatal("pass and fire stakes must stay pending", w.open)
}
tb.action("alice", "unbet all")
if w.paid != before+40 {
t.Fatal("second unbet all moved money", w.paid)
}
// Roll table formatting: grouped totals, single-level tags only.
tb2 := crapsFixture()
tb2.join("alice")
w2 := &testWallet{chips: 100}
tb2.bet("alice", "pass", 1, w2)
table := tb2.rollTable(tb2.players.get("alice"))
for _, tag := range []string{"{casino_payout}7{/}", "{casino_result_win}+1{/}", "{casino_payout}2{/}", "{casino_result_loss}-1{/}"} {
if !strings.Contains(table, tag) {
t.Fatal("missing", tag, table)
}
}
if strings.Contains(table, "{casino_muted}") || strings.Contains(table, "{casino_result_win}+{") {
t.Fatal("nested or unknown tag", table)
}
}
func TestCrapsRollTableHardPairLabels(t *testing.T) {
tb := crapsFixture()
tb.join("alice")
w := &testWallet{chips: 100}
tb.bet("alice", "hard 8", 10, w)
tb.bet("alice", "hop 2 3", 10, w)
tb.point = 8
table := tb.rollTable(tb.players.get("alice"))
for _, want := range []string{"{casino_payout}4+4{/}", "{casino_result_win}+90{/}", "{casino_payout}2+3{/}", "{casino_result_win}+150{/}", "{casino_payout}easy 8{/}", "{casino_payout}any other{/}"} {
if !strings.Contains(table, want) {
t.Fatal("missing", want, table)
}
}
// Bets paying on the same total combine into one row.
tb2 := crapsFixture()
tb2.join("alice")
w2 := &testWallet{chips: 100}
tb2.bet("alice", "field", 10, w2)
tb2.bet("alice", "place 8", 12, w2)
tb2.point = 8
table = tb2.rollTable(tb2.players.get("alice"))
if strings.Count(table, "{casino_payout}8{/}") != 1 || !strings.Contains(table, "{casino_result_win}+4{/}") {
t.Fatal("same-label payouts must combine", table)
}
// Hard bets alone on the come-out pay nothing and say so.
tb3 := crapsFixture()
tb3.join("alice")
w3 := &testWallet{chips: 100}
tb3.bet("alice", "hard 8", 10, w3)
if text := tb3.rollTable(tb3.players.get("alice")); !strings.Contains(text, "OFF on the come-out") {
t.Fatal(text)
}
}
|