Files
bl/modules/player/controller/admin/pet.go
昔念 8c049bcdcd
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
```
feat(admin): 宠物管理功能优化

- 清理宠物控制器中的乱码字符
- 更新宠物获取请求结构体字段注释为英文描述
- 重构变量命名提高代码可读性
- 添加宠物存储信息服务方法
- 优化错误提示信息为英文
- 新增宠物等级
2026-04-02 15:00:08 +08:00

144 lines
3.6 KiB
Go

package admin
import (
"blazing/common/data"
"blazing/cool"
base "blazing/modules/base/service"
config "blazing/modules/config/service"
"blazing/modules/player/model"
"blazing/modules/player/service"
"context"
"fmt"
"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:"Pet type ID"`
IndividualValue int `json:"individualValue" v:"required|between:-1,31" comment:"Individual value, -1 means random"`
NatureId int `json:"natureId" v:"required|between:-1,24" comment:"Nature ID, -1 means random"`
AbilityTypeEnum int `json:"abilityTypeEnum" v:"required|min:-1" comment:"Ability type, -1 means random"`
IsShiny int `json:"isShiny" v:"min:-1" comment:"Shiny config ID, -1 means random"`
Level int `json:"level" v:"required|between:1,100" comment:"Pet level"`
}
func init() {
var taskInfoController = &PetBagController{
&cool.Controller{
Prefix: "/admin/monster/bag",
Api: []string{"Delete", "Update", "Info", "List", "Page"},
Service: service.NewPetService(0),
},
}
cool.RegisterController(taskInfoController)
}
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,
0,
)
t.CatchRect = 1
if req.IsShiny == -1 {
t.RandomByWeightShiny()
}
service.NewUserService(uint32(req.UserID)).Pet.PetAdd(t, 0)
return
}
type PetLevelReq struct {
g.Meta `path:"/getlevel" method:"GET"`
}
type PetLevelRes struct {
UserID int `json:"user_id"`
Level int `json:"level"`
ID int `json:"id"`
}
func (c *PetBagController) Level(ctx context.Context, req *PetLevelReq) (res *cool.BaseRes, err error) {
r := service.NewUserService(0).Pet.PetLevelAll()
res = &cool.BaseRes{}
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 PetStorageReq struct {
g.Meta `path:"/storage" method:"POST"`
IsVIP int `json:"is_vip"`
}
func (c *PetBagController) Storage(ctx context.Context, req *PetStorageReq) (res *cool.BaseRes, err error) {
admin := cool.GetAdmin(ctx)
res = &cool.BaseRes{}
res.Data = service.NewPetService(uint32(admin.UserId)).StorageInfo(req.IsVIP)
return
}
type PriseReq struct {
g.Meta `path:"/modpirse" method:"POST"`
Ctime uint32 `json:"catch_time"`
Price uint32 `json:"sale_price"`
Free uint32 `json:"free"`
}
func (c *PetBagController) ModPrise(ctx context.Context, req *PriseReq) (res *cool.BaseRes, err error) {
admin := cool.GetAdmin(ctx)
res = &cool.BaseRes{}
if req.Price < 5 {
req.Price = 5
}
if req.Free == 2 {
if base.NewBaseSysUserService().GetFreeGold(admin.UserId) < 0 {
err = fmt.Errorf("insufficient free gold")
return
}
}
err = service.NewPetService(uint32(admin.UserId)).UpdatePrice(req.Ctime, req.Price, req.Free)
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
}