chore: refactor structure

This commit is contained in:
Ilia Denisov
2025-11-21 22:06:40 +03:00
parent 269de2184c
commit 1bdb40a27d
8 changed files with 18 additions and 18 deletions
+5 -5
View File
@@ -24,7 +24,7 @@ type Repo interface {
LoadState() (game.Game, error) LoadState() (game.Game, error)
} }
type Ctrl struct { type Controller struct {
param Param param Param
Repo Repo Repo Repo
} }
@@ -33,7 +33,7 @@ type Param struct {
StoragePath string StoragePath string
} }
func NewController(configure func(*Param)) (*Ctrl, error) { func NewController(configure func(*Param)) (*Controller, error) {
c := &Param{ c := &Param{
StoragePath: ".", StoragePath: ".",
} }
@@ -44,13 +44,13 @@ func NewController(configure func(*Param)) (*Ctrl, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Ctrl{ return &Controller{
param: *c, param: *c,
Repo: r, Repo: r,
}, nil }, nil
} }
func (c *Ctrl) ExecuteInit(consumer func(Repo)) error { func (c *Controller) ExecuteInit(consumer func(Repo)) error {
if err := c.Repo.Lock(); err != nil { if err := c.Repo.Lock(); err != nil {
return fmt.Errorf("execute: lock failed: %s", err) return fmt.Errorf("execute: lock failed: %s", err)
} }
@@ -58,7 +58,7 @@ func (c *Ctrl) ExecuteInit(consumer func(Repo)) error {
return c.Repo.Release() 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 { if err := c.Repo.Lock(); err != nil {
return fmt.Errorf("execute: lock failed: %s", err) return fmt.Errorf("execute: lock failed: %s", err)
} }
+1 -1
View File
@@ -6,7 +6,7 @@ import (
) )
func JoinEqualGroups(configure func(*controller.Param), race string) (err error) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = joinEqualGroups(r, g, race) err = joinEqualGroups(r, g, race)
}) })
+1 -1
View File
@@ -6,7 +6,7 @@ import (
) )
func RenamePlanet(configure func(*controller.Param), race string, number int, name string) (err error) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = renamePlanet(r, g, race, number, name) err = renamePlanet(r, g, race, number, name)
}) })
+1 -1
View File
@@ -6,7 +6,7 @@ import (
) )
func PlanetProduction(configure func(*controller.Param), race string, planetNumber int, prodType, subject string) (err error) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = planetProduction(r, g, race, planetNumber, prodType, subject) err = planetProduction(r, g, race, planetNumber, prodType, subject)
}) })
+2 -2
View File
@@ -6,7 +6,7 @@ import (
) )
func CreateScience(configure func(*controller.Param), race, typeName string, drive, weapons, shields, cargo float64) (err error) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = createScience(r, g, race, typeName, drive, weapons, shields, cargo) 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) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = deleteScience(r, g, race, typeName) err = deleteScience(r, g, race, typeName)
}) })
+3 -3
View File
@@ -6,7 +6,7 @@ import (
) )
func CreateShipType(configure func(*controller.Param), race, typeName string, drive, weapons, shields, cargo float64, armament int) (err error) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = createShipType(r, g, race, typeName, drive, weapons, shields, cargo, armament) 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) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = mergeShipType(r, g, race, source, target) 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) { 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) { c.Execute(func(r controller.Repo, g game.Game) {
err = deleteShipType(r, g, race, typeName) err = deleteShipType(r, g, race, typeName)
}) })
+2 -2
View File
@@ -6,14 +6,14 @@ import (
) )
func DeclareWar(configure func(*controller.Param), from, to string) (err error) { 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) }) c.Execute(func(r controller.Repo, g game.Game) { err = updateRelation(r, g, from, to, game.RelationWar) })
}) })
return return
} }
func DeclarePeace(configure func(*controller.Param), from, to string) (err error) { 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) }) c.Execute(func(r controller.Repo, g game.Game) { err = updateRelation(r, g, from, to, game.RelationPeace) })
}) })
return return
+3 -3
View File
@@ -7,18 +7,18 @@ import (
) )
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.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 return
} }
func GenerateGame(configure func(*controller.Param), races []string) (gameID uuid.UUID, err error) { 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) }) c.ExecuteInit(func(r controller.Repo) { gameID, err = controller.NewGame(r, races) })
}) })
return 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) c, err := controller.NewController(configure)
if err != nil { if err != nil {
return err return err