minor refactor

This commit is contained in:
Ilia Denisov
2026-02-08 15:47:59 +02:00
parent 175fb98c3a
commit bf34843568
6 changed files with 28 additions and 21 deletions
+18 -12
View File
@@ -56,24 +56,28 @@ func (c *Cache) TurnCalculateVotes() []int {
return votingWinners(calc, c.g.Votes.F())
}
func VotingGraph(races []game.Race, raceIndex func(uuid.UUID) int) []*VoteNode {
nodes := make([]*VoteNode, len(races))
func VotingGraph(races []game.Race, raceIndex func(uuid.UUID) int) map[int]*VoteNode {
nodes := make(map[int]*VoteNode, len(races))
for ri := range races {
// TODO: filter inactive (RIP) races
if races[ri].Extinct {
continue
}
r := &races[ri]
if nodes[ri] == nil {
if _, ok := nodes[ri]; !ok {
nodes[ri] = &VoteNode{
ID: ri,
}
}
if r.VoteFor != r.ID {
vid := raceIndex(r.VoteFor)
if nodes[vid] == nil {
nodes[vid] = &VoteNode{
ID: vid,
if !races[vid].Extinct {
if _, ok := nodes[vid]; !ok {
nodes[vid] = &VoteNode{
ID: vid,
}
}
nodes[ri].Next = nodes[vid]
}
nodes[ri].Next = nodes[vid]
}
}
return nodes
@@ -93,7 +97,7 @@ func (c *Cache) votesByRace() map[int]float64 {
return result
}
func GroupVotes(raceVotes map[int]float64, nodes []*VoteNode) []*VoteGroup {
func GroupVotes(raceVotes map[int]float64, nodes map[int]*VoteNode) []*VoteGroup {
votes := maps.Clone(raceVotes)
result := make([]*VoteGroup, 0)
chains := VotingChains(nodes)
@@ -131,11 +135,13 @@ func GroupVotes(raceVotes map[int]float64, nodes []*VoteNode) []*VoteGroup {
return result
}
func VotingChains(nodes []*VoteNode) [][]VoteNode {
func VotingChains(nodes map[int]*VoteNode) [][]VoteNode {
visited := make(map[int]bool)
result := make([][]VoteNode, 0)
for i := range nodes {
n := nodes[i]
raceIds := slices.Collect(maps.Keys(nodes))
slices.Sort(raceIds)
for _, rid := range raceIds {
n := nodes[rid]
if v, ok := visited[n.ID]; (ok && v) || n.Next == nil {
continue
}