Files
bl/modules/config/service/shiny.go
xinian f6aa0c3339
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
feat: 重构任务奖励系统并增加宠物技能和皮肤奖励
将任务奖励逻辑重构到单独的文件中,增加对宠物技能和皮肤奖励的支持,优化任务完成处理流程
2026-04-11 19:25:59 +08:00

142 lines
2.6 KiB
Go

package service
import (
"blazing/common/data"
"blazing/cool"
"blazing/modules/config/model"
"context"
"encoding/json"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/grand"
)
type ShinyService struct {
*cool.Service
}
func NewShinyService() *ShinyService {
return &ShinyService{
&cool.Service{
Model: model.NewColorfulSkin(),
InsertParam: func(ctx context.Context) g.MapStrAny {
admin := cool.GetAdmin(ctx)
userId := admin.UserId
return g.MapStrAny{
"author": userId,
}
},
},
}
}
func (s *ShinyService) ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) {
var t data.GlowFilter
if method == "Delete" {
return nil
}
r := json.Unmarshal([]byte(gconv.String(param["color"])), &t)
if r != nil {
return r
}
return nil
}
func (s *ShinyService) listByPetID(id uint32) []model.ColorfulSkin {
var ret []model.ColorfulSkin
dbm_enable(s.Model).
Wheref(`bind_elf_ids @> ?::jsonb`, id).
Wheref(`jsonb_typeof(bind_elf_ids) = ?`, "array").
Scan(&ret)
return ret
}
func pickColorfulSkinByWeight(items []model.ColorfulSkin) *model.ColorfulSkin {
if len(items) == 0 {
return nil
}
totalWeight := 0
for _, item := range items {
if item.ElfProbability > 0 {
totalWeight += int(item.ElfProbability)
}
}
if totalWeight <= 0 {
return &items[grand.Intn(len(items))]
}
randomValue := grand.Intn(totalWeight)
for i := range items {
weight := int(items[i].ElfProbability)
if weight <= 0 {
continue
}
if randomValue < weight {
return &items[i]
}
randomValue -= weight
}
return &items[0]
}
func (s *ShinyService) RandShiny(id uint32) *data.GlowFilter {
ret := s.listByPetID(id)
for _, v := range ret {
id := v.ID
if grand.Meet(int(v.ElfProbability), 1000) {
if cool.Config.ServerInfo.IsVip == 0 {
m := cool.DBM(s.Model).Where("id", id)
m.Increment("usage_count", 1)
}
return &v.Color
}
}
if len(ret) == 0 {
return nil
}
var t = data.GetDef()
t.ColorMatrixFilter = model.GenerateRandomParentMatrix().Get()
return &t
}
func (s *ShinyService) RandomByWeightShiny(id uint32) *data.GlowFilter {
r := pickColorfulSkinByWeight(s.listByPetID(id))
if r == nil {
return nil
}
if cool.Config.ServerInfo.IsVip == 0 {
m := cool.DBM(s.Model).Where("id", r.ID)
m.Increment("refresh_count", 1)
}
return &r.Color
}
func (s *ShinyService) GetShiny(id int) *data.GlowFilter {
var ret []model.ColorfulSkin
// 执行 Raw SQL 并扫描返回值
dbm_nocache_noenable(s.Model).
Where("id", id).Scan(&ret)
if len(ret) == 0 {
return nil
}
v := ret[grand.Intn(len(ret))]
return &v.Color
}