diff --git a/database/poll.go b/database/poll.go index 5c26640..485d648 100644 --- a/database/poll.go +++ b/database/poll.go @@ -2,6 +2,8 @@ package database import ( "context" + "math" + "slices" "sort" "time" @@ -224,19 +226,15 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st for _, picks := range votes { // Go over picks until we find a non-eliminated candidate for _, candidate := range picks { - if !containsValue(eliminated, candidate) { - if _, ok := tallied[candidate]; ok { - tallied[candidate]++ - } else { - tallied[candidate] = 1 - } + if !slices.Contains(eliminated, candidate) { + tallied[candidate]++ voteCount += 1 break } } } // Eliminate lowest vote getter - minVote := 1000000 //the smallest number of votes received thus far (to find who is in last) + minVote := math.MaxInt //the smallest number of votes received thus far (to find who is in last) minPerson := make([]string, 0) //the person(s) with the least votes that need removed for person, vote := range tallied { if vote < minVote { // this should always be true round one, to set a true "who is in last" @@ -343,15 +341,6 @@ func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) { return nil, nil } -func containsValue(slice []string, value string) bool { - for _, item := range slice { - if item == value { - return true - } - } - return false -} - // orderOptions takes a RankedVote's options, and returns an ordered list of // their choices // @@ -366,11 +355,11 @@ func containsValue(slice []string, value string) bool { func orderOptions(ctx context.Context, options map[string]int) []string { // Figure out all the ranks they've listed var ranks []int = make([]int, len(options)) - reverse_map := make(map[int]string) + reverseMap := make(map[int]string) i := 0 for option, rank := range options { ranks[i] = rank - reverse_map[rank] = option + reverseMap[rank] = option i += 1 } @@ -379,7 +368,7 @@ func orderOptions(ctx context.Context, options map[string]int) []string { // normalise the ranks for counts that don't start at 1 var choices []string = make([]string, len(ranks)) for idx, rank := range ranks { - choices[idx] = reverse_map[rank] + choices[idx] = reverseMap[rank] } return choices