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
|
package casino
import (
"errors"
"fmt"
"math"
)
// Currency limits and checked arithmetic. All balances and payouts flow
// through these helpers so overflow and unbounded multipliers fail loudly
// instead of silently wrapping or producing absurd balances.
const (
// MaxCredits is the largest credit balance the economy supports.
// Configured bets are additionally bounded by each game's MaxBet.
MaxCredits = 1_000_000_000_000 // 1e12
// DefaultMaxBet bounds wagers when a configuration does not set one.
// The explicit unlimited sentinel (MaxBet = MaxInt) remains accepted
// for authored configurations that deliberately want no cap.
DefaultMaxBet = 1_000_000
)
// maxBetUnlimited is the MaxBet sentinel meaning "no wager cap".
const maxBetUnlimited = int(^uint(0) >> 1)
// Wallet errors. ErrInsufficientFunds is normal gameplay (the table reports
// it like a rejected bet); ErrWalletPersistence means the wager or payout
// could not be made durable and MUST NOT be presented as successful play.
var (
ErrInsufficientFunds = errors.New("insufficient funds")
ErrWalletPersistence = errors.New("wallet persistence failure")
)
// MulPayout scales an amount by a payout multiplier and converts to whole
// credits (floor, like a house paying in chips). Overflow past MaxCredits
// is an error, not a silent wrap. A non-finite or negative multiplier is
// rejected.
func MulPayout(amount int, multiplier float64) (int, error) {
if multiplier < 0 || math.IsInf(multiplier, 0) || math.IsNaN(multiplier) {
return 0, fmt.Errorf("invalid payout multiplier %v", multiplier)
}
if amount <= 0 {
return 0, nil
}
// Bound the product before conversion: amount * multiplier must fit
// within MaxCredits (multiplier > 1 may grow the stake).
if float64(amount) > float64(MaxCredits)/multiplier {
return MaxCredits, fmt.Errorf("payout overflow: %d * %v exceeds %d", amount, multiplier, MaxCredits)
}
payout := int(math.Floor(float64(amount) * multiplier))
if payout > MaxCredits {
return MaxCredits, fmt.Errorf("payout overflow: %d * %v exceeds %d", amount, multiplier, MaxCredits)
}
return payout, nil
}
// walletErrorMessage renders a wallet error for a player: insufficient
// funds is normal gameplay; a persistence failure makes clear that the
// house could not process the wager rather than implying a short balance.
// context, when non-empty, is the game-specific insufficient-funds wording.
func walletErrorMessage(err error, context ...string) string {
if err == ErrWalletPersistence {
return "The house could not process that transaction (persistence failure). Nothing was charged; try again shortly."
}
if len(context) > 0 && context[0] != "" {
return context[0]
}
return "You don't have enough chips and credits for that bet."
}
// payoutFailedMessage explains a settlement the wallet could not persist:
// the journal record for the payout stays open, so startup recovery will
// re-apply the owed amount. Games must NOT announce such a payout as paid.
func payoutFailedMessage() string {
return "The house could not process your payout (persistence failure). Your settlement is recorded and will be restored when you return."
}
|