Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 41s
新增随机种子生成与验证逻辑,包括: 1. 添加随机承诺生成接口 2. 实现抽奖执行与验证流程 3. 新增批量用户创建与删除功能 4. 添加抽奖收据记录表 5. 完善配置管理与错误码 新增测试用例验证随机算法正确性
124 lines
4.5 KiB
Go
124 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 issueCommitResponse 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"`
|
|
StateVersion int32 `json:"state_version"`
|
|
}
|
|
|
|
func (h *handler) CommitIssueRandom() 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)
|
|
cm, err := h.activity.CommitIssueRandom(ctx.RequestContext(), issueID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CommitIssueRandomError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(&issueCommitResponse{
|
|
AlgoVersion: cm.AlgoVersion,
|
|
IssueID: cm.IssueID,
|
|
ServerSeedHash: hexStr(cm.ServerSeedHash[:]),
|
|
ItemsRoot: hexStr(cm.ItemsRoot[:]),
|
|
WeightsTotal: cm.WeightsTotal,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetIssueRandomCommit() 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)
|
|
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
|
|
}
|
|
ctx.Payload(&issueCommitResponse{
|
|
AlgoVersion: cm.AlgoVersion,
|
|
IssueID: cm.IssueID,
|
|
ServerSeedHash: hexStr(cm.ServerSeedHash[:]),
|
|
ItemsRoot: hexStr(cm.ItemsRoot[:]),
|
|
WeightsTotal: cm.WeightsTotal,
|
|
StateVersion: cm.StateVersion,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetIssueRandomCommitHistory() 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)
|
|
history, err := h.activity.GetIssueRandomCommitHistory(ctx.RequestContext(), issueID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetIssueRandomCommitError, err.Error()))
|
|
return
|
|
}
|
|
|
|
// 转换响应格式
|
|
response := make([]*issueCommitResponse, 0, len(history))
|
|
for _, commit := range history {
|
|
response = append(response, &issueCommitResponse{
|
|
AlgoVersion: commit.AlgoVersion,
|
|
IssueID: commit.IssueID,
|
|
ServerSeedHash: hexStr(commit.ServerSeedHash[:]),
|
|
ItemsRoot: hexStr(commit.ItemsRoot[:]),
|
|
WeightsTotal: commit.WeightsTotal,
|
|
StateVersion: commit.StateVersion,
|
|
})
|
|
}
|
|
ctx.Payload(response)
|
|
}
|
|
}
|
|
|
|
func hexStr(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)
|
|
} |