module GovauDetective
Constants
- DEFAULT_PRIZE
- PLUGIN
- SEARCH_QUERIES
- STYLE_LOSE
- STYLE_TITLE
- STYLE_WIN
Public Instance Methods
Source
# File lib/govau_detective.rb, line 32 def parse_detective_args(parts) tokens = Array(parts).map(&:to_s) return { action: :start } if tokens.empty? case tokens[0].downcase when "guess" guess = tokens[1..].join(" ").strip return { error: usage_message } if guess.empty? { action: :guess, guess: guess } else { error: usage_message } end end
Source
# File lib/govau_detective.rb, line 47 def pick_query(rand: ->(max) { Random.rand(0...max) }) SEARCH_QUERIES[rand.call(SEARCH_QUERIES.length)] end
Source
# File lib/govau_detective.rb, line 75 def score_guess(guess, round) needle = guess.to_s.downcase org = round["organization"].to_s.downcase title = round["title"].to_s.downcase return true if !org.empty? && (org.include?(needle) || needle.include?(org.split.first.to_s)) return true if title.include?(needle) state_tokens = %w[nsw vic qld wa sa tas act nt australia] state_tokens.any? { |token| needle.include?(token) && (org.include?(token) || title.include?(token)) } end
Source
# File lib/govau_detective.rb, line 51 def start_round(ckan_fetch: nil, rand: ->(max) { Random.rand(0...max) }, network: nil, channel: nil, store: RabbotDb) query = pick_query(rand: rand) results = GovauCkan.search_datasets(query, rows: 10, fetch: ckan_fetch) return { error: "No datasets found for detective round." } if results.empty? row = results[rand.call(results.length)] round = { "title" => row[:title], "organization" => row[:organization], "notes" => row[:notes], "name" => row[:name] } GameStore.save(plugin: PLUGIN, network: network, channel: channel, game: round, store: store) lines = [ IrcFormat.report_header(tag: "GOVAU", title: "Dataset Detective", style: STYLE_TITLE), row[:title] ] lines << row[:notes] unless row[:notes].to_s.empty? lines << "Which agency or state published this? !govau detective guess <answer>" lines << IrcFormat.report_footer("CKAN ยท data.gov.au") { message: lines.join("\n") } end
Source
# File lib/govau_detective.rb, line 86 def submit_guess(network:, channel:, user:, guess:, store: RabbotDb, game_store: Game.default_store) round = GameStore.load(plugin: PLUGIN, network: network, channel: channel, store: store) return { error: "No detective round โ !govau detective to start." } unless round won = score_guess(guess, round) GameStore.clear(plugin: PLUGIN, network: network, channel: channel, store: store) answer = round["organization"].empty? ? "unknown publisher" : round["organization"] outcome_style = won ? STYLE_WIN : STYLE_LOSE outcome_word = won ? "CORRECT" : "WRONG" lines = [ IrcFormat.report_header(tag: "GOVAU", title: "Dataset Detective", style: STYLE_TITLE), "#{user} guessed #{guess} โ answer: #{answer}", IrcFormat.decorate(outcome_word, outcome_style) ] if won Game.credit_winnings(user, DEFAULT_PRIZE, store: game_store) balance = Game.balance_for(user, store: game_store) lines << "Prize #{DEFAULT_PRIZE} credits โ balance #{balance}" end lines << IrcFormat.report_footer("CKAN ยท data.gov.au") { message: lines.join("\n") } end
Source
# File lib/govau_detective.rb, line 28 def usage_message "Usage: !govau detective โ new round | !govau detective guess <agency or state>" end