This commit is contained in:
Jesse C. Fisher 2015-04-15 06:44:07 -07:00
parent 1d1598968a
commit 327f7d8f1b
5 changed files with 43 additions and 24 deletions

View File

@ -51,13 +51,16 @@ class GamesController < ApplicationController
def start
@game = Game.find(params[:game_id])
# If game is already started, simply return
if @game.status == 'Game Started'
flash[:notice] = "Game already started..."
return redirect_to(@game)
begin
@game.start
rescue StandardError => e
@game.errors.messages.each do |key,msg|
flash[key] = msg.join
end
redirect_to(@game)
return
end
@game.start
flash[:notice] = "Game started."
redirect_to(@game)
end

View File

@ -2,15 +2,16 @@
class Game < ActiveRecord::Base
has_many :players, dependent: :destroy
has_many :users, through: :players
has_many :player_cards, through: :players
accepts_nested_attributes_for :players
validates :title, presence: true
def start
# Save the game to the database
validate_startable
return false unless startable?
# Start the game TODO
self.status = 'Game Started' if startable?
# Start the game
self.status = 'Game Started'
# Create a deck
d = Deck.new
@ -28,21 +29,24 @@ class Game < ActiveRecord::Base
end
def validate_startable
@player_count = players.size
@player_count = players.count
errors[:players] = 'Must be at least 2 players.' if @player_count < 2
errors[:players] = 'Must be less than 5 players.' if @player_count > 4
errors[:status] = 'Game already started...' if self.status == 'Game Started'
raise StandardError, errors if errors
end
def startable?
@player_count = players.size
@player_count = players.count
tests = [
@player_count.between?(2, 4),
status != 'Game Started'
]
tests.each do |test|
if test == false then return false end
end
return true
return tests.all?
end
def lowest_card
self.player_cards.order(:value).first
end
def add_player_from_user(user)
@ -59,6 +63,6 @@ class Game < ActiveRecord::Base
end
def full?
players.size >= 4
players.count >= 4
end
end

View File

@ -12,6 +12,8 @@
- if @game.startable? && @game.already_has?(current_user)
= link_to 'Start', game_start_path(@game)
= link_to 'Start', game_start_path(@game)
%p
%b Players:
%ol

View File

@ -1,6 +1,6 @@
FactoryGirl.define do
factory :game do
title "MyString"
title "Test Game"
status nil
end
end

View File

@ -1,7 +1,11 @@
require 'rails_helper'
RSpec.describe Game, type: :model do
before(:each) { @game = Game.new(title: 'Test Game') }
before(:each) do
@game = FactoryGirl.create(:game)
@user1 = FactoryGirl.create(:user)
@user2 = FactoryGirl.create(:user)
end
subject { @game }
@ -15,8 +19,8 @@ RSpec.describe Game, type: :model do
describe 'player_count' do
context 'when there are between 2 and 4 players' do
it 'is startable' do
@game.players.new
@game.players.new
@game.add_player_from_user(@user1)
@game.add_player_from_user(@user2)
expect(@game.players.size).to eq(2)
expect(@game.startable?).to eq(true)
end
@ -24,7 +28,7 @@ RSpec.describe Game, type: :model do
context 'when there are less than 2 players' do
it 'is not startable with too few players' do
@game.validate_startable
expect{@game.validate_startable}.to raise_error StandardError
expect(@game.players.size).to eq(0)
expect(@game.startable?).to eq(false)
expect(@game.errors[:players])
@ -34,9 +38,12 @@ RSpec.describe Game, type: :model do
context 'when there are more than 4 players' do
it 'fails validation with too many players' do
6.times do
@game.players.new
@user = FactoryGirl.create(:user)
@player = @game.players.new(user_id: @user.id)
@player.save
end
@game.validate_startable
@game.players.new(user_id: User.last.id)
expect{@game.validate_startable}.to raise_error StandardError
expect(@game.startable?).to eq(false)
expect(@game.errors[:players])
.to include 'Must be less than 5 players.'
@ -45,6 +52,9 @@ RSpec.describe Game, type: :model do
end
end
#when the game starts
#the player with the lowest card goes first
#it 'Can not be started if game is invalid' do
#expect(@game.start).to raise_error
#end