aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/slots.go
blob: 9d53bb15333d1f71644b4ec63f1680a13bd829c4 (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
package casino

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

type SlotSymbol struct {
	ID     string          `yaml:"id"`
	Weight int             `yaml:"weight"`
	Payout map[int]float64 `yaml:"payout"`
}

type SlotProfile struct {
	Symbols         []SlotSymbol `yaml:"symbols"`
	Rows            int          `yaml:"rows"`
	Reels           int          `yaml:"reels"`
	FreeSpinTrigger string       `yaml:"free_spin_trigger"`
	FreeSpinCount   int          `yaml:"free_spin_count"`
	Ways            int          `yaml:"ways"`
	TargetRTP       float64      `yaml:"payout"`
}

type SlotSpin struct {
	Grid         [3][5]string
	Payout       float64
	ScatterCount int
	Wins         []SlotWin
}

type SlotWin struct {
	Symbol     string
	Length     int
	Ways       int
	Multiplier float64
	Payout     float64
}

// slotResult preserves the small rules-engine hook used by the unfinished
// generic table implementation while slots move onto SlotMachine.
type slotSpin struct {
	Display string
	Payout  int
}

func slotResult(rng *rand.Rand, bet int) slotSpin {
	spin := generateSlotSpin(rng, bet)
	return slotSpin{Display: spin.render(5), Payout: int(math.Floor(spin.Payout + 0.5))}
}

func DefaultHuffAndPuffProfile() SlotProfile {
	return SlotProfile{
		Rows: 3, Reels: 5, Ways: 243,
		FreeSpinTrigger: "scatter", FreeSpinCount: 8,
		Symbols: []SlotSymbol{
			{ID: "straw", Weight: 22, Payout: map[int]float64{3: .5, 4: 1, 5: 2}},
			{ID: "stick", Weight: 12, Payout: map[int]float64{3: 1, 4: 2, 5: 5}},
			{ID: "brick", Weight: 7, Payout: map[int]float64{3: 2, 4: 5, 5: 15}},
			{ID: "hat", Weight: 4, Payout: map[int]float64{3: 5, 4: 15, 5: 50}},
			{ID: "wolf", Weight: 2, Payout: map[int]float64{3: 10, 4: 50, 5: 250}},
			{ID: "scatter", Weight: 3},
		},
	}
}

func (p SlotProfile) Normalize() SlotProfile {
	if p.Rows <= 0 {
		p.Rows = 3
	}
	if p.Reels <= 0 {
		p.Reels = 5
	}
	if p.Ways <= 0 {
		p.Ways = 243
	}
	if p.FreeSpinCount <= 0 {
		p.FreeSpinCount = 8
	}
	if len(p.Symbols) == 0 {
		return DefaultHuffAndPuffProfile()
	}
	return p
}

func (p SlotProfile) symbol(id string) *SlotSymbol {
	for i := range p.Symbols {
		if p.Symbols[i].ID == id {
			return &p.Symbols[i]
		}
	}
	return nil
}

func generateSlotSpin(rng *rand.Rand, bet int) *SlotSpin {
	return generateSlotSpinWithProfile(rng, bet, DefaultHuffAndPuffProfile())
}

func generateSlotSpinWithProfile(rng *rand.Rand, bet int, profile SlotProfile) *SlotSpin {
	p := profile.Normalize()
	spin := &SlotSpin{}
	for reel := 0; reel < p.Reels && reel < 5; reel++ {
		for row := 0; row < p.Rows && row < 3; row++ {
			symbol := weightedSymbol(rng, p.Symbols)
			spin.Grid[row][reel] = symbol
			if symbol == p.FreeSpinTrigger {
				spin.ScatterCount++
			}
		}
	}
	spin.Payout, spin.Wins = evaluateSlot(spin.Grid, bet, p)
	return spin
}

func weightedSymbol(rng *rand.Rand, symbols []SlotSymbol) string {
	total := 0
	for _, symbol := range symbols {
		if symbol.Weight > 0 {
			total += symbol.Weight
		}
	}
	if total <= 0 {
		return ""
	}
	n := rng.Intn(total)
	for _, symbol := range symbols {
		if symbol.Weight <= 0 {
			continue
		}
		if n < symbol.Weight {
			return symbol.ID
		}
		n -= symbol.Weight
	}
	return symbols[len(symbols)-1].ID
}

func evaluateSlot(grid [3][5]string, bet int, profile SlotProfile) (float64, []SlotWin) {
	p := profile.Normalize()
	if bet <= 0 {
		return 0, nil
	}
	total := 0.0
	var wins []SlotWin
	for _, symbol := range p.Symbols {
		length, ways := matchingLength(grid, symbol.ID)
		if length >= 3 && symbol.Payout[length] > 0 {
			payout := float64(bet) * symbol.Payout[length] * float64(ways) / 243.0
			total += payout
			wins = append(wins, SlotWin{Symbol: symbol.ID, Length: length, Ways: ways, Multiplier: symbol.Payout[length], Payout: payout})
		}
	}
	return total, wins
}

func matchingLength(grid [3][5]string, symbol string) (int, int) {
	ways := 1
	length := 0
	for reel := 0; reel < 5; reel++ {
		count := 0
		for row := 0; row < 3; row++ {
			if grid[row][reel] == symbol {
				count++
			}
		}
		if count == 0 {
			break
		}
		ways *= count
		length = reel + 1
	}
	if length < 3 {
		return length, 0
	}
	return length, ways
}

func (s *SlotSpin) render(stoppedReels int) string {
	var out string
	out += "+---------+---------+---------+---------+---------+\n"
	for row := 0; row < 3; row++ {
		out += "|"
		for reel := 0; reel < 5; reel++ {
			value := "..."
			if reel < stoppedReels {
				value = s.Grid[row][reel]
			}
			out += fmt.Sprintf(" %-7s |", value)
		}
		out += "\n"
	}
	out += "+---------+---------+---------+---------+---------+"
	return out
}

// CalculateRTP returns the exact mathematical RTP for the independent-cell
// model used by the baseline machine, before integer-credit rounding.
func CalculateRTP(profile SlotProfile) float64 {
	p := profile.Normalize()
	totalWeight := 0
	for _, symbol := range p.Symbols {
		if symbol.Weight > 0 {
			totalWeight += symbol.Weight
		}
	}
	if totalWeight == 0 {
		return 0
	}
	baseRTP := 0.0
	scatterProbability := 0.0
	for _, symbol := range p.Symbols {
		probability := float64(symbol.Weight) / float64(totalWeight)
		if symbol.ID == p.FreeSpinTrigger {
			scatterProbability = probability
		}
		if symbol.ID == p.FreeSpinTrigger {
			continue
		}
		for length := 3; length <= 5; length++ {
			ways := math.Pow(float64(p.Rows), float64(length))
			nextReelNoMatch := 1.0
			if length < 5 {
				nextReelNoMatch = math.Pow(1-probability, float64(p.Rows))
			}
			baseRTP += symbol.Payout[length] * ways * math.Pow(probability, float64(length)) * nextReelNoMatch / float64(p.Ways)
		}
	}
	// The baseline feature awards a fixed number of automatic spins and does
	// not retrigger during those spins. Its expected value is therefore the
	// trigger probability multiplied by the free-spin count and base RTP.
	triggerProbability := 0.0
	for scatters := 3; scatters <= p.Rows*p.Reels; scatters++ {
		triggerProbability += binomial(p.Rows*p.Reels, scatters) * math.Pow(scatterProbability, float64(scatters)) * math.Pow(1-scatterProbability, float64(p.Rows*p.Reels-scatters))
	}
	return baseRTP * (1 + triggerProbability*float64(p.FreeSpinCount))
}

func binomial(n, k int) float64 {
	if k < 0 || k > n {
		return 0
	}
	result := 1.0
	for i := 1; i <= k; i++ {
		result *= float64(n-k+i) / float64(i)
	}
	return result
}

func ValidateRTP(profile SlotProfile, tolerance float64) error {
	if profile.TargetRTP <= 0 {
		return nil
	}
	actual := CalculateRTP(profile)
	if math.Abs(actual-profile.TargetRTP) > tolerance {
		return fmt.Errorf("slot profile RTP %.6f does not match target %.6f", actual, profile.TargetRTP)
	}
	return nil
}

// ScaleToRTP adjusts the configured paytable, without changing reel weights or
// feature probabilities, so the profile's exact mathematical RTP reaches the
// requested target. Runtime payouts retain fractional credits internally until
// they can be paid as whole credits.
func ScaleToRTP(profile SlotProfile, target float64) SlotProfile {
	profile = profile.Normalize()
	actual := CalculateRTP(profile)
	if actual <= 0 || target <= 0 {
		return profile
	}
	scale := target / actual
	for i := range profile.Symbols {
		for length, payout := range profile.Symbols[i].Payout {
			profile.Symbols[i].Payout[length] = payout * scale
		}
	}
	profile.TargetRTP = target
	return profile
}