Game start logic, shuffle and deal
This commit is contained in:
parent
1d2d213a91
commit
ee7165614d
@ -50,7 +50,29 @@ class GamesController < ApplicationController
|
|||||||
|
|
||||||
def start
|
def start
|
||||||
@game = Game.find(params[:game_id])
|
@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)
|
redirect_to(@game)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,8 @@ class Game < ActiveRecord::Base
|
|||||||
|
|
||||||
def startable?
|
def startable?
|
||||||
@player_count = players.size
|
@player_count = players.size
|
||||||
@player_count.between?(2, 4)
|
bool = @player_count.between?(2, 4)
|
||||||
|
bool = status != 'Game Started'
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_player_from_user(user)
|
def add_player_from_user(user)
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
- if @game.can_accomodate(current_user)
|
- if @game.can_accomodate(current_user)
|
||||||
= link_to 'Join', game_join_path(@game)
|
= 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)
|
= link_to 'Start', game_start_path(@game)
|
||||||
|
|
||||||
%p
|
%p
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user