chore: refactor structure

This commit is contained in:
Ilia Denisov
2025-11-21 21:40:15 +03:00
parent 126f381b04
commit 269de2184c
72 changed files with 512 additions and 393 deletions
+71
View File
@@ -0,0 +1,71 @@
package controller
import (
"fmt"
"github.com/iliadenisov/galaxy/internal/model/game"
"github.com/iliadenisov/galaxy/internal/repo"
)
type Repo interface {
// Lock must be called before any repository operations
Lock() error
// Release must be called after first and only repository operation
Release() error
// SaveTurn stores just generated new turn
SaveTurn(uint, game.Game) error
// SaveState stores current game state updated between turns
SaveState(game.Game) error
// LoadState retrieves game current state
LoadState() (game.Game, error)
}
type Ctrl struct {
param Param
Repo Repo
}
type Param struct {
StoragePath string
}
func NewController(configure func(*Param)) (*Ctrl, error) {
c := &Param{
StoragePath: ".",
}
if configure != nil {
configure(c)
}
r, err := repo.NewFileRepo(c.StoragePath)
if err != nil {
return nil, err
}
return &Ctrl{
param: *c,
Repo: r,
}, nil
}
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()
}