diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 0ad12ef9..6671af34 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -35,6 +35,10 @@ class GamesController < ApplicationController card.save end + # TODO: Ensure active player only changes when valid hand is played + @game.set_active_player 'next' + @game.save + # TODO: add current_hand logic # @hand > @game.hand_to_beat redirect_to @game diff --git a/app/models/game.rb b/app/models/game.rb index 844aebf2..93aa9fbf 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -7,6 +7,11 @@ class Game < ActiveRecord::Base accepts_nested_attributes_for :players validates :title, presence: true + # TODO: Simplify to a single attribute + alias_attribute :current_player_id, :control_player_id + alias_attribute :active_player_id, :control_player_id + alias_attribute :active_player, :control_player + def start validate_startable return false unless startable? @@ -81,6 +86,17 @@ class Game < ActiveRecord::Base players.count >= 4 end + def set_active_player(arg) + case arg + when 'next' + self.active_player_id = self.next_player_id + end + end + + def next_player_id + player_ids[(player_ids.index(current_player_id) + 1 ) % player_ids.length] + end + # The player whose turn it is def control_player self.players.find(self.control_player_id) diff --git a/spec/features/plays_spec.rb b/spec/features/plays_spec.rb index 5e0a7bdf..f92fa8ad 100644 --- a/spec/features/plays_spec.rb +++ b/spec/features/plays_spec.rb @@ -68,19 +68,26 @@ feature 'Play a hand', type: :feature do # Given I am the current player # When I play a hand # Then the next player becomes the current player - scenario 'current player rotates after a valid hand' do - skip 'to be implemented' - game = setup_game - login_as(game.current_player.user, scope: :user) - @current_player_id = game.current_player.id - @current_player_index = game.players.index(game.current_player) - visit game_path game - @card_to_play = game.current_player.player_cards.order(:value).first + scenario 'active player rotates after a valid hand' do + @game = setup_game + login_as(@game.active_player.user, scope: :user) + @next_player_id = @game.next_player_id + @active_player_id = @game.active_player.id + @active_player_index = @game.players.index(@game.active_player) + visit game_path @game + @card_to_play = @game.active_player.player_cards.order(:value).first check @card_to_play.to_s click_button 'Play Hand' + # reload the game object instance + @game.reload expect(page).to have_content('Played ' + @card_to_play.to_s) - expect(game.current_player.id).to_not eq(@current_player_id) - expect(game.players.index(game.current_player)).to eq( (@current_player_id + 1) % 2) + + # render active player style + expect(page).to have_content(@game.players.find(@next_player_id).user.email + " My Turn") + + # next player is now the active player + expect(@game.active_player_id).to_not eq(@active_player_id) + expect(@game.active_player_id).to eq(@next_player_id) end # Scenario: Player can play a valid hand on their turn