Files
bl/logic/controller/pet_繁殖.go
昔念 2f756c77bb
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
1
2026-02-12 12:43:28 +08:00

195 lines
5.1 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 controller
import (
"blazing/common/socket/errorcode"
"blazing/logic/service/fight"
"blazing/logic/service/pet"
"blazing/logic/service/player"
"blazing/modules/config/service"
"blazing/modules/player/model"
"time"
"github.com/gogf/gf/v2/util/grand"
"github.com/samber/lo"
"github.com/yudeguang/ratelimit"
)
// GetBreedInfo 获取繁殖信息协议
// 前端到后端无数据 请求协议
func (ctl Controller) GetBreedInfo(
data *pet.C2S_GET_BREED_INFO, player *player.Player) (result *model.S2C_GET_BREED_INFO, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &model.S2C_GET_BREED_INFO{}
r := player.Service.Egg.Get()
if r == nil {
return
}
result = &r.Data
// TODO: 实现获取繁殖信息的具体逻辑
return result, 0
}
// GetBreedPet 获取繁殖精灵
// 前端到后端
func (ctl Controller) GetBreedPet(
data *pet.C2S_GET_BREED_PET, playerObj *player.Player) (result *pet.S2C_GET_BREED_PET, err errorcode.ErrorCode) { //这个时候player应该是空的
_, fPet, found := playerObj.FindPet(data.MaleCatchTime)
if !found {
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
}
if fPet.Gender != 1 {
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
}
if fPet.Generation > 9 {
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
}
result = &pet.S2C_GET_BREED_PET{}
MPETS := service.NewEggService().GetData(fPet.ID)
// 这里只是示例实际应该根据雄性精灵的catchTime查找可繁殖的雌性精灵
for _, v := range playerObj.Info.PetList {
if v.Level < 50 {
continue
}
//不是雌性
if v.Gender != 2 {
continue
}
if v.Generation > 9 {
continue
}
_, ok := lo.Find(MPETS, func(item int32) bool {
return item == int32(v.ID)
})
if !ok {
continue
}
// 如果是雌性精灵,且可以繁殖,则添加到列表
result.FemaleList = append(result.FemaleList, v.CatchTime)
}
return result, 0
}
// StartBreed 开始繁殖协议
// 前端到后端
func (ctl Controller) StartBreed(
data *pet.C2S_START_BREED, player *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
if !player.GetCoins(1000) {
return result, errorcode.ErrorCodes.ErrSunDouInsufficient10016
}
player.Info.Coins -= 1000
_, MalePet, found := player.FindPet(data.Male)
if !found {
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
}
_, Female, found := player.FindPet(data.Female)
if !found {
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
}
// TODO: 实现开始繁殖的具体逻辑
result = &fight.NullOutboundInfo{}
r := player.Service.Egg.StartBreed(MalePet, Female)
if !r {
return nil, errorcode.ErrorCodes.ErrCannotPerformAction
}
MalePet.Generation++
Female.Generation++
r1 := grand.Meet(1, 2)
if r1 {
MalePet.Gender = 0
} else {
Female.Gender = 0
}
return result, 0
}
// GetEggList 获取精灵蛋数组
// 前端到后端无数据 请求协议
func (ctl Controller) GetEggList(
data *pet.C2S_GET_EGG_LIST, player *player.Player) (result *pet.S2C_GET_EGG_LIST, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.S2C_GET_EGG_LIST{}
// TODO: 实现获取精灵蛋列表的逻辑
// 示例数据,实际应从玩家数据中获取
r := player.Service.Egg.Get()
if r == nil {
return
}
result.EggList = append(result.EggList, r.EggList...)
return result, 0
}
var limiter *ratelimit.Rule = ratelimit.NewRule()
// 简单规则案例
func init() {
//步骤二:增加一条或者多条规则组成复合规则,此复合规则必须至少包含一条规则
limiter.AddRule(time.Second*1, 1)
//步骤三:调用函数判断某用户是否允许访问 allow:= r.AllowVisit(user)
}
// EffectHatch 精灵蛋互动协议
// 前端到后端
func (ctl Controller) EffectHatch(
data *pet.C2S_EFFECT_HATCH, playerObj *player.Player) (result *pet.S2C_EFFECT_HATCH, err errorcode.ErrorCode) {
if !limiter.AllowVisit(data.Head.UserID) {
return nil, errorcode.ErrorCodes.ErrCannotPerformAction
}
result = &pet.S2C_EFFECT_HATCH{}
tt := uint32(grand.N(1, 4))
if tt == data.Index {
result.Intimacy = playerObj.Service.Egg.EffectHatch()
}
return result, 0
}
// StartHatch 开始孵化精灵蛋
// 前端到后端
func (ctl Controller) StartHatch(
data *pet.C2S_START_HATCH, playerObj *player.Player) (result *pet.S2C_START_HATCH, err errorcode.ErrorCode) {
// TODO: 实现开始孵化精灵蛋的具体逻辑
result = &pet.S2C_START_HATCH{}
r := playerObj.Service.Egg.StartEgg(data.OwnerID, data.EggCatchTime)
if !r {
return nil, errorcode.ErrorCodes.ErrCannotPerformAction
}
return result, 0
}
// GetHatchPet 获得孵化精灵协议
// 前端到后端无数据内容 请求协议
func (ctl Controller) GetHatchPet(
data *pet.C2S_GET_HATCH_PET, playerObj *player.Player) (result *pet.S2C_GET_HATCH_PET, err errorcode.ErrorCode) {
result = &pet.S2C_GET_HATCH_PET{}
// TODO: 实现获得孵化精灵的具体逻辑,这里暂时返回默认值
r := playerObj.Service.Egg.GetEgg()
if r == nil {
return nil, errorcode.ErrorCodes.ErrSystemError
}
playerObj.Service.Pet.PetAdd(r)
result.PetID = r.ID
result.CatchTime = r.CatchTime
return result, 0
}