Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV-1761: Clean up Ruleset interface #115

Merged
merged 3 commits into from
Oct 26, 2022
Merged

Conversation

robbles
Copy link
Contributor

@robbles robbles commented Oct 24, 2022

Removes and simplifies some of the Ruleset and RulesetBuilder interfaces, and integrates them into the CLI. This fixes some error handling in the CLI as well.

Ruleset now looks like this:

rules/ruleset.go

Lines 3 to 13 in 2766ad7

type Ruleset interface {
// Returns the name of the ruleset, if applicable.
Name() string
// Returns the settings used by the ruleset.
Settings() Settings
// Processes the next turn of the ruleset, returning whether the game has ended, the next BoardState, or an error.
// For turn zero (initialization), moves will be left empty.
Execute(prevState *BoardState, moves []SnakeMove) (gameOver bool, nextState *BoardState, err error)
}

This is a little less friendly than the old multiple method interface, but I'm planning on adding some helpers in the same release to make it simpler to take a ruleset and run a game one turn at a time.

And RulesetBuilder takes an explicit ruleset name through the NamedRuleset to construct one of the standard rulesets.

rules/ruleset.go

Lines 77 to 104 in 2766ad7

// NamedRuleset constructs a known ruleset by using name to look up a standard pipeline.
func (rb rulesetBuilder) NamedRuleset(name string) Ruleset {
var stages []string
if rb.solo {
stages = append(stages, StageGameOverSoloSnake)
} else {
stages = append(stages, StageGameOverStandard)
}
switch name {
case GameTypeStandard:
stages = append(stages, standardRulesetStages[1:]...)
case GameTypeConstrictor:
stages = append(stages, constrictorRulesetStages[1:]...)
case GameTypeWrappedConstrictor:
stages = append(stages, wrappedConstrictorRulesetStages[1:]...)
case GameTypeRoyale:
stages = append(stages, royaleRulesetStages[1:]...)
case GameTypeSolo:
stages = soloRulesetStages
case GameTypeWrapped:
stages = append(stages, wrappedRulesetStages[1:]...)
default:
name = GameTypeStandard
stages = append(stages, standardRulesetStages[1:]...)
}
return rb.PipelineRuleset(name, NewPipeline(stages...))
}

rules.Settings has also been converted into a generic set of string key-value pairs, with maps and pipeline stages now parsing out settings at runtime. This loses some type safety and adds some redundant parsing, but allows us to add new settings without them being coupled to the API. The standard set of settings exposed through client requests is preserved through client.RulesetSettings with a conversion helper.
https://github.com/BattlesnakeOfficial/rules/blob/2766ad70f4ff1ccc399539951c0972b922b9cf21/settings.go

@robbles robbles marked this pull request as ready for review October 25, 2022 21:40
@robbles robbles merged commit 3a2ef6e into rules-2.0 Oct 26, 2022
@robbles robbles deleted the DEV-1761-api-cleanup branch October 26, 2022 23:32
@robbles robbles mentioned this pull request Oct 28, 2022
robbles added a commit that referenced this pull request Oct 28, 2022
* DEV-1761: Clean up Ruleset interface (#115)

* remove legacy ruleset types and simplify ruleset interface

* remove unnecessary settings argument from Ruleset interface

* decouple rules.Settings from client API and store settings as strings

* DEV 1761: Add new BoardState and Point fields (#117)

* add Point.TTL, Point.Value, GameState and PointState to BoardState

* allow maps to access BoardState.GameState,PointState

* add PreUpdateBoard and refactor snail_mode with it

* fix bug where an extra turn was printed to the console

* fix formatting

* fix lint errors

Co-authored-by: JonathanArns <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant