module GovauDerby
Constants
- DEFAULT_BET
- DEFAULT_STATION
- PAYOUT_MULTIPLIER
- STYLE_LOSE
- STYLE_TITLE
- STYLE_WIN
Public Instance Methods
Source
# File lib/govau_derby.rb, line 114 def delayed?(service) !GovauPtv.on_time?(service) end
Source
# File lib/govau_derby.rb, line 97 def format_result(user:, pick:, amount:, station:, service:, won:, balance:) status = delayed?(service) ? "LATE" : "ON TIME" outcome_style = won ? STYLE_WIN : STYLE_LOSE outcome_word = won ? "WIN" : "LOSS" delay_min = service[:delay_sec].to_i / 60 delay_text = delay_min.positive? ? ", +#{delay_min}m" : "" time_label = GovauPtv.format_local_time(service[:estimated_at] || service[:scheduled_at]) lines = [ IrcFormat.report_header(tag: "GOVAU", title: "Delay Derby ยท #{station}", style: STYLE_TITLE), "#{user} bet #{pick} #{amount} โ next #{service[:destination]} departs #{time_label} (#{status}#{delay_text})", IrcFormat.decorate("#{outcome_word} โ balance #{balance} credits", outcome_style), IrcFormat.report_footer("PTV real-time", "data.gov.au") ] lines.join("\n") end
Source
# File lib/govau_derby.rb, line 28 def parse_derby_args(parts) tokens = Array(parts).map(&:to_s) return { error: usage_message } if tokens.empty? pick = tokens[0].downcase.to_sym unless %i[ontime late].include?(pick) return { error: usage_message } end amount = DEFAULT_BET station_tokens = tokens[1..] || [] if station_tokens[0]&.match?(/\A\d+\z/) amount = station_tokens[0].to_i station_tokens = station_tokens[1..] || [] end station = station_tokens.join(" ").strip station = DEFAULT_STATION if station.empty? unless Game.valid_amount?(amount) return { error: "Bet must be between #{Game::MIN_BET} and #{Game::MAX_BET} credits." } end { pick: pick, amount: amount, station: station } end
Source
# File lib/govau_derby.rb, line 24 def peak_hour?(time: Time.now) TransitDerby.melbourne_peak_hour?(time: time) end
Source
# File lib/govau_derby.rb, line 58 def play(user:, pick:, amount:, station:, departures:, time: Time.now, store: Game.default_store) unless peak_hour?(time: time) return { error: "Delay Derby runs during weekday peak hour only (07:00-09:30 and 16:00-19:00 Melbourne time)." } end if departures.empty? return { error: "No departures available to settle the bet." } end balance = Game.balance_for(user, store: store) if balance < amount return { error: "#{user}, you need #{amount} credits (balance: #{balance})." } end next_service = departures.first delayed = !GovauPtv.on_time?(next_service) won = (pick == :ontime && !delayed) || (pick == :late && delayed) Game.deduct_bet(user, amount, store: store) winnings = won ? amount * PAYOUT_MULTIPLIER : 0 Game.credit_winnings(user, winnings, store: store) if winnings.positive? new_balance = Game.balance_for(user, store: store) { win: won, service: next_service, balance: new_balance, message: format_result( user: user, pick: pick, amount: amount, station: station, service: next_service, won: won, balance: new_balance ) } end
Source
# File lib/govau_derby.rb, line 54 def usage_message "Usage: !govau derby ontime|late [amount] [station] โ peak-hour Delay Derby (default station: #{DEFAULT_STATION})" end