refactor(model): 重构 GenPetInfo 函数参数处理逻辑

- 将 GenPetInfo 函数的参数从单个值改为切片,以支持更灵活的配置
- 新增 RandomInRange 函数,用于从切片表示的范围内随机选择值
- 更新了 fight 控制器和 task 控制器中调用 GenPetInfo 函数的代码
- 此重构提高了代码的可读性和可维护性,同时保留了原有的功能
This commit is contained in:
2025-09-05 00:26:42 +08:00
parent d0d897337e
commit a86b69dd1e
3 changed files with 40 additions and 14 deletions

View File

@@ -3,9 +3,6 @@ package controller
import (
"blazing/common/socket/errorcode"
"math/rand"
"time"
"blazing/logic/service"
"blazing/logic/service/fight"
"blazing/logic/service/fight/info"
@@ -35,8 +32,12 @@ func (h Controller) OnPlayerFightNpcMonster(data *fight.FightNpcMonsterInboundIn
ttt.OpponentInfo = info.FightUserInfo{UserID: 0}
refpet := c.OgreInfo.Data[data.Number]
dv := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(32)
mo := model.GenPetInfo(refpet.Id, uint32(dv), 0, 1006, refpet.Shiny, refpet.Lv)
mo := model.GenPetInfo(
int(refpet.Id), []int{0, 31},
[]int{0, 24},
[]int{0}, //野怪没特性
[]int{int(refpet.Shiny)},
[]int{int(refpet.Lv)})
ttt.OpponentPetList = make([]info.ReadyFightPetInfo, 1)
err1 := copier.CopyWithOption(&ttt.OpponentPetList[0], &mo, copier.Option{IgnoreEmpty: true, DeepCopy: true})

View File

@@ -91,7 +91,7 @@ func (h Controller) Complete_Task(data *task.CompleteTaskInboundInfo, c *service
}
if data.TaskId == 86 { //新手注册任务
r := model.GenPetInfo(1, 1, 1, 1006, 1, 5)
r := model.GenPetInfo(1, []int{0, 31}, []int{0, 24}, []int{0}, []int{0}, []int{5})
result.CaptureTime = r.CatchTime
result.PetTypeId = r.ID
blservice.NewUserService(c.Info.UserID).PetAdd(*r)

View File

@@ -3,6 +3,7 @@ package model
import (
"blazing/common/data/xmlres"
"blazing/cool"
"math/rand"
"time"
)
@@ -22,6 +23,30 @@ type Pet struct {
Data string `gorm:"type:text;not null;comment:'精灵全部数据'" json:"data"`
}
// RandomInRange 从切片表示的范围中返回对应结果:
// - 切片包含1个元素时返回该元素本身
// - 切片包含2个元素时从[min, max]闭区间随机生成一个整数自动调整min和max顺序
// - 其他情况返回0
func RandomInRange(rangeSlice []int) int {
rand.Seed(time.Now().UnixNano())
switch len(rangeSlice) {
case 1:
// 只有一个元素时返回自身
return rangeSlice[0]
case 2:
// 两个元素时处理为范围
min, max := rangeSlice[0], rangeSlice[1]
// 确保 min ≤ max
if min > max {
min, max = max, min
}
// 生成 [min, max] 区间的随机整数
return (rand.Intn(int(max-min+1)) + int(min))
default:
// 其他长度返回0
return 0
}
}
func LastFourElements[T any](s []T) []T {
n := len(s)
if n <= 4 {
@@ -39,18 +64,18 @@ func LastFourElements[T any](s []T) []T {
// * @param isShiny 是否为闪光
// * @param level 等级
// * @return 生成的精灵实体
func GenPetInfo(id, dv, natureId, abilityTypeEnum, shinyid, level uint32) *PetInfo {
func GenPetInfo(id int, dv, natureId, abilityTypeEnum, shinyid, level []int) *PetInfo {
p := &PetInfo{ID: id,
Shiny: shinyid, //闪光
Nature: natureId, //性格
Dv: dv,
p := &PetInfo{ID: uint32(id),
Shiny: uint32(RandomInRange(shinyid)), //闪光
Nature: uint32(RandomInRange(natureId)), //性格
Dv: uint32(RandomInRange(dv)),
EffectInfo: make([]PetEffectInfo, 0),
CatchTime: uint32(time.Now().Unix()),
Level: level} //等级
Level: uint32(RandomInRange(level))} //等级
p.EffectInfo = append(p.EffectInfo, PetEffectInfo{ItemID: abilityTypeEnum})
naxml := xmlres.NatureRootMap[int(natureId)]
p.EffectInfo = append(p.EffectInfo, PetEffectInfo{ItemID: uint32(RandomInRange(abilityTypeEnum))})
naxml := xmlres.NatureRootMap[int(p.Nature)]
petxml := xmlres.PetMAP[int(id)]
tttt := make([]uint32, 0)
for _, v := range petxml.LearnableMoves.Moves {