feat(player): 添加 UseCoins 方法统一处理玩家金币消耗逻辑 重构购买物品和变更外观功能,使用 UseCoins 方法替代手动操作 Coins 字段, 确保金币扣除的安全性和一致性。同时修复可能因并发导致的金币超扣问题。 此外,调整部分战斗系统接口参数传递方式,将 DamageZone 指
272 lines
6.8 KiB
Go
272 lines
6.8 KiB
Go
package fight
|
|
|
|
import (
|
|
"blazing/common/data/xmlres"
|
|
"blazing/logic/service/common"
|
|
"blazing/logic/service/fight/action"
|
|
"blazing/logic/service/fight/info"
|
|
"blazing/logic/service/fight/input"
|
|
"blazing/logic/service/player"
|
|
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
func (f *FightC) battleLoop() {
|
|
|
|
f.actionChan = make(chan action.BattleActionI, 2)
|
|
fmt.Println("战斗开始精灵", f.Our.Player.GetInfo().PetList[0].CatchTime)
|
|
|
|
ourID := f.Our.Player.GetInfo().UserID
|
|
oppID := f.Opp.Player.GetInfo().UserID
|
|
|
|
for !f.closefight {
|
|
|
|
f.Round++
|
|
fmt.Printf("—— 第 %d 回合开始 ——\n", f.Round)
|
|
|
|
actions := f.collectPlayerActions(ourID, oppID)
|
|
if f.closefight {
|
|
|
|
break
|
|
}
|
|
f.resolveRound(actions[ourID], actions[oppID])
|
|
|
|
}
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
|
//todo 将血量和技能pp传回enterturn
|
|
|
|
ff.Player.SendFightEndInfo(f.FightOverInfo)
|
|
|
|
})
|
|
close(f.actionChan)
|
|
fmt.Println("战斗循环结束")
|
|
close(f.over)
|
|
|
|
}
|
|
|
|
// 收集玩家动作(含超时判定)
|
|
func (f *FightC) collectPlayerActions(ourID, oppID uint32) map[uint32]action.BattleActionI {
|
|
actions := make(map[uint32]action.BattleActionI)
|
|
|
|
waitr := time.Duration(f.waittime)*time.Millisecond*10 + 30*time.Second
|
|
fmt.Println("开始收集玩家动作", waitr)
|
|
timeout := time.After(waitr)
|
|
for len(actions) < 2 {
|
|
|
|
select {
|
|
case <-f.quit:
|
|
f.closefight = true
|
|
return actions
|
|
|
|
case paction, ok := <-f.actionChan:
|
|
if !ok {
|
|
f.closefight = true
|
|
return actions
|
|
}
|
|
if paction == nil {
|
|
continue
|
|
}
|
|
|
|
pid := paction.GetPlayerID()
|
|
if pid != ourID && pid != oppID {
|
|
continue
|
|
}
|
|
|
|
// 避免重复提交
|
|
if _, exists := actions[pid]; exists {
|
|
fmt.Printf("玩家%d 已经提交过动作,忽略重复\n", pid)
|
|
continue
|
|
}
|
|
|
|
// 被动切换处理(不计入本回合)
|
|
if as, ok := paction.(*action.ActiveSwitchAction); ok {
|
|
if f.GetInputByAction(as, false).CanChange {
|
|
f.GetInputByAction(as, false).CanChange = false
|
|
continue
|
|
}
|
|
}
|
|
|
|
// AI自动技能
|
|
if pid != 0 && (f.Info.Status == info.BattleStatus.FIGHT_WITH_BOSS ||
|
|
f.Info.Status == info.BattleStatus.FIGHT_WITH_NPC) {
|
|
f.GetInputByAction(paction, true).GetAction(f.Our)
|
|
//panic("AI自动技能")
|
|
}
|
|
|
|
actions[pid] = paction
|
|
//fmt.Println("玩家执行动作:", pid, paction.Priority())
|
|
|
|
case <-timeout:
|
|
f.handleTimeout(ourID, oppID, actions)
|
|
return actions
|
|
}
|
|
}
|
|
return actions
|
|
}
|
|
|
|
// 超时处理逻辑
|
|
func (f *FightC) handleTimeout(ourID, oppID uint32, actions map[uint32]action.BattleActionI) {
|
|
for _, pid := range []uint32{ourID, oppID} {
|
|
if _, exists := actions[pid]; exists {
|
|
continue
|
|
}
|
|
fmt.Printf("玩家%d 超时\n", pid)
|
|
// player := f.GetInputByPlayer(f.getPlayerByID(pid), true).Player
|
|
// if !f.GetInputByPlayer(f.getPlayerByID(pid), false).Finished {
|
|
// fmt.Printf("玩家%d 加载超时\n", pid)
|
|
// f.UseSkill(player, 0) //卡加载,给对方也一个action
|
|
// }
|
|
|
|
f.Reason = info.BattleOverReason.PlayerOVerTime
|
|
f.closefight = true
|
|
//对方赢
|
|
f.WinnerId = f.GetInputByPlayer(f.getPlayerByID(pid), true).Player.GetInfo().UserID
|
|
|
|
}
|
|
}
|
|
|
|
// 根据动作类型执行一回合结算
|
|
func (f *FightC) resolveRound(p1Action, p2Action action.BattleActionI) {
|
|
if p1Action == nil || p2Action == nil {
|
|
fmt.Println("某方未选择动作,自动跳过结算")
|
|
return
|
|
}
|
|
|
|
fmt.Println("开始结算回合")
|
|
|
|
// 动作优先级排序
|
|
b1, b2 := f.Compare(p1Action, p2Action)
|
|
|
|
switch a := b1.(type) {
|
|
|
|
case *action.ActiveSwitchAction:
|
|
|
|
if f.GetInputByAction(a, false).CurrentPet.Info.Hp <= 0 {
|
|
f.GetInputByAction(a, false).CurrentPet.Info.Hp = 1
|
|
}
|
|
if b2k, ok := b2.(*action.SelectSkillAction); ok {
|
|
f.waittime = b2k.CD
|
|
f.enterturn(b2.(*action.SelectSkillAction), nil)
|
|
} else {
|
|
|
|
f.enterturn(nil, nil)
|
|
}
|
|
|
|
case *action.UseItemAction:
|
|
f.handleItemAction(a)
|
|
if f.GetInputByAction(a, false).CurrentPet.Info.Hp <= 0 {
|
|
f.GetInputByAction(a, false).CurrentPet.Info.Hp = 1
|
|
}
|
|
if b2k, ok := b2.(*action.SelectSkillAction); ok {
|
|
f.waittime = b2k.CD
|
|
f.enterturn(b2.(*action.SelectSkillAction), nil)
|
|
} else {
|
|
if a1, ok := b2.(*action.UseItemAction); ok {
|
|
f.handleItemAction(a1)
|
|
|
|
}
|
|
f.enterturn(nil, nil)
|
|
}
|
|
|
|
default:
|
|
f.handleSkillActions(b1, b2)
|
|
}
|
|
}
|
|
|
|
// 使用道具的逻辑封装
|
|
func (f *FightC) handleItemAction(a *action.UseItemAction) {
|
|
|
|
item, ok := xmlres.ItemsMAP[int(a.ItemID)]
|
|
if !ok {
|
|
f.enterturn(nil, nil)
|
|
return
|
|
}
|
|
switch {
|
|
case gconv.Int(item.Bonus) != 0:
|
|
our, ok1 := f.Our.Player.(*player.Player)
|
|
opp, ok2 := f.Opp.Player.(*player.AI_player)
|
|
if ok1 && ok2 && opp.CanCapture {
|
|
ok, res := f.Our.Capture(f.Opp.CurrentPet, a.ItemID, -1)
|
|
if ok {
|
|
fmt.Println(res)
|
|
our.Service.PetAdd(*f.Opp.CurrentPet.Info)
|
|
our.CatchPetInfo(info.CatchMonsterOutboundInfo{
|
|
CatchTime: uint32(f.Opp.CurrentPet.Info.CatchTime),
|
|
PetId: uint32(f.Opp.CurrentPet.ID),
|
|
})
|
|
//f.WinnerId = 0 //捕捉成功不算胜利
|
|
|
|
f.closefight = true
|
|
} else {
|
|
our.CatchPetInfo(info.CatchMonsterOutboundInfo{})
|
|
}
|
|
}
|
|
case gconv.Int(item.HP) != 0:
|
|
addhp := item.HP
|
|
f.GetInputByAction(a, false).Exec(func(rr input.Effect) bool {
|
|
rr.Heal_Pre(a, &addhp)
|
|
|
|
return true
|
|
})
|
|
|
|
f.GetInputByAction(a, false).Heal(f.GetInputByAction(a, false), a, decimal.NewFromInt(int64(addhp)))
|
|
f.Broadcast(func(ff *input.Input) {
|
|
ff.Player.SendUsePetItemInfo(info.UsePetIteminfo{
|
|
UserID: f.GetInputByAction(a, false).UserID,
|
|
ChangeHp: int32(addhp),
|
|
ItemID: uint32(item.ID),
|
|
UserHp: uint32(f.GetInputByAction(a, false).CurrentPet.Info.Hp),
|
|
})
|
|
})
|
|
case gconv.Int(item.PP) != 0:
|
|
f.GetInputByAction(a, false).HealPP(item.PP)
|
|
f.Broadcast(func(ff *input.Input) {
|
|
ff.Player.SendUsePetItemInfo(info.UsePetIteminfo{
|
|
UserID: f.GetInputByAction(a, false).UserID,
|
|
|
|
ItemID: uint32(item.ID),
|
|
UserHp: uint32(f.GetInputByAction(a, false).CurrentPet.Info.Hp),
|
|
})
|
|
})
|
|
|
|
default:
|
|
|
|
fmt.Println(a.ItemID, "ItemID 不在指定范围内")
|
|
}
|
|
|
|
}
|
|
|
|
// 双方都是技能时的结算逻辑
|
|
func (f *FightC) handleSkillActions(a1, a2 action.BattleActionI) {
|
|
s1, _ := a1.(*action.SelectSkillAction)
|
|
s2, _ := a2.(*action.SelectSkillAction)
|
|
|
|
switch {
|
|
case s1 == nil || s1.SkillEntity == nil:
|
|
f.waittime = s2.CD
|
|
f.enterturn(s2, nil)
|
|
fmt.Println("1 空过 2玩家执行技能:", s2.PlayerID, s2.Info.ID)
|
|
case s2 == nil || s2.SkillEntity == nil:
|
|
f.waittime = s1.CD
|
|
f.enterturn(s1, nil)
|
|
fmt.Println("2 空过 玩家执行技能:", s1.PlayerID, s1.Info.ID)
|
|
default:
|
|
f.waittime = s1.CD + s2.CD
|
|
f.enterturn(s1, s2)
|
|
fmt.Println("玩家执行技能:", s1.PlayerID, s1.Info.ID, s2.PlayerID, s2.Info.ID)
|
|
}
|
|
}
|
|
|
|
// 根据玩家ID返回对应对象
|
|
func (f *FightC) getPlayerByID(id uint32) common.PlayerI {
|
|
if id == f.Our.Player.GetInfo().UserID {
|
|
return f.Our.Player
|
|
}
|
|
return f.Opp.Player
|
|
}
|