From b90665f866a513bb693ffdda40ac489a199bd752 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sat, 9 May 2015 18:28:49 +0000 Subject: [PATCH 01/11] adding rvm ignroare --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6a502e9..fe0af7c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # Ignore bundler config. /.bundle +.rvm # Ignore the default SQLite database. /db/*.sqlite3 /db/*.sqlite3-journal From ea1324a100038a6a80bf6d2488ea0b5afa9ed906 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sat, 9 May 2015 19:00:22 +0000 Subject: [PATCH 02/11] adding my notes and index reorg --- .gitignore | 2 + app/models/game.rb | 102 ++++++++------------------------- app/views/games/index.html.erb | 47 +++++++++------ lib/card.rb | 45 +++++++++++++++ todo.md | 0 5 files changed, 100 insertions(+), 96 deletions(-) create mode 100644 lib/card.rb create mode 100644 todo.md diff --git a/.gitignore b/.gitignore index fe0af7c..83e5b73 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ # Ignore all logfiles and tempfiles. /log/*.log /tmp +*.sw* +/**/*.sw* diff --git a/app/models/game.rb b/app/models/game.rb index 7fa1b91..2e15f3e 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,19 +1,24 @@ class Game < ActiveRecord::Base require 'yaml' - def set_up - state = {} - - state[:total_rounds] = 10 - state[:rounds_played] = 0 - state[:players] = [] - state[:player_hands] = [] - state[:score] = [] - state[:deck] = [] - state[:names] = [] + before_save :default_values - state[:players].shuffle! + def default_values + self.state ||= { + current_status: :waiting_for_players + }.to_yaml + end + def set_up + data = state_data + data[:total_rounds] = 10 + data[:rounds_played] = 0 + data[:players] = [] + data[:player_hands] = [] + data[:score] = [] + data[:deck] = [] + data[:names] = [] + data[:players].shuffle! save_state state end @@ -174,12 +179,20 @@ def player_action user_id, user_input=nil save_state state return true end + + def state_data + @state_read_at ||= self.updated_at + if @state_data.nil? || self.updated_at > @state_read_at + @state_data = load_state + end + @state_data + end def load_state return YAML.load(self.state) end - def save_state state + def save_state(state = state_data) self.state = state.to_yaml end @@ -244,68 +257,3 @@ def all_players_played_a_card? state end end -class Card - include Comparable - - SUITS = %w{Spades Hearts Diamonds Clubs} - attr_accessor :suit, :value, :playable - - # create a single card - def initialize suit, value - raise 'Invalid card suit' unless SUITS.include? suit - raise 'Invalid card value' unless value.to_i >= 0 and value.to_i < 13 - @suit = suit - @value = value - end - - def value_name - card_names = %w[Two Three Four Five Six Seven Eight Nine Ten Jack Queen King Ace] - return card_names[value] - end - - def <=> other - return 1 if other.nil? - return 0 if @value.nil? and other.value.nil? - return 1 if other.value.nil? - return -1 if @value.nil? - @value.to_i <=> other.value.to_i - end - - def == other - return false if other.nil? - return false if (@value.nil? or other.value.nil?) and @value != other.value - return (@value.to_i == other.value.to_i and @suit == other.suit) - end - - # creates a standard deck of 52 cards, Ace high - # the '0' represents 2, and '12' is Ace - def self.get_deck - cards = [] - SUITS.each do |suit| - 1..13.times do |i| - cards.push Card.new(suit, i) - end - end - return cards - end -end - -class Player - attr_accessor :name, :inventory - - def initialize name - raise 'Invalid player name' if name.nil? or name.empty? - @name = name - @inventory = [] - end - - def place_bid - raise 'TODO: implement me!' # TODO: implement this - return 0 - end - - # play a card from the inventory - def play_card - raise 'TODO: implement me!' # TODO: implement this - end -end diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb index 10e1c0e..e673c7a 100644 --- a/app/views/games/index.html.erb +++ b/app/views/games/index.html.erb @@ -1,29 +1,38 @@ -

Listing games

+
+

Crush, The Card Game

- - +
+ +

+ +

+<% if @_current_user %> + Your Current User: <%= @_current_user %> +<% end %> +

+ +

Current Games

+ +
+ - + - - <% @games.each do |game| %> - - - - - - - - <% end %> + + <% @games.order(:updated_at).each do |game| %> + + + + + + <% end %>
NameStateRound
<%= game.name %><%= game.state %><%= link_to('Show', game, data: { no_turbolink: true }) %><%= link_to 'Edit', edit_game_path(game) %><%= link_to 'Destroy', game, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= game.name %><%= "#{10 - game.state_data[:rounds_played]}" %><%= link_to('Join', game, data: { no_turbolink: true }) %>
-
- -<%= link_to 'New Game', new_game_path %> -
-<%= @_current_user %> +
diff --git a/lib/card.rb b/lib/card.rb new file mode 100644 index 0000000..6e14f9a --- /dev/null +++ b/lib/card.rb @@ -0,0 +1,45 @@ +class Card + include Comparable + + SUITS = %w{Spades Hearts Diamonds Clubs} + attr_accessor :suit, :value, :playable + + # create a single card + def initialize suit, value + raise 'Invalid card suit' unless SUITS.include? suit + raise 'Invalid card value' unless value.to_i >= 0 and value.to_i < 13 + @suit = suit + @value = value + end + + def value_name + card_names = %w[Two Three Four Five Six Seven Eight Nine Ten Jack Queen King Ace] + return card_names[value] + end + + def <=> other + return 1 if other.nil? + return 0 if @value.nil? and other.value.nil? + return 1 if other.value.nil? + return -1 if @value.nil? + @value.to_i <=> other.value.to_i + end + + def == other + return false if other.nil? + return false if (@value.nil? or other.value.nil?) and @value != other.value + return (@value.to_i == other.value.to_i and @suit == other.suit) + end + + # creates a standard deck of 52 cards, Ace high + # the '0' represents 2, and '12' is Ace + def self.get_deck + cards = [] + SUITS.each do |suit| + 1..13.times do |i| + cards.push Card.new(suit, i) + end + end + return cards + end +end diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..e69de29 From d531f412d18e8538da80ac67de21786964cba310 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sat, 9 May 2015 19:12:33 +0000 Subject: [PATCH 03/11] better frontpage --- app/models/game.rb | 13 +++++---- app/views/games/index.html.erb | 50 +++++++++++++++++----------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/app/models/game.rb b/app/models/game.rb index 2e15f3e..72cfbbd 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -4,9 +4,12 @@ class Game < ActiveRecord::Base before_save :default_values def default_values - self.state ||= { - current_status: :waiting_for_players - }.to_yaml + state_data.reverse_merge current_status: :waiting_for_players + save_state + end + + def waiting_for_players? + state_data[:current_status] == :waiting_for_players end def set_up @@ -19,7 +22,7 @@ def set_up data[:deck] = [] data[:names] = [] data[:players].shuffle! - save_state state + save_state end def deal_cards state @@ -189,7 +192,7 @@ def state_data end def load_state - return YAML.load(self.state) + return self.state.nil? ? Hash.new : YAML.load(self.state) end def save_state(state = state_data) diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb index e673c7a..30777bf 100644 --- a/app/views/games/index.html.erb +++ b/app/views/games/index.html.erb @@ -1,38 +1,38 @@
-

Crush, The Card Game

+

Crush, The Card Game

-
- -

+
-

-<% if @_current_user %> - Your Current User: <%= @_current_user %> -<% end %> -

+

+ <% if @_current_user %> + Your Current User: <%= @_current_user %> + <% end %> +

-

Current Games

+

Current Games

- - - - - - - - +
NameRound
+ + + + + + + - - <% @games.order(:updated_at).each do |game| %> - + + <% @games.order(:updated_at).each do |game| %> + - + <% label = game.waiting_for_players? ? "Join Game" : "Game Started Already" %> + <% label_class = game.waiting_for_players? ? "btn-success" : "btn-info" %> + <% end %> - -
Game NameRoundJoin
<%= game.name %> <%= "#{10 - game.state_data[:rounds_played]}" %><%= link_to('Join', game, data: { no_turbolink: true }) %>
<%= link_to(label, game, data: { no_turbolink: true }) %>
+ +
From ae1916e4829db1a518dd5a365cddff7f2f693ef6 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sat, 9 May 2015 19:24:32 +0000 Subject: [PATCH 04/11] reworking formss --- app/controllers/application_controller.rb | 7 +++++-- app/controllers/games_controller.rb | 22 +++++++++++----------- app/models/game.rb | 13 ++++--------- app/views/games/_form.html.erb | 4 ---- app/views/games/index.html.erb | 4 ++-- 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bf01ad1..02701fd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,6 +6,8 @@ class ApplicationController < ActionController::Base # run these methods on page load before_filter :current_user + helper_method :current_user + private # Finds the User with the ID stored in the session with the key @@ -13,7 +15,8 @@ class ApplicationController < ActionController::Base # a Rails application; logging in sets the session value and # logging out removes it. def current_user - session[:id] ||= request.remote_ip.hash + rand(1000) - @_current_user = session[:id] + @_current_user ||= unless @_current_user + session[:id] ||= request.remote_ip.hash + rand(1000) + end end end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 6d9ebfa..fafd9cf 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -2,16 +2,16 @@ class GamesController < ApplicationController before_action :set_game, only: [:show, :edit, :update, :destroy] def add_player - unless @_current_user.nil? + unless current_user.nil? set_game state = @game.load_state if state[:players].size < 5 - unless state[:players].include?(@_current_user) + unless state[:players].include?(current_user) if state[:cards_in_play] redirect_to @game, notice: 'Cant join once the game has already started' return end - state[:players].push @_current_user + state[:players].push current_user while state[:names].include? params[:username] # make sure username is unique by appending random numbers params[:username] += rand(10).to_s @@ -39,7 +39,7 @@ def deal redirect_to @game, notice: 'Must have between 3 and 5 players to start' return end - if state[:players].first == @_current_user + if state[:players].first == current_user if state[:cards_in_play].nil? @game.save_state @game.deal_cards(state) @game.save @@ -57,7 +57,7 @@ def player_action state = @game.load_state # player isnt allowed to do anything if it's not their turn - if state[:waiting_on] != @_current_user + if state[:waiting_on] != current_user redirect_to @game, notice: "Dude... It's not your turn" return end @@ -68,7 +68,7 @@ def player_action redirect_to @game, notice: 'Bidding is over BRO' return else - if @game.player_action(@_current_user, params[:bid]) + if @game.player_action(current_user, params[:bid]) @game.save redirect_to @game, notice: 'Placed bid, YEAAA!' else @@ -78,7 +78,7 @@ def player_action else # user is playing a card card = Card.new(params[:suit], params[:value]) - if @game.player_action(@_current_user, card) + if @game.player_action(current_user, card) @game.save redirect_to @game, notice: "Played a card, niceee!" else @@ -98,9 +98,9 @@ def index def show state = @game.load_state - is_playing = state[:players].include?(@_current_user) + is_playing = state[:players].include?(current_user) game_started = !state[:bids].nil? - player_index = state[:players].index(@_current_user) || 0 + player_index = state[:players].index(current_user) || 0 # round number @round = state[:total_rounds] - state[:rounds_played] @@ -137,7 +137,7 @@ def show # can't play any cards unless it's your turn playable_cards = [] - if state[:waiting_on] == @_current_user + if state[:waiting_on] == current_user playable_cards = @game.get_playable_cards(state[:first_suit_played], state[:player_hands][player_index]) end @cards.each do |card| @@ -152,7 +152,7 @@ def show if state[:waiting_on] waiting_on_index = state[:players].index(state[:waiting_on]) @waiting_on = state[:names][waiting_on_index] - @waiting_on = 'YOU' if @_current_user == state[:waiting_on] + @waiting_on = 'YOU' if current_user == state[:waiting_on] unless @game.done_bidding? state @waiting_on += " (BIDDING)" end diff --git a/app/models/game.rb b/app/models/game.rb index 72cfbbd..ca795ad 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,19 +1,13 @@ class Game < ActiveRecord::Base require 'yaml' - before_save :default_values - - def default_values - state_data.reverse_merge current_status: :waiting_for_players - save_state - end - def waiting_for_players? state_data[:current_status] == :waiting_for_players end def set_up data = state_data + data[:current_status] ||= :waiting_for_players data[:total_rounds] = 10 data[:rounds_played] = 0 data[:players] = [] @@ -184,8 +178,9 @@ def player_action user_id, user_input=nil end def state_data - @state_read_at ||= self.updated_at - if @state_data.nil? || self.updated_at > @state_read_at + @state_read_at ||= self.updated_at || self.created_at + current_time = self.updated_at || self.created_at + if @data_data.nil? || current_time > @state_read_at @state_data = load_state end @state_data diff --git a/app/views/games/_form.html.erb b/app/views/games/_form.html.erb index 44beb73..4acc0df 100644 --- a/app/views/games/_form.html.erb +++ b/app/views/games/_form.html.erb @@ -15,10 +15,6 @@ <%= f.label :name %>
<%= f.text_field :name %> -
- <%= f.label :state %>
- <%= f.text_area :state %> -
<%= f.submit %>
diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb index 30777bf..90a189d 100644 --- a/app/views/games/index.html.erb +++ b/app/views/games/index.html.erb @@ -6,8 +6,8 @@

- <% if @_current_user %> - Your Current User: <%= @_current_user %> + <% if current_user %> + Your Current User: <%= current_user %> <% end %>

From 99f110f924da7287a1fecdb48666ddd59321a563 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sat, 9 May 2015 19:39:55 +0000 Subject: [PATCH 05/11] min working --- app/controllers/games_controller.rb | 2 ++ app/models/game.rb | 9 +++++---- config/application.rb | 2 ++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index fafd9cf..1a8f6f4 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,6 +1,7 @@ class GamesController < ApplicationController before_action :set_game, only: [:show, :edit, :update, :destroy] + def add_player unless current_user.nil? set_game @@ -99,6 +100,7 @@ def show state = @game.load_state is_playing = state[:players].include?(current_user) + game_started = !state[:bids].nil? player_index = state[:players].index(current_user) || 0 diff --git a/app/models/game.rb b/app/models/game.rb index ca795ad..37f529e 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,5 +1,7 @@ class Game < ActiveRecord::Base require 'yaml' + before_save :state_data + def waiting_for_players? state_data[:current_status] == :waiting_for_players @@ -7,10 +9,8 @@ def waiting_for_players? def set_up data = state_data - data[:current_status] ||= :waiting_for_players data[:total_rounds] = 10 data[:rounds_played] = 0 - data[:players] = [] data[:player_hands] = [] data[:score] = [] data[:deck] = [] @@ -178,9 +178,10 @@ def player_action user_id, user_input=nil end def state_data - @state_read_at ||= self.updated_at || self.created_at current_time = self.updated_at || self.created_at - if @data_data.nil? || current_time > @state_read_at + @state_read_at ||= current_time + if @state_data.nil? || (@state_read_at && current_time > @state_read_at) + self.state = { current_status: :waiting_for_players, players: [] }.to_yaml if self.state.nil? @state_data = load_state end @state_data diff --git a/config/application.rb b/config/application.rb index dfc4a8b..e6029f6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -12,6 +12,8 @@ class Application < Rails::Application # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. + config.autoload_paths += %W(#{config.root}/lib) + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' From ffcc6302ef3730395ad3465f232a88c302459f78 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sat, 9 May 2015 19:52:41 +0000 Subject: [PATCH 06/11] min working --- app/controllers/games_controller.rb | 4 ---- app/models/game.rb | 22 +++++++++++++--------- app/views/games/show.html.erb | 26 +++++++++++--------------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 1a8f6f4..583f9bd 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,10 +1,7 @@ class GamesController < ApplicationController before_action :set_game, only: [:show, :edit, :update, :destroy] - def add_player - unless current_user.nil? - set_game state = @game.load_state if state[:players].size < 5 unless state[:players].include?(current_user) @@ -30,7 +27,6 @@ def add_player else redirect_to @game, notice: 'There are too many players in the game already' end - end end def deal diff --git a/app/models/game.rb b/app/models/game.rb index 37f529e..8b43c6f 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -2,20 +2,14 @@ class Game < ActiveRecord::Base require 'yaml' before_save :state_data + def has_player?(player_id) + end def waiting_for_players? state_data[:current_status] == :waiting_for_players end def set_up - data = state_data - data[:total_rounds] = 10 - data[:rounds_played] = 0 - data[:player_hands] = [] - data[:score] = [] - data[:deck] = [] - data[:names] = [] - data[:players].shuffle! save_state end @@ -181,7 +175,17 @@ def state_data current_time = self.updated_at || self.created_at @state_read_at ||= current_time if @state_data.nil? || (@state_read_at && current_time > @state_read_at) - self.state = { current_status: :waiting_for_players, players: [] }.to_yaml if self.state.nil? + if self.state.nil? + self.state = { + current_status: :waiting_for_players, + total_rounds: 10, + rounds_played: 0, + player_hands: [], + score: [], + deck: [], + names: [] + }.to_yaml + end @state_data = load_state end @state_data diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb index 816f6ba..eb76fa6 100644 --- a/app/views/games/show.html.erb +++ b/app/views/games/show.html.erb @@ -116,23 +116,19 @@ function handleCardDrop( event, ui ) {

-

- -

- - - -<%= form_tag(games_add_player_path) do %> -<%= hidden_field_tag(:id, params[:id]) %> -
-<%= text_field_tag("username", "Username", class: "form-control input-lg") %> - - <%= button_tag("Join game", type: "submit", class: "btn btn-lg btn-primary") %> - -
+<% unless @game.waiting_for_players? && ! @game.has_player?(current_user) %> + <%= form_tag(games_add_player_path) do %> + <%= hidden_field_tag(:id, params[:id]) %> +
+ <%= text_field_tag("username", "", placeholder: "Player Name", class: "form-control input-lg") %> + + <%= button_tag("Join game", type: "submit", class: "btn btn-lg btn-primary") %> + +
+ <% end %> <% end %> +
<%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> From f835731ba5fdf2393d45cb73b118d76d89d06d2e Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sun, 10 May 2015 02:43:39 +0000 Subject: [PATCH 07/11] broken but mostly cleaned up --- app/controllers/application_controller.rb | 15 +-- app/controllers/games_controller.rb | 147 ++++++++++------------ app/models/game.rb | 69 ++++++---- app/views/games/index.html.erb | 2 + app/views/games/show.html.erb | 46 ++++--- 5 files changed, 142 insertions(+), 137 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 02701fd..b41e4c0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,15 +8,12 @@ class ApplicationController < ActionController::Base helper_method :current_user - private - - # Finds the User with the ID stored in the session with the key - # :current_user_id This is a common way to handle user login in - # a Rails application; logging in sets the session value and - # logging out removes it. def current_user - @_current_user ||= unless @_current_user - session[:id] ||= request.remote_ip.hash + rand(1000) - end + @current_user ||= unless @current_user + session[:id] ||= request.remote_ip.hash + rand(1000) + end end + + private + end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 583f9bd..f0dae23 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,54 +1,35 @@ class GamesController < ApplicationController - before_action :set_game, only: [:show, :edit, :update, :destroy] + before_action :set_game, only: [:add_player, :deal, :player_action, :show, :edit, :update, :destroy] def add_player - state = @game.load_state - if state[:players].size < 5 - unless state[:players].include?(current_user) - if state[:cards_in_play] - redirect_to @game, notice: 'Cant join once the game has already started' - return - end - state[:players].push current_user - while state[:names].include? params[:username] - # make sure username is unique by appending random numbers - params[:username] += rand(10).to_s - end - state[:names].push params[:username] - @game.save_state state - if @game.save - redirect_to @game, notice: 'Joined the game!' - else - redirect_to @game, notice: 'Failed to join the game' - end - else - redirect_to @game, notice: 'Youre already in the game' - end - else - redirect_to @game, notice: 'There are too many players in the game already' - end - end - - def deal - set_game - state = @game.load_state - if state[:players].size < 3 or state[:players].size > 5 - redirect_to @game, notice: 'Must have between 3 and 5 players to start' - return - end - if state[:players].first == current_user - if state[:cards_in_play].nil? - @game.save_state @game.deal_cards(state) - @game.save - redirect_to @game, notice: 'Game has started!' - else - redirect_to @game, notice: 'Game has already started' - end + if @game.state_data[:players].size > 5 + redirect_to @game, notice: 'There are too many players in the game already' + elsif @game.has_player?(current_user) + redirect_to @game, notice: 'Youre already in the game' + elsif !@game.waiting_for_players? + redirect_to @game, notice: 'Cant join once the game has already started' else - redirect_to @game, notice: 'Only the creator of the game can start it' + session[:player_name] = @game.add_player(current_user, params[:username]) + redirect_to @game, notice: "Welcome to the game, #{session[:player_name]}" end end + def deal + message = if @game.state_data[:players].size < 3 or @game.state_data[:players].size > 5 + 'Must have between 3 and 5 players to start' + elsif !@game.waiting_for_players? + 'Game has already started' + elsif @game.can_deal?(current_user) + 'Only the creator of the game can start it' + else + @game.deal_cards(@game.state_data) + @game.state_data[:current_status] = :in_play + @game.save! + 'Game has started!' + end + redirect_to @game, notice: message + end + def player_action set_game state = @game.load_state @@ -58,7 +39,7 @@ def player_action redirect_to @game, notice: "Dude... It's not your turn" return end - + if params[:bid] # user is making a bid if @game.done_bidding? state @@ -93,50 +74,48 @@ def index # GET /games/1 # GET /games/1.json def show - state = @game.load_state - - is_playing = state[:players].include?(current_user) + is_playing = @game.state_data[:player].has_key?(current_user) + player_index = @game.state_data[:players].index(current_user) || 0 - game_started = !state[:bids].nil? - player_index = state[:players].index(current_user) || 0 + game_started = !@game.state_data[:bids].nil? # round number - @round = state[:total_rounds] - state[:rounds_played] + @round = @game.state_data[:total_rounds] - @game.state_data[:rounds_played] # names/scores around the board # add in different order so the user is always on the bottom @names = [] @round_scores = [] - @game.iterate_through_list_with_start_index(player_index, state[:names]) do |name, i| - tricks_taken = (state[:tricks_taken] and state[:tricks_taken][i]) ? state[:tricks_taken][i].size : 0 - bid = (state[:bids] and state[:bids][i]) ? state[:bids][i] : 'No bid yet' + @game.iterate_through_list_with_start_index(player_index, @game.state_data[:players]) do |player_id, i| + tricks_taken = (@game.state_data[:tricks_taken] and @game.state_data[:tricks_taken][i]) ? @game.state_data[:tricks_taken][i].size : 0 + bid = (@game.state_data[:bids] and @game.state_data[:bids][i]) ? @game.state_data[:bids][i] : 'No bid yet' score = 0 - if state[:score] and state[:score][i] - score = state[:score][i].inject :+ + if @game.state_data[:score] and @game.state_data[:score][i] + score = @game.state_data[:score][i].inject :+ end - @names.push name - @round_scores.push (name.nil?) ? nil : "Tricks taken: #{tricks_taken} | Bid: #{bid} | Score: #{score}" + @names.push @game.state_data[:names][i] + @round_scores.push (player_id.nil?) ? nil : "Tricks taken: #{tricks_taken} | Bid: #{bid} | Score: #{score}" end - + # cards that have been played - @played_cards = state[:cards_in_play] + @played_cards = @game.state_data[:cards_in_play] if game_started # display cards in different order since the user is on the bottom @played_cards = [] - @game.iterate_through_list_with_start_index(player_index, state[:players]) do |player,i| - @played_cards.push state[:cards_in_play][i] + @game.iterate_through_list_with_start_index(player_index, @game.state_data[:players]) do |player,i| + @played_cards.push @game.state_data[:cards_in_play][i] end end # players hand @cards = [] - if is_playing - @cards = state[:player_hands][player_index] || @cards + if is_playing && game_started + @cards = @game.state_data[:player_hands][player_index] || @cards # can't play any cards unless it's your turn playable_cards = [] - if state[:waiting_on] == current_user - playable_cards = @game.get_playable_cards(state[:first_suit_played], state[:player_hands][player_index]) + if @game.state_data[:waiting_on] == current_user + playable_cards = @game.get_playable_cards(@game.state_data[:first_suit_played], @game.state_data[:player_hands][player_index]) end @cards.each do |card| if playable_cards.include? card @@ -145,22 +124,22 @@ def show end end @cards.sort! - + # game status (ie. who we're waiting on) - if state[:waiting_on] - waiting_on_index = state[:players].index(state[:waiting_on]) - @waiting_on = state[:names][waiting_on_index] - @waiting_on = 'YOU' if current_user == state[:waiting_on] - unless @game.done_bidding? state + if @game.state_data[:waiting_on] + waiting_on_index = @game.state_data[:players].index(@game.state_data[:waiting_on]) + @waiting_on = @game.state_data[:names][waiting_on_index] + @waiting_on = 'YOU' if current_user == @game.state_data[:waiting_on] + unless @game.done_bidding? @game.state_data @waiting_on += " (BIDDING)" end else @waiting_on = 'Game to start' end - + # show ace of spades if game hasnt started - @trump = state[:trump_card] || Card.new('Spades', 12) - + @trump = @game.state_data[:trump_card] || Card.new('Spades', 12) + # make sure show.js.erb is executed in the views folder respond_to do |format| format.js @@ -181,7 +160,7 @@ def edit # POST /games.json def create @game = Game.new(game_params) - @game.set_up + @game.state_data[:players] << current_user respond_to do |format| if @game.save @@ -219,13 +198,13 @@ def destroy end private - # Use callbacks to share common setup or constraints between actions. - def set_game - @game = Game.find(params[:id]) - end + # Use callbacks to share common setup or constraints between actions. + def set_game + @game = Game.find(params[:id]) + end - # Never trust parameters from the scary internet, only allow the white list through. - def game_params - params.require(:game).permit(:name, :state) - end + # Never trust parameters from the scary internet, only allow the white list through. + def game_params + params.require(:game).permit(:name, :state) + end end diff --git a/app/models/game.rb b/app/models/game.rb index 8b43c6f..9bba479 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,18 +1,35 @@ class Game < ActiveRecord::Base require 'yaml' - before_save :state_data + after_initialize :state_data + before_save :serialize_state def has_player?(player_id) + state_data[:player].has_key?(player_id) + end + + def serialize_state + self.state = state_data.to_yaml + end + + def can_deal?(player_id) + state_data[:players].first == player_id && state_data.has_key?(player_id) end def waiting_for_players? - state_data[:current_status] == :waiting_for_players + state_data[:current_status] == :waiting_for_players && state_data[:player].size < 5 end - def set_up - save_state + def add_player(player_id, player_name) + state_data[:players] << player_id + while state_data[:names].include? player_name + player_name += rand(10).to_s + end + state_data[:names] << player_name + state_data[:player][player_id] = player_name + save! + player_name end - + def deal_cards state # need to have at least 3 players to start the game raise 'Must have between 3 and 5 players to start' if state[:players].size < 3 or state[:players].size > 5 @@ -28,7 +45,7 @@ def deal_cards state # person to the 'left' of the dealer bids first state[:bids] = [] state[:waiting_on] = get_next_player state[:dealer], state[:players] - + # deal out number of cards equal to whatever round we are on # to each player # deal cards first to player on 'right' of dealer @@ -36,7 +53,7 @@ def deal_cards state iterate_through_list_with_start_index(state[:players].find_index(state[:waiting_on]), state[:players]) { |player, i| state[:player_hands][i] = state[:deck].slice!(0..(num_cards_per_player-1)) } - + # the next card in the deck is trump # whatever suit is trump is valued higher than non-trump suits # an Ace as trump means there is "no trump" @@ -53,24 +70,24 @@ def deal_cards state # player either bids or plays a card if it's their turn def player_action user_id, user_input=nil state = load_state - + # return false if it's not the players turn return false if user_id != state[:waiting_on] current_player_index = state[:players].find_index user_id - + # check if we are in bidding or playing a card if !done_bidding? state # player is making a bid bid = user_input.to_i - + # dealer cannot bid the same amount as the number of cards dealt total_bids = state[:bids].select { |bid| !bid.nil? }.inject(:+) num_cards_per_player = state[:total_rounds] - state[:rounds_played] if state[:dealer] == user_id and total_bids + bid == num_cards_per_player return false end - + # record the bid state[:bids][current_player_index] = bid @@ -93,7 +110,7 @@ def player_action user_id, user_input=nil elsif not state[:player_hands][current_player_index].empty? # player is playing a card card = user_input - + # ensure that the card is in their inventory return false unless state[:player_hands][current_player_index].any? { |player_card| player_card == card } @@ -101,7 +118,7 @@ def player_action user_id, user_input=nil state[:first_suit_played] ||= card.suit playable_cards = get_playable_cards(state[:first_suit_played], state[:player_hands][current_player_index]) return false unless playable_cards.include? card - + # actually play the card state[:cards_in_play][current_player_index] = state[:player_hands][current_player_index].delete(card) @@ -116,7 +133,7 @@ def player_action user_id, user_input=nil # reset variables state[:cards_in_play] = [] state[:first_suit_played] = nil - + # check to see if we're done with this round if state[:player_hands].first.empty? @@ -136,7 +153,7 @@ def player_action user_id, user_input=nil state[:score][i] ||= [] state[:score][i].push player_score end - + # check to see if that was the last round (game over) if state[:rounds_played] == state[:total_rounds] # game is over, determine who won @@ -166,11 +183,11 @@ def player_action user_id, user_input=nil state[:waiting_on] = get_next_player state[:waiting_on], state[:players] end end - + save_state state return true end - + def state_data current_time = self.updated_at || self.created_at @state_read_at ||= current_time @@ -180,13 +197,17 @@ def state_data current_status: :waiting_for_players, total_rounds: 10, rounds_played: 0, - player_hands: [], - score: [], - deck: [], - names: [] + players: nil, + player_hands: nil, + score: nil, + deck: nil, + names: nil }.to_yaml end @state_data = load_state + @state_data[:players] ||= Array.new + @state_data[:names] ||= Array.new + @state_data[:player] ||= Hash.new end @state_data end @@ -194,7 +215,7 @@ def state_data def load_state return self.state.nil? ? Hash.new : YAML.load(self.state) end - + def save_state(state = state_data) self.state = state.to_yaml end @@ -210,14 +231,14 @@ def get_highest_card cards, first_suit_played, trump, start_index # only cards that are the same suit as the first card played matter same_suit_cards = cards.select { |c| c.suit == first_suit_played } card_set = trump_cards.empty? ? same_suit_cards : trump_cards - + # now simply get the highest card left return card_set.max #iterate_through_list_with_start_index(start_index, card_set) do |card| # return card if card.value == highest_value #end end - + def get_playable_cards first_suit_played, cards # player can play any card if they are the first to play a card # or if they only have a single card left diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb index 90a189d..4264aa2 100644 --- a/app/views/games/index.html.erb +++ b/app/views/games/index.html.erb @@ -17,6 +17,7 @@ Game Name + Players Round Join @@ -26,6 +27,7 @@ <% @games.order(:updated_at).each do |game| %> <%= game.name %> + <%= game.state_data[:player].size %> <%= "#{10 - game.state_data[:rounds_played]}" %> <% label = game.waiting_for_players? ? "Join Game" : "Game Started Already" %> <% label_class = game.waiting_for_players? ? "btn-success" : "btn-info" %> diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb index eb76fa6..d341073 100644 --- a/app/views/games/show.html.erb +++ b/app/views/games/show.html.erb @@ -110,14 +110,21 @@ function handleCardDrop( event, ui ) {
-

<%= notice %>

-

- -

+

+ <%= @game.name %>: <%= @game.has_player?(current_user) ? @game.state_data[:player][current_user] : "not joined yet" %> +

+ +Players: <%= @game.state_data[:players].join(", ") %> +
+Player: <%= @game.state_data[:player].keys.join(", ") %> +
+Names: <%= @game.state_data[:names].join(", ") %> + +
<%= notice %> <%= @game.state_data[:current_status] %>
+ -<% unless @game.waiting_for_players? && ! @game.has_player?(current_user) %> +<% if @game.waiting_for_players? && !@game.has_player?(current_user) %> <%= form_tag(games_add_player_path) do %> <%= hidden_field_tag(:id, params[:id]) %>
@@ -127,22 +134,21 @@ function handleCardDrop( event, ui ) {
<% end %> + <%= @game.state_data[:names].join(", ") %> +<% elsif @game.can_deal?(current_user) %> + <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> +<% elsif @game.needs_to_bid?(current_user) %> + <%= form_tag(games_player_action_path) do %> + <%= hidden_field_tag(:id, params[:id]) %> +
+ <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> + + <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> + +
+ <% end %> <% end %> -
- -<%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> -
- -<%= form_tag(games_player_action_path) do %> -<%= hidden_field_tag(:id, params[:id]) %> -
-<%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> - - <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> - -
-<% end %>
From 6842c12e539ca224cf5a83b8fe691bf72642e236 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sun, 10 May 2015 03:32:54 +0000 Subject: [PATCH 08/11] saving --- app/controllers/games_controller.rb | 59 +++++------------------------ app/models/game.rb | 14 +++++-- app/views/games/show.html.erb | 12 ++++-- config/routes.rb | 1 + 4 files changed, 31 insertions(+), 55 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index f0dae23..87024c8 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,5 +1,5 @@ class GamesController < ApplicationController - before_action :set_game, only: [:add_player, :deal, :player_action, :show, :edit, :update, :destroy] + before_action :set_game, only: [:add_player, :deal, :player_action, :show] def add_player if @game.state_data[:players].size > 5 @@ -10,7 +10,7 @@ def add_player redirect_to @game, notice: 'Cant join once the game has already started' else session[:player_name] = @game.add_player(current_user, params[:username]) - redirect_to @game, notice: "Welcome to the game, #{session[:player_name]}" + redirect_to @game, notice: "Welcome to the game" end end @@ -31,18 +31,14 @@ def deal end def player_action - set_game - state = @game.load_state - - # player isnt allowed to do anything if it's not their turn - if state[:waiting_on] != current_user - redirect_to @game, notice: "Dude... It's not your turn" + if @game.state_data[:waiting_on] != current_user + redirect_to @game, notice: "Bro... It's not your turn" return end if params[:bid] # user is making a bid - if @game.done_bidding? state + if @game.done_bidding? redirect_to @game, notice: 'Bidding is over BRO' return else @@ -152,58 +148,23 @@ def new @game = Game.new end - # GET /games/1/edit - def edit - end - - # POST /games - # POST /games.json def create - @game = Game.new(game_params) + @game = Game.create!(game_params) @game.state_data[:players] << current_user + @game.save! respond_to do |format| - if @game.save - format.html { redirect_to @game, notice: 'Game was successfully created.' } - format.json { render :show, status: :created, location: @game } - else - format.html { render :new } - format.json { render json: @game.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /games/1 - # PATCH/PUT /games/1.json - def update - respond_to do |format| - if @game.update(game_params) - format.html { redirect_to @game, notice: 'Game was successfully updated.' } - format.json { render :show, status: :ok, location: @game } - else - format.html { render :edit } - format.json { render json: @game.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /games/1 - # DELETE /games/1.json - def destroy - @game.destroy - respond_to do |format| - format.html { redirect_to games_url, notice: 'Game was successfully destroyed.' } - format.json { head :no_content } + format.html { redirect_to @game, notice: 'Game was successfully created.' } + format.json { render :show, status: :created, location: @game } end end private - # Use callbacks to share common setup or constraints between actions. + def set_game @game = Game.find(params[:id]) end - # Never trust parameters from the scary internet, only allow the white list through. def game_params params.require(:game).permit(:name, :state) end diff --git a/app/models/game.rb b/app/models/game.rb index 9bba479..2b12a02 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -3,6 +3,10 @@ class Game < ActiveRecord::Base after_initialize :state_data before_save :serialize_state + def enough_players? + state_data[:player].size > 2 && state_data[:player].size <= 5 + end + def has_player?(player_id) state_data[:player].has_key?(player_id) end @@ -12,7 +16,11 @@ def serialize_state end def can_deal?(player_id) - state_data[:players].first == player_id && state_data.has_key?(player_id) + state_data[:players].first == player_id && state_data[:player].has_key?(player_id) + end + + def needs_to_bid?(current_user) + raise "Not a player in this game" unless has_player? current_user end def waiting_for_players? @@ -69,7 +77,7 @@ def deal_cards state # player either bids or plays a card if it's their turn def player_action user_id, user_input=nil - state = load_state + state = game_state # return false if it's not the players turn return false if user_id != state[:waiting_on] @@ -272,7 +280,7 @@ def player_size_and_nil_check arr, state # return true or false if we're done bidding # done bidding if there are the same number of valids bids # as there are players in the game - def done_bidding? state + def done_bidding? state = game_state return player_size_and_nil_check(state[:bids], state) end diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb index d341073..9abd777 100644 --- a/app/views/games/show.html.erb +++ b/app/views/games/show.html.erb @@ -120,9 +120,9 @@ Players: <%= @game.state_data[:players].join(", ") %> Player: <%= @game.state_data[:player].keys.join(", ") %>
Names: <%= @game.state_data[:names].join(", ") %> +
-
<%= notice %> <%= @game.state_data[:current_status] %>
- +
<%= notice %> <%= @game.state_data[:current_status] %>
<% if @game.waiting_for_players? && !@game.has_player?(current_user) %> <%= form_tag(games_add_player_path) do %> @@ -135,8 +135,14 @@ Names: <%= @game.state_data[:names].join(", ") %>
<% end %> <%= @game.state_data[:names].join(", ") %> + <% elsif @game.can_deal?(current_user) %> - <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> + <% if @game.enough_players? %> + <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> + <% else %> + Waiting on more players before you can deal. + <% end %> + <% elsif @game.needs_to_bid?(current_user) %> <%= form_tag(games_player_action_path) do %> <%= hidden_field_tag(:id, params[:id]) %> diff --git a/config/routes.rb b/config/routes.rb index d59edd3..878be0f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do resources :games + # TODO these should be member vars post 'games/add_player' post 'games/deal' post 'games/player_action' From 40a8b9947974c2fa25087acd14c4204c2aed479e Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sun, 10 May 2015 15:29:14 +0000 Subject: [PATCH 09/11] formatted --- app/views/games/show.html.erb | 335 +++++++++++++++++----------------- 1 file changed, 168 insertions(+), 167 deletions(-) diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb index 42d4374..3b223fc 100644 --- a/app/views/games/show.html.erb +++ b/app/views/games/show.html.erb @@ -1,102 +1,102 @@ <%= stylesheet_link_tag 'style' %> - + - +
@@ -139,32 +139,32 @@ Player: <%= @game.state_data[:player].keys.join(", ") %> <% if @game.waiting_for_players? && !@game.has_player?(current_user) %> <%= form_tag(games_add_player_path) do %> - <%= hidden_field_tag(:id, params[:id]) %> -
- <%= text_field_tag("username", "", placeholder: "Player Name", class: "form-control input-lg") %> - - <%= button_tag("Join game", type: "submit", class: "btn btn-lg btn-primary") %> - -
+ <%= hidden_field_tag(:id, params[:id]) %> +
+ <%= text_field_tag("username", "", placeholder: "Player Name", class: "form-control input-lg") %> + + <%= button_tag("Join game", type: "submit", class: "btn btn-lg btn-primary") %> + +
<% end %> - <%= @game.state_data[:names].join(", ") %> +<%= @game.state_data[:names].join(", ") %> <% elsif @game.can_deal?(current_user) %> <% if @game.enough_players? %> - <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> + <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> <% else %> - Waiting on more players before you can deal. + Waiting on more players before you can deal. <% end %> <% elsif @game.needs_to_bid?(current_user) %> <%= form_tag(games_player_action_path) do %> - <%= hidden_field_tag(:id, params[:id]) %> -
- <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> - - <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> - -
+ <%= hidden_field_tag(:id, params[:id]) %> +
+ <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> + + <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> + +
<% end %> <% end %> @@ -183,41 +183,42 @@ Player: <%= @game.state_data[:player].keys.join(", ") %>
<% unless @is_playing or @game_started %> -
- <%= form_tag(games_add_player_path) do %> - <%= hidden_field_tag(:id, params[:id]) %> -
-
- <%= text_field_tag("username", "Username", class: "form-control input-lg") %> - - <%= button_tag("Join game", type: "submit", class: "btn btn-lg btn-primary") %> - -
- <% end %> -
-
- <% end %> - - <% if @is_playing and not @game_started %> -
- <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> +
+ <%= form_tag(games_add_player_path) do %> + <%= hidden_field_tag(:id, params[:id]) %> +
+
+ <%= text_field_tag("username", "Username", class: "form-control input-lg") %> + + <%= button_tag("Join game", type: "submit", class: "btn btn-lg btn-primary") %> + +
+ <% end %>

- <% end %> - - <% if @is_playing and not @done_bidding %> -
- <%= form_tag(games_player_action_path) do %> - <%= hidden_field_tag(:id, params[:id]) %> -
- <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> - - <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> - -
- <% end %> -
- <% end %> + <% end %> + + + <% if @is_playing and not @game_started %> +
+ <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> +
+
+ <% end %> + + <% if @is_playing and not @done_bidding %> +
+ <%= form_tag(games_player_action_path) do %> + <%= hidden_field_tag(:id, params[:id]) %> +
+ <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> + + <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> + +
+ <% end %> +
+ <% end %>

@@ -232,58 +233,58 @@ Player: <%= @game.state_data[:player].keys.join(", ") %>
- +

You did it!

- +
<%= javascript_include_tag "cards.js" %> From c12f0d614ebce9a093185295e76fb49a01df454a Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sun, 10 May 2015 15:36:26 +0000 Subject: [PATCH 10/11] new logic merged --- app/views/games/show.html.erb | 94 +++++++++++------------------------ 1 file changed, 29 insertions(+), 65 deletions(-) diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb index 3b223fc..973c8b6 100644 --- a/app/views/games/show.html.erb +++ b/app/views/games/show.html.erb @@ -136,7 +136,6 @@ Player: <%= @game.state_data[:player].keys.join(", ") %>
<%= notice %> <%= @game.state_data[:current_status] %>
- <% if @game.waiting_for_players? && !@game.has_player?(current_user) %> <%= form_tag(games_add_player_path) do %> <%= hidden_field_tag(:id, params[:id]) %> @@ -148,26 +147,9 @@ Player: <%= @game.state_data[:player].keys.join(", ") %>
<% end %> <%= @game.state_data[:names].join(", ") %> - -<% elsif @game.can_deal?(current_user) %> - <% if @game.enough_players? %> - <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> - <% else %> - Waiting on more players before you can deal. - <% end %> - -<% elsif @game.needs_to_bid?(current_user) %> - <%= form_tag(games_player_action_path) do %> - <%= hidden_field_tag(:id, params[:id]) %> -
- <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> - - <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> - -
- <% end %> <% end %> +
@@ -181,63 +163,44 @@ Player: <%= @game.state_data[:player].keys.join(", ") %>

- - <% unless @is_playing or @game_started %> -
- <%= form_tag(games_add_player_path) do %> - <%= hidden_field_tag(:id, params[:id]) %> -
-
- <%= text_field_tag("username", "Username", class: "form-control input-lg") %> - - <%= button_tag("Join game", type: "submit", class: "btn btn-lg btn-primary") %> - -
- <% end %> -
-
- <% end %> - - - <% if @is_playing and not @game_started %> -
+ <% if @game.can_deal?(current_user) %> + <% if @game.enough_players? %> <%= button_to 'Deal', games_deal_path, method: :post, params: {:id => params[:id]}, class: "btn btn-lg btn-primary" %> -
-
- <% end %> - - <% if @is_playing and not @done_bidding %> -
- <%= form_tag(games_player_action_path) do %> - <%= hidden_field_tag(:id, params[:id]) %> + <% else %> + Waiting on more players before you can deal. + <% end %> + <% elsif @game.needs_to_bid?(current_user) %> + <%= form_tag(games_player_action_path) do %> + <%= hidden_field_tag(:id, params[:id]) %>
- <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> - - <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> - + <%= number_field_tag("bid", 0, min: 0, max: 10, class: "form-control input-lg") %> + + <%= button_tag("Bid", type: "submit", class: "btn btn-lg btn-primary") %> +
- <% end %> -
+ <% end %> + <% else %> + Waiting for more players or the creator to start the game. <% end %> -
-
-
- Your Cards -
- <%= render "card_pile" %> -
+
+
+
+ Your Cards +
+ <%= render "card_pile" %>
- +
+
-
-

You did it!

- -
+
+

You did it!

+ +
<%= javascript_include_tag "cards.js" %> From f2840cd694af8e6ee32f1c6edc29c482a3997cd9 Mon Sep 17 00:00:00 2001 From: smith11235 Date: Sun, 10 May 2015 15:41:48 +0000 Subject: [PATCH 11/11] fixed logic --- app/views/games/show.html.erb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb index 973c8b6..4b457f3 100644 --- a/app/views/games/show.html.erb +++ b/app/views/games/show.html.erb @@ -162,6 +162,7 @@ Player: <%= @game.state_data[:player].keys.join(", ") %> <%= render "board_info" %>
+ <% if @game.has_player?(current_user) %>
<% if @game.can_deal?(current_user) %> <% if @game.enough_players? %> @@ -183,6 +184,7 @@ Player: <%= @game.state_data[:player].keys.join(", ") %> Waiting for more players or the creator to start the game. <% end %>
+ <% end %>