commands A, W
This commit is contained in:
+21
-3
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/pkg/model/game"
|
||||
"github.com/iliadenisov/galaxy/pkg/repo"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,13 @@ type Param struct {
|
||||
StoragePath string
|
||||
}
|
||||
|
||||
func ComposeGame(configure func(*Param), races []string) (gameID uuid.UUID, err error) {
|
||||
control(configure, func(c *ctrl) { c.execute(func(r Repo) { gameID, err = newGame(r, races) }) })
|
||||
func LoadState(configure func(*Param)) (g game.Game, err error) {
|
||||
control(configure, func(c *ctrl) { c.executeInit(func(r Repo) { g, err = c.repo.LoadState() }) })
|
||||
return
|
||||
}
|
||||
|
||||
func GenerateGame(configure func(*Param), races []string) (gameID uuid.UUID, err error) {
|
||||
control(configure, func(c *ctrl) { c.executeInit(func(r Repo) { gameID, err = newGame(r, races) }) })
|
||||
return
|
||||
}
|
||||
|
||||
@@ -47,10 +53,22 @@ func control(configure func(*Param), consumer func(*ctrl)) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ctrl) execute(consumer func(Repo)) error {
|
||||
func (c *ctrl) executeInit(consumer func(Repo)) error {
|
||||
if err := c.repo.Lock(); err != nil {
|
||||
return fmt.Errorf("execute: lock failed: %s", err)
|
||||
}
|
||||
consumer(c.repo)
|
||||
return c.repo.Release()
|
||||
}
|
||||
|
||||
func (c *ctrl) execute(consumer func(Repo, game.Game)) error {
|
||||
if err := c.repo.Lock(); err != nil {
|
||||
return fmt.Errorf("execute: lock failed: %s", err)
|
||||
}
|
||||
g, err := c.repo.LoadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
consumer(c.repo, g)
|
||||
return c.repo.Release()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user