refactor: executors and routers
* refactor: executors and routers
This commit is contained in:
@@ -2,7 +2,6 @@ package controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
@@ -10,6 +9,8 @@ import (
|
||||
"github.com/iliadenisov/galaxy/internal/repo"
|
||||
)
|
||||
|
||||
type Configurer func(*Param)
|
||||
|
||||
type Repo interface {
|
||||
// Lock must be called before any repository operations
|
||||
Lock() error
|
||||
@@ -42,18 +43,109 @@ type Repo interface {
|
||||
LoadReport(uint, uuid.UUID) (*report.Report, error)
|
||||
}
|
||||
|
||||
type Controller struct {
|
||||
Repo Repo
|
||||
Cache *Cache
|
||||
type Ctrl interface {
|
||||
RaceID(actor string) (uuid.UUID, error)
|
||||
QuitGame(actor string) error
|
||||
GiveVotes(actor, acceptor string) error
|
||||
UpdateRelation(actor, acceptor string, rel string) error
|
||||
JoinShipGroupToFleet(actor, fleetName string, group, count uint) error
|
||||
JoinFleets(actor, fleetSourceName, fleetTargetName string) error
|
||||
SendFleet(actor, fleetName string, planetNumber uint) error
|
||||
RenamePlanet(actor string, planetNumber int, typeName string) error
|
||||
PlanetProduction(actor string, planetNumber int, prodType, subject string) error
|
||||
SetRoute(actor, loadType string, origin, destination uint) error
|
||||
RemoveRoute(actor, loadType string, origin uint) error
|
||||
CreateScience(actor, typeName string, drive, weapons, shields, cargo float64) error
|
||||
DeleteScience(actor, typeName string) error
|
||||
CreateShipType(actor, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error
|
||||
MergeShipType(actor, name, targetName string) error
|
||||
DeleteShipType(actor, typeName string) error
|
||||
SendGroup(actor string, groupIndex, planetNumber, quantity uint) error
|
||||
UpgradeGroup(actor string, groupIndex uint, techInput string, limitShips uint, limitLevel float64) error
|
||||
JoinEqualGroups(actor string) error
|
||||
BreakGroup(actor string, groupIndex, quantity uint) error
|
||||
DisassembleGroup(actor string, groupIndex, quantity uint) error
|
||||
LoadCargo(actor string, groupIndex uint, cargoType string, ships uint, quantity float64) error
|
||||
UnloadCargo(actor string, groupIndex uint, ships uint, quantity float64) error
|
||||
TransferGroup(actor, acceptor string, groupIndex, quantity uint) error
|
||||
}
|
||||
|
||||
type Param struct {
|
||||
StoragePath string
|
||||
func GenerateGame(configure func(*Param), races []string) (ID uuid.UUID, err error) {
|
||||
ec, err := NewRepoController(configure)
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
if err = ec.Repo.Lock(); err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
err = errors.Join(err, ec.Repo.Release())
|
||||
}()
|
||||
|
||||
ID, err = NewGame(ec.Repo, races)
|
||||
return
|
||||
}
|
||||
|
||||
type Configurer func(*Param)
|
||||
func ExecuteCommand(configure func(*Param), consumer func(c Ctrl) error) (err error) {
|
||||
ec, err := NewRepoController(configure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ec.ExecuteCommand(func(c *Controller) error { return consumer(c) })
|
||||
}
|
||||
|
||||
func NewController(config Configurer) (*Controller, error) {
|
||||
func GameState(configure func(*Param)) (s game.State, err error) {
|
||||
ec, err := NewRepoController(configure)
|
||||
|
||||
g, err := ec.Repo.LoadStateSafe()
|
||||
if err != nil {
|
||||
return game.State{}, err
|
||||
}
|
||||
|
||||
result := &game.State{
|
||||
ID: g.ID,
|
||||
Turn: g.Turn,
|
||||
Stage: g.Stage,
|
||||
Players: make([]game.PlayerState, len(g.Race)),
|
||||
}
|
||||
|
||||
for i := range g.Race {
|
||||
r := &g.Race[i]
|
||||
result.Players[i].ID = r.ID
|
||||
result.Players[i].Name = r.Name
|
||||
result.Players[i].Extinct = r.Extinct
|
||||
}
|
||||
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
type RepoController struct {
|
||||
Repo Repo
|
||||
}
|
||||
|
||||
func (ec *RepoController) ExecuteCommand(consumer func(c *Controller) error) (err error) {
|
||||
if err := ec.Repo.Lock(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
err = errors.Join(err, ec.Repo.Release())
|
||||
}()
|
||||
|
||||
g, err := ec.Repo.LoadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = consumer(NewGameController(g))
|
||||
|
||||
if err == nil {
|
||||
g.Stage += 1
|
||||
ec.Repo.SaveLastState(g)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func NewRepoController(config Configurer) (*RepoController, error) {
|
||||
c := &Param{
|
||||
StoragePath: ".",
|
||||
}
|
||||
@@ -64,44 +156,22 @@ func NewController(config Configurer) (*Controller, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Controller{
|
||||
return &RepoController{
|
||||
Repo: r,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewRepoController(r Repo) *Controller {
|
||||
func NewGameController(g *game.Game) *Controller {
|
||||
return &Controller{
|
||||
Repo: r,
|
||||
Cache: NewCache(g),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ExecuteState(consumer func(Repo) error) (err error) {
|
||||
if err := c.Repo.Lock(); err != nil {
|
||||
return fmt.Errorf("execute: lock failed: %s", err)
|
||||
}
|
||||
defer func() {
|
||||
err = errors.Join(err, c.Repo.Release())
|
||||
}()
|
||||
err = consumer(c.Repo)
|
||||
return
|
||||
type Controller struct {
|
||||
Repo Repo
|
||||
Cache *Cache
|
||||
}
|
||||
|
||||
func (c *Controller) ExecuteCommand(consumer func(Repo, *game.Game) error) (err 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
|
||||
}
|
||||
defer func() {
|
||||
err = errors.Join(err, c.Repo.Release())
|
||||
}()
|
||||
c.Cache = NewCache(g)
|
||||
err = consumer(c.Repo, g)
|
||||
if err == nil {
|
||||
g.Stage += 1
|
||||
c.Repo.SaveLastState(g)
|
||||
}
|
||||
return
|
||||
type Param struct {
|
||||
StoragePath string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user