Save the asked questions, and show them at the end

This commit is contained in:
Bill Rossi 2025-08-29 17:46:40 -04:00
parent 86415ef0e1
commit 57b7960de4

View File

@ -1,7 +1,7 @@
require "rufus-scheduler"
class Drill
attr_reader :responder, :current_question, :scheduler
attr_reader :responder, :current_question, :scheduler, :questions
attr_accessor :length, :participants, :timer_job_id
def initialize(responder: nil, scheduler: Rufus::Scheduler.new)
@ -11,6 +11,7 @@ class Drill
@running = false
@scheduler = scheduler
@time_is_up = false
@questions = []
end
def respond(response)
@ -67,6 +68,7 @@ class Drill
self.timer_job_id = scheduler.in(length){ @time_is_up = true }
respond "Drill started!"
participants.shuffle!
@questions = []
next_question!
end
@ -89,7 +91,7 @@ class Drill
end
def handle_question_response(message)
if current_question.correct_answer == message.content
if current_question.answer message.content
respond "Correct!"
else
respond "Incorrect: #{current_question.correct_answer}"
@ -108,17 +110,22 @@ class Drill
end
def end_drill!
respond "Time's up! Everyone did great!"
respond "Lesson's over!"
questions.group_by(&:target).each do |target, target_questions|
correct_count = target_questions.select(&:correct?).count
total_count = target_questions.count
percentage = "#{((correct_count.to_f / total_count) * 100).to_i}%"
respond "#{target.display_name} got #{correct_count} out of #{total_count} correct (#{percentage})"
end
@running = false
@participants = []
end
def generate_question
value = (1..100).to_a.sample
@current_question = OpenStruct.new(
question_text: "Type the number #{value}",
correct_answer: value.to_s
)
q = Question.new current_question_target
questions << q
@current_question = q
end
def current_question_target
@ -132,4 +139,26 @@ class Drill
def time_is_up?
@time_is_up
end
class Question
attr_reader :question_text, :correct_answer, :target
attr_accessor :given_answer
def initialize(target) # some kind of config here
@target = target
value = (1..100).to_a.sample
@question_text = "Type the number #{value}"
@correct_answer = value.to_s
end
def answer(response)
@given_answer = response
correct?
end
def correct?
given_answer == correct_answer
end
end
end