aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/plinko_test.go
blob: 6ddfee51bad6f1cb5030ffd62169173f901186f4 (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
package casino

import (
	"fmt"
	"math"
	"math/rand"
	"strings"
	"testing"
)

func TestPlinkoRoundedRTPAndVariance(t *testing.T) {
	for _, rtp := range []float64{31, 90, 98, 100} {
		lastVariance := -1.0
		for risk := 0; risk < 4; risk++ {
			p := plinkoPockets(risk, rtp)
			choose, mean, second := 1.0, 0.0, 0.0
			for k, v := range p {
				if k > 0 {
					choose *= float64(17-k) / float64(k)
				}
				mean += choose * v / 65536
				second += choose * v * v / 65536
				if v < 0 || v != p[16-k] {
					t.Fatalf("invalid symmetric pockets %v", p)
				}
				if k < 8 && v < p[k+1] {
					t.Fatalf("pockets must grow toward edges: %v", p)
				}
			}
			if math.Abs(mean-rtp/100) > 0.01 {
				t.Fatalf("risk %d RTP %.4f%% is too far from target %.2f%%", risk, mean*100, rtp)
			}
			if rtp == 98 {
				if mean < 0.97 || mean > 0.99 {
					t.Fatalf("risk %d default RTP %.4f%% outside 97–99%%", risk, mean*100)
				}
				t.Logf("%s RTP: %.6f%%", plinkoRisks[risk], mean*100)
			}
			if math.Abs(mean-plinkoReturn(risk, rtp)) > 1e-12 {
				t.Fatal("reported RTP differs from played pockets")
			}
			for _, v := range p {
				if math.Abs(v*10-math.Round(v*10)) > 1e-9 {
					t.Fatalf("not a tenth: %g", v)
				}
			}
			variance := second - mean*mean
			if variance <= lastVariance {
				t.Fatalf("variance did not rise: %g <= %g", variance, lastVariance)
			}
			lastVariance = variance
			if risk == 0 && p[0] != 16 || risk == 3 && p[0] != 10000 {
				t.Fatal("wrong jackpot")
			}
		}
	}
}
func TestPlinkoRTPNeverFallsWithRisk(t *testing.T) {
	// Include fractional builder-configured targets as well as the default.
	for target := 31.0; target <= 100; target += 0.25 {
		previous := 0.0
		for risk := range plinkoRisks {
			actual := plinkoReturn(risk, target)
			if actual+1e-12 < previous {
				t.Fatalf("target %.2f%%: %s RTP %.6f%% fell below %.6f%%", target, plinkoRisks[risk], actual*100, previous*100)
			}
			previous = actual
		}
	}
}

func newTestPlinko() *plinkoTable {
	return newTable(1, TableConfig{ID: "plinko", Game: GamePlinko}, rand.New(rand.NewSource(42))).game.(*plinkoTable)
}
func TestPlinkoDropLifecycle(t *testing.T) {
	p := newTestPlinko()
	p.join("alice")
	p.action("alice", "balls 10")
	p.action("alice", "difficulty expert")
	w := &testWallet{credits: 1000}
	p.bet("alice", "", 10, w)
	if len(w.open) != 1 || w.open[0].Total != 100 {
		t.Fatal("batch must wager once")
	}
	p.bet("alice", "", 10, w)
	p.action("alice", "balls 2")
	p.leave("alice")
	if !p.active || p.balls != 10 || len(w.open) != 1 {
		t.Fatal("active drop changed")
	}
	for row := 1; row <= 16; row++ {
		old := append([]int(nil), p.positions...)
		events := p.tick()
		s := events[0].Snapshot.(*PlinkoSnapshot)
		sum := 0
		for _, n := range s.Counts {
			sum += n
		}
		if s.Row != row || sum != 10 {
			t.Fatal("wrong animation")
		}
		for i, pos := range p.positions {
			if pos-old[i] < 0 || pos-old[i] > 1 {
				t.Fatal("not a left/right bounce")
			}
		}
	}
	if p.active || len(w.open) != 0 {
		t.Fatal("unsettled drop")
	}
	paid := w.paid
	p.tick()
	p.refundAll()
	if w.paid != paid {
		t.Fatal("duplicate payout")
	}
}
func TestPlinkoRefundAndDisconnect(t *testing.T) {
	for _, disconnect := range []bool{false, true} {
		p := newTestPlinko()
		p.join("alice")
		w := &testWallet{credits: 100}
		p.bet("alice", "", 10, w)
		p.tick()
		if disconnect {
			p.markDisconnected("alice")
		} else {
			p.refundAll()
		}
		if len(w.open) != 0 || !p.idle() {
			t.Fatal("stake or seat left open")
		}
		if !disconnect && w.credits+w.paid != 100 {
			t.Fatal("shutdown failed to refund")
		}
		if disconnect && w.credits != 90 {
			t.Fatal("disconnect must forfeit")
		}
		if len(p.tick()) != 0 {
			t.Fatal("discarded drop continued")
		}
	}
}
func TestPlinkoRoundedPocketsArePaidAndAnalyzed(t *testing.T) {
	p := newTestPlinko()
	p.join("alice")
	p.action("alice", "difficulty expert")
	w := &testWallet{credits: 100}
	p.bet("alice", "", 10, w)
	// Expert center pockets pay 0x; the displayed one-decimal table is played
	// exactly, without silently rounding a losing pocket upward.
	p.row, p.positions = 15, []int{7}
	events := p.tick()
	if w.paid != 0 || len(w.open) != 0 {
		t.Fatalf("rounded pocket not paid: %+v", w)
	}
	for _, e := range events {
		if strings.Contains(e.Message, "credits returned") {
			t.Fatal("duplicate payout prose")
		}
	}
	table := &Table{tableBase: p.tableBase, game: p}
	want := 1 - plinkoReturn(3, 98)
	if got := table.HouseEdge("alice", "main"); len(got.Parts) != 1 || math.Abs(got.Parts[0].Loss-want) > 1e-12 {
		t.Fatalf("seat edge ignores rounded payouts: %+v", got)
	}
	if got := p.Config.HouseEdge("main"); len(got.Parts) != 4 || math.Abs(got.Parts[3].Loss-want) > 1e-12 {
		t.Fatalf("config edge ignores risk: %+v", got)
	}
}

func TestPlinkoSharedPocketLabels(t *testing.T) {
	for risk := 0; risk < 4; risk++ {
		pockets := plinkoPockets(risk, 98)
		labels := PlinkoPocketLabels(pockets)
		for i := range pockets {
			for j := range pockets {
				if (pockets[i] == pockets[j]) != (labels[i] == labels[j]) {
					t.Fatalf("risk %d: payout/label mismatch at %d,%d", risk, i, j)
				}
			}
		}
		if labels[0] != 'A' || labels[16] != 'A' {
			t.Fatal("both edges must be A")
		}
	}
	p := newTestPlinko()
	p.join("alice")
	w := &testWallet{credits: 100}
	p.bet("alice", "", 1, w)
	p.row, p.positions = 15, []int{15}
	events := p.tick()
	pockets := plinkoPockets(0, 98)
	labels := PlinkoPocketLabels(pockets)
	want := fmt.Sprintf("[%c] %s", labels[p.positions[0]], plinkoMultiplierText(pockets[p.positions[0]]))
	found := false
	for _, e := range events {
		for _, line := range e.Settlement {
			found = found || line.Label == want
		}
	}
	if !found {
		t.Fatalf("settlement did not itemize mirrored label %s", want)
	}
}

func TestPlinkoReturnsItemizeEveryPocket(t *testing.T) {
	pockets := [17]float64{16, 7.8, 5.9, 4.4, 3.1, 2, 1.1, 0.5, 0.2, 0.5, 1.1, 2, 3.1, 4.4, 5.9, 7.8, 16}
	counts := [17]int{0: 1, 7: 2, 16: 1}
	total := 0.0
	for p, count := range counts {
		total += pockets[p] * float64(count) * 3
	}
	paid := int(math.Floor(total))
	returns := plinkoReturns(counts, 3, pockets, paid)
	sum := 0
	for p, count := range counts {
		if count == 0 {
			continue
		}
		if returns[p] < 0 {
			t.Fatalf("negative return for pocket %d", p)
		}
		sum += returns[p]
	}
	if sum != paid {
		t.Fatalf("itemized returns %d do not sum to paid %d", sum, paid)
	}
	if returns[16] <= returns[7] {
		t.Fatalf("higher pocket %d must out-return %d", returns[16], returns[7])
	}
	if whole := plinkoReturns([17]int{0: 2}, 5, pockets, 160); whole[0] != 160 {
		t.Fatalf("whole return changed: %d", whole[0])
	}
}

func TestPlinkoReceiptMergesSharedPayouts(t *testing.T) {
	p := newTestPlinko()
	p.join("alice")
	p.action("alice", "balls 4")
	w := &testWallet{credits: 100}
	p.bet("alice", "", 5, w)
	if len(w.open) != 1 || w.open[0].Total != 20 {
		t.Fatal("batch stake wrong")
	}
	// Mirrored pockets 1 and 15 share payout B; two balls in center pocket 8.
	// newTestPlinko seeds the RNG at 42 and the drop is its first consumer, so
	// undo the first four bounces to land the sealed balls on named pockets.
	bounces := rand.New(rand.NewSource(42))
	p.positions = []int{1 - bounces.Intn(2), 15 - bounces.Intn(2), 8 - bounces.Intn(2), 8 - bounces.Intn(2)}
	p.row = 15
	events := p.tick()
	if p.positions[0] != 1 || p.positions[1] != 15 {
		t.Fatalf("unexpected final pockets: %v", p.positions)
	}
	var lines []SettlementLine
	for _, e := range events {
		lines = append(lines, e.Settlement...)
	}
	if len(lines) != 2 {
		t.Fatalf("want one row per distinct payout, got %d: %+v", len(lines), lines)
	}
	staked, returned := 0, 0
	for _, line := range lines {
		staked += line.Stake
		returned += line.Returned
	}
	if staked != 20 || returned != 80 || w.paid != 80 {
		t.Fatalf("merged receipt mismatch: staked %d returned %d paid %d", staked, returned, w.paid)
	}
	if lines[0].Label != "[B] 7.8x" || lines[0].Returned != 78 || lines[1].Label != "[I] 0.2x" || lines[1].Returned != 2 {
		t.Fatalf("unexpected rows: %+v", lines)
	}
}

func TestPlinkoConfig(t *testing.T) {
	for _, rtp := range []float64{-1, 30, 101, math.NaN(), math.Inf(1)} {
		if (TableConfig{ID: "p", Game: GamePlinko, Plinko: PlinkoConfig{RTP: rtp}}).Validate() == nil {
			t.Fatalf("accepted RTP %g", rtp)
		}
	}
	c := TableConfig{ID: "p", Game: GamePlinko}
	if err := c.Validate(); err != nil {
		t.Fatal(err)
	}
	if c.Normalize().Plinko.RTP != 98 {
		t.Fatal("wrong default")
	}
	if (TableConfig{ID: "p", Game: GameRoulette, Plinko: PlinkoConfig{RTP: 98}}).Validate() == nil {
		t.Fatal("wrong rule block accepted")
	}
}
func TestPlinkoFailedPayoutRetainsStake(t *testing.T) {
	p := newTestPlinko()
	p.join("alice")
	w := &failingPayoutWallet{testWallet: testWallet{credits: 100}}
	p.bet("alice", "", 10, w)
	for i := 0; i < 16; i++ {
		p.tick()
	}
	if len(w.open) != 1 {
		t.Fatal("failed settlement consumed stake")
	}
	p.leave("alice")
	if len(w.open) != 1 {
		t.Fatal("leave consumed failed stake")
	}
}