refactor: load/save state as ptr
This commit is contained in:
@@ -15,15 +15,15 @@ type Repo interface {
|
||||
Release() error
|
||||
|
||||
// SaveTurn stores just generated new turn
|
||||
SaveTurn(uint, game.Game) error
|
||||
SaveTurn(uint, *game.Game) error
|
||||
|
||||
// SaveState stores current game state updated between turns
|
||||
SaveState(game.Game) error
|
||||
SaveState(*game.Game) error
|
||||
|
||||
// LoadState retrieves game current state
|
||||
LoadState() (game.Game, error)
|
||||
LoadState() (*game.Game, error)
|
||||
|
||||
LoadStateSafe() (game.Game, error)
|
||||
LoadStateSafe() (*game.Game, error)
|
||||
}
|
||||
|
||||
type Controller struct {
|
||||
@@ -62,7 +62,7 @@ func (c *Controller) ExecuteState(consumer func(Repo)) error {
|
||||
return c.Repo.Release()
|
||||
}
|
||||
|
||||
func (c *Controller) ExecuteGame(consumer func(Repo, game.Game)) error {
|
||||
func (c *Controller) ExecuteGame(consumer func(Repo, *game.Game)) error {
|
||||
if err := c.Repo.Lock(); err != nil {
|
||||
return fmt.Errorf("execute: lock failed: %s", err)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func newGameOnMap(r Repo, races []string, m generator.Map) (uuid.UUID, error) {
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
if err := r.SaveTurn(0, *g); err != nil {
|
||||
if err := r.SaveTurn(0, g); err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
return g.ID, nil
|
||||
|
||||
@@ -7,14 +7,14 @@ import (
|
||||
|
||||
func JoinEqualGroups(configure func(*controller.Param), race string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = joinEqualGroups(r, g, race)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func joinEqualGroups(r controller.Repo, g game.Game, race string) error {
|
||||
func joinEqualGroups(r controller.Repo, g *game.Game, race string) error {
|
||||
if err := g.JoinEqualGroups(race); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestJoinEqualGroups(t *testing.T) {
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
err := game.JoinEqualGroups(p, "race_01")
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
@@ -7,14 +7,14 @@ import (
|
||||
|
||||
func RenamePlanet(configure func(*controller.Param), race string, number int, name string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = renamePlanet(r, g, race, number, name)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func renamePlanet(r controller.Repo, g game.Game, race string, number int, name string) error {
|
||||
func renamePlanet(r controller.Repo, g *game.Game, race string, number int, name string) error {
|
||||
if err := g.RenamePlanet(race, number, name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func TestRenamePlanet(t *testing.T) {
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
cg := g()
|
||||
var number int
|
||||
var owner uuid.UUID
|
||||
|
||||
@@ -7,14 +7,14 @@ import (
|
||||
|
||||
func PlanetProduction(configure func(*controller.Param), race string, planetNumber int, prodType, subject string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = planetProduction(r, g, race, planetNumber, prodType, subject)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func planetProduction(r controller.Repo, g game.Game, race string, planetNumber int, prodType, subject string) error {
|
||||
func planetProduction(r controller.Repo, g *game.Game, race string, planetNumber int, prodType, subject string) error {
|
||||
if err := g.PlanetProduction(race, planetNumber, prodType, subject); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ import (
|
||||
|
||||
func CreateScience(configure func(*controller.Param), race, typeName string, drive, weapons, shields, cargo float64) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = createScience(r, g, race, typeName, drive, weapons, shields, cargo)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func createScience(r controller.Repo, g game.Game, race, typeName string, d, w, s, c float64) error {
|
||||
func createScience(r controller.Repo, g *game.Game, race, typeName string, d, w, s, c float64) error {
|
||||
if err := g.CreateScience(race, typeName, d, w, s, c); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -23,14 +23,14 @@ func createScience(r controller.Repo, g game.Game, race, typeName string, d, w,
|
||||
|
||||
func DeleteScience(configure func(*controller.Param), race, typeName string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = deleteScience(r, g, race, typeName)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func deleteScience(r controller.Repo, g game.Game, race, typeName string) error {
|
||||
func deleteScience(r controller.Repo, g *game.Game, race, typeName string) error {
|
||||
if err := g.DeleteScience(race, typeName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func TestCreateScience(t *testing.T) {
|
||||
race := "race_01"
|
||||
typeName := "First Step"
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
err := game.DeleteScience(p, race, typeName)
|
||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityNotExists))
|
||||
err = game.CreateScience(p, unknownRaceName, " "+typeName+" ", 1, 0, 0, 0) // TODO: test on dead race
|
||||
@@ -74,7 +74,7 @@ func TestCreateScienceValidation(t *testing.T) {
|
||||
{typeName, 0, 0, 0, -1, e.GenericErrorText(e.ErrInputCargoValue)},
|
||||
{typeName, 0, 1, 1, -1, e.GenericErrorText(e.ErrInputCargoValue)},
|
||||
}
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
for i, tc := range table {
|
||||
if tc.err == "" {
|
||||
err := game.CreateScience(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c)
|
||||
|
||||
@@ -7,14 +7,14 @@ import (
|
||||
|
||||
func CreateShipType(configure func(*controller.Param), race, typeName string, drive, weapons, shields, cargo float64, armament int) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = createShipType(r, g, race, typeName, drive, weapons, shields, cargo, armament)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func createShipType(r controller.Repo, g game.Game, race, typeName string, d, w, s, c float64, a int) error {
|
||||
func createShipType(r controller.Repo, g *game.Game, race, typeName string, d, w, s, c float64, a int) error {
|
||||
if err := g.CreateShipType(race, typeName, d, w, s, c, a); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -23,14 +23,14 @@ func createShipType(r controller.Repo, g game.Game, race, typeName string, d, w,
|
||||
|
||||
func MergeShipType(configure func(*controller.Param), race, source, target string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = mergeShipType(r, g, race, source, target)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func mergeShipType(r controller.Repo, g game.Game, race, source, target string) error {
|
||||
func mergeShipType(r controller.Repo, g *game.Game, race, source, target string) error {
|
||||
if err := g.MergeShipType(race, source, target); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -39,14 +39,14 @@ func mergeShipType(r controller.Repo, g game.Game, race, source, target string)
|
||||
|
||||
func DeleteShipType(configure func(*controller.Param), race, typeName string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) {
|
||||
err = deleteShipType(r, g, race, typeName)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func deleteShipType(r controller.Repo, g game.Game, race, typeName string) error {
|
||||
func deleteShipType(r controller.Repo, g *game.Game, race, typeName string) error {
|
||||
if err := g.DeleteShipType(race, typeName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func TestCreateShipType(t *testing.T) {
|
||||
race := "race_01"
|
||||
typeName := "Drone"
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
err := game.DeleteShipType(p, race, typeName)
|
||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityNotExists))
|
||||
err = game.CreateShipType(p, unknownRaceName, " "+typeName+" ", 1, 0, 0, 0, 0) // TODO: test on dead race
|
||||
@@ -79,7 +79,7 @@ func TestCreateShipTypeValidation(t *testing.T) {
|
||||
{typeName, 0, 1, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeWeaponsAndArmamentValue)},
|
||||
{typeName, 0, 0, 0, 0, 1, e.GenericErrorText(e.ErrInputShipTypeWeaponsAndArmamentValue)},
|
||||
}
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
for i, tc := range table {
|
||||
if tc.err == "" {
|
||||
err := game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
|
||||
@@ -96,7 +96,7 @@ func TestCreateShipTypeValidation(t *testing.T) {
|
||||
|
||||
func TestMergeShipType(t *testing.T) {
|
||||
race := "race_01"
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
err := game.CreateShipType(p, race, "Drone", 1, 0, 0, 0, 0)
|
||||
assert.NoError(t, err)
|
||||
err = game.CreateShipType(p, race, "Spy", 1, 0, 0, 0, 0)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"github.com/iliadenisov/galaxy/internal/controller"
|
||||
"github.com/iliadenisov/galaxy/internal/game/turn"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
)
|
||||
|
||||
func MakeTurn(configure func(*controller.Param), race string, number int, name string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) { turn.MakeTurn(r, g) })
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -7,19 +7,19 @@ import (
|
||||
|
||||
func DeclareWar(configure func(*controller.Param), from, to string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) { err = updateRelation(r, g, from, to, game.RelationWar) })
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) { err = updateRelation(r, g, from, to, game.RelationWar) })
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func DeclarePeace(configure func(*controller.Param), from, to string) (err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteGame(func(r controller.Repo, g game.Game) { err = updateRelation(r, g, from, to, game.RelationPeace) })
|
||||
c.ExecuteGame(func(r controller.Repo, g *game.Game) { err = updateRelation(r, g, from, to, game.RelationPeace) })
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func updateRelation(r controller.Repo, g game.Game, hostRace, opponentRace string, rel game.Relation) error {
|
||||
func updateRelation(r controller.Repo, g *game.Game, hostRace, opponentRace string, rel game.Relation) error {
|
||||
if err := g.UpdateRelation(hostRace, opponentRace, rel); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDeclarePeaceAndWarSingle(t *testing.T) {
|
||||
g(t, func(f func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(f func(*controller.Param), g func() *mg.Game) {
|
||||
hostRace := "race_05"
|
||||
opponentRace := "race_01"
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestDeclarePeaceAndWarSingle(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeclarePeaceAndWarAll(t *testing.T) {
|
||||
g(t, func(f func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(f func(*controller.Param), g func() *mg.Game) {
|
||||
hostRace := "race_07"
|
||||
|
||||
for i := range testRaceCount {
|
||||
|
||||
@@ -14,13 +14,13 @@ func GenerateGame(configure func(*controller.Param), races []string) (gameID uui
|
||||
}
|
||||
|
||||
// LoadState used for lock-safe loading game state and may be called concurrently.
|
||||
func LoadState(configure func(*controller.Param)) (g game.Game, err error) {
|
||||
func LoadState(configure func(*controller.Param)) (g *game.Game, err error) {
|
||||
control(configure, func(c *controller.Controller) { g, err = c.Repo.LoadStateSafe() })
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: command for loading report by players (MUST be limited by router)
|
||||
func LoadReport(configure func(*controller.Param)) (g game.Game, err error) {
|
||||
func LoadReport(configure func(*controller.Param)) (g *game.Game, err error) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteState(func(r controller.Repo) { g, err = c.Repo.LoadState() })
|
||||
})
|
||||
|
||||
@@ -22,14 +22,14 @@ func raceNum(i int) string {
|
||||
}
|
||||
|
||||
func TestComposeGame(t *testing.T) {
|
||||
g(t, func(p func(*controller.Param), g func() mg.Game) {
|
||||
g(t, func(p func(*controller.Param), g func() *mg.Game) {
|
||||
_, err := game.GenerateGame(p, []string{"r1", "r2"})
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, "state for turn 0 already saved")
|
||||
})
|
||||
}
|
||||
|
||||
func g(t *testing.T, f func(p func(*controller.Param), g func() mg.Game)) {
|
||||
func g(t *testing.T, f func(p func(*controller.Param), g func() *mg.Game)) {
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
races := make([]string, testRaceCount)
|
||||
@@ -42,11 +42,11 @@ func g(t *testing.T, f func(p func(*controller.Param), g func() mg.Game)) {
|
||||
assert.FailNow(t, "g: ComposeGame", err)
|
||||
return
|
||||
}
|
||||
g := func() mg.Game {
|
||||
g := func() *mg.Game {
|
||||
g, err := game.LoadState(p)
|
||||
if err != nil {
|
||||
assert.FailNow(t, "g: LoadState", err)
|
||||
return mg.Game{}
|
||||
return nil // mg.Game{}
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ type Battle struct {
|
||||
BattleReport BattleReport
|
||||
|
||||
shipAmmo map[int]uint
|
||||
attacker map[int]map[int]float64 // a group able to attack and destroy an opponent with some probability
|
||||
attacker map[int]map[int]float64 // a group able to attack and destroy an opponent with some probability > 0
|
||||
}
|
||||
|
||||
type BattleReport struct {
|
||||
@@ -200,7 +200,7 @@ func SingleBattle(g *Game, b *Battle) {
|
||||
case probability > 0:
|
||||
destroyed = rand.Float64() >= probability
|
||||
default:
|
||||
panic("probabilities cache returned unexpected value")
|
||||
panic("SingleBattle: probability unexpected: value <= 0")
|
||||
}
|
||||
|
||||
b.BattleReport.BattleAction = append(b.BattleReport.BattleAction, BattleAction{
|
||||
|
||||
+15
-15
@@ -18,11 +18,11 @@ const (
|
||||
statePath = "state.json"
|
||||
)
|
||||
|
||||
func (r *repo) SaveTurn(t uint, g game.Game) error {
|
||||
func (r *repo) SaveTurn(t uint, g *game.Game) error {
|
||||
return saveTurn(r.s, t, g)
|
||||
}
|
||||
|
||||
func saveTurn(s Storage, t uint, g game.Game) error {
|
||||
func saveTurn(s Storage, t uint, g *game.Game) error {
|
||||
path := fmt.Sprintf("%03d/state.json", t)
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
@@ -42,46 +42,46 @@ func saveTurn(s Storage, t uint, g game.Game) error {
|
||||
return saveState(s, g) // FIXME: either save it here, or in tre repo controller
|
||||
}
|
||||
|
||||
func saveRace(s Storage, g game.Game, i int) {
|
||||
func saveRace(s Storage, g *game.Game, i int) {
|
||||
|
||||
}
|
||||
|
||||
func (r *repo) SaveState(g game.Game) error {
|
||||
func (r *repo) SaveState(g *game.Game) error {
|
||||
return saveState(r.s, g)
|
||||
}
|
||||
|
||||
func saveState(s Storage, g game.Game) error {
|
||||
func saveState(s Storage, g *game.Game) error {
|
||||
if err := s.Write(statePath, g); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) LoadState() (game.Game, error) {
|
||||
func (r *repo) LoadState() (*game.Game, error) {
|
||||
return loadState(r.s, true)
|
||||
}
|
||||
|
||||
func (r *repo) LoadStateSafe() (game.Game, error) {
|
||||
func (r *repo) LoadStateSafe() (*game.Game, error) {
|
||||
return loadState(r.s, false)
|
||||
}
|
||||
|
||||
func loadState(s Storage, locked bool) (game.Game, error) {
|
||||
var g game.Game
|
||||
func loadState(s Storage, locked bool) (*game.Game, error) {
|
||||
var g *game.Game = new(game.Game)
|
||||
path := statePath
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return g, NewStorageError(err)
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return g, NewGameNotInitializedError()
|
||||
return nil, NewGameNotInitializedError()
|
||||
}
|
||||
if locked {
|
||||
if err := s.Read(path, &g); err != nil {
|
||||
return g, NewStorageError(err)
|
||||
if err := s.Read(path, g); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
} else {
|
||||
if err := s.ReadSafe(path, &g); err != nil {
|
||||
return g, NewStorageError(err)
|
||||
if err := s.ReadSafe(path, g); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
}
|
||||
return g, nil
|
||||
|
||||
Reference in New Issue
Block a user