DEV-1096 - add a new "pipeline" concept (#67)
* add a new "pipeline" concept - added new Pipeline type which is a series of stages - added a global registry to facilitate plugin architecture - 100% test coverage * Refactor rulesets to provide and use Pipeline * fix copypasta comments * fix lint for unused method * include game over stages in ruleset pipelines * clean up unused private standard methods * remove unused private methods in squad ruleset * remove unused private methods in royale ruleset * refactor: pipeline clone + return next board state * YAGNI: remove unused Append * refactor: improve stage names * add no-op behavior to stages for initial state * refactor: no-op decision within stage functions * remove misleading comment that isn't true * dont bother checking for init in gameover stages * remove redundant test * refactor: provide a combined ruleset/pipeline type * fix: movement no-op for GameOver check IsGameOver needs to run pipeline, move snakes needs to no-op for that * add test coverage * refactor: improve stage names and use constants * add Error method Support error checking before calling Execute() * update naming to be American style * panic when overwriting stages in global registry * rename "Error" method and improve docs * use testify lib for panic assertion * remove redundant food stage * use ruleset-specific logic for game over checks * re-work Pipeline errors * rework errors again * add defensive check for zero length snake * use old logic which checks current state, not next * add warning about how PipelineRuleset checks for game over
This commit is contained in:
parent
86ef6ad068
commit
d378759d58
18 changed files with 723 additions and 235 deletions
57
squad.go
57
squad.go
|
|
@ -4,6 +4,18 @@ import (
|
|||
"errors"
|
||||
)
|
||||
|
||||
var squadRulesetStages = []string{
|
||||
StageMovementStandard,
|
||||
StageStarvationStandard,
|
||||
StageHazardDamageStandard,
|
||||
StageFeedSnakesStandard,
|
||||
StageSpawnFoodStandard,
|
||||
StageEliminationStandard,
|
||||
StageEliminationResurrectSquadCollisions,
|
||||
StageModifySnakesShareAttributes,
|
||||
StageGameOverBySquad,
|
||||
}
|
||||
|
||||
type SquadRuleset struct {
|
||||
StandardRuleset
|
||||
|
||||
|
|
@ -18,23 +30,13 @@ type SquadRuleset struct {
|
|||
|
||||
func (r *SquadRuleset) Name() string { return GameTypeSquad }
|
||||
|
||||
func (r SquadRuleset) Execute(bs *BoardState, s Settings, sm []SnakeMove) (bool, *BoardState, error) {
|
||||
return NewPipeline(squadRulesetStages...).Execute(bs, s, sm)
|
||||
}
|
||||
|
||||
func (r *SquadRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
||||
nextBoardState, err := r.StandardRuleset.CreateNextBoardState(prevState, moves)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.resurrectSquadBodyCollisions(nextBoardState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.shareSquadAttributes(nextBoardState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nextBoardState, nil
|
||||
_, nextState, err := r.Execute(prevState, r.Settings(), moves)
|
||||
return nextState, err
|
||||
}
|
||||
|
||||
func areSnakesOnSameSquad(squadMap map[string]string, snake *Snake, other *Snake) bool {
|
||||
|
|
@ -45,12 +47,10 @@ func areSnakeIDsOnSameSquad(squadMap map[string]string, snakeID string, otherID
|
|||
return squadMap[snakeID] == squadMap[otherID]
|
||||
}
|
||||
|
||||
func (r *SquadRuleset) resurrectSquadBodyCollisions(b *BoardState) error {
|
||||
_, err := r.callStageFunc(ResurrectSnakesSquad, b, []SnakeMove{})
|
||||
return err
|
||||
}
|
||||
|
||||
func ResurrectSnakesSquad(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
||||
if IsInitialization(b, settings, moves) {
|
||||
return false, nil
|
||||
}
|
||||
if !settings.SquadSettings.AllowBodyCollisions {
|
||||
return false, nil
|
||||
}
|
||||
|
|
@ -71,12 +71,10 @@ func ResurrectSnakesSquad(b *BoardState, settings Settings, moves []SnakeMove) (
|
|||
return false, nil
|
||||
}
|
||||
|
||||
func (r *SquadRuleset) shareSquadAttributes(b *BoardState) error {
|
||||
_, err := r.callStageFunc(ShareAttributesSquad, b, []SnakeMove{})
|
||||
return err
|
||||
}
|
||||
|
||||
func ShareAttributesSquad(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
||||
if IsInitialization(b, settings, moves) {
|
||||
return false, nil
|
||||
}
|
||||
squadSettings := settings.SquadSettings
|
||||
|
||||
if !(squadSettings.SharedElimination || squadSettings.SharedLength || squadSettings.SharedHealth) {
|
||||
|
|
@ -120,7 +118,7 @@ func ShareAttributesSquad(b *BoardState, settings Settings, moves []SnakeMove) (
|
|||
}
|
||||
|
||||
func (r *SquadRuleset) IsGameOver(b *BoardState) (bool, error) {
|
||||
return r.callStageFunc(GameOverSquad, b, []SnakeMove{})
|
||||
return GameOverSquad(b, r.Settings(), nil)
|
||||
}
|
||||
|
||||
func GameOverSquad(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
||||
|
|
@ -152,8 +150,3 @@ func (r SquadRuleset) Settings() Settings {
|
|||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Adaptor for integrating stages into SquadRuleset
|
||||
func (r *SquadRuleset) callStageFunc(stage StageFunc, boardState *BoardState, moves []SnakeMove) (bool, error) {
|
||||
return stage(boardState, r.Settings(), moves)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue