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:
Torben 2022-04-19 15:52:57 -07:00 committed by GitHub
parent 86ef6ad068
commit d378759d58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 723 additions and 235 deletions

View file

@ -15,7 +15,7 @@ func TestRoyaleRulesetInterface(t *testing.T) {
func TestRoyaleDefaultSanity(t *testing.T) {
boardState := &BoardState{}
r := RoyaleRuleset{StandardRuleset: StandardRuleset{HazardDamagePerTurn: 1}, ShrinkEveryNTurns: 0}
_, err := r.CreateNextBoardState(boardState, []SnakeMove{})
_, err := r.CreateNextBoardState(boardState, []SnakeMove{{"", ""}})
require.Error(t, err)
require.Equal(t, errors.New("royale game can't shrink more frequently than every turn"), err)
@ -102,7 +102,7 @@ func TestRoyaleHazards(t *testing.T) {
ShrinkEveryNTurns: test.ShrinkEveryNTurns,
}
err := r.populateHazards(b)
_, err := PopulateHazardsRoyale(b, r.Settings(), mockSnakeMoves())
require.Equal(t, test.Error, err)
if err == nil {
// Obstacles should match
@ -131,7 +131,7 @@ func TestRoyalDamageNextTurn(t *testing.T) {
stateAfterTurn := func(prevState *BoardState, turn int32) *BoardState {
nextState := prevState.Clone()
nextState.Turn = turn - 1
err := r.populateHazards(nextState)
_, err := PopulateHazardsRoyale(nextState, r.Settings(), nil)
require.NoError(t, err)
nextState.Turn = turn
return nextState
@ -265,7 +265,16 @@ func TestRoyaleCreateNextBoardState(t *testing.T) {
ShrinkEveryNTurns: 1,
}
rand.Seed(0)
rb := NewRulesetBuilder().WithParams(map[string]string{
ParamGameType: GameTypeRoyale,
ParamHazardDamagePerTurn: "1",
ParamShrinkEveryNTurns: "1",
})
for _, gc := range cases {
gc.requireValidNextState(t, &r)
// also test a RulesBuilder constructed instance
gc.requireValidNextState(t, rb.Ruleset())
// also test a pipeline with the same settings
gc.requireValidNextState(t, rb.PipelineRuleset(GameTypeRoyale, NewPipeline(royaleRulesetStages...)))
}
}