module FuelHunt
Constants
- DEFAULT_PRIZE
- FUEL_TYPES
- PLUGIN
- STYLE_TITLE
- STYLE_WIN
- SUBURBS
- SUBURB_DATA_PATH
- TOLERANCE_CENTS
Public Instance Methods
Source
# File lib/fuel_hunt.rb, line 30 def postcode_for(suburb) suburb_postcodes[suburb.to_s] || suburb_postcodes.find { |name, _| name.casecmp?(suburb.to_s) }&.last end
Source
# File lib/fuel_hunt.rb, line 26 def reset_suburb_postcodes! @suburb_postcodes = nil end
Source
# File lib/fuel_hunt.rb, line 34 def start_hunt(network:, channel:, suburb: nil, fuel_type: nil, cheapest_price: nil, rand: ->(max) { Random.rand(0...max) }) suburb ||= SUBURBS[rand.call(SUBURBS.length)] fuel_type ||= FUEL_TYPES[rand.call(FUEL_TYPES.length)] hunt = { "suburb" => suburb, "fuel_type" => fuel_type, "answer" => cheapest_price, "winner" => nil } GameStore.save(plugin: PLUGIN, network: network, channel: channel, game: hunt) { message: [ IrcFormat.report_header(tag: "FUEL", title: "Fuel Hunt", style: STYLE_TITLE), "Suburb: #{suburb} — fuel: #{fuel_type}", "First !fuel guess <price> within #{TOLERANCE_CENTS}c wins #{DEFAULT_PRIZE} credits", IrcFormat.report_footer("FuelCheck NSW") ].join("\n") } end
Source
# File lib/fuel_hunt.rb, line 54 def submit_guess(network:, channel:, user:, price:, store: Game.default_store) hunt = GameStore.load(plugin: PLUGIN, network: network, channel: channel) return { error: "No Fuel Hunt active — !fuel hunt to start." } unless hunt return { error: "Fuel Hunt already won by #{hunt['winner']}." } if hunt["winner"] answer = hunt["answer"] return { error: "Fuel Hunt not ready — wait for price lookup." } if answer.nil? diff = (price.to_f - answer.to_f).abs if diff <= TOLERANCE_CENTS hunt = hunt.merge("winner" => user) GameStore.clear(plugin: PLUGIN, network: network, channel: channel) Game.credit_winnings(user, DEFAULT_PRIZE, store: store) balance = Game.balance_for(user, store: store) { message: [ IrcFormat.report_header(tag: "FUEL", title: "Fuel Hunt", style: STYLE_TITLE), IrcFormat.decorate("#{user} wins — #{format('%.1f', price)}c/L (answer #{format('%.1f', answer)}c/L)", STYLE_WIN), "Prize #{DEFAULT_PRIZE} credits — balance #{balance}", IrcFormat.report_footer("FuelCheck NSW") ].join("\n") } else { message: "#{user} guessed #{format('%.1f', price)}c/L — not within #{TOLERANCE_CENTS}c." } end end
Source
# File lib/fuel_hunt.rb, line 22 def suburb_postcodes @suburb_postcodes ||= JSON.parse(File.read(SUBURB_DATA_PATH)) end