# Associates users to games
class Player < ActiveRecord::Base
  belongs_to :game
  belongs_to :user

  has_many :player_cards
  has_many :plays

  #TODO: Note: soft_user is not associated with a user
  #validates :user, presence: true
  validates :game, presence: true

  def inventory
    player_cards.where(play_id: nil).order(:value)
  end

  def display_name
    return user.email if self.user
    return ("Guest" + self.soft_token)[0..9] if self.soft_token
    return "Empty"
  end

  def play_hand(cards)
    self.plays.create(game_id: self.game.id, player_cards: cards)
  end

  def pass
    #TODO: Create a Play instead of modifying the controlling player directly
    #      Then you won't need this return/fail validation
    return false unless self.game.controlling_player == self

    self.game.set_controlling_player 'next'
    self.game.save
  end

end
