diff options
| author | Paul Buetow <paul@buetow.org> | 2023-05-22 00:06:30 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-05-22 00:06:30 +0300 |
| commit | 14e73217895307983ae762cb5ffd29ac45d8d1d1 (patch) | |
| tree | e90dd9743a30433348bfa9729011db1fad3886df /internal/quorum.go | |
| parent | 1eb99dc0b6d04d85b9736ab5a050e08d410abb4a (diff) | |
move quorum into its own package
Diffstat (limited to 'internal/quorum.go')
| -rw-r--r-- | internal/quorum.go | 72 |
1 files changed, 0 insertions, 72 deletions
diff --git a/internal/quorum.go b/internal/quorum.go deleted file mode 100644 index a64b07b..0000000 --- a/internal/quorum.go +++ /dev/null @@ -1,72 +0,0 @@ -package internal - -import ( - "fmt" - "log" - "sort" - - "codeberg.org/snonux/gorum/internal/config" - "codeberg.org/snonux/gorum/internal/vote" -) - -type quorumMap map[string]vote.Vote - -type score struct { - id string - score int -} - -func (q quorumMap) vote(v vote.Vote) { - log.Printf("Adding vote %v", v) - q[v.From] = v -} - -func (q quorumMap) winner(conf config.Config) (string, error) { - scores := q.score(conf) - if len(scores) == 0 { - return "", fmt.Errorf("unable to find a winner, empty score list") - } - return scores[0].id, nil -} - -func (q quorumMap) score(conf config.Config) (scores []score) { - scoreMap := make(map[string]int) - - for _, vote := range q { - for _, id := range vote.IDs { - score, _ := scoreMap[id] - scoreMap[id] = score + 1 - } - } - - for id, score_ := range scoreMap { - scores = append(scores, score{id, score_}) - } - - sort.Slice(scores, func(i, j int) bool { - if scores[i].score != scores[j].score { - return scores[i].score > scores[j].score - } - - // Score tie, use participant number. - i_, _ := conf.ParticipantNumber(scores[i].id) - j_, _ := conf.ParticipantNumber(scores[j].id) - return i_ < j_ - }) - - return -} - -func (q quorumMap) cleanExpired() { - var expired []string - - for from, vote := range q { - if vote.Expired() { - expired = append(expired, from) - } - } - - for _, e := range expired { - delete(q, e) - } -} |
