refactor: race index by name
This commit is contained in:
+29
-62
@@ -27,98 +27,65 @@ 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) raceIndex(name string) (int, error) {
|
||||
i := slices.IndexFunc(g.Race, func(r Race) bool { return r.Name == name })
|
||||
if i < 0 {
|
||||
return i, e.NewHostRaceUnknownError(name)
|
||||
return i, e.NewRaceUnknownError(name)
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
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(hostRace, opponentRace string, rel Relation) error {
|
||||
hostID, err := g.hostRaceID(hostRace)
|
||||
func (g Game) UpdateRelation(race, opponent string, rel Relation) error {
|
||||
ri, err := g.raceIndex(race)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var opponentID uuid.UUID
|
||||
if hostRace == opponentRace {
|
||||
opponentID = hostID
|
||||
} else if opponentID, err = g.opponentRaceID(opponentRace); err != nil {
|
||||
var other int
|
||||
if race == opponent {
|
||||
other = ri
|
||||
} else if other, err = g.raceIndex(opponent); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.updateRelationInternal(hostID, opponentID, rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.updateRelationInternal(ri, other, 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 {
|
||||
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
|
||||
}
|
||||
}
|
||||
if hostID != opponentID {
|
||||
return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
}
|
||||
func (g Game) updateRelationInternal(ri, other int, rel Relation) error {
|
||||
for o := range g.Race[ri].Relations {
|
||||
switch {
|
||||
case ri == other:
|
||||
g.Race[ri].Relations[o].Relation = rel
|
||||
case g.Race[ri].Relations[o].RaceID == g.Race[other].ID:
|
||||
g.Race[ri].Relations[o].Relation = rel
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if hostID != opponentID {
|
||||
return e.NewGameStateError("UpdateRelation: host %v not found", hostID)
|
||||
if ri != other {
|
||||
return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g Game) Relation(hostRace, opponentRace string) (RaceRelation, error) {
|
||||
hostID, err := g.hostRaceID(hostRace)
|
||||
ri, err := g.raceIndex(hostRace)
|
||||
if err != nil {
|
||||
return RaceRelation{}, err
|
||||
}
|
||||
opponentID, err := g.opponentRaceID(opponentRace)
|
||||
other, err := g.raceIndex(opponentRace)
|
||||
if err != nil {
|
||||
return RaceRelation{}, err
|
||||
}
|
||||
return g.relationInternal(hostID, opponentID)
|
||||
return g.relationInternal(ri, other)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
func (g Game) relationInternal(ri, other int) (RaceRelation, error) {
|
||||
rel := slices.IndexFunc(g.Race[ri].Relations, func(r RaceRelation) bool { return r.RaceID == g.Race[other].ID })
|
||||
if rel < 0 {
|
||||
return RaceRelation{}, e.NewGameStateError("Relation: opponent not found")
|
||||
}
|
||||
return RaceRelation{}, e.NewGameStateError("Relation: host %v not found", hostID)
|
||||
return g.Race[ri].Relations[rel], nil
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user