2025-11-11 05:54:24 +00:00
|
|
|
|
package fight
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-19 16:11:02 +08:00
|
|
|
|
"blazing/common/socket/errorcode"
|
2025-11-11 05:54:24 +00:00
|
|
|
|
"blazing/cool"
|
2025-11-20 15:19:13 +08:00
|
|
|
|
"fmt"
|
2025-11-15 22:17:43 +00:00
|
|
|
|
|
2025-11-11 05:54:24 +00:00
|
|
|
|
"blazing/logic/service/common"
|
|
|
|
|
|
"blazing/logic/service/fight/action"
|
|
|
|
|
|
"blazing/logic/service/fight/info"
|
|
|
|
|
|
"blazing/logic/service/fight/input"
|
2025-11-15 01:53:51 +08:00
|
|
|
|
"blazing/modules/blazing/model"
|
2025-11-11 05:54:24 +00:00
|
|
|
|
"math/rand"
|
2025-11-11 15:21:45 +00:00
|
|
|
|
"sync"
|
2025-11-11 05:54:24 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type FightC struct {
|
2025-11-20 15:19:13 +08:00
|
|
|
|
ReadyInfo info.NoteReadyToFightInfo
|
|
|
|
|
|
Info info.Fightinfo
|
|
|
|
|
|
IsReady bool
|
|
|
|
|
|
ownerID uint32 // 战斗发起者ID
|
|
|
|
|
|
Our *input.Input //始终等于房主ID
|
|
|
|
|
|
Opp *input.Input //对手ID
|
|
|
|
|
|
Switch []*action.ActiveSwitchAction
|
|
|
|
|
|
startl sync.Once
|
2025-11-11 05:54:24 +00:00
|
|
|
|
rand *rand.Rand
|
|
|
|
|
|
StartTime time.Time
|
|
|
|
|
|
actionChan chan action.BattleActionI // 所有操作统一从这里进入
|
|
|
|
|
|
Round int //回合数
|
2025-11-11 11:45:09 +00:00
|
|
|
|
quit chan struct{}
|
2025-11-12 01:19:24 +08:00
|
|
|
|
over chan struct{}
|
2025-11-11 05:54:24 +00:00
|
|
|
|
First *input.Input
|
|
|
|
|
|
Second *input.Input
|
|
|
|
|
|
closefight bool
|
2025-11-11 15:21:45 +00:00
|
|
|
|
overl sync.Once
|
2025-11-13 02:43:00 +08:00
|
|
|
|
waittime int
|
2025-11-11 05:54:24 +00:00
|
|
|
|
info.FightOverInfo
|
2025-11-15 23:02:46 +00:00
|
|
|
|
//战斗结束的插装
|
|
|
|
|
|
callback func(*info.FightOverInfo)
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f *FightC) Ownerid() uint32 {
|
|
|
|
|
|
|
|
|
|
|
|
return f.ownerID
|
|
|
|
|
|
}
|
|
|
|
|
|
func (f *FightC) GetInputByPlayer(c common.PlayerI, isOpposite bool) *input.Input {
|
|
|
|
|
|
// 判断当前玩家是否为我方玩家
|
|
|
|
|
|
isOurPlayer := c.GetInfo().UserID == f.ownerID
|
|
|
|
|
|
|
|
|
|
|
|
// 当isOurPlayer与isOpposite值不同时返回我方,相同时返回对方
|
|
|
|
|
|
if isOurPlayer != isOpposite {
|
|
|
|
|
|
return f.Our
|
|
|
|
|
|
}
|
|
|
|
|
|
return f.Opp
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f *FightC) GetInputByAction(c action.BattleActionI, isOpposite bool) *input.Input {
|
|
|
|
|
|
// 判断动作所属玩家是否为我方
|
|
|
|
|
|
isOurAction := c.GetPlayerID() == f.Our.Player.GetInfo().UserID
|
|
|
|
|
|
|
|
|
|
|
|
// 根据isOpposite决定是否返回相反方向的输入
|
|
|
|
|
|
if isOurAction == !isOpposite {
|
|
|
|
|
|
return f.Our
|
|
|
|
|
|
}
|
|
|
|
|
|
return f.Opp
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 玩家使用技能
|
|
|
|
|
|
func (f *FightC) GetCurrPET(c common.PlayerI) *info.BattlePetEntity {
|
|
|
|
|
|
if f.Our.Player.GetInfo().UserID == c.GetInfo().UserID {
|
|
|
|
|
|
return f.Our.CurrentPet
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return f.Opp.CurrentPet
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
func (f *FightC) GetOpp(c common.PlayerI) *input.Input {
|
|
|
|
|
|
return f.GetInputByPlayer(c, true)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取随机数
|
|
|
|
|
|
func (f *FightC) GetRand() *rand.Rand {
|
|
|
|
|
|
|
|
|
|
|
|
return f.rand
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取随机数
|
|
|
|
|
|
func (f *FightC) IsFirst(play common.PlayerI) bool {
|
|
|
|
|
|
|
|
|
|
|
|
return f.First.Player == play
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 加载进度
|
|
|
|
|
|
func (f *FightC) LoadPercent(c common.PlayerI, percent int32) {
|
2025-11-19 16:11:02 +08:00
|
|
|
|
|
|
|
|
|
|
f.GetInputByPlayer(c, true).Player.SendPackCmd(2441, &info.LoadPercentOutboundInfo{
|
2025-11-11 05:54:24 +00:00
|
|
|
|
Id: c.GetInfo().UserID,
|
|
|
|
|
|
Percent: uint32(percent),
|
|
|
|
|
|
})
|
2025-11-18 20:52:04 +00:00
|
|
|
|
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-20 05:57:29 +08:00
|
|
|
|
func (f *FightC) initplayer(c common.PlayerI) (*input.Input, errorcode.ErrorCode) {
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-20 05:57:29 +08:00
|
|
|
|
if !c.CanFight() {
|
|
|
|
|
|
return nil, errorcode.ErrorCodes.ErrNoEligiblePokemon
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
in := input.NewInput(f, c)
|
|
|
|
|
|
in.AllPet = make([]*info.BattlePetEntity, 0)
|
|
|
|
|
|
in.InitAttackValue()
|
2025-11-11 05:54:24 +00:00
|
|
|
|
for i := 0; i < len(c.GetInfo().PetList); i++ {
|
2025-11-15 23:02:46 +00:00
|
|
|
|
in.AllPet = append(in.AllPet, info.CreateBattlePetEntity(c.GetInfo().PetList[i], f.rand))
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
in.SortPet()
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
|
|
|
|
|
switch f.Info.Mode {
|
|
|
|
|
|
case info.BattleMode.SINGLE_MODE:
|
2025-11-15 01:53:51 +08:00
|
|
|
|
in.AllPet = in.AllPet[:1]
|
|
|
|
|
|
case info.BattleMode.PET_MELEE:
|
|
|
|
|
|
in.AllPet = make([]*info.BattlePetEntity, 0)
|
|
|
|
|
|
for _, v := range RandomElfIDs(3) {
|
|
|
|
|
|
p := model.GenPetInfo(v, 24, -1, -1, -1, 100)
|
2025-11-20 21:37:37 +08:00
|
|
|
|
//p.CatchTime = uint32(v)
|
2025-11-20 05:57:29 +08:00
|
|
|
|
p.Update()
|
2025-11-20 21:37:37 +08:00
|
|
|
|
|
|
|
|
|
|
p = model.GenPetInfo(int(p.ID), 24, -1, -1, -1, 100)
|
|
|
|
|
|
//p.CalculatePetPane()
|
|
|
|
|
|
p.CatchTime = uint32(v)
|
2025-11-15 23:02:46 +00:00
|
|
|
|
in.AllPet = append(in.AllPet, info.CreateBattlePetEntity(*p, f.rand))
|
2025-11-15 01:53:51 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//in.AllPet = in.AllPet[:3]
|
2025-11-11 05:54:24 +00:00
|
|
|
|
default:
|
|
|
|
|
|
}
|
2025-11-13 02:43:00 +08:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
in.CurrentPet = in.AllPet[0]
|
2025-11-20 05:57:29 +08:00
|
|
|
|
return in, 0
|
2025-11-15 01:53:51 +08:00
|
|
|
|
}
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
// RandomElfIDs 从1-2000中随机抽取n个不重复的精灵ID
|
|
|
|
|
|
func RandomElfIDs(n int) []int {
|
|
|
|
|
|
if n <= 0 || n > 2000 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
// 用map记录已抽取的ID,避免重复
|
|
|
|
|
|
used := make(map[int]struct{}, n)
|
|
|
|
|
|
ids := make([]int, 0, n)
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
for len(ids) < n {
|
|
|
|
|
|
// 生成1-2000的随机数
|
|
|
|
|
|
id := rand.Intn(2000) + 1 // rand.Intn(2000)生成0-1999,+1后为1-2000
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
// 检查是否已抽取
|
|
|
|
|
|
if _, exists := used[id]; !exists {
|
|
|
|
|
|
used[id] = struct{}{}
|
|
|
|
|
|
ids = append(ids, id)
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
return ids
|
|
|
|
|
|
}
|
|
|
|
|
|
func initfightready(in *input.Input) (info.FightUserInfo, []info.ReadyFightPetInfo) {
|
|
|
|
|
|
t := make([]info.ReadyFightPetInfo, len(in.AllPet))
|
|
|
|
|
|
userindo := info.FightUserInfo{
|
|
|
|
|
|
UserID: in.UserID,
|
|
|
|
|
|
Nick: in.Player.GetInfo().Nick,
|
|
|
|
|
|
}
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
for i := 0; i < len(in.AllPet); i++ {
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
err := copier.CopyWithOption(&t[i], &in.AllPet[i].Info, copier.Option{IgnoreEmpty: true, DeepCopy: true})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
2025-11-12 01:19:24 +08:00
|
|
|
|
|
2025-11-15 01:53:51 +08:00
|
|
|
|
return userindo, t
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新战斗,邀请方和被邀请方,或者玩家和野怪方
|
2025-11-19 16:11:02 +08:00
|
|
|
|
func NewFight(p1, p2 common.PlayerI, fn func(*info.FightOverInfo)) (*FightC, errorcode.ErrorCode) {
|
2025-11-20 15:19:13 +08:00
|
|
|
|
|
|
|
|
|
|
fmt.Println("NewFight", p1.GetInfo().UserID)
|
2025-11-11 05:54:24 +00:00
|
|
|
|
f := &FightC{}
|
|
|
|
|
|
f.ownerID = p1.GetInfo().UserID
|
2025-11-18 20:52:04 +00:00
|
|
|
|
f.callback = fn //战斗结束的回调
|
2025-11-11 11:45:09 +00:00
|
|
|
|
f.quit = make(chan struct{})
|
2025-11-12 01:19:24 +08:00
|
|
|
|
f.over = make(chan struct{})
|
2025-11-11 05:54:24 +00:00
|
|
|
|
f.StartTime = time.Now()
|
|
|
|
|
|
seed := f.StartTime.UnixNano() ^ int64(p1.GetInfo().UserID) ^ int64(p2.GetInfo().UserID) // ^ int64(f.Round) // 用异或运算混合多维度信息
|
|
|
|
|
|
f.rand = rand.New(rand.NewSource(seed))
|
2025-11-16 20:30:17 +00:00
|
|
|
|
f.Info = p1.Getfightinfo()
|
2025-11-18 20:52:04 +00:00
|
|
|
|
|
|
|
|
|
|
//这里应该挪到玩家初始化执行
|
2025-11-16 20:30:17 +00:00
|
|
|
|
|
2025-11-19 16:11:02 +08:00
|
|
|
|
f.ReadyInfo.Status = f.Info.Status
|
2025-11-20 05:57:29 +08:00
|
|
|
|
var err errorcode.ErrorCode
|
|
|
|
|
|
f.Our, err = f.initplayer(p1)
|
|
|
|
|
|
if err > 0 {
|
|
|
|
|
|
return nil, err
|
2025-11-15 23:02:46 +00:00
|
|
|
|
|
2025-11-20 05:57:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
f.Opp, err = f.initplayer(p2)
|
|
|
|
|
|
if err > 0 {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-16 20:30:17 +00:00
|
|
|
|
f.ReadyInfo.OurInfo, f.ReadyInfo.OurPetList = initfightready(f.Our)
|
|
|
|
|
|
f.ReadyInfo.OpponentInfo, f.ReadyInfo.OpponentPetList = initfightready(f.Opp)
|
2025-11-21 02:40:27 +08:00
|
|
|
|
var loadtime time.Duration = 120 * time.Second
|
|
|
|
|
|
//说明是PVE
|
2025-11-23 00:06:14 +08:00
|
|
|
|
if f.Info.Status == info.BattleMode.FIGHT_WITH_NPC {
|
2025-11-15 01:53:51 +08:00
|
|
|
|
|
2025-11-15 13:20:42 +08:00
|
|
|
|
f.Opp.Finished = true //PVE 默认boss数据直接加载完成
|
2025-11-21 02:40:27 +08:00
|
|
|
|
loadtime = 60 * time.Second
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
2025-11-15 01:53:51 +08:00
|
|
|
|
|
2025-11-19 16:11:02 +08:00
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
|
|
|
|
|
ff.SetOPP(f.GetInputByPlayer(ff.Player, true))
|
|
|
|
|
|
|
|
|
|
|
|
})
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
|
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
|
|
|
|
|
|
2025-11-19 16:11:02 +08:00
|
|
|
|
ff.Player.SendPackCmd(2503, &f.ReadyInfo)
|
2025-11-11 05:54:24 +00:00
|
|
|
|
|
2025-11-13 02:43:00 +08:00
|
|
|
|
})
|
2025-11-20 05:57:29 +08:00
|
|
|
|
|
2025-11-21 02:40:27 +08:00
|
|
|
|
cool.Cron.AfterFunc(loadtime, func() {
|
2025-11-23 00:06:14 +08:00
|
|
|
|
fmt.Println(f.Our.UserID, "战斗超时结算")
|
2025-11-13 02:43:00 +08:00
|
|
|
|
if !f.Our.Finished || !f.Opp.Finished { //如果有任一没有加载完成
|
2025-11-18 20:52:04 +00:00
|
|
|
|
f.closefight = true //阻止继续添加action
|
2025-11-23 00:06:14 +08:00
|
|
|
|
f.Reason = info.BattleOverReason.PlayerOffline
|
2025-11-13 02:43:00 +08:00
|
|
|
|
switch {
|
|
|
|
|
|
case !f.Opp.Finished: //邀请方没加载完成 先判断邀请方,如果都没加载完成,就算做房主胜利
|
|
|
|
|
|
f.WinnerId = f.Our.Player.GetInfo().UserID
|
|
|
|
|
|
case !f.Our.Finished: //被邀请方没加载完成
|
|
|
|
|
|
f.WinnerId = f.Opp.Player.GetInfo().UserID
|
|
|
|
|
|
}
|
|
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
|
|
|
|
|
//todo 将血量和技能pp传回enterturn
|
|
|
|
|
|
|
2025-11-19 16:11:02 +08:00
|
|
|
|
ff.Player.SendPackCmd(2506, &f.FightOverInfo)
|
2025-11-18 22:16:55 +00:00
|
|
|
|
ff.Player.QuitFight()
|
2025-11-13 02:43:00 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-11 05:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-11-19 16:11:02 +08:00
|
|
|
|
return f, 0
|
2025-11-11 05:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 被击败的ID
|
|
|
|
|
|
func (b *FightC) IsWin(c *input.Input, cache uint32) bool {
|
|
|
|
|
|
|
|
|
|
|
|
var tt []*info.BattlePetEntity
|
|
|
|
|
|
bbb := b.Our.AllPet
|
|
|
|
|
|
|
|
|
|
|
|
if c.Player.GetInfo().UserID == b.ownerID { //如果是房主
|
|
|
|
|
|
bbb = b.Opp.AllPet
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range bbb {
|
|
|
|
|
|
if v.Info.CatchTime == cache {
|
|
|
|
|
|
v.NotAlive = true
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
tt = append(tt, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range tt {
|
|
|
|
|
|
if !v.NotAlive { //如果存活
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 广播,并是否结束回合
|
|
|
|
|
|
func (f *FightC) Broadcast(t func(ff *input.Input)) {
|
|
|
|
|
|
|
|
|
|
|
|
t(f.Our)
|
|
|
|
|
|
t(f.Opp)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-12 01:19:24 +08:00
|
|
|
|
|
|
|
|
|
|
func (f *FightC) GetOverChan() chan struct{} {
|
|
|
|
|
|
return f.over
|
|
|
|
|
|
|
|
|
|
|
|
}
|