274 lines
7.8 KiB
Go
274 lines
7.8 KiB
Go
package smashegg
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/model"
|
|
)
|
|
|
|
const legacyGoldImage = "assets/common/yumi_coin.png"
|
|
|
|
// LegacyPrizeList returns the shape expected by the copied H5 bundle.
|
|
func (s *Service) LegacyPrizeList(ctx context.Context, user AuthUser) (map[string]any, error) {
|
|
config, err := s.GetAppConfig(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var balance int64
|
|
if s.java != nil && user.UserID > 0 {
|
|
if balanceMap, balanceErr := s.java.MapGoldBalance(ctx, []int64{user.UserID}); balanceErr == nil {
|
|
balance = balanceMap[user.UserID]
|
|
}
|
|
}
|
|
|
|
drawOptions := config.DrawOptions
|
|
if len(drawOptions) == 0 {
|
|
drawOptions = defaultDrawOptionPayloads(0)
|
|
}
|
|
mapPrice := int64(0)
|
|
if len(drawOptions) > 0 && drawOptions[0].Times > 0 {
|
|
mapPrice = drawOptions[0].PriceGold / int64(drawOptions[0].Times)
|
|
}
|
|
return map[string]any{
|
|
"activity": map[string]any{"enabled": config.Enabled, "rtp": config.RTPPercent},
|
|
"coins": balance,
|
|
"drawOptions": legacyDrawOptions(drawOptions),
|
|
"mapPrice": mapPrice,
|
|
"normalList": legacyPrizeList(config.Rewards),
|
|
"notices": []any{},
|
|
"owner": map[string]any{},
|
|
"rankData": map[string]any{},
|
|
"superList": []any{},
|
|
"topOne": map[string]any{},
|
|
}, nil
|
|
}
|
|
|
|
// LegacyStartHunt executes draw and returns the shape expected by the H5 bundle.
|
|
func (s *Service) LegacyStartHunt(ctx context.Context, user AuthUser, times int) (map[string]any, error) {
|
|
resp, err := s.Draw(ctx, user, DrawRequest{Times: times})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{
|
|
"balance": resp.BalanceAfter,
|
|
"list": legacyRewardList(resp.Rewards),
|
|
}, nil
|
|
}
|
|
|
|
// LegacyMyHuntRecords returns the user's draw records in the H5 bundle shape.
|
|
func (s *Service) LegacyMyHuntRecords(ctx context.Context, user AuthUser, start int, limit int) (map[string]any, error) {
|
|
if start < 0 {
|
|
start = 0
|
|
}
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
if limit > 100 {
|
|
limit = 100
|
|
}
|
|
var rows []model.SmashEggDrawRecord
|
|
if err := s.db.WithContext(ctx).
|
|
Where("sys_origin = ? AND user_id = ?", normalizeSysOrigin(user.SysOrigin), user.UserID).
|
|
Order("create_time desc, id desc").
|
|
Offset(start).
|
|
Limit(limit).
|
|
Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
grouped := make([]map[string]any, 0)
|
|
index := map[string]int{}
|
|
for _, row := range rows {
|
|
if _, exists := index[row.DrawNo]; !exists {
|
|
index[row.DrawNo] = len(grouped)
|
|
grouped = append(grouped, map[string]any{
|
|
"huntTime": strconv.FormatInt(row.CreateTime.Unix(), 10),
|
|
"prizeList": []map[string]any{},
|
|
})
|
|
}
|
|
itemIndex := index[row.DrawNo]
|
|
list := grouped[itemIndex]["prizeList"].([]map[string]any)
|
|
grouped[itemIndex]["prizeList"] = append(list, legacyRecordPrize(row))
|
|
}
|
|
return map[string]any{
|
|
"finished": len(rows) < limit,
|
|
"list": grouped,
|
|
}, nil
|
|
}
|
|
|
|
// LegacyHuntNotices returns recent notices in the H5 bundle shape.
|
|
func (s *Service) LegacyHuntNotices(ctx context.Context, sysOrigin string) (map[string]any, error) {
|
|
notices, err := s.ListRecentNotices(ctx, sysOrigin, 20)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]map[string]any, 0, len(notices))
|
|
for _, item := range notices {
|
|
list = append(list, map[string]any{
|
|
"nick": "User " + strconv.FormatInt(item.UserID, 10),
|
|
"prizeList": []map[string]any{{
|
|
"image": legacyRewardImage(item.RewardType, item.CoverURL),
|
|
"num": legacyRewardNum(item.GoldAmount),
|
|
}},
|
|
})
|
|
}
|
|
return map[string]any{"list": list}, nil
|
|
}
|
|
|
|
// LegacyDayRank returns day rank in the H5 bundle shape.
|
|
func (s *Service) LegacyDayRank(ctx context.Context, sysOrigin string) (map[string]any, error) {
|
|
rank, err := s.PageDayRank(ctx, sysOrigin, 1, 100)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]map[string]any, 0, len(rank))
|
|
for _, item := range rank {
|
|
list = append(list, map[string]any{
|
|
"avatar": "",
|
|
"gotPrices": item.Score,
|
|
"nick": "User " + strconv.FormatInt(item.UserID, 10),
|
|
"uid": item.UserID,
|
|
})
|
|
}
|
|
return map[string]any{"list": list}, nil
|
|
}
|
|
|
|
func legacyDrawOptions(options []DrawOptionPayload) []map[string]any {
|
|
result := make([]map[string]any, 0, len(options))
|
|
for _, option := range options {
|
|
if !option.Enabled {
|
|
continue
|
|
}
|
|
result = append(result, map[string]any{
|
|
"key": option.OptionKey,
|
|
"label": option.Label,
|
|
"price": option.PriceGold,
|
|
"times": option.Times,
|
|
})
|
|
}
|
|
return result
|
|
}
|
|
|
|
func legacyPrizeList(rewards []RewardConfigPayload) []map[string]any {
|
|
result := make([]map[string]any, 0, len(rewards))
|
|
for _, reward := range rewards {
|
|
if !reward.Enabled {
|
|
continue
|
|
}
|
|
result = append(result, map[string]any{
|
|
"id": firstNonZeroInt64(reward.ResourceID.Int64(), int64PtrValue(reward.RewardGroupID), reward.GoldAmount),
|
|
"image": legacyRewardImage(reward.RewardType, reward.CoverURL),
|
|
"name": legacyRewardName(reward.RewardGroupName, reward.RewardType),
|
|
"num": legacyRewardNum(reward.GoldAmount),
|
|
"price": legacyDisplayPrice(reward.RewardType, reward.GoldAmount, reward.DisplayGoldAmount, reward.RewardValueGold),
|
|
"type": legacyRewardType(reward.RewardType),
|
|
"animationUrl": reward.AnimationURL,
|
|
})
|
|
}
|
|
return result
|
|
}
|
|
|
|
func legacyRewardList(rewards []DrawRewardPayload) []map[string]any {
|
|
result := make([]map[string]any, 0, len(rewards))
|
|
for _, reward := range rewards {
|
|
result = append(result, map[string]any{
|
|
"id": firstNonZeroInt64(int64PtrValue(reward.RewardGroupID), reward.GoldAmount),
|
|
"image": legacyRewardImage(reward.RewardType, reward.CoverURL),
|
|
"name": legacyRewardName(reward.RewardGroupName, reward.RewardType),
|
|
"num": legacyDrawRewardNum(reward),
|
|
"price": legacyDisplayPrice(reward.RewardType, reward.GoldAmount, reward.DisplayGoldAmount, reward.RewardValueGold),
|
|
"type": legacyRewardType(reward.RewardType),
|
|
"animationUrl": reward.AnimationURL,
|
|
})
|
|
}
|
|
return result
|
|
}
|
|
|
|
func legacyRecordPrize(row model.SmashEggDrawRecord) map[string]any {
|
|
return map[string]any{
|
|
"id": firstNonZeroInt64(int64PtrValue(row.RewardGroupID), row.GoldAmount),
|
|
"image": legacyRewardImage(row.RewardType, row.CoverURL),
|
|
"name": legacyRewardName(row.RewardGroupName, row.RewardType),
|
|
"num": legacyRewardNum(row.GoldAmount),
|
|
"price": legacyDisplayPrice(row.RewardType, row.GoldAmount, row.DisplayGoldAmount, row.RewardValueGold),
|
|
"type": legacyRewardType(row.RewardType),
|
|
"animationUrl": row.AnimationURL,
|
|
}
|
|
}
|
|
|
|
func legacyRewardImage(rewardType string, coverURL string) string {
|
|
if rewardType == rewardTypeGold {
|
|
return legacyGoldImage
|
|
}
|
|
if coverURL != "" {
|
|
return coverURL
|
|
}
|
|
return legacyGoldImage
|
|
}
|
|
|
|
func legacyRewardName(name string, rewardType string) string {
|
|
if name != "" {
|
|
return name
|
|
}
|
|
if rewardType == rewardTypeGold {
|
|
return "Gold"
|
|
}
|
|
return "Reward"
|
|
}
|
|
|
|
func legacyRewardNum(goldAmount int64) int64 {
|
|
if goldAmount > 0 {
|
|
return goldAmount
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func legacyDrawRewardNum(reward DrawRewardPayload) int64 {
|
|
if reward.RewardType == rewardTypeGold {
|
|
return legacyRewardNum(reward.GoldAmount)
|
|
}
|
|
return int64(maxInt(1, reward.Count))
|
|
}
|
|
|
|
func legacyRewardType(rewardType string) int {
|
|
if rewardType == rewardTypeGold {
|
|
return 1
|
|
}
|
|
return 2
|
|
}
|
|
|
|
func legacyDisplayPrice(rewardType string, goldAmount int64, displayGoldAmount int64, rewardValueGold int64) int64 {
|
|
if rewardType == rewardTypeGold && goldAmount > 0 {
|
|
return goldAmount
|
|
}
|
|
if displayGoldAmount > 0 {
|
|
return displayGoldAmount
|
|
}
|
|
return rewardValueGold
|
|
}
|
|
|
|
func firstNonZeroInt64(values ...int64) int64 {
|
|
for _, value := range values {
|
|
if value > 0 {
|
|
return value
|
|
}
|
|
}
|
|
return time.Now().UnixNano()
|
|
}
|
|
|
|
func maxInt(minimum int, value int) int {
|
|
if value < minimum {
|
|
return minimum
|
|
}
|
|
return value
|
|
}
|
|
|
|
func maxInt64(minimum int64, value int64) int64 {
|
|
if value < minimum {
|
|
return minimum
|
|
}
|
|
return value
|
|
}
|