feat(fight): 优化擂台战斗逻辑与状态管理 - 修改了擂台主人设置逻辑,引入 `Set` 方法统一处理玩家信息更新 - 增加对擂主是否可战斗的判断,防止无效挑战 - 调整连胜计算和广播机制,确保数据一致性 - 修复擂台挑战失败时的状态回滚问题 - 引入错误码替代硬编码返回值,提高代码可读性与维护性 - 统一访问擂台玩家的方式,移除冗余字段
161 lines
5.4 KiB
Go
161 lines
5.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"blazing/common/socket/errorcode"
|
|
"sync/atomic"
|
|
|
|
"blazing/logic/service/fight"
|
|
"blazing/logic/service/fight/info"
|
|
"blazing/logic/service/player"
|
|
"blazing/logic/service/space"
|
|
)
|
|
|
|
// public static const ARENA_SET_OWENR:uint = 2417;
|
|
// 如果星际擂台上无人,站到星际擂台的包
|
|
// 前端到后端无数据内容 空包
|
|
// 后端到前端无数据内容 空包
|
|
// 都需要通过2419包广播更新擂台状态
|
|
func (h Controller) ARENA_SET_OWENR(data *fight.ARENA_SET_OWENR, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
|
|
|
if !c.CanFight() {
|
|
return nil, errorcode.ErrorCodes.ErrPokemonNotEligible
|
|
}
|
|
c.Fightinfo.Mode = 0 //取消队列匹配
|
|
if atomic.CompareAndSwapUint32(&c.GetSpace().Owner.Flag, 0, 1) {
|
|
|
|
c.GetSpace().Owner.Set(c)
|
|
c.GetSpace().Broadcast(c, 2419, c.GetSpace().Owner)
|
|
c.SendPackCmd(2419, c.GetSpace().Owner)
|
|
return
|
|
}
|
|
|
|
return nil, errorcode.ErrorCodes.ErrChampionExists
|
|
}
|
|
|
|
// public static const ARENA_FIGHT_OWENR:uint = 2418;
|
|
// 挑战擂台的包
|
|
// 前端到后端无数据内容 空包
|
|
// 后端到前端无数据内容 空包
|
|
// 还是后端主动发送2503的包给双方前端后 等待前端加载完毕 主动发送2404包通知后端开始战斗
|
|
// 并不会通知对方是否接受挑战。只要有人挑战就直接进入对战
|
|
func (h Controller) ARENA_FIGHT_OWENR(data *fight.ARENA_FIGHT_OWENR, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
|
|
|
if !c.CanFight() {
|
|
return nil, errorcode.ErrorCodes.ErrNoEligiblePokemon
|
|
}
|
|
|
|
if c.Info.UserID == c.GetSpace().Owner.UserID {
|
|
return nil, errorcode.ErrorCodes.ErrNoEligiblePokemon
|
|
}
|
|
|
|
if c.GetSpace().Owner.ARENA_Player == nil {
|
|
return nil, errorcode.ErrorCodes.ErrSystemError200007
|
|
}
|
|
|
|
if !c.GetSpace().Owner.ARENA_Player.CanFight() {
|
|
c.GetSpace().Owner.Set(c)
|
|
c.GetSpace().Broadcast(c, 2419, &c.GetSpace().Owner)
|
|
c.SendPackCmd(2419, &c.GetSpace().Owner)
|
|
return
|
|
}
|
|
//原子操作,修改擂台状态
|
|
if atomic.CompareAndSwapUint32(&c.GetSpace().Owner.Flag, 1, 2) {
|
|
//成功发起擂台挑战后才修改我放状态
|
|
c.Fightinfo.Mode = info.BattleMode.SINGLE_MODE
|
|
c.Fightinfo.Status = info.BattleMode.FIGHT_ARENA
|
|
|
|
_, err = fight.NewFight(c, c.GetSpace().Owner.ARENA_Player, func(foi *info.FightOverInfo) { //我方邀请擂主挑战,我方先手
|
|
|
|
if foi.Reason != 0 && foi.WinnerId == c.GetInfo().UserID { //异常退出
|
|
|
|
c.GetSpace().Owner.Reset()
|
|
|
|
c.GetSpace().Broadcast(c, 2419, &c.GetSpace().Owner)
|
|
c.SendPackCmd(2419, &c.GetSpace().Owner)
|
|
|
|
}
|
|
|
|
if foi.Reason == 0 { //异常退出
|
|
|
|
if foi.WinnerId == c.GetInfo().UserID {
|
|
c.Info.MaxArenaWins += 1
|
|
} else {
|
|
c.GetSpace().Owner.ARENA_Player.GetInfo().MaxArenaWins += 1
|
|
}
|
|
|
|
}
|
|
//todo 擂主输了,那就直接让他放弃擂台
|
|
|
|
}) ///开始对战,房主方以及被邀请方
|
|
|
|
if err <= 0 { //发起战斗成功
|
|
atomic.StoreUint32(&c.GetSpace().Owner.ChallengerID, c.GetInfo().UserID) //传回的指针赋值给ID
|
|
//c.GetSpace().Broadcast(c, 2419, &c.GetSpace().Owner)
|
|
//c.SendPackCmd(2419, &c.GetSpace().Owner)
|
|
} else {
|
|
|
|
atomic.StoreUint32(&c.GetSpace().Owner.Flag, 1)
|
|
|
|
}
|
|
c.GetSpace().Broadcast(c, 2419, &c.GetSpace().Owner)
|
|
c.SendPackCmd(2419, &c.GetSpace().Owner)
|
|
return
|
|
}
|
|
return nil, errorcode.ErrorCodes.ErrChampionExists
|
|
|
|
}
|
|
|
|
// 获取星际擂台信息的包 进入空间站地图前端会发送请求包 或者 有人站到星际擂台上后 广播回包
|
|
// 前端到后端无数据内容
|
|
// 后端到前端
|
|
func (h Controller) ARENA_GET_INFO(data *fight.ARENA_GET_INFO, c *player.Player) (result *space.ARENA, err errorcode.ErrorCode) {
|
|
|
|
result = &c.GetSpace().Owner
|
|
return
|
|
}
|
|
|
|
// public static const ARENA_UPFIGHT:uint = 2420;
|
|
// 放弃擂台挑战的包
|
|
// 前端到后端无数据内容
|
|
// 后端到前端无数据内容
|
|
// 都需要通过2419包广播更新擂台状态
|
|
func (h Controller) ARENA_UPFIGHT(data *fight.ARENA_UPFIGHT, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
|
//原子操作,修改擂台状态
|
|
if atomic.LoadUint32(&c.GetSpace().Owner.UserID) != c.GetInfo().UserID { //说明已经有人了
|
|
return nil, errorcode.ErrorCodes.ErrChampionCannotCancel
|
|
}
|
|
if atomic.CompareAndSwapUint32(&c.GetSpace().Owner.UserID, c.GetInfo().UserID, 0) {
|
|
|
|
c.GetSpace().Owner.Reset()
|
|
|
|
c.GetSpace().Broadcast(c, 2419, &c.GetSpace().Owner)
|
|
c.SendPackCmd(2419, &c.GetSpace().Owner)
|
|
return nil, -1
|
|
}
|
|
return nil, errorcode.ErrorCodes.ErrChampionCannotCancel
|
|
|
|
}
|
|
|
|
// public static const ARENA_OWENR_ACCE:uint = 2422;
|
|
// 此包为擂台战对战结束后 胜方前端会发送给后端 具体作用为通知后端发送2419包更新擂台信息。
|
|
// 前端到后端无数据内容
|
|
// 后端到前端无数据内容
|
|
// public static const ARENA_OWENR_OUT:uint = 2423;
|
|
// 此包不清楚具体怎么触发 但已知此包为后端主动发送。不清楚什么情况下回用到
|
|
func (h Controller) ARENA_OWENR_ACCE(data *fight.ARENA_OWENR_ACCE, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
|
|
|
s := c.GetSpace()
|
|
|
|
if atomic.LoadUint32(&c.GetSpace().Owner.UserID) != c.GetInfo().UserID && c.GetInfo().UserID != atomic.LoadUint32(&c.GetSpace().Owner.ChallengerID) { //说明已经有人了
|
|
return nil, errorcode.ErrorCodes.ErrChampionCannotCancel
|
|
}
|
|
s.Owner.Set(c)
|
|
|
|
atomic.StoreUint32(&s.Owner.Flag, 1)
|
|
|
|
s.Broadcast(c, 2419, &s.Owner)
|
|
c.SendPackCmd(2419, &c.GetSpace().Owner)
|
|
|
|
return nil, -1
|
|
}
|