another stash

This commit is contained in:
Jesse C. Fisher 2015-04-17 05:02:56 -07:00
parent 327f7d8f1b
commit 4db7bb0eb9
5 changed files with 52 additions and 14 deletions

View File

@ -6,7 +6,7 @@ class Deck
self.cards = []
Card::RANKS.each do |rank|
Card::SUITS.each do |suit|
self.cards << Card.new(value, rank.to_i, suit)
self.cards << Card.new(value, rank, suit)
value += 1
end
end

View File

@ -25,6 +25,11 @@ class Game < ActiveRecord::Base
end
player.save
end
# 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
save
end
@ -32,17 +37,23 @@ class Game < ActiveRecord::Base
@player_count = players.count
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[:status] = 'Game already started...' if self.status == 'Game Started'
raise StandardError, errors if errors
errors[:status] = 'Game already started...' if self.status != nil
errors[:player_cards] = 'Cards already dealt...' unless player_cards.empty?
raise StandardError, errors if errors.count > 0
return true
end
def startable?
@player_count = players.count
tests = [
@player_count.between?(2, 4),
status != 'Game Started'
]
return tests.all?
begin
self.validate_startable
rescue
end
#@player_count = players.count
#tests = [
#@player_count.between?(2, 4),
#status != 'Game Started'
#]
#return tests.all?
end
def lowest_card
@ -65,4 +76,15 @@ class Game < ActiveRecord::Base
def full?
players.count >= 4
end
# The player whose turn it is
def control_player
self.players.find(self.control_player_id)
end
# The user whose turn it is
def control_user
self.players.find(self.control_player_id).user
end
end

View File

@ -12,17 +12,27 @@
- if @game.startable? && @game.already_has?(current_user)
= link_to 'Start', game_start_path(@game)
= link_to 'Start', game_start_path(@game)
= link_to 'Debug Start', game_start_path(@game)
%p
%b Players:
%ol
- @game.players.each do |player|
%li= player
%li
%strong= "My Turn " if @game.control_player == player
= player.user.email
= "Me" if player.user == current_user
- unless player.player_cards.empty?
%ul.cards
- player.player_cards.each do |card|
%li= [card.rank, card.suit].join(' ')
- player.player_cards.order(:value).each do |card|
%li
%label
= check_box_tag 'player_card_ids[]', card.id
= [card.rank, card.suit].join(' ')
%p
- if @game.control_user == current_user
= link_to 'Play Hand', '#'
= link_to 'Edit', edit_game_path(@game)
\|

View File

@ -0,0 +1,5 @@
class AddControlPlayerIdToGame < ActiveRecord::Migration
def change
add_column :games, :control_player_id, :integer
end
end

View File

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