Move lessons to a plugin

This commit is contained in:
Bill Rossi 2025-08-23 20:27:45 -04:00
parent 5e18991d58
commit 8320d2dfcb
5 changed files with 127 additions and 29 deletions

View File

@ -1,45 +1,26 @@
require_relative './session'
require_relative './lesson'
require_relative './commands'
require_relative './message'
require_relative './drill_plugin'
require_relative './ika/plugin'
class Ika
attr_reader :handler
attr_reader :lessons, :sessions
attr_accessor :current_lesson, :current_session
attr_reader :drill_plugin
attr_reader :plugins
PLUGIN_TYPES = [
Plugin::HelpPlugin,
Plugin::LessonPlugin
]
def self.for(type:, **kwargs)
type.new **kwargs
end
def initialize
@lessons = {}
@sessions = {}
@current_session = nil
@current_lesson = Lesson.new(responder: responder(nil))
@drill_plugin = DrillPlugin.new(self)
end
def current_lesson_running?
current_lesson.running?
end
def start_lesson!(message)
current_lesson.responder = responder(message.channel)
current_lesson.start!(message.channel)
end
def stop_current_lesson!
current_lesson.stop!
@plugins = PLUGIN_TYPES.map { |plugin_type| plugin_type.new self }
end
def handle_message(message)
if command = Commands::Command.for(self, message)
command.execute
end
# drill_plugin.handle_message message
plugins.find { |plugin| plugin.handle_message message }
end
end

18
lib/ika/plugin.rb Normal file
View File

@ -0,0 +1,18 @@
require_relative "./plugin/help_plugin"
require_relative "./plugin/lesson_plugin"
class Plugin
attr_reader :ika
def initialize(ika)
@ika = ika
end
def handle_message(message)
can_handle_message? message
end
def can_handle_message?(_)
false
end
end

View File

@ -0,0 +1,14 @@
class Plugin
class HelpPlugin < Plugin
def handle_message(message)
return false unless super
message.respond "I'm beyond help"
end
def can_handle_message?(message)
message.content == "!help"
end
end
end

View File

@ -0,0 +1,80 @@
require_relative "../../lesson"
class Plugin
class LessonPlugin < Plugin
attr_reader :lessons
def initialize(ika)
super
@lessons = {}
end
def handle_message(message)
super
Message.new(self, message).handle
end
def can_handle_message?(message)
return true if message.content == "!lesson"
return true if message.content == "!stoplesson"
return false unless lessons[message.channel.id]
true
end
class Message
attr_reader :plugin, :message
def initialize(plugin, message)
@plugin = plugin
@message = message
end
def content
message.content
end
def responder
message.responder
end
def respond(response)
responder.call response
end
def handle
return handle_lesson if content == "!lesson"
return handle_stoplesson if content == "!stoplesson"
lesson.handle_message(message)
end
def lesson
lsn = plugin.lessons[message.channel.id]
return lsn if lsn
lsn = Lesson.new responder: responder
plugin.lessons[message.channel.id] = lsn
end
def handle_lesson
if lesson.running?
respond "There's already a lesson running!"
else
respond "Starting a lesson now!"
lesson.start!
end
end
def handle_stoplesson
if lesson.running?
respond "Ending the current lesson"
lesson.stop!
else
respond "There's no lesson running"
end
end
end
end
end

View File

@ -20,9 +20,10 @@ class Lesson
def stop!
@scheduler.shutdown
@running = false
end
def start!(channel)
def start!
lesson_plan.each_with_index do |(time, message), index|
offset = lesson_plan.map(&:first)[0..index - 1].sum(0)
offset = 0 if index == 0
@ -43,4 +44,8 @@ class Lesson
def running?
running
end
def handle_message(_)
# TODO: make it possible to advance the lesson or whatever
end
end