package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"sort"
)

func main() {
	// 数据库查到的正确seed (活动76)
	seedHex := "162B08DAC70849C68FDBF499583199B9500EE96CCCFC64511719B54FC7D9AFC9"
	seed, _ := hex.DecodeString(seedHex)

	issueID := int64(83)
	userID := int64(9054)
	saltHex := "c4e144b991600f5a5a316d55b6929d19"

	// payload格式
	payload := fmt.Sprintf("draw:issue:%d|user:%d|salt:%s", issueID, userID, saltHex)
	fmt.Printf("Seed: %s\n", seedHex)
	fmt.Printf("Payload: %s\n", payload)

	mac := hmac.New(sha256.New, seed)
	mac.Write([]byte(payload))
	sum := mac.Sum(nil)

	totalWeight := int64(100000)
	rnd := int64(binary.BigEndian.Uint64(sum[:8]) % uint64(totalWeight))

	fmt.Printf("HMAC: %x\n", sum)
	fmt.Printf("计算随机数: %d\n", rnd)
	fmt.Printf("数据库记录: 90555\n")

	if rnd == 90555 {
		fmt.Println("\n✅ 随机数匹配！")
	} else {
		fmt.Println("\n❌ 随机数不匹配")
	}

	// 用计算的随机数选择奖品
	// 用计算的随机数选择奖品
	weights := []struct {
		ID     int64
		Weight int64
		Name   string
	}{
		{280, 88200, "捏捏"},
		{281, 100, "魔灵高达"},
		{282, 6500, "高达徽章"},
		{283, 3200, "打磨工具五件套"},
		{284, 800, "SD随机款"},
		{285, 800, "EG创制强袭高达"},
		{286, 100, "V2高达AB型"},
		{429, 100, "EG创制强袭超银河"},
		{430, 100, "战国异端顽驮无"},
		{431, 100, "牛高达"},
	}

	// 按ID排序（模拟后端逻辑）
	sort.Slice(weights, func(i, j int) bool {
		return weights[i].ID < weights[j].ID
	})

	var acc int64
	for _, w := range weights {
		acc += w.Weight
		if rnd < acc {
			fmt.Printf("\n计算中奖: ID %d (%s)\n", w.ID, w.Name)
			break
		}
	}
	fmt.Println("数据库实际: ID 282 (高达徽章)")
}
