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 {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,19 +56,22 @@ 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 {
|
||||||
|
if _, ok := nodes[vid]; !ok {
|
||||||
nodes[vid] = &VoteNode{
|
nodes[vid] = &VoteNode{
|
||||||
ID: vid,
|
ID: vid,
|
||||||
}
|
}
|
||||||
@@ -76,6 +79,7 @@ func VotingGraph(races []game.Race, raceIndex func(uuid.UUID) int) []*VoteNode {
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -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...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user