From 2b81a1f97624332106ac7f85df5c882e1f16bec7 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Mon, 21 Oct 2019 05:52:44 -0700 Subject: [PATCH] Add jquery reloading of table. Fix Play Now --- app/assets/javascripts/application.js | 9 ++- app/assets/javascripts/table-loader.js | 5 ++ app/assets/stylesheets/game.css.sass | 1 + app/assets/stylesheets/tables.css.scss | 2 +- app/controllers/games_controller.rb | 6 +- app/controllers/tables_controller.rb | 18 +++++ app/models/game.rb | 4 +- app/models/seat.rb | 6 +- app/models/table.rb | 11 +++ app/views/games/show.html.haml | 2 +- app/views/tables/show.html.haml | 101 +++++++++++++------------ config/initializers/assets.rb | 1 + config/routes.rb | 2 +- 13 files changed, 107 insertions(+), 61 deletions(-) create mode 100644 app/assets/javascripts/table-loader.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 8ba77ec0..247a21e9 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -17,4 +17,11 @@ //= require react_ujs //= require components // require react-rails-hot-loader -//= require_tree . +// require_tree . + + +// $(document).ready(function(){ +// setInterval(function() { +// $('#table-component').load("#{request.path} #table-component"); +// }, 5000); +// }); diff --git a/app/assets/javascripts/table-loader.js b/app/assets/javascripts/table-loader.js new file mode 100644 index 00000000..a9c7b498 --- /dev/null +++ b/app/assets/javascripts/table-loader.js @@ -0,0 +1,5 @@ +$(document).ready(function(){ + setInterval(function() { + $('#table-component').load(window.location.pathname +" #table-component"); + }, 5000); +}); diff --git a/app/assets/stylesheets/game.css.sass b/app/assets/stylesheets/game.css.sass index a5d67eb9..23d7f675 100644 --- a/app/assets/stylesheets/game.css.sass +++ b/app/assets/stylesheets/game.css.sass @@ -30,6 +30,7 @@ button font-size: 4vmin +#table-component, #game-component-container-container display: flex flex-direction: column diff --git a/app/assets/stylesheets/tables.css.scss b/app/assets/stylesheets/tables.css.scss index 0c519cce..77cf6a25 100644 --- a/app/assets/stylesheets/tables.css.scss +++ b/app/assets/stylesheets/tables.css.scss @@ -2,7 +2,7 @@ // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ -#table-component-container-container, +#table-component, #table-component-container, .table { width: 100%; diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 257ad6f3..b315ad3a 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -105,9 +105,11 @@ class GamesController < ApplicationController def play_now # First joinable game or create new game - @game = Game.play_now(current_user) + @game = Game.play_now @seats = @game.table.seats.order(:position).map {|s| s.occupied? ? nil : s} - @seats.compact.last.sit(current_user) + # @seats = @game.table.seats + @seat = @seats.compact.last.sit(current_user) + @seat.save redirect_to @game.table end diff --git a/app/controllers/tables_controller.rb b/app/controllers/tables_controller.rb index cc1f7878..628d51f7 100644 --- a/app/controllers/tables_controller.rb +++ b/app/controllers/tables_controller.rb @@ -3,6 +3,24 @@ class TablesController < ApplicationController respond_to :html, :json + def play_now + # First empty seat + @seat = Seat.where(player: nil).first + if @seat + @seat.sit current_user + else + @table = Table.create(title: "Table " + current_user.soft_token[0..4]) + @seat = @table.seats.first + @seat.sit current_user + end + redirect_to @seat.table + # @game = Game.play_now(current_user) + # @table = @game.table + # @seats = @table.seats.order(:position).map {|s| s.occupied? ? nil : s} + # @seat = @seats.compact.last.sit(current_user) + end + + # GET /tables # GET /tables.json def index diff --git a/app/models/game.rb b/app/models/game.rb index 804151a1..cafabf87 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -144,7 +144,9 @@ class Game < ActiveRecord::Base # The player whose turn it is def controlling_player - self.players.find(self.controlling_player_id) + if self.controlling_player_id + self.players.find(self.controlling_player_id) + end end # The user whose turn it is diff --git a/app/models/seat.rb b/app/models/seat.rb index c8dc1208..cdd64a42 100644 --- a/app/models/seat.rb +++ b/app/models/seat.rb @@ -6,7 +6,7 @@ class Seat < ActiveRecord::Base def sit(current_user) if self.occupied? - return "Error. Seat is occupied." + return self else # Stand up from any other seats at this table current_user.stand_from(self.table) @@ -15,9 +15,7 @@ class Seat < ActiveRecord::Base self.player_id = self.table.current_game.add_player_from_user(current_user).id self.user_id = current_user.id self.user_soft_token = current_user.soft_token - self.save - - return "Successfully sat." + return self.save end end diff --git a/app/models/table.rb b/app/models/table.rb index d85f3da1..80949781 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -3,6 +3,7 @@ class Table < ActiveRecord::Base has_many :seats has_many :users, through: :seats has_many :games + has_many :players, through: :games #TODO: Deprecated current_game_id from database. # Instead calculate with self.current_game # @@ -12,6 +13,16 @@ class Table < ActiveRecord::Base after_create :add_seats after_create :add_game + # Returns the newest joinable table or creates a new table if none are found + # scope :play_now, lambda { |user| + # where(status:nil).joins("LEFT OUTER JOIN players ON players.table_id = tables.id").group("tables.id").having("count(players) < 4").last || Table.create(title: "Game " + user.soft_token[0..4]) + # } + scope :play_now, lambda { |user| + # where(status:nil).joins("LEFT OUTER JOIN players ON players.game_id = games.id").group("games.id").having("count(players) < 4").last || Table.create(title: "Game " + user.soft_token[0..4]) + where(game:nil).last || Table.create(title: "Table " + user.soft_token[0..4]) + } + + def current_game self.games.last end diff --git a/app/views/games/show.html.haml b/app/views/games/show.html.haml index b1ded1b3..12cd3858 100644 --- a/app/views/games/show.html.haml +++ b/app/views/games/show.html.haml @@ -32,7 +32,7 @@ // Show whose turn it is - if @game.controlling_player_id - %strong= "My Turn " if @game.controlling_player == player + %strong= "Taking Turn " if @game.controlling_player == player // Show the players' cards - if player.user == current_user && player.inventory.empty? == false diff --git a/app/views/tables/show.html.haml b/app/views/tables/show.html.haml index 91dda68f..691be3a6 100644 --- a/app/views/tables/show.html.haml +++ b/app/views/tables/show.html.haml @@ -1,67 +1,68 @@ -- unless current_user.soft_token == @game.controlling_player.soft_token - - content_for :head do - %meta{:content => "5", "http-equiv" => "refresh"}/ - if policy(@table).update? .owner-controls = link_to 'Edit', edit_table_path(@table) = link_to 'Destroy', game, :method => :delete, :data => { :confirm => 'Are you sure?' } -# #table-component-container-container= react_component 'Table', props = {url: "#{table_path @table}"}, html_options = {id: "table-component-container"} -%h1= @table.title +#table-component + - unless current_user.soft_token == @game.controlling_player.try("soft_token") + = javascript_include_tag 'table-loader' -- if @game.startable? && @game.already_has?(current_user) - = button_to 'Start', start_game_path(@game) + %h1= @table.title --#.debug - = button_to 'Debug Start', game_start_path(@game), method: :get - = link_to 'Edit Game', edit_game_path(@game) + - if @game.startable? && @game.already_has?(current_user) + = button_to 'Start', start_game_path(@game) -.players - %b Players: - %ol - - @game.players.each do |player| - %li - = player.display_name - // Show the user which player they are - = "Me" if player.soft_token == current_user.soft_token + -#.debug + = button_to 'Debug Start', game_start_path(@game), method: :get + = link_to 'Edit Game', edit_game_path(@game) - // Show whose turn it is - - if @game.controlling_player_id - %strong= "My Turn " if @game.controlling_player == player + .players + %b Players: + %ol + - @game.players.each do |player| + %li + = player.display_name + // Show the user which player they are + = "Me" if player.soft_token == current_user.soft_token - // Show the players' cards - - if player.user == current_user && player.inventory.empty? == false - -##inventory + // Show whose turn it is + - if @game.controlling_player_id + %strong= "Taking Turn " if @game.controlling_player == player -%p - %b Game: - = @game.title + // Show the players' cards + - if player.user == current_user && player.inventory.empty? == false + -##inventory -%p - = @game.status + %p + %b Game: + = @game.title + + %p + = @game.status --# TODO better logic, not in view -- 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.controlling_player - - if @game.play_to_beat.play - %h1 - Hand to beat - %span.hand-type= @game.play_to_beat.play.hand_type - %div= @game.play_to_beat.play.to_s - - @game.play_to_beat.play.player_cards.each do |card| - = card.to_s - %span.player= @game.play_to_beat.play.player.display_name - - else - %h1 Open table + -# TODO better logic, not in view + - 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.controlling_player + - if @game.play_to_beat.play + %h1 + Hand to beat + %span.hand-type= @game.play_to_beat.play.hand_type + %div= @game.play_to_beat.play.to_s + - @game.play_to_beat.play.player_cards.each do |card| + = card.to_s + %span.player= @game.play_to_beat.play.player.display_name + - else + %h1 Open table -#game-component - %h1= @game.title + #game-component + %h1= @game.title - #playerList - #top - = render "seat", seat: @seats[0] - #mid - = render "seat", seat: @seats[1] + #playerList + #top + = render "seat", seat: @seats[0] + #mid + = render "seat", seat: @seats[1] diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index d2f4ec33..3292c599 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -6,3 +6,4 @@ Rails.application.config.assets.version = '1.0' # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. # Rails.application.config.assets.precompile += %w( search.js ) +Rails.application.config.assets.precompile += %w( table-loader.js ) diff --git a/config/routes.rb b/config/routes.rb index 7db0097f..fc100a2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,7 +38,7 @@ Rails.application.routes.draw do mount Upmin::Engine => '/admin' root to: 'visitors#index' - get '/play-now', to: 'games#play_now', as: 'play_now' + get '/play-now', to: 'tables#play_now', as: 'play_now' devise_for :users, controllers: { registrations: "users/registrations" } resources :users get '/loaderio-456644f4db1fcd1e8578be882d2fc476.txt', to: redirect('/assets/loaderio-456644f4db1fcd1e8578be882d2fc476.txt')