From 4db7bb0eb9feafa613d823767c019a1bc6abe247 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Fri, 17 Apr 2015 05:02:56 -0700 Subject: [PATCH] another stash --- app/models/deck.rb | 2 +- app/models/game.rb | 38 +++++++++++++++---- app/views/games/show.html.haml | 18 +++++++-- ...417112103_add_control_player_id_to_game.rb | 5 +++ db/schema.rb | 3 +- 5 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20150417112103_add_control_player_id_to_game.rb diff --git a/app/models/deck.rb b/app/models/deck.rb index 27a457d..81a7260 100644 --- a/app/models/deck.rb +++ b/app/models/deck.rb @@ -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 diff --git a/app/models/game.rb b/app/models/game.rb index 9fbc8da..742b607 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -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 diff --git a/app/views/games/show.html.haml b/app/views/games/show.html.haml index 18fd5bc..57b18a3 100644 --- a/app/views/games/show.html.haml +++ b/app/views/games/show.html.haml @@ -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) \| diff --git a/db/migrate/20150417112103_add_control_player_id_to_game.rb b/db/migrate/20150417112103_add_control_player_id_to_game.rb new file mode 100644 index 0000000..02a7aeb --- /dev/null +++ b/db/migrate/20150417112103_add_control_player_id_to_game.rb @@ -0,0 +1,5 @@ +class AddControlPlayerIdToGame < ActiveRecord::Migration + def change + add_column :games, :control_player_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 5c96ccc..19bdfbd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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|