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