module AlaMyth
Constants
- DATA_PATH
- DEFAULT_PRIZE
- PLUGIN
- STYLE_LOSE
- STYLE_TITLE
- STYLE_WIN
Public Instance Methods
Source
# File lib/ala_myth.rb, line 73 def classify_creature(name, fetch: nil) match = AlaSpecies.best_match(name, fetch: fetch) return "myth" unless match if match[:rank] == "species" || match[:occurrence_count].positive? "real" else "myth" end end
Source
# File lib/ala_myth.rb, line 20 def entries @entries ||= JSON.parse(File.read(DATA_PATH)) end
Source
# File lib/ala_myth.rb, line 58 def help_reply lines = [ IrcFormat.report_header(tag: "ALA", title: "Atlas of Living Australia", style: STYLE_TITLE), "!ala species <name> — species lookup", "!ala myth — Marsupial or Myth? round", "!ala guess real|myth — answer the current round", IrcFormat.report_footer("biodiversity data", "Atlas of Living Australia") ] lines.join("\n") end
Source
# File lib/ala_myth.rb, line 32 def parse_command(text) parts = text.to_s.strip.split(/\s+/) return { action: :help } if parts.empty? case parts[0].downcase when "help" { action: :help } when "species" query = parts[1..].join(" ").strip return { error: "Usage: !ala species <name>" } if query.empty? { action: :species, query: query } when "myth" { action: :myth } when "guess" return { error: usage_message } if parts.length < 2 value = parts[1].downcase return { error: "Guess must be real or myth." } unless %w[real myth].include?(value) { action: :guess, guess: value } else { action: :species, query: parts.join(" ") } end end
Source
# File lib/ala_myth.rb, line 69 def pick_entry(rand: ->(max) { Random.rand(0...max) }) entries[rand.call(entries.length)] end
Source
# File lib/ala_myth.rb, line 84 def start_round(network:, channel:, rand: ->(max) { Random.rand(0...max) }, store: RabbotDb) entry = pick_entry(rand: rand) round = { "creature" => entry["name"] } GameStore.save(plugin: PLUGIN, network: network, channel: channel, game: round, store: store) { message: [ IrcFormat.report_header(tag: "ALA", title: "Marsupial or Myth?", style: STYLE_TITLE), "Creature: #{entry['name']}", "Real Australian species, or made up? !ala guess real|myth", IrcFormat.report_footer("ALA lookup settles it") ].join("\n") } end
Source
# File lib/ala_myth.rb, line 98 def submit_guess(network:, channel:, user:, guess:, fetch: nil, store: RabbotDb, game_store: Game.default_store) round = GameStore.load(plugin: PLUGIN, network: network, channel: channel, store: store) return { error: "No myth round active — !ala myth to start." } unless round creature = round["creature"] actual = classify_creature(creature, fetch: fetch) won = guess == actual GameStore.clear(plugin: PLUGIN, network: network, channel: channel, store: store) prize_line = nil if won Game.credit_winnings(user, DEFAULT_PRIZE, store: game_store) balance = Game.balance_for(user, store: game_store) prize_line = "Prize #{DEFAULT_PRIZE} credits — balance #{balance}" end outcome_style = won ? STYLE_WIN : STYLE_LOSE outcome_word = won ? "CORRECT" : "WRONG" lines = [ IrcFormat.report_header(tag: "ALA", title: "Marsupial or Myth?", style: STYLE_TITLE), "#{user} guessed #{guess} — #{creature} is #{actual.upcase}", IrcFormat.decorate(outcome_word, outcome_style) ] lines << prize_line if prize_line lines << IrcFormat.report_footer("ALA species search") { message: lines.join("\n") } end
Source
# File lib/ala_myth.rb, line 28 def usage_message "Usage: !ala myth — start Marsupial or Myth? | !ala guess real|myth" end