Ally/War commands
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user