Compare commits

..

3 Commits

2 changed files with 45 additions and 82 deletions

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)
@ -37,6 +38,8 @@ 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
@ -46,6 +49,8 @@ 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"
@ -56,21 +61,23 @@ 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
respond "Drill stopped!"
end_drill!
end
def handle_customize_length(message)
@ -84,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}"
@ -103,16 +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
@ -126,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

View File

@ -1,72 +0,0 @@
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