Add jquery reloading of table. Fix Play Now

This commit is contained in:
Jesse C. Fisher 2019-10-21 05:52:44 -07:00
parent c2865fb638
commit 2b81a1f976
13 changed files with 107 additions and 61 deletions

View File

@ -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);
// });

View File

@ -0,0 +1,5 @@
$(document).ready(function(){
setInterval(function() {
$('#table-component').load(window.location.pathname +" #table-component");
}, 5000);
});

View File

@ -30,6 +30,7 @@
button
font-size: 4vmin
#table-component,
#game-component-container-container
display: flex
flex-direction: column

View File

@ -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%;

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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 )

View File

@ -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')