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(), ListQueryOp: &cool.QueryOp{ FieldEQ: []string{"is_enable"}, }, 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(`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) 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 }