cmd: vote

This commit is contained in:
Ilia Denisov
2026-01-06 07:58:55 +02:00
parent 0b8c53cedf
commit c9ed52b268
4 changed files with 97 additions and 55 deletions
-53
View File
@@ -76,59 +76,6 @@ func (g Game) raceIndex(name string) (int, error) {
return i, nil
}
func (g Game) UpdateRelation(race, opponent string, rel Relation) error {
ri, err := g.raceIndex(race)
if err != nil {
return err
}
var other int
if race == opponent {
other = ri
} else if other, err = g.raceIndex(opponent); err != nil {
return err
}
if err != nil {
return err
}
return g.updateRelationInternal(ri, other, rel)
}
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 ri != other {
return e.NewGameStateError("UpdateRelation: opponent not found")
}
return nil
}
func (g Game) Relation(hostRace, opponentRace string) (RaceRelation, error) {
ri, err := g.raceIndex(hostRace)
if err != nil {
return RaceRelation{}, err
}
other, err := g.raceIndex(opponentRace)
if err != nil {
return RaceRelation{}, err
}
return g.relationInternal(ri, other)
}
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 g.Race[ri].Relations[rel], nil
}
// -----------------------------------------------------------------------------
// validateTypeName always return v without leading and trailing spaces