2025-12-26 12:22:32 +08:00

109 lines
3.3 KiB
Go

package strategy
import (
"bindbox-game/internal/repository/mysql/dao"
"context"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
)
type defaultStrategy struct {
read *dao.Query
write *dao.Query
}
func NewDefault(read *dao.Query, write *dao.Query) ActivityDrawStrategy {
return &defaultStrategy{read: read, write: write}
}
func (s *defaultStrategy) PreChecks(ctx context.Context, activityID int64, issueID int64, userID int64) error {
issue, err := s.read.ActivityIssues.WithContext(ctx).Where(s.read.ActivityIssues.ID.Eq(issueID), s.read.ActivityIssues.ActivityID.Eq(activityID)).First()
if err != nil || issue == nil {
return errors.New("issue not found")
}
return nil
}
func (s *defaultStrategy) SelectItem(ctx context.Context, activityID int64, issueID int64, userID int64) (int64, map[string]any, error) {
rewards, err := s.read.ActivityRewardSettings.WithContext(ctx).ReadDB().Where(s.read.ActivityRewardSettings.IssueID.Eq(issueID)).Find()
if err != nil || len(rewards) == 0 {
return 0, nil, errors.New("no rewards")
}
var total int64
for _, r := range rewards {
if r.Quantity != 0 {
total += int64(r.Weight)
}
}
if total <= 0 {
return 0, nil, errors.New("no weight")
}
// Determine seed key: use Activity Commitment (REQUIRED)
act, _ := s.read.Activities.WithContext(ctx).Where(s.read.Activities.ID.Eq(activityID)).First()
if act == nil || len(act.CommitmentSeedMaster) == 0 {
return 0, nil, errors.New("活动尚未生成承诺,无法抽奖")
}
seedKey := act.CommitmentSeedMaster
// To ensure uniqueness per draw when using a fixed CommitmentSeedMaster, mix in a random salt
salt := make([]byte, 16)
if _, err := rand.Read(salt); err != nil {
return 0, nil, errors.New("crypto rand salt failed")
}
// Use HMAC-SHA256 to generate random number derived from seed + context + salt
mac := hmac.New(sha256.New, seedKey)
mac.Write([]byte(fmt.Sprintf("draw:issue:%d|user:%d|salt:%x", issueID, userID, salt)))
sum := mac.Sum(nil)
rnd := int64(binary.BigEndian.Uint64(sum[:8]) % uint64(total))
var acc int64
var picked int64
for _, r := range rewards {
if r.Quantity == 0 {
continue
}
acc += int64(r.Weight)
if rnd < acc {
picked = r.ID
break
}
}
if picked == 0 {
return 0, nil, errors.New("pick failed")
}
proof := map[string]any{
"weights_total": total,
"rand": rnd,
"seed_hash": fmt.Sprintf("%x", sha256.Sum256(seedKey)),
"salt": fmt.Sprintf("%x", salt),
"seed_type": "commitment",
}
return picked, proof, nil
}
func (s *defaultStrategy) GrantReward(ctx context.Context, userID int64, rewardID int64) error {
// 【使用乐观锁扣减库存】直接用 Quantity > 0 作为更新条件,避免并发超卖
result, err := s.write.ActivityRewardSettings.WithContext(ctx).Where(
s.write.ActivityRewardSettings.ID.Eq(rewardID),
s.write.ActivityRewardSettings.Quantity.Gt(0), // 乐观锁:只有库存>0才能扣减
).UpdateSimple(s.write.ActivityRewardSettings.Quantity.Add(-1))
if err != nil {
return err
}
if result.RowsAffected == 0 {
return errors.New("sold out or reward not found")
}
return nil
}
func (s *defaultStrategy) PostEffects(ctx context.Context, userID int64, activityID int64, issueID int64, rewardID int64) error {
return nil
}