You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB

extends Resource
class_name MajorityJudgmentCandidateMeritProfile
"""
The merit profile of a candidate is obtained
by putting on a single line the judgments they received,
sorted by increasing (or decreasing) grade.
This is built by the Talliers.
"""
export(Resource) var candidate_tally #:MajorityJudgmentCandidateTally
# For each grade (worst to best), the amount of judgments received
export(Array, int) var grades
export(Array, Color) var colors
#export(int) var grades_amount
func count_judgments() -> int:
var amount := 0
for amount_for_grade in self.grades:
amount += amount_for_grade
return amount
func count_grades_with_judgments() -> int:
var amount := 0
for amount_for_grade in self.grades:
if amount_for_grade:
amount += 1
return amount
func get_median() -> int:
"""
Returns the grade index, not a Grade instance.
0 = worst grade (most conservative?)
Returns -1 if there are no judgments.
"""
assert(self.grades)
var total := count_judgments()
if 0 == total:
return -1
var middle := 0
if 0 == total % 2: # total is even
middle = total / 2
else: # total is odd
middle = (total + 1) / 2
assert(middle > 0)
var amount := 0
for i in range(self.grades.size()):
assert(0 <= self.grades[i], "Negative grade amount… What? Cheater!")
amount += self.grades[i]
if amount >= middle:
return i
assert(false, "That should not happen")
return -1
func remove_one_judgment(grade_index:int) -> void:
"""
Used by the naive sorting algorithm, that works on a copy.
"""
assert(0 < self.grades[grade_index])
self.grades[grade_index] -= 1
func get_color_of_grade(grade_index:int) -> Color:
if self.colors:
return self.colors[grade_index]
if self.candidate_tally and self.candidate_tally.poll:
return self.candidate_tally.poll.grading.get_color_of_grade(grade_index)
return Color.blue