refactor: 重、、、、
This commit is contained in:
101
logic/service/fight/new_options.go
Normal file
101
logic/service/fight/new_options.go
Normal file
@@ -0,0 +1,101 @@
|
||||
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)
|
||||
|
||||
type fightBuildOptions struct {
|
||||
owner common.PlayerI
|
||||
opponent common.PlayerI
|
||||
|
||||
ourPlayers []common.PlayerI
|
||||
oppPlayers []common.PlayerI
|
||||
|
||||
ourPets []model.PetInfo
|
||||
oppPets []model.PetInfo
|
||||
|
||||
ourInputs []*input.Input
|
||||
oppInputs []*input.Input
|
||||
|
||||
callback func(model.FightOverInfo)
|
||||
startTime time.Time
|
||||
fightInfo *info.Fightinfo
|
||||
}
|
||||
|
||||
func defaultFightBuildOptions() *fightBuildOptions {
|
||||
return &fightBuildOptions{
|
||||
startTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func WithFightPlayers(owner, opponent common.PlayerI) FightOption {
|
||||
return func(opts *fightBuildOptions) {
|
||||
opts.owner = owner
|
||||
opts.opponent = opponent
|
||||
}
|
||||
}
|
||||
|
||||
func WithFightPets(ourPets, oppPets []model.PetInfo) FightOption {
|
||||
return func(opts *fightBuildOptions) {
|
||||
opts.ourPets = ourPets
|
||||
opts.oppPets = oppPets
|
||||
}
|
||||
}
|
||||
|
||||
func WithFightInputs(ourInputs, oppInputs []*input.Input) FightOption {
|
||||
return func(opts *fightBuildOptions) {
|
||||
opts.ourInputs = ourInputs
|
||||
opts.oppInputs = oppInputs
|
||||
}
|
||||
}
|
||||
|
||||
func WithFightPlayersOnSide(ourPlayers, oppPlayers []common.PlayerI) FightOption {
|
||||
return func(opts *fightBuildOptions) {
|
||||
opts.ourPlayers = ourPlayers
|
||||
opts.oppPlayers = oppPlayers
|
||||
}
|
||||
}
|
||||
|
||||
func WithFightCallback(fn func(model.FightOverInfo)) FightOption {
|
||||
return func(opts *fightBuildOptions) {
|
||||
opts.callback = fn
|
||||
}
|
||||
}
|
||||
|
||||
func WithFightInfo(fightInfo info.Fightinfo) FightOption {
|
||||
return func(opts *fightBuildOptions) {
|
||||
opts.fightInfo = &fightInfo
|
||||
}
|
||||
}
|
||||
|
||||
func WithFightStartTime(startTime time.Time) FightOption {
|
||||
return func(opts *fightBuildOptions) {
|
||||
opts.startTime = startTime
|
||||
}
|
||||
}
|
||||
|
||||
func NewFightWithOptions(opts ...FightOption) (*FightC, errorcode.ErrorCode) {
|
||||
buildOpts := defaultFightBuildOptions()
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
opt(buildOpts)
|
||||
}
|
||||
}
|
||||
return buildFight(buildOpts)
|
||||
}
|
||||
|
||||
func (o *fightBuildOptions) normalizePlayers() {
|
||||
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}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user