refactor(controller): 重构ParseCmd函数并优化Init方法

- 修改ParseCmd函数参数,移除不必要的泛型参数T
- 为Init函数添加详细注释说明参数含义
- 优化getCmd函数的注释和代码结构
- 重命名ParseCmdTagWithStructField为parseCmdTagWithStructField以符合私有函数命名规范

refactor(pet_info): 统一玩家参数命名从c到player

- 将pet_info.go中所有方法的player参数名从c改为player
- 保持代码逻辑不变,仅统一参数命名规范
- 更新相关
This commit is contained in:
2025-12-25 12:21:15 +08:00
parent 164e70519f
commit d84100a52f
5 changed files with 149 additions and 137 deletions

View File

@@ -14,13 +14,13 @@ import (
// GetPetInfo 获取精灵信息
// data: 包含精灵捕获时间的输入信息
// c: 当前玩家对象
// player: 当前玩家对象
// 返回: 精灵信息和错误码
func (h Controller) GetPetInfo(
data *pet.InInfo,
c *player.Player) (result *pet.OutInfo,
player *player.Player) (result *pet.OutInfo,
err errorcode.ErrorCode) {
_, petInfo, found := c.FindPet(data.CatchTime)
_, petInfo, found := player.FindPet(data.CatchTime)
if found {
result = &pet.OutInfo{
@@ -29,22 +29,22 @@ func (h Controller) GetPetInfo(
return result, 0
}
result = &pet.OutInfo{
PetInfo: c.Service.Pet.PetInfo_One(data.CatchTime).Data,
PetInfo: player.Service.Pet.PetInfo_One(data.CatchTime).Data,
}
return result, 0
}
// GetPetList 获取仓库列表
// data: 空输入结构
// c: 当前玩家对象
// player: 当前玩家对象
// 返回: 精灵列表和错误码
func (h Controller) GetPetList(
data *pet.GetPetListInboundEmpty,
c *player.Player) (result *pet.GetPetListOutboundInfo,
player *player.Player) (result *pet.GetPetListOutboundInfo,
err errorcode.ErrorCode) {
result = &pet.GetPetListOutboundInfo{}
petList := c.Service.Pet.PetInfo(0) // 获取未放生的精灵
petList := player.Service.Pet.PetInfo(0) // 获取未放生的精灵
result.ShortInfoList = make([]pet.PetShortInfo, len(petList))
for i, petItem := range petList {
copier.Copy(&result.ShortInfoList[i], &petItem.Data)
@@ -54,15 +54,15 @@ func (h Controller) GetPetList(
// GetPetReleaseList 获取放生列表
// data: 空输入结构
// c: 当前玩家对象
// player: 当前玩家对象
// 返回: 放生精灵列表和错误码
func (h Controller) GetPetReleaseList(
data *pet.GetPetListFreeInboundEmpty,
c *player.Player) (result *pet.GetPetListOutboundInfo,
player *player.Player) (result *pet.GetPetListOutboundInfo,
err errorcode.ErrorCode) {
result = &pet.GetPetListOutboundInfo{}
petList := c.Service.Pet.PetInfo(1) // 获取已放生的精灵
petList := player.Service.Pet.PetInfo(1) // 获取已放生的精灵
result.ShortInfoList = make([]pet.PetShortInfo, len(petList))
for i, petItem := range petList {
copier.Copy(&result.ShortInfoList[i], &petItem.Data)
@@ -72,16 +72,16 @@ func (h Controller) GetPetReleaseList(
// PetReleaseToWarehouse 将精灵从仓库包中放生
// data: 包含精灵ID和捕获时间的输入信息
// c: 当前玩家对象
// player: 当前玩家对象
// 返回: 无数据和错误码
func (h Controller) PetReleaseToWarehouse(
data *pet.PET_ROWEI, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
c.Service.Pet.PetInfo_One_exec(data.CatchTime, func(pet *model.PetEX) {
_, _, inBag := c.FindPet(data.CatchTime)
data *pet.PET_ROWEI, player *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
player.Service.Pet.PetInfo_One_exec(data.CatchTime, func(petInfo *model.PetEX) {
_, _, inBag := player.FindPet(data.CatchTime)
freeForbidden := xmlres.PetMAP[int(data.ID)].FreeForbidden
// 如果背包没找到,再放入背包
if !inBag && pet.CatchTime != 0 && freeForbidden == 0 {
pet.Free = 1
if !inBag && petInfo.CatchTime != 0 && freeForbidden == 0 {
petInfo.Free = 1
} else {
err = errorcode.ErrorCodes.ErrCannotReleaseNonWarehouse
}
@@ -93,15 +93,13 @@ func (h Controller) PetReleaseToWarehouse(
// PetRetrieveFromWarehouse 领回包
func (h Controller) PetRetrieveFromWarehouse(
data *pet.PET_RETRIEVE, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
data *pet.PET_RETRIEVE, player *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
c.Service.Pet.PetInfo_One_exec(data.CatchTime, func(pet *model.PetEX) {
_, _, ok := c.FindPet(data.CatchTime)
player.Service.Pet.PetInfo_One_exec(data.CatchTime, func(petInfo *model.PetEX) {
//如果背包没找到,再放入背包
if !ok && pet.CatchTime != 0 {
pet.Free = 0
if _, _, ok := player.FindPet(data.CatchTime); !ok && petInfo.CatchTime != 0 {
petInfo.Free = 0
}
})
@@ -113,7 +111,7 @@ func (h Controller) PetRetrieveFromWarehouse(
// TogglePetBagWarehouse 精灵背包仓库切换
func (h Controller) TogglePetBagWarehouse(
data *pet.PetReleaseInboundInfo,
c *player.Player) (
player *player.Player) (
result *pet.PetReleaseOutboundInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的
//放入背包=数据库置1+添加到背包+pet release发包 仓库=数据库置0+移除背包 设置首发等于取到首发精灵后重新排序
@@ -122,34 +120,34 @@ func (h Controller) TogglePetBagWarehouse(
result = &pet.PetReleaseOutboundInfo{}
result.Flag = uint32(data.Flag)
//擂台住不能换精灵
if c.GetSpace().Owner.UserID == c.Info.UserID {
if player.GetSpace().Owner.UserID == player.Info.UserID {
return result, errorcode.ErrorCodes.ErrChampionCannotSwitch
}
switch data.Flag {
case 0:
index, pet, ok := c.FindPet(data.CatchTime)
index, pet, ok := player.FindPet(data.CatchTime)
if ok {
c.Service.Pet.PetInfo_One_exec(data.CatchTime, func(petData *model.PetEX) {
player.Service.Pet.PetInfo_One_exec(data.CatchTime, func(petData *model.PetEX) {
petData.Data = *pet
//t.InBag = 0
})
c.Info.PetList = append(c.Info.PetList[:index], c.Info.PetList[index+1:]...)
player.Info.PetList = append(player.Info.PetList[:index], player.Info.PetList[index+1:]...)
}
// break // 只移除第一个匹配值,若需移除所有,可省略 break 继续循环
case 1:
if len(c.Info.PetList) < 6 {
if len(player.Info.PetList) < 6 {
//todo 背包
c.Service.Pet.PetInfo_One_exec(data.CatchTime, func(petData *model.PetEX) {
player.Service.Pet.PetInfo_One_exec(data.CatchTime, func(petData *model.PetEX) {
_, _, ok := c.FindPet(data.CatchTime)
_, _, ok := player.FindPet(data.CatchTime)
//如果背包没找到,再放入背包
if !ok && petData.CatchTime != 0 {
//t.InBag = 1
c.Info.PetList = append(c.Info.PetList, petData.Data)
player.Info.PetList = append(player.Info.PetList, petData.Data)
result.PetInfo = petData.Data
}
@@ -158,8 +156,8 @@ func (h Controller) TogglePetBagWarehouse(
}
if len(c.Info.PetList) > 0 {
result.FirstPetTime = c.Info.PetList[0].CatchTime //设置首发
if len(player.Info.PetList) > 0 {
result.FirstPetTime = player.Info.PetList[0].CatchTime //设置首发
}
return result, 0
@@ -167,15 +165,15 @@ func (h Controller) TogglePetBagWarehouse(
// PlayerShowPet 精灵展示
func (h Controller) PlayerShowPet(
data *pet.PetShowInboundInfo, c *player.Player) (result *pet.PetShowOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
data *pet.PetShowInboundInfo, player *player.Player) (result *pet.PetShowOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.PetShowOutboundInfo{}
_, currentPet, ok := c.FindPet(data.CatchTime)
_, currentPet, ok := player.FindPet(data.CatchTime)
if ok {
copier.Copy(&result, currentPet)
result.Flag = data.Flag
result.UserID = data.Head.UserID
defer c.GetSpace().Broadcast(c, data.Head.CMD, result)
defer player.GetSpace().Broadcast(player, data.Head.CMD, result)
}
return
@@ -183,12 +181,12 @@ func (h Controller) PlayerShowPet(
// PetOneCure 单体治疗
func (h Controller) PetOneCure(
data *pet.PetOneCureInboundInfo, c *player.Player) (result *pet.PetOneCureOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
if c.GetSpace().Owner.UserID == c.Info.UserID {
data *pet.PetOneCureInboundInfo, player *player.Player) (result *pet.PetOneCureOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
if player.GetSpace().Owner.UserID == player.Info.UserID {
return result, errorcode.ErrorCodes.ErrChampionCannotHeal
}
_, currentPet, ok := c.FindPet(data.CatchTime)
_, currentPet, ok := player.FindPet(data.CatchTime)
if ok {
defer currentPet.Cure()
@@ -202,18 +200,18 @@ func (h Controller) PetOneCure(
// PetFirst 精灵首发
func (h Controller) PetFirst(
data *pet.PetDefaultInboundInfo, c *player.Player) (result *pet.PetDefaultOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
data *pet.PetDefaultInboundInfo, player *player.Player) (result *pet.PetDefaultOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
//擂台住不能换精灵
if c.GetSpace().Owner.UserID == c.Info.UserID {
if player.GetSpace().Owner.UserID == player.Info.UserID {
return result, errorcode.ErrorCodes.ErrChampionCannotSwitch
}
result = &pet.PetDefaultOutboundInfo{}
index, _, ok := c.FindPet(data.CatchTime)
index, _, ok := player.FindPet(data.CatchTime)
if ok && index != 0 {
c.Info.PetList[index], c.Info.PetList[0] = c.Info.PetList[0], c.Info.PetList[index]
player.Info.PetList[index], player.Info.PetList[0] = player.Info.PetList[0], player.Info.PetList[index]
result.IsDefault = 1
}
@@ -222,28 +220,26 @@ func (h Controller) PetFirst(
}
// FindWithIndex 遍历slice找到第一个满足条件的元素
// 返回:索引、元素指针、是否找到
// SetPetExp 设置宠物经验
func (h Controller) SetPetExp(data *pet.PetSetExpInboundInfo, player *player.Player) (result *pet.PetSetExpOutboundInfo, err errorcode.ErrorCode) {
_, currentPet, found := player.FindPet(data.CatchTime)
if found && currentPet.Level < 100 {
func (h Controller) SetPetExp(data *pet.PetSetExpInboundInfo, c *player.Player) (result *pet.PetSetExpOutboundInfo, err errorcode.ErrorCode) {
_, currentPet, ok := c.FindPet(data.CatchTime)
if ok && currentPet.Level < 100 {
c.AddPetExp(currentPet, data.Exp)
player.AddPetExp(currentPet, data.Exp)
return &pet.PetSetExpOutboundInfo{
Exp: c.Info.ExpPool,
Exp: player.Info.ExpPool,
}, 0
}
return &pet.PetSetExpOutboundInfo{
Exp: c.Info.ExpPool,
Exp: player.Info.ExpPool,
}, errorcode.ErrorCodes.ErrSystemError
}
// GetPetBargeList 精灵图鉴
func (h Controller) GetPetBargeList(data *pet.PetBargeListInboundInfo, c *player.Player) (result *pet.PetBargeListOutboundInfo, err errorcode.ErrorCode) {
func (h Controller) GetPetBargeList(data *pet.PetBargeListInboundInfo, player *player.Player) (result *pet.PetBargeListOutboundInfo, err errorcode.ErrorCode) {
return &pet.PetBargeListOutboundInfo{
PetBargeList: make([]pet.PetBargeListInfo, 0),
}, 0
}
}