Compare commits
	
		
			No commits in common. "57b7960de4fbc9cc52dbebb674d0d08b817c4c7f" and "597084178478ba1b5734e8645f16daa191e19ff9" have entirely different histories.
		
	
	
		
			57b7960de4
			...
			5970841784
		
	
		
							
								
								
									
										55
									
								
								lib/drill.rb
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								lib/drill.rb
									
									
									
									
									
								
							| @ -1,7 +1,7 @@ | ||||
| require "rufus-scheduler" | ||||
| 
 | ||||
| class Drill | ||||
|   attr_reader :responder, :current_question, :scheduler, :questions | ||||
|   attr_reader :responder, :current_question, :scheduler | ||||
|   attr_accessor :length, :participants, :timer_job_id | ||||
| 
 | ||||
|   def initialize(responder: nil, scheduler: Rufus::Scheduler.new) | ||||
| @ -11,7 +11,6 @@ class Drill | ||||
|     @running = false | ||||
|     @scheduler = scheduler | ||||
|     @time_is_up = false | ||||
|     @questions = [] | ||||
|   end | ||||
| 
 | ||||
|   def respond(response) | ||||
| @ -38,8 +37,6 @@ class Drill | ||||
|   end | ||||
| 
 | ||||
|   def handle_join(message) | ||||
|     return respond "The drill's already started!" if running? | ||||
| 
 | ||||
|     if participants.include? message.author | ||||
|       respond "You've already joined" | ||||
|     else | ||||
| @ -49,8 +46,6 @@ class Drill | ||||
|   end | ||||
| 
 | ||||
|   def handle_leave(message) | ||||
|     return respond "The drill's already started!" if running? | ||||
| 
 | ||||
|     if participants.include? message.author | ||||
|       self.participants -= [message.author] | ||||
|       respond "#{message.author.display_name} has left" | ||||
| @ -61,23 +56,21 @@ class Drill | ||||
| 
 | ||||
|   def handle_start(message) | ||||
|     return respond "The drill's already started!" if running? | ||||
|     return respond "Nobody has joined the drill! Type `join` to join it!" if participants.empty? | ||||
| 
 | ||||
|     @running = true | ||||
|     @time_is_up = false | ||||
|     self.timer_job_id = scheduler.in(length){ @time_is_up = true } | ||||
|     respond "Drill started!" | ||||
|     participants.shuffle! | ||||
|     @questions = [] | ||||
|     next_question! | ||||
|   end | ||||
| 
 | ||||
|   def handle_stop(message) | ||||
|     return respond "The drill's not running yet" unless running? | ||||
| 
 | ||||
|     @running = false | ||||
|     scheduler.unschedule(timer_job_id) if timer_job_id | ||||
| 
 | ||||
|     end_drill! | ||||
|     respond "Drill stopped!" | ||||
|   end | ||||
| 
 | ||||
|   def handle_customize_length(message) | ||||
| @ -91,7 +84,7 @@ class Drill | ||||
|   end | ||||
| 
 | ||||
|   def handle_question_response(message) | ||||
|     if current_question.answer message.content | ||||
|     if current_question.correct_answer == message.content | ||||
|       respond "Correct!" | ||||
|     else | ||||
|       respond "Incorrect: #{current_question.correct_answer}" | ||||
| @ -110,22 +103,16 @@ class Drill | ||||
|   end | ||||
| 
 | ||||
|   def end_drill! | ||||
|     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 | ||||
| 
 | ||||
|     respond "Time's up! Everyone did great!" | ||||
|     @running = false | ||||
|     @participants = [] | ||||
|   end | ||||
| 
 | ||||
|   def generate_question | ||||
|     q = Question.new current_question_target | ||||
|     questions << q | ||||
|     @current_question = q | ||||
|     value = (1..100).to_a.sample | ||||
|     @current_question = OpenStruct.new( | ||||
|       question_text: "Type the number #{value}", | ||||
|       correct_answer: value.to_s | ||||
|     ) | ||||
|   end | ||||
| 
 | ||||
|   def current_question_target | ||||
| @ -139,26 +126,4 @@ 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 | ||||
|  | ||||
							
								
								
									
										72
									
								
								lib/drill_plugin.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								lib/drill_plugin.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,72 @@ | ||||
| require_relative "drill" | ||||
| 
 | ||||
| class DrillPlugin | ||||
|   COMMANDS = %w[ | ||||
|     help | ||||
|   ] | ||||
| 
 | ||||
|   attr_reader :bot, :drills | ||||
| 
 | ||||
|   def initialize(bot) | ||||
|     @bot = bot | ||||
|     @drills = Hash.new { Drill.new }  | ||||
|   end | ||||
| 
 | ||||
|   def handle_message(message) | ||||
|     puts "[#{self.class.name}] Handling \"#{message.content}\" from #{message.author.display_name}" | ||||
|     command = strip_prefix(message.content).split.first | ||||
|     p command | ||||
|     if command.nil? || command == "" || command == "help" || command == "!drill" | ||||
|       handle_help message | ||||
|     elsif COMMANDS.include? command | ||||
|       drill = drills[message.channel.id] | ||||
|       send("handle_#{command}", message, drill) | ||||
|     else | ||||
|       drill = drills[message.channel.id] | ||||
|       drill.handle_message(message) | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def handle_init(message, drill) | ||||
|     if drill.exists? | ||||
|       message.respond "You've already got a drill running in this channel" | ||||
|     else | ||||
|       drills[message.channel.id] = Drill.new message.channel | ||||
|       message.respond "New drill created!" | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def handle_start(message, drill) | ||||
|     if drill.started? | ||||
|       message.respond "This drill has already started!" | ||||
|     elsif drill.exists? | ||||
|       drill.start! | ||||
|       message.respond "Drill started!" | ||||
|     else | ||||
|       message.respond "You have to `!drill init` before you can start the drill" | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def handle_end(message, drill) | ||||
|     if !drill.exists? | ||||
|       message.respond "There's no drill to end" | ||||
|     elsif drill.started? | ||||
|       drills.delete message.channel.id | ||||
|       message.respond "Ending the drill now!" | ||||
|     else | ||||
|       message.respond "You have to `!drill start` before you can end the drill" | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def handle_help(message) | ||||
|     message.respond "Insert help message here" | ||||
|   end | ||||
| 
 | ||||
|   def strip_prefix(content) | ||||
|     if content.start_with? "!drill" | ||||
|       content.sub("!drill ", "") | ||||
|     else | ||||
|       content | ||||
|     end | ||||
|   end | ||||
| end | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user