35 lines
817 B
Ruby

# 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
self.try(:user).try(:email) || "Guest#{self.soft_token[0..5]}"
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