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) (existing_judgment.candidate == judgment.candidate)
): ):
judgments[i] = judgment judgments[i] = judgment
return
judgments.append(judgment) judgments.append(judgment)

@ -20,57 +20,60 @@ func set_poll(__poll:MajorityJudgmentPoll):
craft_nodes() craft_nodes()
update_nodes() update_nodes()
yield(get_tree().create_timer(3), "timeout") var provider = MajorityJudgmentDemoProvider.new()
start_provider(provider)
var j = MajorityJudgmentJudgment.new() # yield(get_tree().create_timer(3), "timeout")
j.set_participant(MajorityJudgmentParticipant.make("Plume")) #
j.set_grade(poll.grading.grades[2]) # var j = MajorityJudgmentJudgment.new()
j.set_candidate(poll.candidates[0]) # j.set_participant(MajorityJudgmentParticipant.make("Plume"))
poll.add_judgment(j) # j.set_grade(poll.grading.grades[2])
# j.set_candidate(poll.candidates[0])
var sylvain = MajorityJudgmentParticipant.make("Sylvain") # poll.add_judgment(j)
j = MajorityJudgmentJudgment.new() #
j.set_participant(sylvain) # var sylvain = MajorityJudgmentParticipant.make("Sylvain")
j.set_grade(poll.grading.grades[3]) # j = MajorityJudgmentJudgment.new()
j.set_candidate(poll.candidates[0]) # j.set_participant(sylvain)
poll.add_judgment(j) # j.set_grade(poll.grading.grades[3])
j = MajorityJudgmentJudgment.new() # j.set_candidate(poll.candidates[0])
j.set_participant(sylvain) # poll.add_judgment(j)
j.set_grade(poll.grading.grades[3]) # j = MajorityJudgmentJudgment.new()
j.set_candidate(poll.candidates[1]) # j.set_participant(sylvain)
poll.add_judgment(j) # j.set_grade(poll.grading.grades[3])
j = MajorityJudgmentJudgment.new() # j.set_candidate(poll.candidates[1])
j.set_participant(sylvain) # poll.add_judgment(j)
j.set_grade(poll.grading.grades[4]) # j = MajorityJudgmentJudgment.new()
j.set_candidate(poll.candidates[2]) # j.set_participant(sylvain)
poll.add_judgment(j) # j.set_grade(poll.grading.grades[4])
# j.set_candidate(poll.candidates[2])
j = MajorityJudgmentJudgment.new() # poll.add_judgment(j)
j.set_participant(MajorityJudgmentParticipant.make("Sabre")) #
j.set_grade(poll.grading.grades[5]) # j = MajorityJudgmentJudgment.new()
j.set_candidate(poll.candidates[0]) # j.set_participant(MajorityJudgmentParticipant.make("Sabre"))
poll.add_judgment(j) # j.set_grade(poll.grading.grades[5])
# j.set_candidate(poll.candidates[0])
update_nodes() # poll.add_judgment(j)
#
yield(get_tree().create_timer(2), "timeout") # update_nodes()
#
var tiger = MajorityJudgmentParticipant.make("Tiger") # yield(get_tree().create_timer(2), "timeout")
j = MajorityJudgmentJudgment.new() #
j.set_participant(tiger) # var tiger = MajorityJudgmentParticipant.make("Tiger")
j.set_grade(poll.grading.grades[4]) # j = MajorityJudgmentJudgment.new()
j.set_candidate(poll.candidates[0]) # j.set_participant(tiger)
poll.add_judgment(j) # j.set_grade(poll.grading.grades[4])
update_nodes() # j.set_candidate(poll.candidates[0])
# poll.add_judgment(j)
yield(get_tree().create_timer(2), "timeout") # update_nodes()
#
j = MajorityJudgmentJudgment.new() # yield(get_tree().create_timer(2), "timeout")
j.set_participant(tiger) #
j.set_grade(poll.grading.grades[1]) # j = MajorityJudgmentJudgment.new()
j.set_candidate(poll.candidates[2]) # j.set_participant(tiger)
poll.add_judgment(j) # j.set_grade(poll.grading.grades[1])
update_nodes() # j.set_candidate(poll.candidates[2])
# poll.add_judgment(j)
# update_nodes()
var profiles_nodes := Array() var profiles_nodes := Array()
func craft_nodes(): func craft_nodes():
@ -115,8 +118,6 @@ func update_nodes():
var tally : MajorityJudgmentPollTally = get_poll().tally() var tally : MajorityJudgmentPollTally = get_poll().tally()
var candidate_index = 0 # as they were initially written, before the sort var candidate_index = 0 # as they were initially written, before the sort
for candidate in get_poll().get_candidates(): 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] var profile = profiles_nodes[candidate_index]
if tally: if tally:
var merit = tally.get_tally_of_candidate(candidate).merit_profile 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.set_poll(get_poll())
mps.craft_nodes(gradation_size) mps.craft_nodes(gradation_size)
return mps 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"] [node name="ProfilesContainer" type="VBoxContainer" parent="CenterContainer"]
margin_left = 312.0 margin_left = 312.0
margin_top = 99.0 margin_top = 100.0
margin_right = 712.0 margin_right = 712.0
margin_bottom = 499.0 margin_bottom = 500.0
rect_min_size = Vector2( 400, 400 ) rect_min_size = Vector2( 400, 400 )

@ -1,13 +1,42 @@
extends Reference extends Reference
class_name MajorityJudgmentAbstractJudgmentsProvider 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. # 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( signal judgment_emitted(
author_identifier, # String, unique per author (aka participant) 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 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 # Command examples
# ---------------- # ----------------
# #
@ -10,9 +34,6 @@ class_name MajorityJudgmentChatCommandJudgmentsProvider
func process_chat_command(author_identifier:String, chat_command:String): func process_chat_command(author_identifier:String, chat_command:String):
var candidate_index := 0
var grade_index := 0
var regex = RegEx.new() var regex = RegEx.new()
regex.compile("(?<candidate>[a-zA-Z]{1})(?<grade>[0-9]{1})") 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: if results:
for result in results: for result in results:
var candidate_string : String = result.get_string('candidate') 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_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( emit_signal(
"judgment_emitted", "judgment_emitted",
author_identifier, 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) get_tree().current_scene.set_poll(poll)
func timer(duration:int) -> SceneTreeTimer:
return get_tree().create_timer(duration)

Loading…
Cancel
Save