commands A, W
This commit is contained in:
+54
-6
@@ -25,14 +25,14 @@ func (g Game) Votes(raceID uuid.UUID) float64 {
|
||||
return pop / 1000.
|
||||
}
|
||||
|
||||
func (g Game) HostRaceID(name string) (uuid.UUID, error) {
|
||||
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) {
|
||||
func (g Game) opponentRaceID(name string) (uuid.UUID, error) {
|
||||
if v, ok := g.raceID(name); ok {
|
||||
return v, nil
|
||||
}
|
||||
@@ -48,19 +48,67 @@ func (g Game) raceID(raceName string) (uuid.UUID, bool) {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
func (g Game) UpdateRelation(hostID, opponentID uuid.UUID, rel Relation) error {
|
||||
func (g Game) UpdateRelation(hostRace, opponentRace string, rel Relation) error {
|
||||
hostID, err := g.hostRaceID(hostRace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var opponentID uuid.UUID
|
||||
if hostRace == opponentRace {
|
||||
opponentID = hostID
|
||||
} else if opponentID, err = g.opponentRaceID(opponentRace); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.updateRelationInternal(hostID, opponentID, rel)
|
||||
}
|
||||
|
||||
func (g Game) updateRelationInternal(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 {
|
||||
switch {
|
||||
case hostID == opponentID:
|
||||
g.Race[r].Relations[o].Relation = rel
|
||||
case g.Race[r].Relations[o].RaceID == opponentID:
|
||||
g.Race[r].Relations[o].Relation = rel
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
if hostID != opponentID {
|
||||
return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
return e.NewGameStateError("UpdateRelation: host %v not found", hostID)
|
||||
if hostID != opponentID {
|
||||
return e.NewGameStateError("UpdateRelation: host %v not found", hostID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g Game) Relation(hostRace, opponentRace string) (RaceRelation, error) {
|
||||
hostID, err := g.hostRaceID(hostRace)
|
||||
if err != nil {
|
||||
return RaceRelation{}, err
|
||||
}
|
||||
opponentID, err := g.opponentRaceID(opponentRace)
|
||||
if err != nil {
|
||||
return RaceRelation{}, err
|
||||
}
|
||||
return g.relationInternal(hostID, opponentID)
|
||||
}
|
||||
|
||||
func (g Game) relationInternal(hostID, opponentID uuid.UUID) (RaceRelation, 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 {
|
||||
return g.Race[r].Relations[o], nil
|
||||
}
|
||||
}
|
||||
return RaceRelation{}, e.NewGameStateError("Relation: opponent not found")
|
||||
}
|
||||
}
|
||||
return RaceRelation{}, e.NewGameStateError("Relation: host %v not found", hostID)
|
||||
}
|
||||
|
||||
func (g Game) MarshalBinary() (data []byte, err error) {
|
||||
|
||||
@@ -25,8 +25,8 @@ type Race struct {
|
||||
type Relation string
|
||||
|
||||
const (
|
||||
RelationWar = "War"
|
||||
RelationPeace = "Peace"
|
||||
RelationWar Relation = "War"
|
||||
RelationPeace Relation = "Peace"
|
||||
)
|
||||
|
||||
type RaceRelation struct {
|
||||
|
||||
Reference in New Issue
Block a user