feat(fight): 优化擂台战斗逻辑与状态管理 - 修改了擂台主人设置逻辑,引入 `Set` 方法统一处理玩家信息更新 - 增加对擂主是否可战斗的判断,防止无效挑战 - 调整连胜计算和广播机制,确保数据一致性 - 修复擂台挑战失败时的状态回滚问题 - 引入错误码替代硬编码返回值,提高代码可读性与维护性 - 统一访问擂台玩家的方式,移除冗余字段
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package player
|
||
|
||
import (
|
||
"blazing/common/socket/errorcode"
|
||
"blazing/logic/service/common"
|
||
"blazing/logic/service/fight/info"
|
||
"sync/atomic"
|
||
)
|
||
|
||
func (p *Player) JoinFight(fn func(p common.PlayerI) bool) errorcode.ErrorCode {
|
||
//加入队列前就开始判断一次
|
||
if !p.CanFight() {
|
||
|
||
return errorcode.ErrorCodes.ErrNoEligiblePokemon
|
||
|
||
}
|
||
if p.GetSpace().Owner.UserID == p.Info.UserID {
|
||
return errorcode.ErrorCodes.ErrSystemError
|
||
}
|
||
//修复发包进入,如果此时是擂台主
|
||
|
||
p.GetSpace().User.Range(func(key uint32, v common.PlayerI) bool {
|
||
|
||
if v.GetInfo().UserID != p.Info.UserID {
|
||
//确认是乱斗模式
|
||
|
||
if v.Getfightinfo() == p.Getfightinfo() && p.CanFight() {
|
||
|
||
// p.Fightinfo = nil //先将自身的准备信息置空
|
||
// //value.PVPinfo = nil
|
||
ttt := fn(v)
|
||
if ttt {
|
||
|
||
atomic.StoreUint32(&v.(*Player).Fightinfo.Mode, 0)
|
||
atomic.StoreUint32(&p.Fightinfo.Mode, 0)
|
||
|
||
}
|
||
|
||
// lw = value
|
||
return ttt //如果发起成功就停止,否则继续遍历队列
|
||
}
|
||
|
||
}
|
||
return false
|
||
})
|
||
return 0
|
||
}
|
||
|
||
func (p *Player) SendLoadPercent(b info.LoadPercentOutboundInfo) {
|
||
|
||
p.SendPack(common.NewTomeeHeader(2441, p.Info.UserID).Pack(&b)) //准备包由各自发,因为协议不一样
|
||
}
|
||
|
||
// 同意对战
|
||
// AgreeBattle 处理战斗邀请响应(同意/拒绝)
|
||
// 参数:
|
||
//
|
||
// userid:邀请者ID
|
||
// flag:0-拒绝,1-同意
|
||
// mode:战斗模式
|
||
//
|
||
// 返回:
|
||
//
|
||
// bool:是否成功(仅同意且符合条件时为true)
|
||
// common.PlayerI:对应的邀请者玩家(成功时有效)
|