class Brigitte::Player

A Player in Brigitte has a: hand where from player can only throw cards from visible_cards where from player can draw from if hands are empty blind_cards cards that are face down where from player can take only one if all cards are played

A player is ready if cards are swapped between it's hand and visible_cards

Attributes

blind_cards[RW]
hand[RW]
id[R]
name[RW]
ready[RW]
visible_cards[RW]

Public Class Methods

from_h(hash) click to toggle source
# File lib/brigitte/player.rb, line 87
def self.from_h(hash) # rubocop:disable Metrics/AbcSize
  return if hash.empty?

  new(hash[:name], hash[:id]) do |p|
    p.hand = hash[:hand].map { |h| Card.from_h(h) }
    p.blind_cards = hash[:blind_cards].map { |h| Card.from_h(h) }
    p.visible_cards = hash[:visible_cards].map { |h| Card.from_h(h) }
    p.ready = hash[:ready]
  end
end
new(name, id = nil) { |self| ... } click to toggle source
# File lib/brigitte/player.rb, line 19
def initialize(name, id = nil)
  @id = id || SecureRandom.uuid
  @name = name
  @hand = []
  @blind_cards = []
  @visible_cards = []
  @ready = false

  yield self if block_given?
end

Public Instance Methods

==(other) click to toggle source
# File lib/brigitte/player.rb, line 39
def ==(other)
  id == other&.id
end
pull_blind_card(index) click to toggle source
# File lib/brigitte/player.rb, line 55
def pull_blind_card(index)
  return false if hand.any?
  return false if visible_cards.any?

  blind_card = blind_cards[index]
  return false unless blind_card

  blind_cards[index] = nil
  hand << blind_card
  sort_hand!
  true
end
ready!() click to toggle source
# File lib/brigitte/player.rb, line 30
def ready!
  sort_hand!
  @ready = true
end
ready?() click to toggle source
# File lib/brigitte/player.rb, line 35
def ready?
  @ready
end
sort_hand!() click to toggle source
# File lib/brigitte/player.rb, line 72
def sort_hand!
  hand.sort_by!(&:weight).reverse!
end
swap(hand_card, visible_card) click to toggle source
# File lib/brigitte/player.rb, line 43
def swap(hand_card, visible_card)
  return if @ready

  hand_card_index = hand.find_index(hand_card)
  visible_card_index = visible_cards.find_index(visible_card)
  return unless hand_card_index
  return unless visible_card_index

  visible_cards[visible_card_index] = hand_card
  hand[hand_card_index] = visible_card
end
throw(card) click to toggle source
# File lib/brigitte/player.rb, line 68
def throw(card)
  hand.delete(card)
end
to_h() click to toggle source
# File lib/brigitte/player.rb, line 76
def to_h
  {
    id: id,
    name: name,
    hand: hand.map(&:to_h),
    blind_cards: blind_cards.map(&:to_h),
    visible_cards: visible_cards.map(&:to_h),
    ready: ready
  }
end