Move game start logic to model

This commit is contained in:
Jesse C. Fisher 2015-04-14 18:07:49 -07:00
parent 39adcdb9e4
commit 1d1598968a
2 changed files with 26 additions and 19 deletions

View File

@ -56,22 +56,8 @@ class GamesController < ApplicationController
flash[:notice] = "Game already started..."
return redirect_to(@game)
end
@game.status = 'Game Started' if @game.startable?
@game.save
# Create a deck
d = Deck.new
# Shuffle the deck
d.cards.shuffle!
# Deal the cards
@game.players.each do |player|
13.times do
card = d.cards.shift
player.player_cards.new(card.instance_values)
end
player.save
end
@game.start
redirect_to(@game)
end

View File

@ -8,8 +8,23 @@ class Game < ActiveRecord::Base
def start
# Save the game to the database
return false unless startable?
save
# Start the game TODO
self.status = 'Game Started' if startable?
# 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
save
end
def validate_startable
@ -20,8 +35,14 @@ class Game < ActiveRecord::Base
def startable?
@player_count = players.size
bool = @player_count.between?(2, 4)
#bool = status != 'Game Started'
tests = [
@player_count.between?(2, 4),
status != 'Game Started'
]
tests.each do |test|
if test == false then return false end
end
return true
end
def add_player_from_user(user)