class Game
Constants
- BEG_COMMAND_HINT
- BEG_MAX
- BEG_MIN
- BEG_STEP
- IRC_CTRL
- IRC_RESET
- MAX_BET
- MIN_BET
- RANKS
- STARTING_CREDITS
- STYLE_BLACK
- STYLE_CARD_BLACK
- STYLE_CARD_RED
- STYLE_GREEN
- STYLE_HINT
- STYLE_LABEL
- STYLE_LOSE
- STYLE_NICK
- STYLE_POINTER
- STYLE_PUSH
- STYLE_RED
- STYLE_TITLE
- STYLE_VALUE
- STYLE_WHEEL
- STYLE_WIN
- SUITS
- SUIT_SYMBOLS
Attributes
Public Class Methods
Source
# File lib/game.rb, line 258 def self.active_channel_game(channel, games: default_games) games[channel.to_s] end
Source
# File lib/game.rb, line 250 def self.active_game(user, games: default_games) games[normalize_user(user)] end
Source
# File lib/game.rb, line 266 def self.active_table(channel, tables: default_tables) tables[channel.to_s] end
Source
# File lib/game.rb, line 166 def self.balance_for(user, store: default_store) key = normalize_user(user) store.fetch(key, STARTING_CREDITS) end
Source
# File lib/game.rb, line 237 def self.beg(user, store: default_store, title:, rand: ->(max) { Random.rand(0...max) }) unless zero_balance?(user, store: store) balance = balance_for(user, store: store) return { error: "#{user}, beg only works when you're broke (balance: #{balance})." } end amount = random_beg_amount(rand: rand) credit_winnings(user, amount, store: store) balance = balance_for(user, store: store) message = format_beg(user, amount, balance, title: title) { amount: amount, balance: balance, message: message } end
Source
# File lib/game.rb, line 209 def self.beg_steps_count (BEG_MAX - BEG_MIN) / BEG_STEP + 1 end
Source
# File lib/game.rb, line 274 def self.build_deck SUITS.product(RANKS).map { |suit, rank| { rank: rank, suit: suit } } end
Source
# File lib/game.rb, line 262 def self.clear_channel_game(channel, games: default_games) games.delete(channel.to_s) end
Source
# File lib/game.rb, line 254 def self.clear_game(user, games: default_games) games.delete(normalize_user(user)) end
Source
# File lib/game.rb, line 270 def self.clear_table(channel, tables: default_tables) tables.delete(channel.to_s) end
Source
# File lib/game.rb, line 158 def self.command_error { error: usage_message } end
Source
# File lib/game.rb, line 176 def self.credit_winnings(user, amount, store: default_store) key = normalize_user(user) store[key] = balance_for(user, store: store) + amount end
Source
# File lib/game.rb, line 287 def self.deal_card(deck) [deck.shift, deck] end
Source
# File lib/game.rb, line 171 def self.deduct_bet(user, amount, store: default_store) key = normalize_user(user) store[key] = balance_for(user, store: store) - amount end
Source
# File lib/game.rb, line 51 def self.default_inventory @inventory ||= PersistentStore.new(plugin: "blackjack", key: "inventory") end
Source
# File lib/game.rb, line 47 def self.default_stats @stats ||= PersistentStore.new(plugin: "blackjack", key: "stats", symbolize_loaded_hashes: true) end
Source
# File lib/game.rb, line 39 def self.default_store @store ||= PersistentStore.new(plugin: "game", key: "store") end
Source
# File lib/game.rb, line 186 def self.format_balance(user, balance, title:) "#{STYLE_TITLE}#{title}#{IRC_RESET} #{STYLE_LABEL}Balance for#{IRC_RESET} " \ "#{STYLE_NICK}#{user}#{IRC_RESET}#{STYLE_LABEL}:#{IRC_RESET} " \ "#{STYLE_VALUE}#{balance}#{IRC_RESET} #{STYLE_LABEL}credits#{IRC_RESET}" end
Source
# File lib/game.rb, line 229 def self.format_beg(user, amount, balance, title:) "#{STYLE_TITLE}#{title}#{IRC_RESET} #{STYLE_LABEL}The house takes pity on#{IRC_RESET} " \ "#{STYLE_NICK}#{user}#{IRC_RESET}#{STYLE_LABEL} —#{IRC_RESET} " \ "#{STYLE_VALUE}#{amount}#{IRC_RESET} #{STYLE_LABEL}credits!#{IRC_RESET} " \ "#{STYLE_LABEL}New balance:#{IRC_RESET} #{STYLE_VALUE}#{balance}#{IRC_RESET} " \ "#{STYLE_LABEL}credits#{IRC_RESET}" end
Source
# File lib/game.rb, line 291 def self.format_card(card, hidden: false) return "#{STYLE_HINT}[??]#{IRC_RESET}" if hidden suit = card[:suit] style = %w[H D].include?(suit) ? STYLE_CARD_RED : STYLE_CARD_BLACK "#{style}[#{card[:rank]}#{SUIT_SYMBOLS[suit]}]#{IRC_RESET}" end
Source
# File lib/game.rb, line 299 def self.format_hand(cards, hidden: false, hide_second: false) cards.each_with_index.map do |card, index| format_card(card, hidden: hidden || (hide_second && index == 1)) end.join(" ") end
Source
# File lib/game.rb, line 192 def self.format_leaderboard(store, title:, empty_message:) board = leaderboard(store: store) lines = ["#{STYLE_TITLE}#{title}#{IRC_RESET}"] if board.empty? lines << "#{STYLE_LABEL}#{empty_message}#{IRC_RESET}" return lines.join("\n") end board.each_with_index do |entry, index| medal = %w[🥇 🥈 🥉][index] || " " lines << "#{STYLE_LABEL}#{medal} #{index + 1}.#{IRC_RESET} " \ "#{STYLE_NICK}#{entry[:user]}#{IRC_RESET} " \ "#{STYLE_LABEL}—#{IRC_RESET} #{STYLE_VALUE}#{entry[:balance]}#{IRC_RESET}" end lines.join("\n") end
Source
# File lib/game.rb, line 223 def self.insufficient_credits_error(user, message, store: default_store) error = message error += BEG_COMMAND_HINT if zero_balance?(user, store: store) { error: error } end
Source
# File lib/game.rb, line 59 def self.irc_color(fg, bg = nil) IrcFormat.color(fg, bg) end
Source
# File lib/game.rb, line 181 def self.leaderboard(store: default_store) store.map { |user, balance| { user: user, balance: balance } } .sort_by { |entry| [-entry[:balance], entry[:user]] } end
Source
# File lib/game.rb, line 143 def self.normalize_channel(channel) Normalize.channel(channel) end
Source
# File lib/game.rb, line 139 def self.normalize_user(user) Normalize.user_key(user) end
Source
# File lib/game.rb, line 147 def self.parse_meta_command(parts) case parts[0].downcase when "balance", "score", "credits" { action: :balance } when "top", "leaderboard", "scores" { action: :leaderboard } when "help" { action: :help } end end
Source
# File lib/game.rb, line 213 def self.random_beg_amount(rand: ->(max) { Random.rand(0...max) }) BEG_MIN + rand.call(beg_steps_count) * BEG_STEP end
Source
# File lib/game.rb, line 31 def self.reset_store! @store = nil @games = nil @stats = nil @inventory = nil @tables = nil end
Source
# File lib/game.rb, line 278 def self.shuffle_deck(deck, rand: ->(max) { Random.rand(0...max) }) shuffled = deck.dup (shuffled.length - 1).downto(1) do |index| swap_index = rand.call(index + 1) shuffled[index], shuffled[swap_index] = shuffled[swap_index], shuffled[index] end shuffled end
Source
# File lib/game.rb, line 91 def self.style_card_black irc_color(0, 1) end
Source
# File lib/game.rb, line 162 def self.valid_amount?(amount) amount >= MIN_BET && amount <= MAX_BET end
Source
# File lib/game.rb, line 217 def self.zero_balance?(user, store: default_store) balance_for(user, store: store).zero? end