bindbox-game/internal/api/admin/draw_simulate.go
邹方成 6ee627139c
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 40s
feat: 新增支付测试小程序与微信支付集成
feat(pay): 添加支付API基础结构
feat(miniapp): 创建支付测试小程序页面与配置
feat(wechatpay): 配置微信支付参数与证书
fix(guild): 修复成员列表查询条件
docs: 更新代码规范文档与需求文档
style: 统一前后端枚举显示与注释格式
refactor(admin): 重构用户奖励发放接口参数处理
test(title): 添加称号效果参数验证测试
2025-11-17 00:42:08 +08:00

145 lines
4.4 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"`
}
// SimulateIssueDraw 模拟抽奖分布
// @Summary 模拟抽奖分布
// @Description 按给定样本数量模拟指定期的抽奖结果分布,用于验算权重与承诺
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Param RequestBody body simulateDrawRequest true "请求参数"
// @Success 200 {object} simulateDrawResponse
// @Failure 400 {object} code.Failure
// @Security LoginVerifyToken
// @Router /api/admin/activities/{activity_id}/issues/{issue_id}/simulate_draw [post]
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)
req := new(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)
}