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.

127 lines
3.0 KiB

extends Resource
class_name MajorityJudgmentPoll
"""
A poll with multiple candidates, where each participant
gives a single grade to each of the candidates.
The candidates are then sorted by their median grade.
https://en.wikipedia.org/wiki/Majority_Judgment
The constitutive details of the poll are up to you.
"""
# Minimum length: 4 unicode characters (? TBD)
# Maximum length: 256 unicode characters (? TBD)
export(String) var title:String setget set_title, get_title
# > How do we localize this?
# Something like this, perhaps?
# title_of_locale = {
# 'en_US': "President of the United States of America in 2020",
# 'fr_FR': "Président des États-Unis d'Amérique en 2020",
# …
# }
#export(Dictionary) var localized_titles:Dictionary
# or perhaps
# https://docs.godotengine.org/en/stable/tutorials/i18n/internationalizing_games.html
export(Resource) var grading #:MajorityJudgmentGrading
# Array of MajorityJudgmentCandidate
export(Array, Resource) var candidates:Array setget set_candidates, get_candidates
# Array of MajorityJudgmentJudgments
export(Array, Resource) var judgments:Array setget set_judgments, get_judgments
# Don't pass parameters in _init().
# ResourceLoader.load() won't like it.
func _init():
pass
# Instead, use a factory approach
#static func make(__title, __grading):
func set_title(__title:String) -> void:
title = __title
func get_title() -> String:
if null == title:
return ''
return title
func set_grading(__grading:MajorityJudgmentGrading) -> void:
grading = __grading
func get_grading() -> MajorityJudgmentGrading:
assert(grading, "Poll has no grading.")
return grading
func set_candidates(__candidates:Array) -> void:
assert(__candidates, "Poll requires at least one candidate.")
candidates = __candidates
func add_candidate(candidate:MajorityJudgmentCandidate) -> void:
if not candidates:
candidates = Array()
candidates.append(candidate)
func get_candidates() -> Array:
assert(candidates, "Poll has no candidates.")
return candidates
func set_judgments(__judgments:Array) -> void:
judgments = __judgments
func add_judgment(judgment:MajorityJudgmentJudgment) -> void:
if not judgments:
judgments = Array()
for i in range(judgments.size()):
var existing_judgment = judgments[i]
if (
(existing_judgment.participant == judgment.participant)
and
(existing_judgment.candidate == judgment.candidate)
):
judgments[i] = judgment
judgments.append(judgment)
func get_judgments() -> Array:
assert(judgments, "Poll has no judgments.")
return judgments
func tally() -> MajorityJudgmentPollTally:
# Pick relevant tallier from settings later on
var tallier = MajorityJudgmentEasyTallier.new()
# var tallier = MajorityJudgmentLiquidTallier.new()
return tallier.tally(self)
func get_participants() -> Array:
var participants := Array()
for judgment in judgments:
var participant : MajorityJudgmentParticipant = judgment.participant
if not participants.has(participant):
participants.append(participant)
return participants
func count_participants() -> int:
return get_participants().size()