feat: enable the provider pattern, with a demo provider

So far it's working quite well,
and it is rather pleasant to code.

There are no safeguards whatsoever for now.
They will come along with their tests :3
master
Dominique Merle 4 years ago
parent 205608d75f
commit e5cb86068d

@ -107,6 +107,7 @@ func add_judgment(judgment:MajorityJudgmentJudgment) -> void:
(existing_judgment.candidate == judgment.candidate)
):
judgments[i] = judgment
return
judgments.append(judgment)

@ -20,57 +20,60 @@ func set_poll(__poll:MajorityJudgmentPoll):
craft_nodes()
update_nodes()
yield(get_tree().create_timer(3), "timeout")
var provider = MajorityJudgmentDemoProvider.new()
start_provider(provider)
var j = MajorityJudgmentJudgment.new()
j.set_participant(MajorityJudgmentParticipant.make("Plume"))
j.set_grade(poll.grading.grades[2])
j.set_candidate(poll.candidates[0])
poll.add_judgment(j)
var sylvain = MajorityJudgmentParticipant.make("Sylvain")
j = MajorityJudgmentJudgment.new()
j.set_participant(sylvain)
j.set_grade(poll.grading.grades[3])
j.set_candidate(poll.candidates[0])
poll.add_judgment(j)
j = MajorityJudgmentJudgment.new()
j.set_participant(sylvain)
j.set_grade(poll.grading.grades[3])
j.set_candidate(poll.candidates[1])
poll.add_judgment(j)
j = MajorityJudgmentJudgment.new()
j.set_participant(sylvain)
j.set_grade(poll.grading.grades[4])
j.set_candidate(poll.candidates[2])
poll.add_judgment(j)
j = MajorityJudgmentJudgment.new()
j.set_participant(MajorityJudgmentParticipant.make("Sabre"))
j.set_grade(poll.grading.grades[5])
j.set_candidate(poll.candidates[0])
poll.add_judgment(j)
update_nodes()
yield(get_tree().create_timer(2), "timeout")
var tiger = MajorityJudgmentParticipant.make("Tiger")
j = MajorityJudgmentJudgment.new()
j.set_participant(tiger)
j.set_grade(poll.grading.grades[4])
j.set_candidate(poll.candidates[0])
poll.add_judgment(j)
update_nodes()
yield(get_tree().create_timer(2), "timeout")
j = MajorityJudgmentJudgment.new()
j.set_participant(tiger)
j.set_grade(poll.grading.grades[1])
j.set_candidate(poll.candidates[2])
poll.add_judgment(j)
update_nodes()
# yield(get_tree().create_timer(3), "timeout")
#
# var j = MajorityJudgmentJudgment.new()
# j.set_participant(MajorityJudgmentParticipant.make("Plume"))
# j.set_grade(poll.grading.grades[2])
# j.set_candidate(poll.candidates[0])
# poll.add_judgment(j)
#
# var sylvain = MajorityJudgmentParticipant.make("Sylvain")
# j = MajorityJudgmentJudgment.new()
# j.set_participant(sylvain)
# j.set_grade(poll.grading.grades[3])
# j.set_candidate(poll.candidates[0])
# poll.add_judgment(j)
# j = MajorityJudgmentJudgment.new()
# j.set_participant(sylvain)
# j.set_grade(poll.grading.grades[3])
# j.set_candidate(poll.candidates[1])
# poll.add_judgment(j)
# j = MajorityJudgmentJudgment.new()
# j.set_participant(sylvain)
# j.set_grade(poll.grading.grades[4])
# j.set_candidate(poll.candidates[2])
# poll.add_judgment(j)
#
# j = MajorityJudgmentJudgment.new()
# j.set_participant(MajorityJudgmentParticipant.make("Sabre"))
# j.set_grade(poll.grading.grades[5])
# j.set_candidate(poll.candidates[0])
# poll.add_judgment(j)
#
# update_nodes()
#
# yield(get_tree().create_timer(2), "timeout")
#
# var tiger = MajorityJudgmentParticipant.make("Tiger")
# j = MajorityJudgmentJudgment.new()
# j.set_participant(tiger)
# j.set_grade(poll.grading.grades[4])
# j.set_candidate(poll.candidates[0])
# poll.add_judgment(j)
# update_nodes()
#
# yield(get_tree().create_timer(2), "timeout")
#
# j = MajorityJudgmentJudgment.new()
# j.set_participant(tiger)
# j.set_grade(poll.grading.grades[1])
# j.set_candidate(poll.candidates[2])
# poll.add_judgment(j)
# update_nodes()
var profiles_nodes := Array()
func craft_nodes():
@ -115,8 +118,6 @@ 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 candidate_tally := tally.get_tally_of_candidate(candidate)
# var merit_profile = candidate_tally.merit_profile
var profile = profiles_nodes[candidate_index]
if tally:
var merit = tally.get_tally_of_candidate(candidate).merit_profile
@ -178,3 +179,39 @@ func create_merit_profile_scene(gradation_size:int):
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
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()

@ -16,7 +16,7 @@ anchor_bottom = 1.0
[node name="ProfilesContainer" type="VBoxContainer" parent="CenterContainer"]
margin_left = 312.0
margin_top = 99.0
margin_top = 100.0
margin_right = 712.0
margin_bottom = 499.0
margin_bottom = 500.0
rect_min_size = Vector2( 400, 400 )

@ -1,13 +1,42 @@
extends Reference
class_name MajorityJudgmentAbstractJudgmentsProvider
#class_name MajorityJudgmentAbstractProvider # perhaps this? (short = sweet)
# Abstract class for Judgment Providers
# Abstract class for (Judgment) Providers
# The job of the children is to emit the judgment_emitted signal.
#
# Example providers (ideas):
# - Test/Demo providers
# - Twitch chat commands (like "A3B2C0")
# - Youtube Live chat commands (if possible)
# - IRC chat commands (low, as a bot would be better)
# - Discord, somehow
# - Riot, Jami, etc.
# - Heck, any videoconferencing software with a workable API
#
# So far, providers don't care about whether the candidate or grade indices
# are within correct range ; that work will be done by the listener to the
# "judgment_emitted" event, and it will do its best, and ignore bad data.
#
# See MajorityJudgmentLinearResultsControl.gd, that's where a (the) listener is.
signal judgment_emitted(
author_identifier, # String, unique per author (aka participant)
candidate_index, # int
grade_index # int (0 == REJECT, up to the grading size minus one)
grade_index, # int (0 == REJECT, up to the grading size minus one)
candidate_index # int, position in the original array of candidates
)
# Called by the scene managing the poll's lifecycle.
# See MajorityJudgmentLinearResultsControl.gd
# Meant to be overridden (put your logic here instead of _init)
func start_providing():
pass
# Meant to be overridden.
# In here you can stop your timers, close your network connections, etc.
func stop_providing():
pass

@ -2,6 +2,30 @@ extends MajorityJudgmentAbstractJudgmentsProvider
class_name MajorityJudgmentChatCommandJudgmentsProvider
# Base class for chat command providers to extend.
# Is able to process_chat_command().
#
# Command Syntax
# --------------
#
# <candidate><grade>
#
# such as
#
# A2
#
# means give grade #2 (passable) to candidate A.
#
# Grades (when there are 6):
#
# 0. TO_REJECT .0
# 1. POOR .1
# 2. PASSABLE .2
# 3. GOOD .3
# 4. VERY_GOOD .4
# 5. EXCELLENT .5
#
#
# Command examples
# ----------------
#
@ -10,9 +34,6 @@ class_name MajorityJudgmentChatCommandJudgmentsProvider
func process_chat_command(author_identifier:String, chat_command:String):
var candidate_index := 0
var grade_index := 0
var regex = RegEx.new()
regex.compile("(?<candidate>[a-zA-Z]{1})(?<grade>[0-9]{1})")
@ -20,15 +41,14 @@ func process_chat_command(author_identifier:String, chat_command:String):
if results:
for result in results:
var candidate_string : String = result.get_string('candidate')
var grade_string : String = result.get_string('grade')
grade_index = str2var(grade_string)
candidate_string = candidate_string.to_upper()
candidate_index = ord(candidate_string) - ord("A")
var candidate_index = ord(candidate_string) - ord("A")
var grade_string : String = result.get_string('grade')
var grade_index = str2var(grade_string) # FIXME: safer, please
emit_signal(
"judgment_emitted",
author_identifier,
candidate_index,
grade_index
grade_index,
candidate_index
)

@ -0,0 +1,20 @@
extends MajorityJudgmentChatCommandJudgmentsProvider
class_name MajorityJudgmentDemoProvider
func start_providing():
yield(App.timer(1), "timeout")
process_chat_command("Stup", "A0B1C3")
yield(App.timer(1), "timeout")
process_chat_command("Flip", "A0B2C1")
yield(App.timer(1), "timeout")
process_chat_command("Flip", "A1")
yield(App.timer(1), "timeout")
process_chat_command("Clap", "A3B4")
yield(App.timer(1), "timeout")
process_chat_command("Clap", "C5B3")
yield(App.timer(2), "timeout")
process_chat_command("Neo", "B5")
yield(App.timer(1), "timeout")
process_chat_command("April", "A4")

@ -44,3 +44,5 @@ func start_poll(poll):
get_tree().current_scene.set_poll(poll)
func timer(duration:int) -> SceneTreeTimer:
return get_tree().create_timer(duration)

Loading…
Cancel
Save