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.

133 lines
3.7 KiB

extends Control
const MeritProfileScene = preload("res://addons/majority_judgment/nodes/MajorityJudgmentMeritProfileControl.tscn")
export(Resource) var poll setget set_poll, get_poll
#func _ready():
# set_poll(load("res://test_poll.tres"))
func get_poll() -> MajorityJudgmentPoll:
return poll
func set_poll(__poll:MajorityJudgmentPoll):
poll = __poll
craft_nodes()
update_nodes()
# var provider = MajorityJudgmentDemoProvider.new()
var provider = MajorityJudgmentTwitchChatProvider.new()
start_provider(provider)
var profiles_nodes := Array()
func craft_nodes():
var tally : MajorityJudgmentPollTally = get_poll().tally()
var candidate_index = 0 # as they were initially written, before the sort
for candidate in get_poll().get_candidates():
# var candidate_tally := tally.get_tally_of_candidate(candidate)
# var merit_profile = candidate_tally.merit_profile
var profile = create_merit_profile_scene(6)
if tally:
profile.refresh(tally.get_tally_of_candidate(candidate).merit_profile)
var height = profile.compute_height()
# profile.margin_top = candidate_index * 50
# profile.margin_left = 0
# profile.margin_right = 0
# profile.margin_bottom = candidate_index * 50 + 42
profile.rect_min_size = Vector2(400, height)
var container = $CenterContainer/ProfilesContainer
var wrapper = HBoxContainer.new()
var candidate_label = Label.new()
candidate_label.text = candidate.get_name()
candidate_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
# candidate_label.size_flags_vertical = Control.SIZE_EXPAND_FILL
candidate_label.rect_min_size = Vector2(70, height)
candidate_label.valign = Label.VALIGN_CENTER
candidate_label.align = Label.ALIGN_RIGHT
wrapper.add_child(candidate_label)
wrapper.add_child(profile)
container.add_child(wrapper)
profiles_nodes.append(profile)
candidate_index += 1
func update_nodes():
var tally : MajorityJudgmentPollTally = get_poll().tally()
var candidate_index = 0 # as they were initially written, before the sort
for candidate in get_poll().get_candidates():
var profile = profiles_nodes[candidate_index]
if tally:
var merit = tally.get_tally_of_candidate(candidate).merit_profile
profile.refresh(merit)
else:
var merit = poll.get_dummy_merit_profile(candidate)
profile.refresh(merit)
candidate_index += 1
func create_merit_profile_scene(gradation_size:int):
var mps = MeritProfileScene.instance()
mps.set_poll(get_poll())
mps.craft_nodes(gradation_size)
return mps
var provider:MajorityJudgmentAbstractJudgmentsProvider
func start_provider(__provider):
self.provider = __provider
var connected = self.provider.connect(
"judgment_emitted",
self, "__on_judgment_emitted"
)
self.provider.start_providing()
var known_participants := Dictionary() # id => Participant
func get_or_create_participant(identifier:String) -> MajorityJudgmentParticipant:
if not known_participants.has(identifier):
known_participants[identifier] = MajorityJudgmentParticipant.make(identifier)
return known_participants[identifier]
func __on_judgment_emitted(author_identifier, grade_index, candidate_index):
# Data comes from userland, best be careful here
if (
grade_index < 0
or
grade_index >= poll.grading.grades.size()
):
# printerr("Ignoring grade #%d since it's out of range.")
return
if (
candidate_index < 0
or
candidate_index >= poll.candidates.size()
):
# printerr("Ignoring candidate %s since it's out of range." % [char(candidate_index)])
return
var j = MajorityJudgmentJudgment.new()
j.set_participant(get_or_create_participant(author_identifier))
j.set_grade(poll.grading.grades[grade_index])
j.set_candidate(poll.candidates[candidate_index])
poll.add_judgment(j)
update_nodes()