require "rufus-scheduler" DEFAULT_LESSON_PLAN = [ [15, "login and catch up on the week"], [45, "review chapter vocab and grammar"], [15, "answer Seth Clydesdale Genki review questions"], [15, "ask closing questions and have Japanese conversation"], ] class Lesson attr_reader :lesson_plan, :channel, :running, :scheduler, :responder attr_accessor :current_lesson_step_index, :current_lesson_step_job_id def initialize(lesson_plan=DEFAULT_LESSON_PLAN, scheduler: Rufus::Scheduler.new, responder: nil) @lesson_plan = lesson_plan @running = false @scheduler = scheduler @responder = responder @current_lesson_step_index = 0 @current_lesson_step_job_id = nil end def respond(response) responder.call response end def stop! @scheduler.shutdown @running = false end def start! self.current_lesson_step_index = 0 run_current_lesson_step @running = true end def current_lesson_step lesson_plan[current_lesson_step_index] end def next_lesson_step lesson_plan[current_lesson_step_index + 1] end def run_current_lesson_step scheduler.unschedule(current_lesson_step_job_id) if current_lesson_step_job_id if current_lesson_step.nil? end_lesson return end duration, comment = current_lesson_step respond "#{duration} minutes to #{comment}" self.current_lesson_step_job_id = scheduler.in "#{duration}s" do if current_lesson_step_index == lesson_plan.length - 1 end_lesson else respond "Time's up! Type `next` to move to the next part of the lesson" end self.current_lesson_step_job_id = nil end end def end_lesson respond "Lesson over!" @running = false self.current_lesson_step_job_id = nil end def running? running end ACTIONS = %w[next previous pause help] def can_handle_message?(message) return false unless running? ACTIONS.include? message.content.downcase end def handle_message(message) return false unless can_handle_message?(message) send("handle_#{message.content.downcase}", message) end def handle_next(_) self.current_lesson_step_index += 1 run_current_lesson_step end def handle_previous(message) respond "Prev!" end def handle_pause(message) respond "Stop!" end def handle_help(message) respond "HALP!" end end