refactor: load/save state as ptr
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user