ika_bot/lib/lesson.rb

108 lines
2.3 KiB
Ruby
Raw Permalink Normal View History

require "rufus-scheduler"
2025-08-14 20:02:00 -04:00
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
2025-08-24 08:36:10 -04:00
attr_reader :lesson_plan, :channel, :running, :scheduler, :responder
attr_accessor :current_lesson_step_index, :current_lesson_step_job_id
2025-08-14 20:02:00 -04:00
2025-08-23 14:38:42 -04:00
def initialize(lesson_plan=DEFAULT_LESSON_PLAN, scheduler: Rufus::Scheduler.new, responder: nil)
2025-08-14 20:02:00 -04:00
@lesson_plan = lesson_plan
@running = false
@scheduler = scheduler
2025-08-23 14:38:42 -04:00
@responder = responder
2025-08-24 08:36:10 -04:00
@current_lesson_step_index = 0
@current_lesson_step_job_id = nil
end
def respond(response)
responder.call response
2025-08-14 20:02:00 -04:00
end
def stop!
@scheduler.shutdown
2025-08-23 20:27:45 -04:00
@running = false
2025-08-14 20:02:00 -04:00
end
2025-08-23 20:27:45 -04:00
def start!
2025-08-24 08:36:10 -04:00
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
2025-08-24 08:36:10 -04:00
duration, comment = current_lesson_step
respond "#{duration} minutes to #{comment}"
2025-08-27 18:03:07 -04:00
self.current_lesson_step_job_id = scheduler.in "#{duration}m" do
2025-08-24 08:36:10 -04:00
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
2025-08-14 20:02:00 -04:00
end
2025-08-24 08:36:10 -04:00
end
2025-08-24 08:36:10 -04:00
def end_lesson
respond "Lesson over!"
@running = false
self.current_lesson_step_job_id = nil
2025-08-14 20:02:00 -04:00
end
def running?
running
end
2025-08-23 20:27:45 -04:00
2025-08-24 08:36:10 -04:00
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!"
2025-08-23 20:27:45 -04:00
end
2025-08-14 20:02:00 -04:00
end