Files
bl/modules/config/service/shiny.go

142 lines
2.7 KiB
Go
Raw Normal View History

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
2026-02-14 23:14:43 +08:00
dbm_enable(s.Model).
Wheref(`CAST(? AS text) = ANY(ARRAY(SELECT jsonb_array_elements_text(bind_elf_ids)))`, 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)
2026-02-17 22:56:55 +08:00
for _, v := range ret {
id := v.ID
2026-02-17 22:56:55 +08:00
if grand.Meet(int(v.ElfProbability), 1000) {
2026-02-17 22:56:55 +08:00
if cool.Config.ServerInfo.IsVip == 0 {
m := cool.DBM(s.Model).Where("id", id)
m.Increment("usage_count", 1)
}
2026-02-21 17:41:49 +08:00
return &v.Color
}
2026-02-08 17:57:42 +08:00
}
2026-02-21 18:07:46 +08:00
if len(ret) == 0 {
return nil
}
2026-02-21 17:41:49 +08:00
var t = data.GetDef()
t.ColorMatrixFilter = model.GenerateRandomParentMatrix().Get()
2026-02-21 17:41:49 +08:00
return &t
2026-02-17 22:36:18 +08:00
}
2026-02-21 17:41:49 +08:00
func (s *ShinyService) RandomByWeightShiny(id uint32) *data.GlowFilter {
r := pickColorfulSkinByWeight(s.listByPetID(id))
if r == nil {
2026-02-17 22:36:18 +08:00
return nil
}
if cool.Config.ServerInfo.IsVip == 0 {
m := cool.DBM(s.Model).Where("id", r.ID)
m.Increment("refresh_count", 1)
2026-02-17 22:36:18 +08:00
}
2026-02-21 17:41:49 +08:00
return &r.Color
2026-02-17 22:36:18 +08:00
2026-02-08 17:57:42 +08:00
}
func (s *ShinyService) GetShiny(id int) *data.GlowFilter {
var ret []model.ColorfulSkin
// 执行 Raw SQL 并扫描返回值
dbm_nocache_noenable(s.Model).
2026-02-08 17:57:42 +08:00
Where("id", id).Scan(&ret)
if len(ret) == 0 {
return nil
}
2026-02-08 17:57:42 +08:00
v := ret[grand.Intn(len(ret))]
return &v.Color
}