Save the asked questions, and show them at the end
This commit is contained in:
		
							parent
							
								
									86415ef0e1
								
							
						
					
					
						commit
						57b7960de4
					
				
							
								
								
									
										45
									
								
								lib/drill.rb
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								lib/drill.rb
									
									
									
									
									
								
							| @ -1,7 +1,7 @@ | |||||||
| require "rufus-scheduler" | require "rufus-scheduler" | ||||||
| 
 | 
 | ||||||
| class Drill | class Drill | ||||||
|   attr_reader :responder, :current_question, :scheduler |   attr_reader :responder, :current_question, :scheduler, :questions | ||||||
|   attr_accessor :length, :participants, :timer_job_id |   attr_accessor :length, :participants, :timer_job_id | ||||||
| 
 | 
 | ||||||
|   def initialize(responder: nil, scheduler: Rufus::Scheduler.new) |   def initialize(responder: nil, scheduler: Rufus::Scheduler.new) | ||||||
| @ -11,6 +11,7 @@ class Drill | |||||||
|     @running = false |     @running = false | ||||||
|     @scheduler = scheduler |     @scheduler = scheduler | ||||||
|     @time_is_up = false |     @time_is_up = false | ||||||
|  |     @questions = [] | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def respond(response) |   def respond(response) | ||||||
| @ -67,6 +68,7 @@ class Drill | |||||||
|     self.timer_job_id = scheduler.in(length){ @time_is_up = true } |     self.timer_job_id = scheduler.in(length){ @time_is_up = true } | ||||||
|     respond "Drill started!" |     respond "Drill started!" | ||||||
|     participants.shuffle! |     participants.shuffle! | ||||||
|  |     @questions = [] | ||||||
|     next_question! |     next_question! | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
| @ -89,7 +91,7 @@ class Drill | |||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def handle_question_response(message) |   def handle_question_response(message) | ||||||
|     if current_question.correct_answer == message.content |     if current_question.answer message.content | ||||||
|       respond "Correct!" |       respond "Correct!" | ||||||
|     else |     else | ||||||
|       respond "Incorrect: #{current_question.correct_answer}" |       respond "Incorrect: #{current_question.correct_answer}" | ||||||
| @ -108,17 +110,22 @@ class Drill | |||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def end_drill! |   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 |     @running = false | ||||||
|     @participants = [] |     @participants = [] | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def generate_question |   def generate_question | ||||||
|     value = (1..100).to_a.sample |     q = Question.new current_question_target | ||||||
|     @current_question = OpenStruct.new( |     questions << q | ||||||
|       question_text: "Type the number #{value}", |     @current_question = q | ||||||
|       correct_answer: value.to_s |  | ||||||
|     ) |  | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def current_question_target |   def current_question_target | ||||||
| @ -132,4 +139,26 @@ class Drill | |||||||
|   def time_is_up? |   def time_is_up? | ||||||
|     @time_is_up |     @time_is_up | ||||||
|   end |   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 | end | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user