bindbox-game/internal/api/admin/draw_simulate.go
邹方成 81e2fb5a75
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 41s
feat(activity): 实现抽奖随机承诺与验证功能
新增随机种子生成与验证逻辑,包括:
1. 添加随机承诺生成接口
2. 实现抽奖执行与验证流程
3. 新增批量用户创建与删除功能
4. 添加抽奖收据记录表
5. 完善配置管理与错误码

新增测试用例验证随机算法正确性
2025-11-15 20:39:13 +08:00

131 lines
4.5 KiB
Go

package admin
import (
"net/http"
"strconv"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
)
type simulateDrawRequest struct {
Samples int `json:"samples"`
}
type simulateItemStat struct {
RewardID int64 `json:"reward_id"`
Name string `json:"name"`
Weight int32 `json:"weight"`
Quantity int64 `json:"quantity"`
ExpectedRate float64 `json:"expected_rate"`
Count int64 `json:"count"`
ObservedRate float64 `json:"observed_rate"`
}
type simulateDrawResponse struct {
AlgoVersion string `json:"algo_version"`
IssueID int64 `json:"issue_id"`
ServerSeedHash string `json:"server_seed_hash"`
ItemsRoot string `json:"items_root"`
WeightsTotal int64 `json:"weights_total"`
Samples int `json:"samples"`
Stats []simulateItemStat `json:"stats"`
}
func (h *handler) SimulateIssueDraw() core.HandlerFunc {
return func(ctx core.Context) {
issueIDStr := ctx.Param("issue_id")
if issueIDStr == "" {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递期ID"))
return
}
if _, err := strconv.ParseInt(issueIDStr, 10, 64); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
issueID, _ := strconv.ParseInt(issueIDStr, 10, 64)
var req simulateDrawRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
if req.Samples <= 0 {
req.Samples = 1000
}
rewards, err := h.activity.ListIssueRewards(ctx.RequestContext(), issueID)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ExecuteDrawError, err.Error()))
return
}
cm, err := h.activity.GetIssueRandomCommit(ctx.RequestContext(), issueID)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetIssueRandomCommitError, err.Error()))
return
}
if cm == nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetIssueRandomCommitError, "未找到承诺"))
return
}
counts := make(map[int64]int64)
var eligibleTotal int64
for _, it := range rewards {
if it.Weight > 0 && (it.Quantity == -1 || it.Quantity > 0) {
eligibleTotal += int64(it.Weight)
}
}
for i := 0; i < req.Samples; i++ {
rec, err := h.activity.ExecuteDraw(ctx.RequestContext(), issueID)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ExecuteDrawError, err.Error()))
return
}
counts[rec.SelectedItemId]++
}
stats := make([]simulateItemStat, 0, len(rewards))
for _, it := range rewards {
var expected float64
if eligibleTotal > 0 && it.Weight > 0 && (it.Quantity == -1 || it.Quantity > 0) {
expected = float64(it.Weight) / float64(eligibleTotal)
}
c := counts[it.ID]
var observed float64
if req.Samples > 0 {
observed = float64(c) / float64(req.Samples)
}
stats = append(stats, simulateItemStat{
RewardID: it.ID,
Name: it.Name,
Weight: it.Weight,
Quantity: it.Quantity,
ExpectedRate: expected,
Count: c,
ObservedRate: observed,
})
}
ctx.Payload(&simulateDrawResponse{
AlgoVersion: cm.AlgoVersion,
IssueID: cm.IssueID,
ServerSeedHash: hexStr2(cm.ServerSeedHash[:]),
ItemsRoot: hexStr2(cm.ItemsRoot[:]),
WeightsTotal: cm.WeightsTotal,
Samples: req.Samples,
Stats: stats,
})
}
}
func hexStr2(b []byte) string {
const hextable = "0123456789abcdef"
dst := make([]byte, len(b)*2)
j := 0
for _, v := range b {
dst[j] = hextable[v>>4]
dst[j+1] = hextable[v&0x0f]
j += 2
}
return string(dst)
}