Files
bl/logic/controller/pet.go
昔念 e84d55aec0 ```
feat(player): 优化玩家连接与消息发送逻辑

- 为 `Conn` 结构体新增 `Close()` 方法,并在多处替换原有的 `GetConn().Close()` 调用,
  统一通过 `MainConn` 操作以提高代码一致性。
- 在 `SendPack` 和 `GetPlayer` 等方法中增加互斥锁保护,确保对 `MainConn` 的访问是线程安全的。
- 移除冗余的 `GetConn()` 方法,直接使用 `MainConn` 提升性能并简化结构。

refactor(rpc): 临时将 RPC 客户端地址硬编码为本地地址

- 将 rpcaddr 临时设置为 "127.0.0.1",便于调试和开发环境测试。

refactor(handler): 扩展 JSON-RPC 方法过滤条件

- 增加了对 "Kick" 和 "RegisterLogic" 方法的排除,防止其被注册到 RPC 处理器中。

refactor(fight): 精简战斗服务中的判断逻辑

- 简化 `IsFirst` 函数返回表达式。
- 移除了多余的 defer 语句,提前设置 `closefight` 标志位。

refactor(pet): 改进宠物经验添加机制

- `AddPetExp` 方法新增布尔参数 `bro` 控制是否广播更新。
- 修改调用点以适配新参数,修复潜在的数据同步问题。
- 初始化宠物时禁止触发经验变更广播。

chore(build): 删除旧版二进制文件 logic1

- 清理无用的编译产物,避免混淆项目结构。
```
2025-10-14 01:26:59 +08:00

200 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/data/xmlres"
"blazing/common/socket/errorcode"
"blazing/logic/service/pet"
"blazing/logic/service/player"
"blazing/modules/blazing/model"
"github.com/jinzhu/copier"
)
// 获取精灵信息
func (h *Controller) GetPetInfo(
data *pet.InInfo,
c *player.Player) (result *pet.OutInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的
for _, pi := range c.Info.PetList {
if pi.CatchTime == data.CatchTime {
result = &pet.OutInfo{
PetInfo: pi,
}
}
}
if result == nil {
result = &pet.OutInfo{
PetInfo: c.Service.PetInfo_One(data.CatchTime).Data,
}
}
return result, 0
}
// 获取仓库列表
func (h *Controller) GetPetList(
data *pet.GetPetListInboundEmpty,
c *player.Player) (result *pet.GetPetListOutboundInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.GetPetListOutboundInfo{}
tt := c.Service.PetInfo(0) //获得未放生的精灵
result.ShortInfoList = make([]pet.PetShortInfo, len(tt))
for i, v := range tt {
copier.Copy(&result.ShortInfoList[i], &v.Data)
}
return result, 0
}
// 精灵背包仓库切换
func (h *Controller) PetRelease(
data *pet.PetReleaseInboundInfo,
c *player.Player) (
result *pet.PetReleaseOutboundInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的
//放入背包=数据库置1+添加到背包+pet release发包 仓库=数据库置0+移除背包 设置首发等于取到首发精灵后重新排序
//这里只修改,因为添加和移除背包在宠物获取时已经做了
result = &pet.PetReleaseOutboundInfo{}
result.Flag = uint32(data.Flag)
c.Service.PetInfo_One_exec(data.CatchTime, func(t *model.PetEX) {
t.InBag = int(data.Flag)
})
switch data.Flag {
case 0:
removeIndex := -1
for i, v := range c.Info.PetList {
if v.CatchTime == uint32(data.CatchTime) {
removeIndex = i
break // 只移除第一个匹配值,若需移除所有,可省略 break 继续循环
}
}
if removeIndex != -1 {
c.Info.PetList = append(c.Info.PetList[:removeIndex], c.Info.PetList[removeIndex+1:]...)
}
case 1:
//todo 背包
c.Service.PetInfo_One_exec(data.CatchTime, func(t *model.PetEX) {
c.Info.PetList = append(c.Info.PetList, t.Data)
result.PetInfo = t.Data
})
}
if len(c.Info.PetList) > 0 {
result.FirstPetTime = c.Info.PetList[0].CatchTime //设置首发
}
//service.NewUserService(c.Info.UserID).PetAdd( *r)
return result, 0
}
// 精灵展示
func (h *Controller) PlayerShowPet(
data *pet.PetShowInboundInfo, c *player.Player) (result *pet.PetShowOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
results := pet.PetShowOutboundInfo{}
for _, pi := range c.Info.PetList {
if pi.CatchTime == data.CatchTime {
copier.Copy(&results, pi)
results.Flag = data.Flag
results.UserID = data.Head.UserID
data.Broadcast(c.Info.MapID, results) //同步广播
}
}
return result, -1
}
func (h *Controller) PetOneCure(
data *pet.PetOneCureInboundInfo, c *player.Player) (result *pet.PetOneCureOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
_, onpet, ok := FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == data.CatchTime
})
if ok {
onpet.Cure()
}
return &pet.PetOneCureOutboundInfo{
CatchTime: data.CatchTime,
}, 0
}
// 精灵首发
func (h *Controller) PetFirst(
data *pet.PetDefaultInboundInfo, c *player.Player) (result *pet.PetDefaultOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.PetDefaultOutboundInfo{}
var ttt []model.PetInfo
for index, pi := range c.Info.PetList {
if pi.CatchTime == data.CatchTime {
ttt = append(ttt, pi)
ttt = append(ttt, c.Info.PetList[:index]...)
ttt = append(ttt, c.Info.PetList[index+1:]...)
result.IsDefault = 1
break
}
}
c.Info.PetList = ttt
return result, 0
}
// FindWithIndex 遍历slice找到第一个满足条件的元素
// 返回:索引、元素指针、是否找到
func FindWithIndex[T any](slice []T, predicate func(item T) bool) (int, *T, bool) {
for i := range slice {
if predicate(slice[i]) {
return i, &slice[i], true
}
}
return -1, nil, false
}
func (h Controller) SetPetExp(data *pet.PetSetExpInboundInfo, c *player.Player) (result *pet.PetSetExpOutboundInfo, err errorcode.ErrorCode) {
_, onpet, ok := FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == data.CatchTime
})
if ok {
c.AddPetExp(onpet, data.Exp, false)
}
return &pet.PetSetExpOutboundInfo{
c.Info.ExpPool,
}, 0
}
func (h Controller) SetPetSkill(data *pet.ChangeSkillInfo, c *player.Player) (result *pet.ChangeSkillOutInfo, err errorcode.ErrorCode) {
_, onpet, ok := FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == data.CatchTime
})
if ok {
for i := 0; i < 4; i++ {
if onpet.SkillList[i].ID == data.HasSkill {
onpet.SkillList[i].ID = data.ReplaceSkill
onpet.SkillList[i].PP = uint32(xmlres.SkillMap[int(onpet.SkillList[i].ID)].MaxPP)
}
}
}
return &pet.ChangeSkillOutInfo{
CatchTime: data.CatchTime,
}, 0
}