Ally/War commands
This commit is contained in:
+41
-2
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/pkg/error"
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
@@ -24,10 +25,48 @@ func (g Game) Votes(raceID uuid.UUID) float64 {
|
||||
return pop / 1000.
|
||||
}
|
||||
|
||||
func (g Game) HostRaceID(name string) (uuid.UUID, error) {
|
||||
if v, ok := g.raceID(name); ok {
|
||||
return v, nil
|
||||
}
|
||||
return uuid.Nil, e.NewHostRaceUnknownError(name)
|
||||
}
|
||||
|
||||
func (g Game) OpponentRaceID(name string) (uuid.UUID, error) {
|
||||
if v, ok := g.raceID(name); ok {
|
||||
return v, nil
|
||||
}
|
||||
return uuid.Nil, e.NewOpponentRaceUnknownError(name)
|
||||
}
|
||||
|
||||
func (g Game) raceID(raceName string) (uuid.UUID, bool) {
|
||||
for i := range g.Race {
|
||||
if g.Race[i].Name == raceName {
|
||||
return g.Race[i].ID, true
|
||||
}
|
||||
}
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
func (g Game) UpdateRelation(hostID, opponentID uuid.UUID, rel Relation) error {
|
||||
for r := range g.Race {
|
||||
if g.Race[r].ID == hostID {
|
||||
for o := range g.Race[r].Relations {
|
||||
if g.Race[r].Relations[o].RaceID == opponentID {
|
||||
g.Race[r].Relations[o].Relation = rel
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
}
|
||||
}
|
||||
return e.NewGameStateError("UpdateRelation: host %v not found", hostID)
|
||||
}
|
||||
|
||||
func (g Game) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(&g)
|
||||
}
|
||||
|
||||
func (g Game) UnmarshalBinary(data []byte) error {
|
||||
return json.Unmarshal(data, &g)
|
||||
func (g *Game) UnmarshalBinary(data []byte) error {
|
||||
return json.Unmarshal(data, g)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user