def self.dispatch_command(args_text, user: nil, fetch: nil, network: nil, channel: nil, store: Game.default_store)
parsed = parse_command(args_text)
return { error: parsed[:error] } if parsed[:error]
return { reply: help_reply } if parsed[:action] == :help
creds = FuelConfig.credentials
case parsed[:action]
when :cheapest
return { error: FuelConfig.setup_hint } unless FuelConfig.configured?
token = FuelNsw.fetch_token(key: creds[:key], secret: creds[:secret], fetch: fetch)
return { error: "Could not authenticate with FuelCheck API." } unless token
prices = FuelNsw.fetch_location_prices(
postcode: parsed[:postcode],
fuel_type: parsed[:fuel_type],
token: token,
fetch: fetch
)
row = FuelNsw.cheapest(prices)
{ reply: FuelNsw.format_cheapest(parsed[:fuel_type], parsed[:postcode], row) }
when :hunt
return { error: "Fuel Hunt must be started in a channel." } unless network && channel
return { error: FuelConfig.setup_hint } unless FuelConfig.configured?
suburb = FuelHunt::SUBURBS.sample
fuel_type = FuelHunt::FUEL_TYPES.sample
postcode = FuelHunt.postcode_for(suburb)
return { error: "Unknown suburb #{suburb} for Fuel Hunt." } unless postcode
token = FuelNsw.fetch_token(key: creds[:key], secret: creds[:secret], fetch: fetch)
return { error: "Could not authenticate with FuelCheck API." } unless token
prices = FuelNsw.fetch_location_prices(
postcode: postcode,
fuel_type: fuel_type,
token: token,
fetch: fetch,
suburb: suburb
)
cheapest = FuelNsw.cheapest(prices)
return { error: "No FuelCheck prices for #{suburb} (#{postcode})." } unless cheapest
result = FuelHunt.start_hunt(
network: network,
channel: channel,
suburb: suburb,
fuel_type: fuel_type,
cheapest_price: cheapest[:price]
)
{ reply: result[:message] }
when :guess
return { error: "Fuel Hunt must be played in a channel." } unless network && channel
result = FuelHunt.submit_guess(
network: network,
channel: channel,
user: user.to_s,
price: parsed[:price],
store: store
)
return { error: result[:error] } if result[:error]
{ reply: result[:message] }
else
{ error: help_reply }
end
end