2026-04-04 06:11:01 +08:00
|
|
|
|
package fight
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"blazing/common/socket/errorcode"
|
|
|
|
|
|
"blazing/logic/service/common"
|
|
|
|
|
|
"blazing/logic/service/fight/info"
|
|
|
|
|
|
"blazing/logic/service/fight/input"
|
|
|
|
|
|
"blazing/modules/player/model"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type FightOption func(*fightBuildOptions)
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
const (
|
|
|
|
|
|
// InputControllerBindingKeep 保持输入中现有的 Player 绑定,不做覆盖。
|
|
|
|
|
|
InputControllerBindingKeep = iota
|
|
|
|
|
|
// InputControllerBindingSingle 单侧全部站位由 players[0] 控制(双打单人多站位)。
|
|
|
|
|
|
InputControllerBindingSingle
|
|
|
|
|
|
// InputControllerBindingPerSlot 单侧按站位顺序绑定 players[i](组队每人一个站位)。
|
|
|
|
|
|
InputControllerBindingPerSlot
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-04-04 06:11:01 +08:00
|
|
|
|
type fightBuildOptions struct {
|
|
|
|
|
|
owner common.PlayerI
|
|
|
|
|
|
opponent common.PlayerI
|
|
|
|
|
|
|
|
|
|
|
|
ourPlayers []common.PlayerI
|
|
|
|
|
|
oppPlayers []common.PlayerI
|
|
|
|
|
|
|
|
|
|
|
|
ourInputs []*input.Input
|
|
|
|
|
|
oppInputs []*input.Input
|
|
|
|
|
|
|
|
|
|
|
|
callback func(model.FightOverInfo)
|
|
|
|
|
|
startTime time.Time
|
|
|
|
|
|
fightInfo *info.Fightinfo
|
2026-04-05 00:03:32 +08:00
|
|
|
|
|
|
|
|
|
|
controllerBinding int
|
2026-04-08 01:28:55 +08:00
|
|
|
|
legacyGroupProtocol bool
|
2026-04-04 06:11:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// defaultFightBuildOptions 返回建战默认参数。
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func defaultFightBuildOptions() *fightBuildOptions {
|
|
|
|
|
|
return &fightBuildOptions{
|
2026-04-05 00:03:32 +08:00
|
|
|
|
startTime: time.Now(),
|
|
|
|
|
|
controllerBinding: InputControllerBindingKeep,
|
2026-04-04 06:11:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// WithFightInputs 注入双方站位输入(当前建战主路径)。
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func WithFightInputs(ourInputs, oppInputs []*input.Input) FightOption {
|
|
|
|
|
|
return func(opts *fightBuildOptions) {
|
|
|
|
|
|
opts.ourInputs = ourInputs
|
|
|
|
|
|
opts.oppInputs = oppInputs
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// WithFightPlayersOnSide 显式指定双方操作者列表(可选)。
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func WithFightPlayersOnSide(ourPlayers, oppPlayers []common.PlayerI) FightOption {
|
|
|
|
|
|
return func(opts *fightBuildOptions) {
|
|
|
|
|
|
opts.ourPlayers = ourPlayers
|
|
|
|
|
|
opts.oppPlayers = oppPlayers
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// WithFightCallback 设置战斗结束回调。
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func WithFightCallback(fn func(model.FightOverInfo)) FightOption {
|
|
|
|
|
|
return func(opts *fightBuildOptions) {
|
|
|
|
|
|
opts.callback = fn
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// WithFightInfo 覆盖战斗信息(模式/状态等)。
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func WithFightInfo(fightInfo info.Fightinfo) FightOption {
|
|
|
|
|
|
return func(opts *fightBuildOptions) {
|
|
|
|
|
|
opts.fightInfo = &fightInfo
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// WithFightStartTime 指定战斗创建时间(测试/回放可用)。
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func WithFightStartTime(startTime time.Time) FightOption {
|
|
|
|
|
|
return func(opts *fightBuildOptions) {
|
|
|
|
|
|
opts.startTime = startTime
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// WithInputControllerBinding 设置站位控制绑定策略。
|
|
|
|
|
|
func WithInputControllerBinding(mode int) FightOption {
|
|
|
|
|
|
return func(opts *fightBuildOptions) {
|
|
|
|
|
|
opts.controllerBinding = mode
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 01:28:55 +08:00
|
|
|
|
// WithLegacyGroupProtocol 设置是否使用旧组队协议 CMD。
|
|
|
|
|
|
func WithLegacyGroupProtocol(enabled bool) FightOption {
|
|
|
|
|
|
return func(opts *fightBuildOptions) {
|
|
|
|
|
|
opts.legacyGroupProtocol = enabled
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func NewFightWithOptions(opts ...FightOption) (*FightC, errorcode.ErrorCode) {
|
|
|
|
|
|
buildOpts := defaultFightBuildOptions()
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
|
if opt != nil {
|
|
|
|
|
|
opt(buildOpts)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return buildFight(buildOpts)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 00:03:32 +08:00
|
|
|
|
// uniquePlayersFromInputs 从输入中提取去重后的操作者列表。
|
|
|
|
|
|
func uniquePlayersFromInputs(inputs []*input.Input) []common.PlayerI {
|
|
|
|
|
|
if len(inputs) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
seen := make(map[uint32]struct{}, len(inputs))
|
|
|
|
|
|
players := make([]common.PlayerI, 0, len(inputs))
|
|
|
|
|
|
for _, in := range inputs {
|
|
|
|
|
|
if in == nil || in.Player == nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
userID := in.Player.GetInfo().UserID
|
|
|
|
|
|
if _, ok := seen[userID]; ok {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
seen[userID] = struct{}{}
|
|
|
|
|
|
players = append(players, in.Player)
|
|
|
|
|
|
}
|
|
|
|
|
|
return players
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// normalizePlayers 归一化 owner/opponent 与双方 players。
|
|
|
|
|
|
// 若未显式传 players,会尝试从 inputs 中自动提取。
|
2026-04-04 06:11:01 +08:00
|
|
|
|
func (o *fightBuildOptions) normalizePlayers() {
|
2026-04-05 00:03:32 +08:00
|
|
|
|
if len(o.ourPlayers) == 0 && len(o.ourInputs) > 0 {
|
|
|
|
|
|
o.ourPlayers = uniquePlayersFromInputs(o.ourInputs)
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(o.oppPlayers) == 0 && len(o.oppInputs) > 0 {
|
|
|
|
|
|
o.oppPlayers = uniquePlayersFromInputs(o.oppInputs)
|
|
|
|
|
|
}
|
2026-04-04 06:11:01 +08:00
|
|
|
|
if len(o.ourPlayers) == 0 && o.owner != nil {
|
|
|
|
|
|
o.ourPlayers = []common.PlayerI{o.owner}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(o.oppPlayers) == 0 && o.opponent != nil {
|
|
|
|
|
|
o.oppPlayers = []common.PlayerI{o.opponent}
|
|
|
|
|
|
}
|
2026-04-05 00:03:32 +08:00
|
|
|
|
if o.owner == nil && len(o.ourPlayers) > 0 {
|
|
|
|
|
|
o.owner = o.ourPlayers[0]
|
|
|
|
|
|
}
|
|
|
|
|
|
if o.opponent == nil && len(o.oppPlayers) > 0 {
|
|
|
|
|
|
o.opponent = o.oppPlayers[0]
|
|
|
|
|
|
}
|
2026-04-04 06:11:01 +08:00
|
|
|
|
}
|