Files
bl/logic/service/fight/info/ctx.go

51 lines
1.3 KiB
Go

package info
import "math/rand"
// PlayerCaptureContext 用户捕捉上下文(每次登录创建)
type PlayerCaptureContext struct {
Random *rand.Rand
Denominator int // 统一分母=1000
DecayFactor float64 // 衰减系数
MinDecayNum int // 衰减分子最小值
Guarantees map[int]int // 按分母分组的保底分子 map[denominator]numerator
}
// Roll 通用概率判定(带共享保底)
func (c *PlayerCaptureContext) Roll(numerator, denominator int) (bool, float64, float64) {
if denominator <= 0 {
return false, 0, 0
}
base := float64(numerator) / float64(denominator)
bonus := float64(c.getGuaranteeNumerator(denominator)) / float64(denominator)
total := base + bonus
if total > 1.0 {
total = 1.0
}
success := c.Random.Float64() < total
if success {
c.resetGuarantee(denominator)
} else {
c.increaseGuarantee(denominator)
}
return success, base * 100, bonus * 100
}
// 保底操作
func (c *PlayerCaptureContext) getGuaranteeNumerator(denominator int) int {
if num, ok := c.Guarantees[denominator]; ok {
return num
}
return 0
}
func (c *PlayerCaptureContext) increaseGuarantee(denominator int) {
c.Guarantees[denominator]++
}
func (c *PlayerCaptureContext) resetGuarantee(denominator int) {
c.Guarantees[denominator] = 0
}