All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
feat(pet): 完善宠物购买功能的错误处理和数据验证 - 在控制器层添加错误返回值处理,确保购买宠物操作的错误能够正确传递 - 添加时间验证逻辑,防止使用过期或异常的数据进行购买操作 - 修正金币更新逻辑,确保玩家和系统金币扣除与增加的准确性 - 优化代码结构,增强代码可读性和维护性 ```
118 lines
3.9 KiB
Go
118 lines
3.9 KiB
Go
package admin
|
||
|
||
import (
|
||
"blazing/common/data"
|
||
"blazing/cool"
|
||
config "blazing/modules/config/service"
|
||
"blazing/modules/player/model"
|
||
"blazing/modules/player/service"
|
||
"context"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
)
|
||
|
||
type PetBagController struct {
|
||
*cool.Controller
|
||
}
|
||
type PetGetReq struct {
|
||
g.Meta `path:"/genpet" method:"POST"`
|
||
UserID uint32 `json:"user_id"`
|
||
IsVIP int `json:"is_vip"`
|
||
// 新增:精灵相关参数(完全对齐自定义表单配置)
|
||
PetTypeId int `json:"petTypeId" v:"required|min:1" comment:"精灵类型ID,必填,正整数(最小1,无0或负数类型),对应GenPetInfo的id参数"`
|
||
IndividualValue int `json:"individualValue" v:"required|between:-1,31" comment:"精灵个体值,必填,范围-1~31(-1表示随机生成满个体,0~31为固定值,决定基础属性上限)"`
|
||
NatureId int `json:"natureId" v:"required|between:-1,24" comment:"精灵性格ID,必填,范围-1~24(-1表示随机,0~24对应25种固定性格,影响属性加成)"`
|
||
AbilityTypeEnum int `json:"abilityTypeEnum" v:"required|min:-1" comment:"精灵特性类型ID,必填,最小值-1(-1表示随机选择1个特性,0及以上为固定特性ID,需与PlayerEffectMAP匹配)"`
|
||
IsShiny int `json:"isShiny" v:"min:-1" comment:"精灵异色(闪光)ID,必填,最小值-1(-1表示随机,0及以上为固定异色状态,稀有精灵特殊外观/属性),原表单hidden但需保留必填逻辑"`
|
||
Level int `json:"level" v:"required|between:1,100" comment:"精灵等级,必填,范围1~100(常规游戏等级上限,影响精灵当前属性强度)"`
|
||
}
|
||
|
||
func init() {
|
||
var task_info_controller = &PetBagController{
|
||
&cool.Controller{
|
||
Prefix: "/admin/monster/bag",
|
||
Api: []string{"Delete", "Update", "Info", "List", "Page"},
|
||
Service: service.NewPetService(0), //因为page已经过滤,所以这里需要改成0
|
||
},
|
||
}
|
||
// 注册路由
|
||
cool.RegisterController(task_info_controller)
|
||
}
|
||
func (c *PetBagController) GetSession(ctx context.Context, req *PetGetReq) (res *cool.BaseRes, err error) {
|
||
var shiny []data.GlowFilter
|
||
if req.IsShiny != 0 {
|
||
r := config.NewShinyService().GetShiny(req.IsShiny)
|
||
shiny = append(shiny, *r)
|
||
}
|
||
|
||
t := model.GenPetInfo(
|
||
req.PetTypeId, req.IndividualValue, req.NatureId, req.AbilityTypeEnum, req.Level, shiny, -1)
|
||
t.CatchRect = 1 //代表这是人工合成的
|
||
|
||
service.NewUserService(uint32(req.UserID)).Pet.PetAdd(t)
|
||
return
|
||
|
||
}
|
||
|
||
func (c *PetBagController) Level(ctx context.Context, req *PetLevelReq) (res *cool.BaseRes, err error) {
|
||
// m := gmap.New()
|
||
r := service.NewUserService(0).Pet.Pet_LEVEL_all()
|
||
|
||
ress := make([]PetLevelRes, 0)
|
||
|
||
for _, v := range r {
|
||
ress = append(ress, PetLevelRes{
|
||
UserID: int(v.PlayerID),
|
||
Level: int(v.Data.Level),
|
||
ID: int(v.Data.ID),
|
||
})
|
||
|
||
}
|
||
res.Data = ress
|
||
return
|
||
|
||
}
|
||
|
||
type PetLevelReq struct {
|
||
g.Meta `path:"/getlevel" method:"GET"`
|
||
// 新增:精灵相关参数(完全对齐自定义表单配置)
|
||
}
|
||
type PetLevelRes struct {
|
||
UserID int `json:"user_id"`
|
||
//等级PetLevelRes
|
||
Level int `json:"level"`
|
||
//ID
|
||
ID int `json:"id"`
|
||
}
|
||
type PriseReq struct {
|
||
g.Meta `path:"/modpirse" method:"POST"`
|
||
Ctime uint32 `json:"catch_time"`
|
||
Price float32 `json:"sale_price"`
|
||
}
|
||
|
||
func (c *PetBagController) ModPrise(ctx context.Context, req *PriseReq) (res *cool.BaseRes, err error) {
|
||
admin := cool.GetAdmin(ctx)
|
||
|
||
res = &cool.BaseRes{}
|
||
service.NewPetService(uint32(admin.UserId)).UPdatePrice(req.Ctime, req.Price)
|
||
|
||
return
|
||
|
||
}
|
||
|
||
type BuyPetReq struct {
|
||
g.Meta `path:"/buy" method:"POST"`
|
||
Cid uint32 `json:"id"`
|
||
}
|
||
|
||
func (c *PetBagController) BuyPet(ctx context.Context, req *BuyPetReq) (res *cool.BaseRes, err error) {
|
||
admin := cool.GetAdmin(ctx)
|
||
|
||
res = &cool.BaseRes{}
|
||
|
||
err = service.NewPetService(uint32(admin.UserId)).BuyPet(req.Cid)
|
||
|
||
return
|
||
|
||
}
|