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
+69
View File
@@ -1,7 +1,10 @@
package game
import (
"slices"
"github.com/google/uuid"
e "github.com/iliadenisov/galaxy/internal/error"
)
type Race struct {
@@ -47,3 +50,69 @@ func (r Race) FlightDistance() float64 {
func (r Race) VisibilityDistance() float64 {
return r.TechLevel(TechDrive) * 30
}
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) 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) GiveVotes(race, recipient string) error {
ri, err := g.raceIndex(race)
if err != nil {
return err
}
rec, err := g.raceIndex(recipient)
if err != nil {
return err
}
g.Race[ri].Vote = g.Race[rec].ID
return nil
}
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
}
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
}