stash
This commit is contained in:
parent
1d1598968a
commit
327f7d8f1b
@ -51,13 +51,16 @@ class GamesController < ApplicationController
|
|||||||
def start
|
def start
|
||||||
@game = Game.find(params[:game_id])
|
@game = Game.find(params[:game_id])
|
||||||
|
|
||||||
# If game is already started, simply return
|
begin
|
||||||
if @game.status == 'Game Started'
|
@game.start
|
||||||
flash[:notice] = "Game already started..."
|
rescue StandardError => e
|
||||||
return redirect_to(@game)
|
@game.errors.messages.each do |key,msg|
|
||||||
|
flash[key] = msg.join
|
||||||
|
end
|
||||||
|
redirect_to(@game)
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
flash[:notice] = "Game started."
|
||||||
@game.start
|
|
||||||
|
|
||||||
redirect_to(@game)
|
redirect_to(@game)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,15 +2,16 @@
|
|||||||
class Game < ActiveRecord::Base
|
class Game < ActiveRecord::Base
|
||||||
has_many :players, dependent: :destroy
|
has_many :players, dependent: :destroy
|
||||||
has_many :users, through: :players
|
has_many :users, through: :players
|
||||||
|
has_many :player_cards, through: :players
|
||||||
accepts_nested_attributes_for :players
|
accepts_nested_attributes_for :players
|
||||||
validates :title, presence: true
|
validates :title, presence: true
|
||||||
|
|
||||||
def start
|
def start
|
||||||
# Save the game to the database
|
validate_startable
|
||||||
return false unless startable?
|
return false unless startable?
|
||||||
|
|
||||||
# Start the game TODO
|
# Start the game
|
||||||
self.status = 'Game Started' if startable?
|
self.status = 'Game Started'
|
||||||
|
|
||||||
# Create a deck
|
# Create a deck
|
||||||
d = Deck.new
|
d = Deck.new
|
||||||
@ -28,21 +29,24 @@ class Game < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def validate_startable
|
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 at least 2 players.' if @player_count < 2
|
||||||
errors[:players] = 'Must be less than 5 players.' if @player_count > 4
|
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
|
end
|
||||||
|
|
||||||
def startable?
|
def startable?
|
||||||
@player_count = players.size
|
@player_count = players.count
|
||||||
tests = [
|
tests = [
|
||||||
@player_count.between?(2, 4),
|
@player_count.between?(2, 4),
|
||||||
status != 'Game Started'
|
status != 'Game Started'
|
||||||
]
|
]
|
||||||
tests.each do |test|
|
return tests.all?
|
||||||
if test == false then return false end
|
end
|
||||||
end
|
|
||||||
return true
|
def lowest_card
|
||||||
|
self.player_cards.order(:value).first
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_player_from_user(user)
|
def add_player_from_user(user)
|
||||||
@ -59,6 +63,6 @@ class Game < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def full?
|
def full?
|
||||||
players.size >= 4
|
players.count >= 4
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -12,6 +12,8 @@
|
|||||||
- if @game.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)
|
||||||
|
|
||||||
|
= link_to 'Start', game_start_path(@game)
|
||||||
|
|
||||||
%p
|
%p
|
||||||
%b Players:
|
%b Players:
|
||||||
%ol
|
%ol
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
FactoryGirl.define do
|
FactoryGirl.define do
|
||||||
factory :game do
|
factory :game do
|
||||||
title "MyString"
|
title "Test Game"
|
||||||
|
status nil
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe Game, type: :model do
|
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 }
|
subject { @game }
|
||||||
|
|
||||||
@ -15,8 +19,8 @@ RSpec.describe Game, type: :model do
|
|||||||
describe 'player_count' do
|
describe 'player_count' do
|
||||||
context 'when there are between 2 and 4 players' do
|
context 'when there are between 2 and 4 players' do
|
||||||
it 'is startable' do
|
it 'is startable' do
|
||||||
@game.players.new
|
@game.add_player_from_user(@user1)
|
||||||
@game.players.new
|
@game.add_player_from_user(@user2)
|
||||||
expect(@game.players.size).to eq(2)
|
expect(@game.players.size).to eq(2)
|
||||||
expect(@game.startable?).to eq(true)
|
expect(@game.startable?).to eq(true)
|
||||||
end
|
end
|
||||||
@ -24,7 +28,7 @@ RSpec.describe Game, type: :model do
|
|||||||
|
|
||||||
context 'when there are less than 2 players' do
|
context 'when there are less than 2 players' do
|
||||||
it 'is not startable with too few 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.players.size).to eq(0)
|
||||||
expect(@game.startable?).to eq(false)
|
expect(@game.startable?).to eq(false)
|
||||||
expect(@game.errors[:players])
|
expect(@game.errors[:players])
|
||||||
@ -34,9 +38,12 @@ RSpec.describe Game, type: :model do
|
|||||||
context 'when there are more than 4 players' do
|
context 'when there are more than 4 players' do
|
||||||
it 'fails validation with too many players' do
|
it 'fails validation with too many players' do
|
||||||
6.times do
|
6.times do
|
||||||
@game.players.new
|
@user = FactoryGirl.create(:user)
|
||||||
|
@player = @game.players.new(user_id: @user.id)
|
||||||
|
@player.save
|
||||||
end
|
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.startable?).to eq(false)
|
||||||
expect(@game.errors[:players])
|
expect(@game.errors[:players])
|
||||||
.to include 'Must be less than 5 players.'
|
.to include 'Must be less than 5 players.'
|
||||||
@ -45,6 +52,9 @@ RSpec.describe Game, type: :model do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#when the game starts
|
||||||
|
#the player with the lowest card goes first
|
||||||
|
|
||||||
#it 'Can not be started if game is invalid' do
|
#it 'Can not be started if game is invalid' do
|
||||||
#expect(@game.start).to raise_error
|
#expect(@game.start).to raise_error
|
||||||
#end
|
#end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user