DEV-1761: New rules API (#118)

* 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 <jonathan.arns@googlemail.com>
This commit is contained in:
Rob O'Dwyer 2022-10-28 16:49:49 -07:00 committed by GitHub
parent 639362ef46
commit 82e1999126
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 1349 additions and 1610 deletions

90
settings.go Normal file
View file

@ -0,0 +1,90 @@
package rules
import "strconv"
// Settings contains all settings relevant to a game.
// The settings are stored as raw string values, which should not be accessed
// directly. Calling code should instead use the Int/Bool methods to parse them.
type Settings struct {
rawValues map[string]string
rand Rand
seed int64
}
func NewSettings(params map[string]string) Settings {
rawValues := make(map[string]string, len(params))
// Copy incoming params into a new map
for key, value := range params {
rawValues[key] = value
}
return Settings{
rawValues: rawValues,
}
}
func NewSettingsWithParams(params ...string) Settings {
rawValues := map[string]string{}
for index := 1; index < len(params); index += 2 {
rawValues[params[index-1]] = params[index]
}
return Settings{
rawValues: rawValues,
}
}
// Get a random number generator initialized based on the seed and current turn.
func (settings Settings) GetRand(turn int) Rand {
// Allow overriding the random generator for testing
if settings.rand != nil {
return settings.rand
}
if settings.seed != 0 {
return NewSeedRand(settings.seed + int64(turn))
}
// Default to global random number generator if neither seed or rand are set.
return GlobalRand
}
func (settings Settings) WithRand(rand Rand) Settings {
settings.rand = rand
return settings
}
func (settings Settings) Seed() int64 {
return settings.seed
}
func (settings Settings) WithSeed(seed int64) Settings {
settings.seed = seed
return settings
}
// Bool returns the boolean value for the specified parameter.
// If the parameter doesn't exist, the default value will be returned.
// If the parameter does exist, but is not "true", false will be returned.
func (settings Settings) Bool(paramName string, defaultValue bool) bool {
if val, ok := settings.rawValues[paramName]; ok {
return val == "true"
}
return defaultValue
}
// Int returns the int value for the specified parameter.
// If the parameter doesn't exist, the default value will be returned.
// If the parameter does exist, but is not a valid int, the default value will be returned.
func (settings Settings) Int(paramName string, defaultValue int) int {
if val, ok := settings.rawValues[paramName]; ok {
i, err := strconv.Atoi(val)
if err == nil {
return i
}
}
return defaultValue
}