Compare commits
3 Commits
5970841784
...
57b7960de4
Author | SHA1 | Date | |
---|---|---|---|
57b7960de4 | |||
86415ef0e1 | |||
f2870ae4f6 |
55
lib/drill.rb
55
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)
|
||||||
@ -37,6 +38,8 @@ class Drill
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_join(message)
|
def handle_join(message)
|
||||||
|
return respond "The drill's already started!" if running?
|
||||||
|
|
||||||
if participants.include? message.author
|
if participants.include? message.author
|
||||||
respond "You've already joined"
|
respond "You've already joined"
|
||||||
else
|
else
|
||||||
@ -46,6 +49,8 @@ class Drill
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_leave(message)
|
def handle_leave(message)
|
||||||
|
return respond "The drill's already started!" if running?
|
||||||
|
|
||||||
if participants.include? message.author
|
if participants.include? message.author
|
||||||
self.participants -= [message.author]
|
self.participants -= [message.author]
|
||||||
respond "#{message.author.display_name} has left"
|
respond "#{message.author.display_name} has left"
|
||||||
@ -56,21 +61,23 @@ class Drill
|
|||||||
|
|
||||||
def handle_start(message)
|
def handle_start(message)
|
||||||
return respond "The drill's already started!" if running?
|
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
|
@running = true
|
||||||
@time_is_up = false
|
@time_is_up = false
|
||||||
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
|
||||||
|
|
||||||
def handle_stop(message)
|
def handle_stop(message)
|
||||||
return respond "The drill's not running yet" unless running?
|
return respond "The drill's not running yet" unless running?
|
||||||
|
|
||||||
@running = false
|
|
||||||
scheduler.unschedule(timer_job_id) if timer_job_id
|
scheduler.unschedule(timer_job_id) if timer_job_id
|
||||||
respond "Drill stopped!"
|
|
||||||
|
end_drill!
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_customize_length(message)
|
def handle_customize_length(message)
|
||||||
@ -84,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}"
|
||||||
@ -103,16 +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 = []
|
||||||
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
|
||||||
@ -126,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
|
||||||
|
@ -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
|
|
Loading…
Reference in New Issue
Block a user