All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
feat(fight): 使用专用函数构建战斗结束数据包 为战斗结束消息创建专用的构建函数, 统一处理战斗结束信息的数据包构建逻辑, 提高代码的一致性和可维护性。 fix(config): 优化数据库查询语句以提高性能 将数组包含操作(@>)替换为 ANY 操作符, 在 Egg、MapPit、PetFusion 等服务中使用更高效 的查询方式
142 lines
2.7 KiB
Go
142 lines
2.7 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(`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
|
|
|
|
}
|