diff --git a/logic/controller/fight.go b/logic/controller/fight.go index 5c983786..f893bf29 100644 --- a/logic/controller/fight.go +++ b/logic/controller/fight.go @@ -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}) diff --git a/logic/controller/task.go b/logic/controller/task.go index dea64b4c..c2a98e07 100644 --- a/logic/controller/task.go +++ b/logic/controller/task.go @@ -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) diff --git a/modules/blazing/model/pet.go b/modules/blazing/model/pet.go index bfce71a2..2275843d 100644 --- a/modules/blazing/model/pet.go +++ b/modules/blazing/model/pet.go @@ -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 {