33 lines
883 B
Go
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)
|
|
}
|