Move game start logic to model
This commit is contained in:
parent
39adcdb9e4
commit
1d1598968a
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user