minor refactor
This commit is contained in:
@@ -34,12 +34,7 @@ func (c Controller) UpdateRelation(actor, acceptor string, rel game.Relation) er
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var other int
|
||||
if actor == acceptor {
|
||||
other = ri
|
||||
} else if other, err = c.Cache.validRace(acceptor); err != nil {
|
||||
return err
|
||||
}
|
||||
other, err := c.Cache.validRace(acceptor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -137,6 +137,14 @@ func (c *Cache) wipeRace(ri int) {
|
||||
}
|
||||
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.VoteFor = r.ID
|
||||
r.Extinct = true
|
||||
|
||||
@@ -44,6 +44,6 @@ func TestListMoveableGroupIds(t *testing.T) {
|
||||
for _, i := range movableGroups {
|
||||
sg := c.ShipGroup(i)
|
||||
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
@@ -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
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ func TestVotesByRace(t *testing.T) {
|
||||
assert.Equal(t, 0.78, vbr[Race_0_idx])
|
||||
assert.Contains(t, 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) {
|
||||
|
||||
@@ -4,7 +4,6 @@ func NewRaceUnknownError(arg ...any) error {
|
||||
return newGenericError(ErrInputUnknownRace, arg...)
|
||||
}
|
||||
func NewSameRaceError(arg ...any) error {
|
||||
// TODO: check all possible commands
|
||||
return newGenericError(ErrInputSameRace, arg...)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user