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..." flash[:notice] = "Game already started..."
return redirect_to(@game) return redirect_to(@game)
end end
@game.status = 'Game Started' if @game.startable? @game.start
@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
redirect_to(@game) redirect_to(@game)
end end

View File

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