Files
bl/modules/blazing/service/pet_fusion_service.go
昔念 1436cc0117 ```
refactor(service): 统一服务实例调用方式

将多个模块中手动创建服务实例的方式替换为全局单例模式,
包括 PetFusionService、PetFusionMaterialService 和 EffectService。
同时修改了相关调用代码以适配新的服务引用方式。

此外,重构了 talk 模块的数据结构与逻辑实现,
优化了挖矿次数检查及更新机制。
```
2025-12-08 19:50:54 +08:00

97 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"blazing/cool"
"blazing/modules/blazing/model"
"context"
"fmt"
"strings"
"github.com/alpacahq/alpacadecimal"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/util/grand"
)
// PetFusionService 宠物融合配方主表Service对应pet_fusion表
type PetFusionService struct {
*cool.Service // 嵌入通用Service继承基础CRUD方法
}
var PetFusionServiceS = NewPetFusionService()
// NewPetFusionService 创建PetFusionService实例
func NewPetFusionService() *PetFusionService {
return &PetFusionService{
&cool.Service{
Model: model.NewPetFusion(), // 绑定PetFusion模型
Cache: gcache.New(),
PageQueryOp: &cool.QueryOp{FieldEQ: []string{"is_enable", "main_pet_id", "sub_pet_id", "result_pet_id"}},
},
}
}
//获取主副精灵融合的id,如果不存在,那就给一个保底的id
func (s *PetFusionService) Data(p1, p2, rand uint32) uint32 {
rand = uint32(alpacadecimal.NewFromInt(int64(rand)).Mul(alpacadecimal.NewFromFloat(0.5)).IntPart())
pet := s.getData(p1, p2)
for _, v := range pet {
rr := grand.Intn(100)
if rr < int(v.Probability+int32(rand)) {
return uint32(v.ResultPetID)
}
}
//说明是失败,直接返回失败
if len(pet) > 0 {
return 0
}
pets := s.def()
res := pets[grand.Intn(len(pets)-1)]
rr := grand.Intn(100)
if rr < int(res.Probability+int32(rand)) {
return uint32(res.ResultPetID)
}
//到这里相当于直接失败
return 0
}
func (s *PetFusionService) getData(p1, p2 uint32) []model.PetFusion {
cacheKey := strings.Join([]string{fmt.Sprintf("%d", p1), fmt.Sprintf("%d", p2)}, ":")
//println(cacheKey, "获取融合id")
ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), cacheKey, func(context.Context) (interface{}, error) {
m := cool.DBM(s.Model)
var pet []model.PetFusion //一个特性应该是唯一的,但是我们要获取默认随机特性
condition := g.Map{
"main_pet_id": p1,
"sub_pet_id": p2,
"is_enable": 1,
// "hits between ? and ?" : g.Slice{1, 10},
// "exp > 0" : nil,
// "category" : g.Slice{100, 200},
}
m.Where(condition).Scan(&pet)
return pet, nil
}, 0)
return ret.Interface().([]model.PetFusion)
}
func (s *PetFusionService) def() []model.PetFusion {
//println(cacheKey, "获取融合id")
ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), "-1", func(context.Context) (interface{}, error) {
var pets []model.PetFusion
m := cool.DBM(s.Model)
m.Where("is_enable", 1).Where("is_default", 1).Scan(&pets)
return pets, nil
}, 0)
return ret.Interface().([]model.PetFusion)
}