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
+1 -6
View File
@@ -34,12 +34,7 @@ func (c Controller) UpdateRelation(actor, acceptor string, rel game.Relation) er
if err != nil { if err != nil {
return err return err
} }
var other int other, err := c.Cache.validRace(acceptor)
if actor == acceptor {
other = ri
} else if other, err = c.Cache.validRace(acceptor); err != nil {
return err
}
if err != nil { if err != nil {
return err return err
} }
+8
View File
@@ -137,6 +137,14 @@ func (c *Cache) wipeRace(ri int) {
} }
p.Wipe() p.Wipe()
} }
for i := range c.listRaceActingIdx() {
if i == ri {
continue
}
if c.g.Race[i].VoteFor == r.ID {
c.g.Race[i].VoteFor = c.g.Race[i].ID
}
}
r.Votes = 0 r.Votes = 0
r.VoteFor = r.ID r.VoteFor = r.ID
r.Extinct = true r.Extinct = true
+1 -1
View File
@@ -44,6 +44,6 @@ func TestListMoveableGroupIds(t *testing.T) {
for _, i := range movableGroups { for _, i := range movableGroups {
sg := c.ShipGroup(i) sg := c.ShipGroup(i)
assert.NotEqual(t, game.StateUpgrade, sg.State()) assert.NotEqual(t, game.StateUpgrade, sg.State())
assert.NotEqual(t, game.StateTransfer, sg.State()) // TODO: Transfer state movable or not? assert.NotEqual(t, game.StateTransfer, sg.State())
} }
} }
+18 -12
View File
@@ -56,24 +56,28 @@ func (c *Cache) TurnCalculateVotes() []int {
return votingWinners(calc, c.g.Votes.F()) return votingWinners(calc, c.g.Votes.F())
} }
func VotingGraph(races []game.Race, raceIndex func(uuid.UUID) int) []*VoteNode { func VotingGraph(races []game.Race, raceIndex func(uuid.UUID) int) map[int]*VoteNode {
nodes := make([]*VoteNode, len(races)) nodes := make(map[int]*VoteNode, len(races))
for ri := range races { for ri := range races {
// TODO: filter inactive (RIP) races if races[ri].Extinct {
continue
}
r := &races[ri] r := &races[ri]
if nodes[ri] == nil { if _, ok := nodes[ri]; !ok {
nodes[ri] = &VoteNode{ nodes[ri] = &VoteNode{
ID: ri, ID: ri,
} }
} }
if r.VoteFor != r.ID { if r.VoteFor != r.ID {
vid := raceIndex(r.VoteFor) vid := raceIndex(r.VoteFor)
if nodes[vid] == nil { if !races[vid].Extinct {
nodes[vid] = &VoteNode{ if _, ok := nodes[vid]; !ok {
ID: vid, nodes[vid] = &VoteNode{
ID: vid,
}
} }
nodes[ri].Next = nodes[vid]
} }
nodes[ri].Next = nodes[vid]
} }
} }
return nodes return nodes
@@ -93,7 +97,7 @@ func (c *Cache) votesByRace() map[int]float64 {
return result 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) votes := maps.Clone(raceVotes)
result := make([]*VoteGroup, 0) result := make([]*VoteGroup, 0)
chains := VotingChains(nodes) chains := VotingChains(nodes)
@@ -131,11 +135,13 @@ func GroupVotes(raceVotes map[int]float64, nodes []*VoteNode) []*VoteGroup {
return result return result
} }
func VotingChains(nodes []*VoteNode) [][]VoteNode { func VotingChains(nodes map[int]*VoteNode) [][]VoteNode {
visited := make(map[int]bool) visited := make(map[int]bool)
result := make([][]VoteNode, 0) result := make([][]VoteNode, 0)
for i := range nodes { raceIds := slices.Collect(maps.Keys(nodes))
n := nodes[i] slices.Sort(raceIds)
for _, rid := range raceIds {
n := nodes[rid]
if v, ok := visited[n.ID]; (ok && v) || n.Next == nil { if v, ok := visited[n.ID]; (ok && v) || n.Next == nil {
continue continue
} }
-1
View File
@@ -27,7 +27,6 @@ func TestVotesByRace(t *testing.T) {
assert.Equal(t, 0.78, vbr[Race_0_idx]) assert.Equal(t, 0.78, vbr[Race_0_idx])
assert.Contains(t, vbr, Race_1_idx) assert.Contains(t, vbr, Race_1_idx)
assert.Equal(t, 0.9, vbr[Race_1_idx]) assert.Equal(t, 0.9, vbr[Race_1_idx])
// TODO: add races without planets / dead races
} }
func prepareRaces() ([]game.Race, func(u uuid.UUID) int) { func prepareRaces() ([]game.Race, func(u uuid.UUID) int) {
-1
View File
@@ -4,7 +4,6 @@ func NewRaceUnknownError(arg ...any) error {
return newGenericError(ErrInputUnknownRace, arg...) return newGenericError(ErrInputUnknownRace, arg...)
} }
func NewSameRaceError(arg ...any) error { func NewSameRaceError(arg ...any) error {
// TODO: check all possible commands
return newGenericError(ErrInputSameRace, arg...) return newGenericError(ErrInputSameRace, arg...)
} }