stash play hand logic

This commit is contained in:
Jesse C. Fisher 2015-05-03 13:13:36 -07:00
parent fc5ef4ce32
commit a83c0679d7
6 changed files with 35 additions and 3 deletions

View File

@ -22,7 +22,18 @@ class GamesController < ApplicationController
def play_hand
# TODO validate player's turn, current_user == @game.current_player.user
@hand = params[:player_card_ids]
@cards_to_play = @game.current_player.player_cards.find(params[:player_card_ids])
# Instantiate the play
@play = @game.current_player.plays.new
@play.game_id = @game.id
@play.save
@cards_to_play.each do |card|
card.play_id = @play.id
card.save
end
# TODO add current_hand logic
# @hand > @game.hand_to_beat
redirect_to @game

View File

@ -3,6 +3,7 @@ class Game < ActiveRecord::Base
has_many :players, dependent: :destroy
has_many :users, through: :players
has_many :player_cards, through: :players
has_many :plays, through: :players
accepts_nested_attributes_for :players
validates :title, presence: true

View File

@ -4,6 +4,7 @@ class Player < ActiveRecord::Base
belongs_to :user
has_many :player_cards
has_many :plays
validates_presence_of :user_id
validates_presence_of :game_id

View File

@ -1,3 +1,8 @@
class PlayerCard < ActiveRecord::Base
belongs_to :player
belongs_to :play
def to_s
[rank, suit].join ' '
end
end

View File

@ -47,7 +47,10 @@
%h1 Hand to beat
- else
%h1 Open table
%h2= "Played #{@hand}"
%h2
Played
- @game.plays.last.player_cards.each do |card|
= card.to_s
.controls
- if @game.control_user == current_user

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150417112103) do
ActiveRecord::Schema.define(version: 20150503133556) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -32,6 +32,7 @@ ActiveRecord::Schema.define(version: 20150417112103) do
t.integer "value"
t.string "rank"
t.string "suit"
t.integer "play_id"
end
add_index "player_cards", ["card_id"], name: "index_player_cards_on_card_id", using: :btree
@ -47,6 +48,16 @@ ActiveRecord::Schema.define(version: 20150417112103) do
add_index "players", ["game_id"], name: "index_players_on_game_id", using: :btree
add_index "players", ["user_id"], name: "index_players_on_user_id", using: :btree
create_table "plays", force: true do |t|
t.integer "game_id"
t.integer "player_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "plays", ["game_id"], name: "index_plays_on_game_id", using: :btree
add_index "plays", ["player_id"], name: "index_plays_on_player_id", using: :btree
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: ""