Rename current_player to controlling_player

This commit is contained in:
Jesse C. Fisher 2015-11-08 17:39:24 -08:00
parent 992da8e21e
commit 2a5504a982
13 changed files with 68 additions and 71 deletions

View File

@ -4,10 +4,10 @@ Thirteen Tien Len
TODO TODO
---- ----
- rename `current_player(_id)` to `controlling_player(_id)`
- graphics - graphics
- Remove PlayToBeat model and table.
- Test `play_hand` of run including face cards - Test `play_hand` of run including face cards
- BUG TODO: First player / lowest card holder should not be able to pass - BUG TODO: First player / lowest card holder should not be able to pass

View File

@ -92,14 +92,14 @@ var Game = React.createClass({
<div> <div>
<div>Title: {this.state.data.title}</div> <div>Title: {this.state.data.title}</div>
<div>Id: {this.state.data.id}</div> <div>Id: {this.state.data.id}</div>
<div>Current Player: {this.state.data.current_player_id}</div> <div>Current Player: {this.state.data.controlling_player_id}</div>
<div>Hand to beat: {this.state.data.play_to_beat_string}</div> <div>Hand to beat: {this.state.data.play_to_beat_string}</div>
<div className='player-list'> <div className='player-list'>
<PlayerList <PlayerList
game_id={this.props.id} game_id={this.props.id}
players={this.state.data.players} players={this.state.data.players}
onPlayHandSubmit={this.handlePlayHandSubmit} onPlayHandSubmit={this.handlePlayHandSubmit}
current_player_id={this.state.data.current_player_id} controlling_player_id={this.state.data.controlling_player_id}
current_user_id={this.state.data.current_user_id} /> current_user_id={this.state.data.current_user_id} />
</div> </div>
<div> <div>

View File

@ -19,7 +19,7 @@ var Player = React.createClass({
game_id={this.props.game_id} game_id={this.props.game_id}
player_id={this.props.id} player_id={this.props.id}
inventory={this.props.inventory} inventory={this.props.inventory}
current_player_id={this.props.current_player_id} /> ) controlling_player_id={this.props.controlling_player_id} /> )
: null } : null }
</div> </div>
); );

View File

@ -28,13 +28,13 @@ var PlayerControls = React.createClass({
<input type="hidden" value="patch" name="_method"></input> <input type="hidden" value="patch" name="_method"></input>
<div><Inventory ref="inventory" cards={this.props.inventory} player_id={this.props.id} /></div> <div><Inventory ref="inventory" cards={this.props.inventory} player_id={this.props.id} /></div>
<input id={"play_hand_button_" + this.props.player_id} <input id={"play_hand_button_" + this.props.player_id}
disabled={this.props.current_player_id == this.props.player_id ? null : 'disabled'} disabled={this.props.controlling_player_id == this.props.player_id ? null : 'disabled'}
type="submit" type="submit"
value="Play Hand" value="Play Hand"
name="commit"> name="commit">
</input> </input>
<input id={"pass_hand_button_" + this.props.player_id} <input id={"pass_hand_button_" + this.props.player_id}
disabled={this.props.current_player_id == this.props.player_id ? null : 'disabled'} disabled={this.props.controlling_player_id == this.props.player_id ? null : 'disabled'}
type="submit" type="submit"
onClick={this.handlePass} onClick={this.handlePass}
value="Pass" value="Pass"

View File

@ -11,7 +11,7 @@ var PlayerList = React.createClass({
<Player <Player
key={player.id} key={player.id}
onPlayHandSubmit={playerList.props.onPlayHandSubmit} onPlayHandSubmit={playerList.props.onPlayHandSubmit}
current_player_id={playerList.props.current_player_id} controlling_player_id={playerList.props.controlling_player_id}
current_user_id={playerList.props.current_user_id} current_user_id={playerList.props.current_user_id}
user_id={player.user_id} user_id={player.user_id}
game_id={playerList.props.game_id} {...player} /> game_id={playerList.props.game_id} {...player} />

View File

@ -24,13 +24,13 @@ class GamesController < ApplicationController
# TODO: move this logic out of controller maybe to model(s) ? # TODO: move this logic out of controller maybe to model(s) ?
# validate player's turn # validate player's turn
if current_user == @game.current_player.user if current_user == @game.controlling_player.user
# Player action: pass # Player action: pass
# TODO: wtf do we need game params for? And why nest hand_type in it? # TODO: wtf do we need game params for? And why nest hand_type in it?
if params[:game] if params[:game]
if params[:game][:hand_type] == 'pass' if params[:game][:hand_type] == 'pass'
@game.set_active_player 'next' @game.set_controlling_player 'next'
flash[:notice] = 'Successfully passed.' flash[:notice] = 'Successfully passed.'
@game.save @game.save
end end
@ -38,13 +38,13 @@ class GamesController < ApplicationController
# Player action: play_hand # Player action: play_hand
if params[:player_card_ids] if params[:player_card_ids]
@cards_to_play = @game.current_player.try(:player_cards).order(:value).find(params[:player_card_ids]) @cards_to_play = @game.controlling_player.try(:player_cards).order(:value).find(params[:player_card_ids])
# Instantiate the play # Instantiate the play
@play = @game.current_player.plays.new @play = @game.controlling_player.plays.new
@play.game_id = @game.id @play.game_id = @game.id
@play.player_cards << @cards_to_play @play.player_cards << @cards_to_play
if @play.save if @play.save
@game.set_active_player 'next' @game.set_controlling_player 'next'
@game.save @game.save
else else
# TODO: Does anything need to happen here? # TODO: Does anything need to happen here?

View File

@ -4,15 +4,11 @@ class Game < ActiveRecord::Base
has_many :users, through: :players has_many :users, through: :players
has_many :player_cards, through: :players has_many :player_cards, through: :players
has_many :plays, through: :players has_many :plays, through: :players
has_one :controlling_player
belongs_to :play_to_beat belongs_to :play_to_beat
accepts_nested_attributes_for :players accepts_nested_attributes_for :players
validates :title, presence: true 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 def start
validate_startable validate_startable
return false unless startable? return false unless startable?
@ -36,14 +32,10 @@ class Game < ActiveRecord::Base
# Set status to first turn # Set status to first turn
# Player with the lowest card # Player with the lowest card
self.status = 'First Play at: ' + self.lowest_card.player.user.email self.status = 'First Play at: ' + self.lowest_card.player.user.email
self.control_player_id = self.lowest_card.player.id self.controlling_player_id = self.lowest_card.player.id
save save
end end
def current_player
self.players.find_by(id: control_player_id)
end
def validate_startable def validate_startable
@player_count = players.count @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
@ -87,10 +79,10 @@ class Game < ActiveRecord::Base
players.count >= 4 players.count >= 4
end end
def set_active_player(arg) def set_controlling_player(arg)
case arg case arg
when 'next' when 'next'
self.active_player_id = self.next_player_id self.controlling_player_id = self.next_player_id
end end
end end
@ -99,17 +91,17 @@ class Game < ActiveRecord::Base
end end
def next_player_id def next_player_id
player_ids[(player_ids.index(current_player_id) + 1 ) % player_ids.length] player_ids[(player_ids.index(controlling_player_id) + 1 ) % player_ids.length]
end end
# The player whose turn it is # The player whose turn it is
def control_player def controlling_player
self.players.find(self.control_player_id) self.players.find(self.controlling_player_id)
end end
# The user whose turn it is # The user whose turn it is
def control_user def controlling_user
self.players.find(self.control_player_id).user self.players.find(self.controlling_player_id).user
end end
def hand_to_beat def hand_to_beat

View File

@ -32,8 +32,8 @@
= "Me" if player.user == current_user = "Me" if player.user == current_user
// Show whose turn it is // Show whose turn it is
- if @game.active_player_id - if @game.controlling_player_id
%strong= "My Turn " if @game.active_player == player %strong= "My Turn " if @game.controlling_player == player
// Show the players' cards // Show the players' cards
- if player.user == current_user && player.inventory.empty? == false - if player.user == current_user && player.inventory.empty? == false
@ -43,7 +43,7 @@
-#- unless @game.status == nil -#- unless @game.status == nil
-#.table -#.table
-# TODO better logic, not in view. Set play to beat = nil; instead of view logic -# TODO better logic, not in view. Set play to beat = nil; instead of view logic
- if @game.play_to_beat && @game.player_to_beat != @game.current_player - if @game.play_to_beat && @game.player_to_beat != @game.controlling_player
- if @game.play_to_beat.play - if @game.play_to_beat.play
%h1 %h1
Hand to beat Hand to beat

View File

@ -1,5 +1,5 @@
json.extract! @game, :id, :title, :created_at, :updated_at, :current_player_id json.extract! @game, :id, :title, :created_at, :updated_at, :controlling_player_id
if @game.current_player == @game.try(:player_to_beat) if @game.controlling_player == @game.try(:player_to_beat)
json.play_to_beat_string "None" json.play_to_beat_string "None"
else else
json.play_to_beat_string @game.play_to_beat.try(:play).try(:to_s) json.play_to_beat_string @game.play_to_beat.try(:play).try(:to_s)

View File

@ -0,0 +1,5 @@
class RenameControlPlayerIdToControllingPlayerId < ActiveRecord::Migration
def change
rename_column :games, :control_player_id, :controlling_player_id
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20151107111235) do ActiveRecord::Schema.define(version: 20151108232044) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20151107111235) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "status" t.string "status"
t.integer "control_player_id" t.integer "controlling_player_id"
t.integer "play_to_beat_id" t.integer "play_to_beat_id"
t.integer "winner_player_id" t.integer "winner_player_id"
end end

View File

@ -6,7 +6,7 @@ feature 'Play a hand', type: :feature, js: true do
before(:each) do before(:each) do
@game = setup_game @game = setup_game
@game.reload @game.reload
@player = @game.current_player @player = @game.controlling_player
login_as(@player.user, scope: :user) login_as(@player.user, scope: :user)
visit game_path @game visit game_path @game
end end
@ -16,7 +16,7 @@ feature 'Play a hand', type: :feature, js: true do
end end
scenario 'first play does not contain lowest card' do scenario 'first play does not contain lowest card' do
# current_player plays hand not containing lowest card # controlling_player plays hand not containing lowest card
# expect unsuccessful play message hand MUST contain lowest card # expect unsuccessful play message hand MUST contain lowest card
# Play the highest card # Play the highest card
@ -92,7 +92,7 @@ feature 'Play a hand', type: :feature, js: true do
sleep 1 sleep 1
# Player inventory method still returns played cards because the play was invalid # Player inventory method still returns played cards because the play was invalid
# TODO: Flash messages # TODO: Flash messages
#expect(@game.current_player.inventory.order(:value).join(' ')).to have_content(@card_to_play.to_s) #expect(@game.controlling_player.inventory.order(:value).join(' ')).to have_content(@card_to_play.to_s)
end end
# Scenario: Current player status updates after successful play # Scenario: Current player status updates after successful play
@ -102,8 +102,8 @@ feature 'Play a hand', type: :feature, js: true do
scenario 'active player rotates after a valid hand' do scenario 'active player rotates after a valid hand' do
@game.reload @game.reload
@next_player_id = @game.next_player_id @next_player_id = @game.next_player_id
@current_player_id = @player.id @controlling_player_id = @player.id
@current_player_index = @game.player_ids.index(@game.active_player.id) @controlling_player_index = @game.player_ids.index(@game.controlling_player_id)
@card_to_play = @player.inventory.first @card_to_play = @player.inventory.first
check @card_to_play.to_s check @card_to_play.to_s
@ -118,8 +118,8 @@ feature 'Play a hand', type: :feature, js: true do
#expect(page).to have_content(@game.players.find(@next_player_id).user.email + " My Turn") #expect(page).to have_content(@game.players.find(@next_player_id).user.email + " My Turn")
# next player is now the active player # next player is now the active player
#expect(@game.active_player_id).to_not eq(@active_player_id) #expect(@game.controlling_player_id).to_not eq(@controlling_player_id)
#expect(@game.active_player_id).to eq(@next_player_id) #expect(@game.controlling_player_id).to eq(@next_player_id)
expect(page).to have_content("Current Player: #{@next_player_id}") expect(page).to have_content("Current Player: #{@next_player_id}")
end end
@ -129,18 +129,18 @@ feature 'Play a hand', type: :feature, js: true do
# Then I see an invalid hand message # Then I see an invalid hand message
scenario 'active player can not play an invalid hand' do scenario 'active player can not play an invalid hand' do
#@game.reload #@game.reload
@cards_to_play = [@game.current_player.player_cards.order(:value).first.to_s, @game.current_player.player_cards.order(:value).last.to_s] @cards_to_play = [@game.controlling_player.player_cards.order(:value).first.to_s, @game.controlling_player.player_cards.order(:value).last.to_s]
@cards_to_play.each do |card| @cards_to_play.each do |card|
check card check card
end end
#click_button 'Play Hand' #click_button 'Play Hand'
click_button "play_hand_button_#{@player.id}" click_button "play_hand_button_#{@player.id}"
# current_player plays an invalid hand # controlling_player plays an invalid hand
# expect invalid hand message # expect invalid hand message
#:TODO: Flash messages #:TODO: Flash messages
#expect(page).to have_content("Invalid pair.") #expect(page).to have_content("Invalid pair.")
# expect inventory to remain unchanged # expect inventory to remain unchanged
expect(page).to have_content("#{@game.current_player.inventory.map {|c| c.to_s}.join ' '}") expect(page).to have_content("#{@game.controlling_player.inventory.map {|c| c.to_s}.join ' '}")
# TODO: This scenario is causing other scenarios to fail unless we slow down here # TODO: This scenario is causing other scenarios to fail unless we slow down here
sleep 1 sleep 1
end end
@ -149,15 +149,15 @@ feature 'Play a hand', type: :feature, js: true do
# Given I am not the active player # Given I am not the active player
# When I play a hand # When I play a hand
# Then I see an unsuccessful play message # Then I see an unsuccessful play message
scenario 'inactive player can not play a hand' do scenario 'non-controlling player can not play a hand' do
# create game with players # create game with players
# not_current_player plays a valid hand # not_controlling_player plays a valid hand
# expect not to be able to submit a play # expect not to be able to submit a play
@inactive_player = @game.players.where.not(id: @game.current_player_id).first @non_controlling_player = @game.players.where.not(id: @game.controlling_player_id).first
login_as(@inactive_player.user, scope: :user) login_as(@non_controlling_player.user, scope: :user)
visit game_path @game visit game_path @game
#expect(page).to_not have_button 'Play Hand' #expect(page).to_not have_button 'Play Hand'
elem = page.find "input#play_hand_button_#{@inactive_player.id}" elem = page.find "input#play_hand_button_#{@non_controlling_player.id}"
expect(elem.disabled?).to be true expect(elem.disabled?).to be true
# TODO: Submit a POST request # TODO: Submit a POST request
end end
@ -167,7 +167,7 @@ feature 'Play a hand', type: :feature, js: true do
# When I pass # When I pass
# Then I see an successful pass message # Then I see an successful pass message
scenario 'active player can pass' do scenario 'active player can pass' do
@current_user = @game.current_player @current_user = @game.controlling_player
#click_button 'Pass' #click_button 'Pass'
click_button "pass_hand_button_#{@player.id}" click_button "pass_hand_button_#{@player.id}"
## TODO: Flash messages ## TODO: Flash messages
@ -177,8 +177,8 @@ feature 'Play a hand', type: :feature, js: true do
elem = page.find "input#play_hand_button_#{@player.id}" elem = page.find "input#play_hand_button_#{@player.id}"
expect(elem.disabled?).to be false expect(elem.disabled?).to be false
@new_current_player = @game.current_player @new_controlling_player = @game.controlling_player
expect(@game.current_player).to_not eq(@current_player) expect(@game.controlling_player).to_not eq(@controlling_player)
# TODO: This scenario is causing other scenarios to fail unless we slow down here # TODO: This scenario is causing other scenarios to fail unless we slow down here
sleep 1 sleep 1
end end
@ -190,7 +190,7 @@ feature 'Play a hand', type: :feature, js: true do
scenario 'active player to beat has open table' do scenario 'active player to beat has open table' do
# play a card # play a card
@game.reload @game.reload
@player = @game.current_player @player = @game.controlling_player
@card_to_play = @player.inventory.first @card_to_play = @player.inventory.first
check @card_to_play.to_s check @card_to_play.to_s
#click_button 'Play Hand' #click_button 'Play Hand'
@ -201,7 +201,7 @@ feature 'Play a hand', type: :feature, js: true do
expect(@game.player_to_beat).to eq @player expect(@game.player_to_beat).to eq @player
# set control player to player to beat # set control player to player to beat
@game.control_player_id = @player.id @game.controlling_player_id = @player.id
@game.save @game.save
@game.reload @game.reload
visit game_path @game visit game_path @game
@ -209,7 +209,7 @@ feature 'Play a hand', type: :feature, js: true do
# Expect active player should rotate # Expect active player should rotate
#expect(page).to have_button 'Play Hand' #expect(page).to have_button 'Play Hand'
expect(page).to have_button "play_hand_button_#{@player.id}" expect(page).to have_button "play_hand_button_#{@player.id}"
#expect(@game.player_to_beat).to eq(@game.active_player) #expect(@game.player_to_beat).to eq(@game.controlling_player)
expect(page).to have_content 'Hand to beat: None' expect(page).to have_content 'Hand to beat: None'
end end

View File

@ -1,7 +1,7 @@
RSpec.describe Play, type: :model do RSpec.describe Play, type: :model do
before(:all) do before(:all) do
@game = setup_game @game = setup_game
@play = @game.current_player.plays.new @play = @game.controlling_player.plays.new
@play.game_id = @game.id @play.game_id = @game.id
@play.save @play.save
end end
@ -59,9 +59,9 @@ RSpec.describe Play, type: :model do
it 'valid if it contains the lowest card' do it 'valid if it contains the lowest card' do
# expect(@game.plays.count).to eq(1) # expect(@game.plays.count).to eq(1)
# Active player is the lowest card holder # Active player is the lowest card holder
expect(@game.current_player).to eq(@game.lowest_card.player) expect(@game.controlling_player).to eq(@game.lowest_card.player)
# Play the lowest card # Play the lowest card
@cards_to_play = @game.current_player.player_cards.order(:value).first @cards_to_play = @game.controlling_player.player_cards.order(:value).first
@play.player_cards << @cards_to_play @play.player_cards << @cards_to_play
#@play.save #@play.save
expect(@play.player_cards.include? @game.lowest_card).to eq true expect(@play.player_cards.include? @game.lowest_card).to eq true
@ -80,7 +80,7 @@ RSpec.describe Play, type: :model do
# Try to play a losing card # Try to play a losing card
@losing_play = Play.new @losing_play = Play.new
@losing_play.player = @game.players.where.not(id: @game.active_player.id).last @losing_play.player = @game.players.where.not(id: @game.controlling_player_id).last
@losing_play.game = @game @losing_play.game = @game
@losing_play.player_cards << @losing_play.player.player_cards.order(:value).first @losing_play.player_cards << @losing_play.player.player_cards.order(:value).first
expect(@losing_play.beats? @play).to eq(false) expect(@losing_play.beats? @play).to eq(false)
@ -99,7 +99,7 @@ RSpec.describe Play, type: :model do
] ]
@play_to_beat = Play.new @play_to_beat = Play.new
@play_to_beat.player = @game.players.where.not(id: @game.active_player.id).first @play_to_beat.player = @game.players.where.not(id: @game.controlling_player_id).first
@play_to_beat.player_cards << @cards_to_beat @play_to_beat.player_cards << @cards_to_beat
@play_to_beat.game = @game @play_to_beat.game = @game
#@play_to_beat.save #@play_to_beat.save
@ -134,7 +134,7 @@ RSpec.describe Play, type: :model do
] ]
@play_to_beat = Play.new @play_to_beat = Play.new
@play_to_beat.player = @game.players.where.not(id: @game.active_player.id).first @play_to_beat.player = @game.players.where.not(id: @game.controlling_player_id).first
@play_to_beat.player_cards << @cards_to_beat @play_to_beat.player_cards << @cards_to_beat
@play_to_beat.game = @game @play_to_beat.game = @game
#@play_to_beat.save #@play_to_beat.save
@ -176,29 +176,29 @@ RSpec.describe Play, type: :model do
@deck = Deck.new @deck = Deck.new
# Give 3 of clubs to current player # Give 3 of clubs to current player
@club3 = @game.player_cards.find_by(value: 2) @club3 = @game.player_cards.find_by(value: 2)
@club3.player_id = @game.current_player.id @club3.player_id = @game.controlling_player.id
@club3.save @club3.save
@cards_to_beat = [ @cards_to_beat = [
@spade3 = @game.current_player.player_cards.order(:value).first, @spade3 = @game.controlling_player.player_cards.order(:value).first,
@club3 @club3
] ]
@play.player_cards << @cards_to_beat @play.player_cards << @cards_to_beat
expect(@play.save).to be true expect(@play.save).to be true
@game.reload @game.reload
@game.current_player_id = @game.next_player_id @game.controlling_player_id = @game.next_player_id
@game.save @game.save
@game.reload @game.reload
@winning_play = Play.new @winning_play = Play.new
@winning_play.player = @game.current_player @winning_play.player = @game.controlling_player
@winning_play.game = @game @winning_play.game = @game
# Give needed cards to current player # Give needed cards to current player
@spade4 = @game.player_cards.find_by(value: 5) @spade4 = @game.player_cards.find_by(value: 5)
@spade4.player_id = @game.current_player.id @spade4.player_id = @game.controlling_player.id
@spade4.save @spade4.save
@club4 = @game.player_cards.find_by(value: 6) @club4 = @game.player_cards.find_by(value: 6)
@club4.player_id = @game.current_player.id @club4.player_id = @game.controlling_player.id
@club4.save @club4.save
@winning_play.player_cards = [ @winning_play.player_cards = [
@spade4, @spade4,
@ -225,7 +225,7 @@ RSpec.describe Play, type: :model do
describe 'hand_type' do describe 'hand_type' do
context 'when a single card is played' do context 'when a single card is played' do
it 'single' do it 'single' do
@cards_to_play = @game.current_player.player_cards.order(:value).first @cards_to_play = @game.controlling_player.player_cards.order(:value).first
@play.player_cards << @cards_to_play @play.player_cards << @cards_to_play
expect(@play.hand_type).to eq('single') expect(@play.hand_type).to eq('single')
end end