From 327f7d8f1bce79d07773f3b1dd7a11d2b8267f42 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Wed, 15 Apr 2015 06:44:07 -0700 Subject: [PATCH] stash --- app/controllers/games_controller.rb | 15 +++++++++------ app/models/game.rb | 24 ++++++++++++++---------- app/views/games/show.html.haml | 2 ++ spec/factories/games.rb | 4 ++-- spec/models/game_spec.rb | 22 ++++++++++++++++------ 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index de4c0e4..cbc00c7 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -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 diff --git a/app/models/game.rb b/app/models/game.rb index baef028..9fbc8da 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -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 diff --git a/app/views/games/show.html.haml b/app/views/games/show.html.haml index bc97983..18fd5bc 100644 --- a/app/views/games/show.html.haml +++ b/app/views/games/show.html.haml @@ -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 diff --git a/spec/factories/games.rb b/spec/factories/games.rb index e13770a..3c94744 100644 --- a/spec/factories/games.rb +++ b/spec/factories/games.rb @@ -1,6 +1,6 @@ FactoryGirl.define do factory :game do - title "MyString" + title "Test Game" + status nil end - end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index d9b7fc9..cec409e 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -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