Game start logic, shuffle and deal

This commit is contained in:
Jesse C. Fisher 2015-04-09 05:34:41 -07:00
parent 1d2d213a91
commit ee7165614d
3 changed files with 26 additions and 3 deletions

View File

@ -50,7 +50,29 @@ class GamesController < ApplicationController
def start
@game = Game.find(params[:game_id])
@status = 'Game Started' if @game.is_startable?
# If game is already started, simply return
if @game.status == 'Game Started'
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
redirect_to(@game)
end

View File

@ -20,7 +20,8 @@ class Game < ActiveRecord::Base
def startable?
@player_count = players.size
@player_count.between?(2, 4)
bool = @player_count.between?(2, 4)
bool = status != 'Game Started'
end
def add_player_from_user(user)

View File

@ -9,7 +9,7 @@
- if @game.can_accomodate(current_user)
= link_to 'Join', game_join_path(@game)
- if @game.is_startable? && @game.already_has?(current_user)
- if @game.startable? && @game.already_has?(current_user)
= link_to 'Start', game_start_path(@game)
%p