chore: refactor structure
This commit is contained in:
@@ -24,7 +24,7 @@ type Repo interface {
|
||||
LoadState() (game.Game, error)
|
||||
}
|
||||
|
||||
type Ctrl struct {
|
||||
type Controller struct {
|
||||
param Param
|
||||
Repo Repo
|
||||
}
|
||||
@@ -33,7 +33,7 @@ type Param struct {
|
||||
StoragePath string
|
||||
}
|
||||
|
||||
func NewController(configure func(*Param)) (*Ctrl, error) {
|
||||
func NewController(configure func(*Param)) (*Controller, error) {
|
||||
c := &Param{
|
||||
StoragePath: ".",
|
||||
}
|
||||
@@ -44,13 +44,13 @@ func NewController(configure func(*Param)) (*Ctrl, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Ctrl{
|
||||
return &Controller{
|
||||
param: *c,
|
||||
Repo: r,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Ctrl) ExecuteInit(consumer func(Repo)) error {
|
||||
func (c *Controller) ExecuteInit(consumer func(Repo)) error {
|
||||
if err := c.Repo.Lock(); err != nil {
|
||||
return fmt.Errorf("execute: lock failed: %s", err)
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func (c *Ctrl) ExecuteInit(consumer func(Repo)) error {
|
||||
return c.Repo.Release()
|
||||
}
|
||||
|
||||
func (c *Ctrl) Execute(consumer func(Repo, game.Game)) error {
|
||||
func (c *Controller) Execute(consumer func(Repo, game.Game)) error {
|
||||
if err := c.Repo.Lock(); err != nil {
|
||||
return fmt.Errorf("execute: lock failed: %s", err)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func JoinEqualGroups(configure func(*controller.Param), race string) (err error) {
|
||||
control(configure, func(c *controller.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = joinEqualGroups(r, g, race)
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func RenamePlanet(configure func(*controller.Param), race string, number int, name string) (err error) {
|
||||
control(configure, func(c *controller.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = renamePlanet(r, g, race, number, name)
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func PlanetProduction(configure func(*controller.Param), race string, planetNumber int, prodType, subject string) (err error) {
|
||||
control(configure, func(c *controller.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = planetProduction(r, g, race, planetNumber, prodType, subject)
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func CreateScience(configure func(*controller.Param), race, typeName string, drive, weapons, shields, cargo float64) (err error) {
|
||||
control(configure, func(c *controller.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = createScience(r, g, race, typeName, drive, weapons, shields, cargo)
|
||||
})
|
||||
@@ -22,7 +22,7 @@ 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.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = deleteScience(r, g, race, typeName)
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func CreateShipType(configure func(*controller.Param), race, typeName string, drive, weapons, shields, cargo float64, armament int) (err error) {
|
||||
control(configure, func(c *controller.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = createShipType(r, g, race, typeName, drive, weapons, shields, cargo, armament)
|
||||
})
|
||||
@@ -22,7 +22,7 @@ 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.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = mergeShipType(r, g, race, source, target)
|
||||
})
|
||||
@@ -38,7 +38,7 @@ 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.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) {
|
||||
err = deleteShipType(r, g, race, typeName)
|
||||
})
|
||||
|
||||
@@ -6,14 +6,14 @@ import (
|
||||
)
|
||||
|
||||
func DeclareWar(configure func(*controller.Param), from, to string) (err error) {
|
||||
control(configure, func(c *controller.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(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.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.Execute(func(r controller.Repo, g game.Game) { err = updateRelation(r, g, from, to, game.RelationPeace) })
|
||||
})
|
||||
return
|
||||
|
||||
@@ -7,18 +7,18 @@ import (
|
||||
)
|
||||
|
||||
func LoadState(configure func(*controller.Param)) (g game.Game, err error) {
|
||||
control(configure, func(c *controller.Ctrl) { c.ExecuteInit(func(r controller.Repo) { g, err = c.Repo.LoadState() }) })
|
||||
control(configure, func(c *controller.Controller) { c.ExecuteInit(func(r controller.Repo) { g, err = c.Repo.LoadState() }) })
|
||||
return
|
||||
}
|
||||
|
||||
func GenerateGame(configure func(*controller.Param), races []string) (gameID uuid.UUID, err error) {
|
||||
control(configure, func(c *controller.Ctrl) {
|
||||
control(configure, func(c *controller.Controller) {
|
||||
c.ExecuteInit(func(r controller.Repo) { gameID, err = controller.NewGame(r, races) })
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func control(configure func(*controller.Param), consumer func(*controller.Ctrl)) error {
|
||||
func control(configure func(*controller.Param), consumer func(*controller.Controller)) error {
|
||||
c, err := controller.NewController(configure)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user