Add jquery reloading of table. Fix Play Now

This commit is contained in:
Jesse C. Fisher 2019-10-21 05:52:44 -07:00 committed by Jesse C. Fisher
parent 97568ad303
commit 88a8dbe6a8
13 changed files with 107 additions and 61 deletions

View File

@ -17,4 +17,11 @@
//= require react_ujs //= require react_ujs
//= require components //= require components
// require react-rails-hot-loader // 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 button
font-size: 4vmin font-size: 4vmin
#table-component,
#game-component-container-container #game-component-container-container
display: flex display: flex
flex-direction: column flex-direction: column

View File

@ -2,7 +2,7 @@
// They will automatically be included in application.css. // They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/ // You can use Sass (SCSS) here: http://sass-lang.com/
#table-component-container-container, #table-component,
#table-component-container, #table-component-container,
.table { .table {
width: 100%; width: 100%;

View File

@ -105,9 +105,11 @@ class GamesController < ApplicationController
def play_now def play_now
# First joinable game or create new game # 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 = @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 redirect_to @game.table
end end

View File

@ -3,6 +3,24 @@ class TablesController < ApplicationController
respond_to :html, :json 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
# GET /tables.json # GET /tables.json
def index def index

View File

@ -144,7 +144,9 @@ class Game < ActiveRecord::Base
# The player whose turn it is # The player whose turn it is
def controlling_player def controlling_player
self.players.find(self.controlling_player_id) if self.controlling_player_id
self.players.find(self.controlling_player_id)
end
end end
# The user whose turn it is # The user whose turn it is

View File

@ -6,7 +6,7 @@ class Seat < ActiveRecord::Base
def sit(current_user) def sit(current_user)
if self.occupied? if self.occupied?
return "Error. Seat is occupied." return self
else else
# Stand up from any other seats at this table # Stand up from any other seats at this table
current_user.stand_from(self.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.player_id = self.table.current_game.add_player_from_user(current_user).id
self.user_id = current_user.id self.user_id = current_user.id
self.user_soft_token = current_user.soft_token self.user_soft_token = current_user.soft_token
self.save return self.save
return "Successfully sat."
end end
end end

View File

@ -3,6 +3,7 @@ class Table < ActiveRecord::Base
has_many :seats has_many :seats
has_many :users, through: :seats has_many :users, through: :seats
has_many :games has_many :games
has_many :players, through: :games
#TODO: Deprecated current_game_id from database. #TODO: Deprecated current_game_id from database.
# Instead calculate with self.current_game # Instead calculate with self.current_game
# #
@ -12,6 +13,16 @@ class Table < ActiveRecord::Base
after_create :add_seats after_create :add_seats
after_create :add_game 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 def current_game
self.games.last self.games.last
end end

View File

@ -32,7 +32,7 @@
// Show whose turn it is // Show whose turn it is
- if @game.controlling_player_id - 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 // Show the players' cards
- if player.user == current_user && player.inventory.empty? == false - 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? - if policy(@table).update?
.owner-controls .owner-controls
= link_to 'Edit', edit_table_path(@table) = link_to 'Edit', edit_table_path(@table)
= link_to 'Destroy', game, :method => :delete, :data => { :confirm => 'Are you sure?' } = 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"} -# #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) %h1= @table.title
= button_to 'Start', start_game_path(@game)
-#.debug - if @game.startable? && @game.already_has?(current_user)
= button_to 'Debug Start', game_start_path(@game), method: :get = button_to 'Start', start_game_path(@game)
= link_to 'Edit Game', edit_game_path(@game)
.players -#.debug
%b Players: = button_to 'Debug Start', game_start_path(@game), method: :get
%ol = link_to 'Edit Game', edit_game_path(@game)
- @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 whose turn it is .players
- if @game.controlling_player_id %b Players:
%strong= "My Turn " if @game.controlling_player == player %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 // Show whose turn it is
- if player.user == current_user && player.inventory.empty? == false - if @game.controlling_player_id
-##inventory %strong= "Taking Turn " if @game.controlling_player == player
%p // Show the players' cards
%b Game: - if player.user == current_user && player.inventory.empty? == false
= @game.title -##inventory
%p %p
= @game.status %b Game:
= @game.title
%p
= @game.status
-# TODO better logic, not in view -# TODO better logic, not in view
- unless @game.status == nil - unless @game.status == nil
.table .table
-# TODO better logic, not in view. Set play to beat = nil; instead of view logic -# 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 && @game.player_to_beat != @game.controlling_player
- if @game.play_to_beat.play - if @game.play_to_beat.play
%h1 %h1
Hand to beat Hand to beat
%span.hand-type= @game.play_to_beat.play.hand_type %span.hand-type= @game.play_to_beat.play.hand_type
%div= @game.play_to_beat.play.to_s %div= @game.play_to_beat.play.to_s
- @game.play_to_beat.play.player_cards.each do |card| - @game.play_to_beat.play.player_cards.each do |card|
= card.to_s = card.to_s
%span.player= @game.play_to_beat.play.player.display_name %span.player= @game.play_to_beat.play.player.display_name
- else - else
%h1 Open table %h1 Open table
#game-component #game-component
%h1= @game.title %h1= @game.title
#playerList #playerList
#top #top
= render "seat", seat: @seats[0] = render "seat", seat: @seats[0]
#mid #mid
= render "seat", seat: @seats[1] = render "seat", seat: @seats[1]

View File

@ -6,3 +6,4 @@ Rails.application.config.assets.version = '1.0'
# Precompile additional assets. # Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. # 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( 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' mount Upmin::Engine => '/admin'
root to: 'visitors#index' 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" } devise_for :users, controllers: { registrations: "users/registrations" }
resources :users resources :users
get '/loaderio-456644f4db1fcd1e8578be882d2fc476.txt', to: redirect('/assets/loaderio-456644f4db1fcd1e8578be882d2fc476.txt') get '/loaderio-456644f4db1fcd1e8578be882d2fc476.txt', to: redirect('/assets/loaderio-456644f4db1fcd1e8578be882d2fc476.txt')