134 lines
3.2 KiB
Ruby

# The game class
class Game < ActiveRecord::Base
belongs_to :table
has_many :players, dependent: :destroy
has_many :users, through: :players
has_many :player_cards, through: :players
has_many :plays, through: :players
has_one :controlling_player
belongs_to :play_to_beat
accepts_nested_attributes_for :players
validates :title, presence: true
scope :play_now, lambda { |user|
where(status:nil).joins("LEFT OUTER JOIN players ON players.game_id = games.id").group("games.id").having("count(players) < 4").first || Game.create(title: "Game " + user.soft_token[0..4])
}
def start
validate_startable
return false unless startable?
# Start the game
self.status = 'Game Started'
# Create a deck
d = Deck.new
# Shuffle the deck
d.cards.shuffle!
# Deal the cards
players.each do |player|
13.times do
card = d.cards.shift
player.player_cards.new(card.instance_values)
end
player.save
end
# Set status to first turn
# Player with the lowest card
self.status = 'First Play at: ' + self.lowest_card.player.display_name
self.controlling_player_id = self.lowest_card.player.id
save
end
def validate_startable
@player_count = players.count
errors[:players] = 'Must be at least 2 players.' if @player_count < 2
errors[:players] = 'Must be less than 5 players.' if @player_count > 4
errors[:status] = 'Game already started...' if self.status.nil? == false
errors[:player_cards] = 'Cards already dealt...' unless player_cards.empty?
fail StandardError, errors if errors.count > 0
return true
end
def current_hand
return nil
end
def startable?
begin
self.validate_startable
rescue
return false
end
end
def lowest_card
self.player_cards.order(:value).first
end
def add_player_from_user(user)
return false unless self.joinable?(user)
new_player = self.players.create(user: user, soft_token: user.soft_token)
end
def can_accomodate(user)
!(already_has?(user) || full?)
end
def already_has?(user)
self.players.pluck(:user_id, :soft_token).flatten.compact.to_set.intersect? ([user.id, user.soft_token].to_set)
end
def full?
players.count >= 4
end
def set_controlling_player(arg)
case arg
when 'next'
self.controlling_player_id = self.next_player_id
end
end
def joinable? user
!self.already_has?(user) && !self.full?
end
def next_player_id
player_ids[(player_ids.index(controlling_player_id) + 1 ) % player_ids.length]
end
# The player whose turn it is
def controlling_player
self.players.find(self.controlling_player_id)
end
# The user whose turn it is
def controlling_user
self.players.find(self.controlling_player_id).user
end
def hand_to_beat
self.play_to_beat.try(:play).try(:hand)
end
def player_to_beat
self.play_to_beat.try(:play).try(:player)
end
def started?
!self.status.nil?
end
def over?
return false unless self.started?
@inventory_counts = []
self.players.each do |player|
@inventory_counts << player.inventory.count
end
@inventory_counts.include? 0
end
end