91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package service
|
||
|
||
import (
|
||
"blazing/common/utils"
|
||
"blazing/cool"
|
||
"blazing/modules/config/model"
|
||
|
||
"github.com/gogf/gf/v2/database/gdb"
|
||
"github.com/gogf/gf/v2/util/grand"
|
||
)
|
||
|
||
// PetFusionService 宠物融合配方主表Service(对应pet_fusion表)
|
||
type PetFusionService struct {
|
||
*cool.Service // 嵌入通用Service(继承基础CRUD方法)
|
||
}
|
||
|
||
// 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 {
|
||
|
||
if !grand.Meet(int(rand/2)+50, 100) {
|
||
return 0
|
||
|
||
}
|
||
|
||
pet := s.getData(p1, p2)
|
||
|
||
if len(pet) != 0 {
|
||
var pets, props []int
|
||
|
||
for _, v := range pet {
|
||
pets = append(pets, int(v.ResultPetID))
|
||
props = append(props, int(v.Probability))
|
||
|
||
}
|
||
t,_:utils.RandomByIntProbs(pets, props)
|
||
return t
|
||
//说明是失败,直接返回失败
|
||
} else {
|
||
|
||
pets := s.def()
|
||
res := pets[grand.Intn(len(pets))]
|
||
rr := grand.Intn(100)
|
||
if rr < int(res.Probability+int32(rand)) {
|
||
return uint32(res.ResultPetID)
|
||
}
|
||
//到这里相当于直接失败
|
||
return 0
|
||
}
|
||
|
||
return 0
|
||
|
||
}
|
||
func (s *PetFusionService) getData(p1, p2 uint32) []model.PetFusion {
|
||
//cacheKey := strings.Join([]string{fmt.Sprintf("%d", p1), fmt.Sprintf("%d", p2)}, ":")
|
||
m := dbm(s.Model)
|
||
|
||
var pet []model.PetFusion //一个特性应该是唯一的,但是我们要获取默认随机特性
|
||
m.Where("main_pet_id", p1).Wheref(`sub_pet_ids @> ARRAY[?]::integer[]`, p2).
|
||
Where("is_enable", 1).Scan(&pet)
|
||
|
||
return pet
|
||
|
||
}
|
||
|
||
func (s *PetFusionService) def() []model.PetFusion {
|
||
|
||
var pets []model.PetFusion
|
||
m := cool.DBM(s.Model)
|
||
m.Where("is_enable", 1).Where("is_default", 1).Cache(gdb.CacheOption{
|
||
// Duration: time.Hour,
|
||
|
||
Force: false,
|
||
}).Scan(&pets)
|
||
|
||
return pets
|
||
// return ret.Interface().([]model.PetFusion)
|
||
|
||
}
|