diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 8e355b36..41989e2f 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -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 diff --git a/app/models/game.rb b/app/models/game.rb index e3789704..1897fede 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -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) diff --git a/app/views/games/show.html.haml b/app/views/games/show.html.haml index 03ed576c..bc97983a 100644 --- a/app/views/games/show.html.haml +++ b/app/views/games/show.html.haml @@ -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