Rename current_player to controlling_player
This commit is contained in:
parent
992da8e21e
commit
2a5504a982
@ -4,10 +4,10 @@ Thirteen Tien Len
|
||||
TODO
|
||||
----
|
||||
|
||||
- rename `current_player(_id)` to `controlling_player(_id)`
|
||||
|
||||
- graphics
|
||||
|
||||
- Remove PlayToBeat model and table.
|
||||
|
||||
- Test `play_hand` of run including face cards
|
||||
|
||||
- BUG TODO: First player / lowest card holder should not be able to pass
|
||||
|
||||
@ -92,14 +92,14 @@ var Game = React.createClass({
|
||||
<div>
|
||||
<div>Title: {this.state.data.title}</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 className='player-list'>
|
||||
<PlayerList
|
||||
game_id={this.props.id}
|
||||
players={this.state.data.players}
|
||||
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} />
|
||||
</div>
|
||||
<div>
|
||||
|
||||
@ -19,7 +19,7 @@ var Player = React.createClass({
|
||||
game_id={this.props.game_id}
|
||||
player_id={this.props.id}
|
||||
inventory={this.props.inventory}
|
||||
current_player_id={this.props.current_player_id} /> )
|
||||
controlling_player_id={this.props.controlling_player_id} /> )
|
||||
: null }
|
||||
</div>
|
||||
);
|
||||
|
||||
@ -28,13 +28,13 @@ var PlayerControls = React.createClass({
|
||||
<input type="hidden" value="patch" name="_method"></input>
|
||||
<div><Inventory ref="inventory" cards={this.props.inventory} player_id={this.props.id} /></div>
|
||||
<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"
|
||||
value="Play Hand"
|
||||
name="commit">
|
||||
</input>
|
||||
<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"
|
||||
onClick={this.handlePass}
|
||||
value="Pass"
|
||||
|
||||
@ -11,7 +11,7 @@ var PlayerList = React.createClass({
|
||||
<Player
|
||||
key={player.id}
|
||||
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}
|
||||
user_id={player.user_id}
|
||||
game_id={playerList.props.game_id} {...player} />
|
||||
|
||||
@ -24,13 +24,13 @@ class GamesController < ApplicationController
|
||||
# TODO: move this logic out of controller maybe to model(s) ?
|
||||
|
||||
# validate player's turn
|
||||
if current_user == @game.current_player.user
|
||||
if current_user == @game.controlling_player.user
|
||||
|
||||
# Player action: pass
|
||||
# TODO: wtf do we need game params for? And why nest hand_type in it?
|
||||
if params[:game]
|
||||
if params[:game][:hand_type] == 'pass'
|
||||
@game.set_active_player 'next'
|
||||
@game.set_controlling_player 'next'
|
||||
flash[:notice] = 'Successfully passed.'
|
||||
@game.save
|
||||
end
|
||||
@ -38,13 +38,13 @@ class GamesController < ApplicationController
|
||||
|
||||
# Player action: play_hand
|
||||
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
|
||||
@play = @game.current_player.plays.new
|
||||
@play = @game.controlling_player.plays.new
|
||||
@play.game_id = @game.id
|
||||
@play.player_cards << @cards_to_play
|
||||
if @play.save
|
||||
@game.set_active_player 'next'
|
||||
@game.set_controlling_player 'next'
|
||||
@game.save
|
||||
else
|
||||
# TODO: Does anything need to happen here?
|
||||
|
||||
@ -4,15 +4,11 @@ class Game < ActiveRecord::Base
|
||||
has_many :users, through: :players
|
||||
has_many :player_cards, through: :players
|
||||
has_many :plays, through: :players
|
||||
has_one :controlling_player
|
||||
belongs_to :play_to_beat
|
||||
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?
|
||||
@ -36,14 +32,10 @@ class Game < ActiveRecord::Base
|
||||
# Set status to first turn
|
||||
# Player with the lowest card
|
||||
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
|
||||
end
|
||||
|
||||
def current_player
|
||||
self.players.find_by(id: control_player_id)
|
||||
end
|
||||
|
||||
def validate_startable
|
||||
@player_count = players.count
|
||||
errors[:players] = 'Must be at least 2 players.' if @player_count < 2
|
||||
@ -87,10 +79,10 @@ class Game < ActiveRecord::Base
|
||||
players.count >= 4
|
||||
end
|
||||
|
||||
def set_active_player(arg)
|
||||
def set_controlling_player(arg)
|
||||
case arg
|
||||
when 'next'
|
||||
self.active_player_id = self.next_player_id
|
||||
self.controlling_player_id = self.next_player_id
|
||||
end
|
||||
end
|
||||
|
||||
@ -99,17 +91,17 @@ class Game < ActiveRecord::Base
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
# The player whose turn it is
|
||||
def control_player
|
||||
self.players.find(self.control_player_id)
|
||||
def controlling_player
|
||||
self.players.find(self.controlling_player_id)
|
||||
end
|
||||
|
||||
# The user whose turn it is
|
||||
def control_user
|
||||
self.players.find(self.control_player_id).user
|
||||
def controlling_user
|
||||
self.players.find(self.controlling_player_id).user
|
||||
end
|
||||
|
||||
def hand_to_beat
|
||||
|
||||
@ -32,8 +32,8 @@
|
||||
= "Me" if player.user == current_user
|
||||
|
||||
// Show whose turn it is
|
||||
- if @game.active_player_id
|
||||
%strong= "My Turn " if @game.active_player == player
|
||||
- if @game.controlling_player_id
|
||||
%strong= "My Turn " if @game.controlling_player == player
|
||||
|
||||
// Show the players' cards
|
||||
- if player.user == current_user && player.inventory.empty? == false
|
||||
@ -43,7 +43,7 @@
|
||||
-#- unless @game.status == nil
|
||||
-#.table
|
||||
-# 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
|
||||
%h1
|
||||
Hand to beat
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
json.extract! @game, :id, :title, :created_at, :updated_at, :current_player_id
|
||||
if @game.current_player == @game.try(:player_to_beat)
|
||||
json.extract! @game, :id, :title, :created_at, :updated_at, :controlling_player_id
|
||||
if @game.controlling_player == @game.try(:player_to_beat)
|
||||
json.play_to_beat_string "None"
|
||||
else
|
||||
json.play_to_beat_string @game.play_to_beat.try(:play).try(:to_s)
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
class RenameControlPlayerIdToControllingPlayerId < ActiveRecord::Migration
|
||||
def change
|
||||
rename_column :games, :control_player_id, :controlling_player_id
|
||||
end
|
||||
end
|
||||
@ -11,7 +11,7 @@
|
||||
#
|
||||
# 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
|
||||
enable_extension "plpgsql"
|
||||
@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20151107111235) do
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "status"
|
||||
t.integer "control_player_id"
|
||||
t.integer "controlling_player_id"
|
||||
t.integer "play_to_beat_id"
|
||||
t.integer "winner_player_id"
|
||||
end
|
||||
|
||||
@ -6,7 +6,7 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
before(:each) do
|
||||
@game = setup_game
|
||||
@game.reload
|
||||
@player = @game.current_player
|
||||
@player = @game.controlling_player
|
||||
login_as(@player.user, scope: :user)
|
||||
visit game_path @game
|
||||
end
|
||||
@ -16,7 +16,7 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
# Play the highest card
|
||||
@ -92,7 +92,7 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
sleep 1
|
||||
# Player inventory method still returns played cards because the play was invalid
|
||||
# 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
|
||||
|
||||
# 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
|
||||
@game.reload
|
||||
@next_player_id = @game.next_player_id
|
||||
@current_player_id = @player.id
|
||||
@current_player_index = @game.player_ids.index(@game.active_player.id)
|
||||
@controlling_player_id = @player.id
|
||||
@controlling_player_index = @game.player_ids.index(@game.controlling_player_id)
|
||||
|
||||
@card_to_play = @player.inventory.first
|
||||
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")
|
||||
|
||||
# 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)
|
||||
#expect(@game.controlling_player_id).to_not eq(@controlling_player_id)
|
||||
#expect(@game.controlling_player_id).to eq(@next_player_id)
|
||||
expect(page).to have_content("Current Player: #{@next_player_id}")
|
||||
end
|
||||
|
||||
@ -129,18 +129,18 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
# Then I see an invalid hand message
|
||||
scenario 'active player can not play an invalid hand' do
|
||||
#@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|
|
||||
check card
|
||||
end
|
||||
#click_button 'Play Hand'
|
||||
click_button "play_hand_button_#{@player.id}"
|
||||
# current_player plays an invalid hand
|
||||
# controlling_player plays an invalid hand
|
||||
# expect invalid hand message
|
||||
#:TODO: Flash messages
|
||||
#expect(page).to have_content("Invalid pair.")
|
||||
# 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
|
||||
sleep 1
|
||||
end
|
||||
@ -149,15 +149,15 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
# Given I am not the active player
|
||||
# When I play a hand
|
||||
# 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
|
||||
# not_current_player plays a valid hand
|
||||
# not_controlling_player plays a valid hand
|
||||
# expect not to be able to submit a play
|
||||
@inactive_player = @game.players.where.not(id: @game.current_player_id).first
|
||||
login_as(@inactive_player.user, scope: :user)
|
||||
@non_controlling_player = @game.players.where.not(id: @game.controlling_player_id).first
|
||||
login_as(@non_controlling_player.user, scope: :user)
|
||||
visit game_path @game
|
||||
#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
|
||||
# TODO: Submit a POST request
|
||||
end
|
||||
@ -167,7 +167,7 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
# When I pass
|
||||
# Then I see an successful pass message
|
||||
scenario 'active player can pass' do
|
||||
@current_user = @game.current_player
|
||||
@current_user = @game.controlling_player
|
||||
#click_button 'Pass'
|
||||
click_button "pass_hand_button_#{@player.id}"
|
||||
## 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}"
|
||||
expect(elem.disabled?).to be false
|
||||
|
||||
@new_current_player = @game.current_player
|
||||
expect(@game.current_player).to_not eq(@current_player)
|
||||
@new_controlling_player = @game.controlling_player
|
||||
expect(@game.controlling_player).to_not eq(@controlling_player)
|
||||
# TODO: This scenario is causing other scenarios to fail unless we slow down here
|
||||
sleep 1
|
||||
end
|
||||
@ -190,7 +190,7 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
scenario 'active player to beat has open table' do
|
||||
# play a card
|
||||
@game.reload
|
||||
@player = @game.current_player
|
||||
@player = @game.controlling_player
|
||||
@card_to_play = @player.inventory.first
|
||||
check @card_to_play.to_s
|
||||
#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
|
||||
|
||||
# set control player to player to beat
|
||||
@game.control_player_id = @player.id
|
||||
@game.controlling_player_id = @player.id
|
||||
@game.save
|
||||
@game.reload
|
||||
visit game_path @game
|
||||
@ -209,7 +209,7 @@ feature 'Play a hand', type: :feature, js: true do
|
||||
# Expect active player should rotate
|
||||
#expect(page).to have_button 'Play Hand'
|
||||
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'
|
||||
end
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
RSpec.describe Play, type: :model do
|
||||
before(:all) do
|
||||
@game = setup_game
|
||||
@play = @game.current_player.plays.new
|
||||
@play = @game.controlling_player.plays.new
|
||||
@play.game_id = @game.id
|
||||
@play.save
|
||||
end
|
||||
@ -59,9 +59,9 @@ RSpec.describe Play, type: :model do
|
||||
it 'valid if it contains the lowest card' do
|
||||
# expect(@game.plays.count).to eq(1)
|
||||
# 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
|
||||
@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.save
|
||||
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
|
||||
@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.player_cards << @losing_play.player.player_cards.order(:value).first
|
||||
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.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.game = @game
|
||||
#@play_to_beat.save
|
||||
@ -134,7 +134,7 @@ RSpec.describe Play, type: :model do
|
||||
]
|
||||
|
||||
@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.game = @game
|
||||
#@play_to_beat.save
|
||||
@ -176,29 +176,29 @@ RSpec.describe Play, type: :model do
|
||||
@deck = Deck.new
|
||||
# Give 3 of clubs to current player
|
||||
@club3 = @game.player_cards.find_by(value: 2)
|
||||
@club3.player_id = @game.current_player.id
|
||||
@club3.player_id = @game.controlling_player.id
|
||||
@club3.save
|
||||
@cards_to_beat = [
|
||||
@spade3 = @game.current_player.player_cards.order(:value).first,
|
||||
@spade3 = @game.controlling_player.player_cards.order(:value).first,
|
||||
@club3
|
||||
]
|
||||
|
||||
@play.player_cards << @cards_to_beat
|
||||
expect(@play.save).to be true
|
||||
@game.reload
|
||||
@game.current_player_id = @game.next_player_id
|
||||
@game.controlling_player_id = @game.next_player_id
|
||||
@game.save
|
||||
@game.reload
|
||||
|
||||
@winning_play = Play.new
|
||||
@winning_play.player = @game.current_player
|
||||
@winning_play.player = @game.controlling_player
|
||||
@winning_play.game = @game
|
||||
# Give needed cards to current player
|
||||
@spade4 = @game.player_cards.find_by(value: 5)
|
||||
@spade4.player_id = @game.current_player.id
|
||||
@spade4.player_id = @game.controlling_player.id
|
||||
@spade4.save
|
||||
@club4 = @game.player_cards.find_by(value: 6)
|
||||
@club4.player_id = @game.current_player.id
|
||||
@club4.player_id = @game.controlling_player.id
|
||||
@club4.save
|
||||
@winning_play.player_cards = [
|
||||
@spade4,
|
||||
@ -225,7 +225,7 @@ RSpec.describe Play, type: :model do
|
||||
describe 'hand_type' do
|
||||
context 'when a single card is played' 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
|
||||
expect(@play.hand_type).to eq('single')
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user