refactor(controller): 重构控制器代码结构并优化战斗状态检查

- 添加包级注释说明controller包的功能和架构设计
- 重命名Controller结构体注释,使其更清晰明了
- 添加ParseCmd函数的
This commit is contained in:
2025-12-23 10:46:17 +08:00
parent 83ee9fba43
commit 9baca27033
11 changed files with 195 additions and 150 deletions

View File

@@ -9,87 +9,91 @@ import (
"blazing/logic/service/player"
)
// 准备战斗
func (h Controller) OnReadyToFight(data *fight.ReadyToFightInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
// checkFightStatus 检查战斗状态
func (h Controller) checkFightStatus(c *player.Player) errorcode.ErrorCode {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
return errorcode.ErrorCodes.ErrBattleEnded
}
return 0
}
// OnReadyToFight 准备战斗
func (h Controller) OnReadyToFight(data *fight.ReadyToFightInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
defer c.FightC.ReadyFight(c)
return nil, -1
}
// 使用技能包
// UseSkill 使用技能包
func (h Controller) UseSkill(data *fight.UseSkillInInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
defer c.FightC.UseSkill(c, data.SkillId)
return nil, 0
}
// 战斗逃跑
// Escape 战斗逃跑
func (h Controller) Escape(data *fight.EscapeFightInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
if atomic.LoadUint32(&c.Fightinfo.Mode) == 0 {
return nil, errorcode.ErrorCodes.ErrBattleNotStarted //,没开始对战
battleMode := atomic.LoadUint32(&c.Fightinfo.Mode)
if battleMode == 0 {
return nil, errorcode.ErrorCodes.ErrBattleNotStarted
}
if atomic.LoadUint32(&c.Fightinfo.Mode) == 1 { //用户对战不能逃跑
if battleMode == 1 {
return nil, errorcode.ErrorCodes.ErrCannotFleePlayerBattle
}
defer c.FightC.Over(c, info.BattleOverReason.PlayerEscape)
return nil, 0
}
// 切换精灵
// ChangePet 切换精灵
func (h Controller) ChangePet(data *fight.ChangePetInboundInfo, c *player.Player) (result *info.ChangePetInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
defer c.FightC.ChangePet(c, data.CatchTime)
return nil, -1
}
// 切换精灵
// Capture 捕捉精灵
func (h Controller) Capture(data *fight.CatchMonsterInboundInfo, c *player.Player) (result *info.CatchMonsterOutboundInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
defer c.FightC.Capture(c, data.CapsuleId)
return nil, -1
}
// 加载进度
// LoadPercent 加载进度
func (h Controller) LoadPercent(data *fight.LoadPercentInboundInfo, c *player.Player) (result *info.LoadPercentOutboundInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, -1
}
defer c.FightC.LoadPercent(c, int32(data.Percent))
return nil, -1
}
// UsePetItemInboundInfo 使用宠物道具
func (h Controller) UsePetItemInboundInfo(data *fight.UsePetItemInboundInfo, c *player.Player) (result *info.UsePetIteminfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
defer c.FightC.UseItem(c, data.CatchTime, data.ItemId)
return nil, -1
}
// FightChat 战斗聊天
func (h Controller) FightChat(data *fight.ChatInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
defer c.FightC.Chat(c, data.Message)
return nil, -1
}