class Trivia
Constants
- DATA_PATH
- ROOT
Public Class Methods
Source
# File plugins/trivia.rb, line 83 def self.check_answer(round, guess) normalized = guess.to_s.strip.downcase return false if normalized.empty? answers = [round["answer"], *Array(round["aliases"])].map { |value| value.to_s.downcase } answers.any? { |answer| normalized.include?(answer) || answer.include?(normalized) } end
Source
# File plugins/trivia.rb, line 22 def self.command_pattern /trivia(?:\s+(.+))?$/i end
Source
# File plugins/trivia.rb, line 39 def self.default_fetch(url) RabbotHttp.fetch(url, user_agent: FandomTrivia::USER_AGENT) end
Source
# File plugins/trivia.rb, line 51 def self.fallback_round(rand: ->(max) { Random.rand(0...max) }) question = questions[rand.call(questions.length)] { "question" => question["question"], "answer" => question["answer"], "aliases" => Array(question["aliases"]), "source_type" => "static" } end
Source
# File plugins/trivia.rb, line 43 def self.load_round(network:, channel:, store: RabbotDb) store.get_plugin_json(plugin: "trivia", key: "round", network: network, channel: channel, default: nil) end
Source
# File plugins/trivia.rb, line 91 def self.message_guessable?(text) normalized = text.to_s.strip return false if normalized.empty? return false if normalized.start_with?("!") normalized.length >= 2 end
Source
# File plugins/trivia.rb, line 99 def self.process_answer(network:, channel:, guess:, store: RabbotDb) round = load_round(network: network, channel: channel, store: store) return :no_round unless round if check_answer(round, guess) save_round(network: network, channel: channel, round: nil, store: store) :correct else :wrong end end
Source
# File plugins/trivia.rb, line 31 def self.questions @questions ||= JSON.parse(File.read(DATA_PATH)) end
Source
# File plugins/trivia.rb, line 35 def self.reset_questions! @questions = nil end
Source
# File plugins/trivia.rb, line 47 def self.save_round(network:, channel:, round:, store: RabbotDb) store.set_plugin_json(plugin: "trivia", key: "round", network: network, channel: channel, value: round) end
Source
# File plugins/trivia.rb, line 61 def self.start_round(network:, channel:, profile: nil, channel_users: nil, bots_dir: nil, root: ROOT, fetch: method(:default_fetch), rand: ->(max) { Random.rand(0...max) }, store: RabbotDb, bot_nicks: ChannelTrust.persona_bot_nicks) round = nil bots_dir ||= FandomTriviaSources.default_bots_dir(root: root) profiles = FandomTriviaSources.discover_profiles( channel_users: channel_users, bot_nicks: bot_nicks, bots_dir: bots_dir, root: root ) if profiles.empty? && profile && FandomTrivia.fandom_source?(profile) profiles = [profile] end if profiles.any? round = FandomTrivia.fetch_round_from_sources(profiles, fetch: fetch, rand: rand, store: store) end round ||= fallback_round(rand: rand) save_round(network: network, channel: channel, round: round, store: store) round end
Public Instance Methods
Source
# File plugins/trivia.rb, line 111 def execute(m, args = nil) unless m.channel m.reply "Use !trivia in a channel" return end network = bot.config.server channel = m.channel.name text = args.to_s.strip if text.match?(/\Aanswer\s+(.+)\z/i) guess = text.sub(/\Aanswer\s+/i, "") case self.class.process_answer(network: network, channel: channel, guess: guess) when :no_round m.reply "No active trivia question — use !trivia" when :correct m.reply "Correct! #{m.user.nick} got it." when :wrong m.reply "Not quite — try again" end return end round = self.class.start_round( network: network, channel: channel, profile: bot.config.shared[:character_profile], channel_users: m.channel.users ) themed_flood_safe_reply(m, "Trivia: #{round['question']}") end
Source
# File plugins/trivia.rb, line 143 def on_message(m) return unless m.channel return if m.user.nick == bot.nick return unless self.class.message_guessable?(m.message) network = bot.config.server channel = m.channel.name return unless self.class.load_round(network: network, channel: channel) case self.class.process_answer(network: network, channel: channel, guess: m.message) when :correct m.reply "Correct! #{m.user.nick} got it." end end