Files
galaxy-game/pkg/game/command.go
T
2025-09-28 22:43:27 +03:00

33 lines
883 B
Go

package game
import "github.com/iliadenisov/galaxy/pkg/model/game"
func DeclareWar(configure func(*Param), from, to string) (err error) {
control(configure, func(c *ctrl) { c.execute(func(r Repo) { err = updateRelation(r, from, to, game.RelationWar) }) })
return
}
func DeclarePeace(configure func(*Param), from, to string) (err error) {
control(configure, func(c *ctrl) { c.execute(func(r Repo) { err = updateRelation(r, from, to, game.RelationPeace) }) })
return
}
func updateRelation(r Repo, hostRace, opponentRace string, rel game.Relation) error {
g, err := r.LoadState()
if err != nil {
return err
}
hostID, err := g.HostRaceID(hostRace)
if err != nil {
return err
}
opponentID, err := g.OpponentRaceID(opponentRace)
if err != nil {
return err
}
if err := g.UpdateRelation(hostID, opponentID, rel); err != nil {
return err
}
return r.SaveState(g)
}