From 14e73217895307983ae762cb5ffd29ac45d8d1d1 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 22 May 2023 00:06:30 +0300 Subject: move quorum into its own package --- internal/quorum.go | 72 ------------------------------------------------------ 1 file changed, 72 deletions(-) delete mode 100644 internal/quorum.go (limited to 'internal/quorum.go') 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) - } -} -- cgit v1.2.3