From 5453be7d2dc764b264181c4addce0e96f6f6b60d Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Mon, 28 Oct 2019 07:08:38 -0700 Subject: [PATCH] Start adding New Game button to table --- app/assets/stylesheets/game.css.sass | 3 ++- app/controllers/tables_controller.rb | 7 ++++++- app/models/game.rb | 5 +++++ app/models/table.rb | 8 +++++--- app/views/tables/_seat.html.haml | 2 +- app/views/tables/show.html.haml | 8 ++++++-- config/routes.rb | 3 +++ 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/assets/stylesheets/game.css.sass b/app/assets/stylesheets/game.css.sass index 8478df1d..3e5234cb 100644 --- a/app/assets/stylesheets/game.css.sass +++ b/app/assets/stylesheets/game.css.sass @@ -27,7 +27,8 @@ //// Show Game#show -button +button, +input[type=submit] font-size: 4vmin #table-component, diff --git a/app/controllers/tables_controller.rb b/app/controllers/tables_controller.rb index 764cac26..a6ebdf8c 100644 --- a/app/controllers/tables_controller.rb +++ b/app/controllers/tables_controller.rb @@ -1,5 +1,5 @@ class TablesController < ApplicationController - before_action :set_table, only: [:show, :edit, :update, :destroy] + before_action :set_table, only: [:show, :edit, :update, :destroy, :new_game] respond_to :html, :json @@ -39,6 +39,11 @@ class TablesController < ApplicationController end end + def new_game + @table.add_game + redirect_to @table + end + # GET /tables/new def new @table = Table.new diff --git a/app/models/game.rb b/app/models/game.rb index 196e6814..8f7b251c 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -181,6 +181,11 @@ private def add_table self.table ||= Table.create current_game_id: self.id self.seats = self.table.seats + self.seats.each do |seat| + if seat.user_id || seat.user_soft_token + new_player = self.players.create(user_id: seat.user_id, soft_token: seat.user_soft_token) + end + end self.save end diff --git a/app/models/table.rb b/app/models/table.rb index 80949781..3cb983ad 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -27,6 +27,11 @@ class Table < ActiveRecord::Base self.games.last end + + def add_game + self.games.create + end + private def add_seats @@ -35,7 +40,4 @@ class Table < ActiveRecord::Base end end - def add_game - self.games.create if self.current_game_id.blank? - end end diff --git a/app/views/tables/_seat.html.haml b/app/views/tables/_seat.html.haml index f95d3da0..6a1cd6e8 100644 --- a/app/views/tables/_seat.html.haml +++ b/app/views/tables/_seat.html.haml @@ -19,7 +19,7 @@ -# Show player controls if player is owned by current_user - if seat.player.soft_token == current_user.soft_token = form_for(@game, - url: play_hand_game_path, + url: play_hand_game_path(@game), method: "post", name: "player_controls_" + seat.player_id.to_s, id: "player_controls_" + seat.player_id.to_s) do |f| diff --git a/app/views/tables/show.html.haml b/app/views/tables/show.html.haml index 210cf4e4..4156f216 100644 --- a/app/views/tables/show.html.haml +++ b/app/views/tables/show.html.haml @@ -10,8 +10,12 @@ %p= @table.title - - if @game.startable? && @game.already_has?(current_user) - = button_to 'Start', start_game_path(@game) + - if @game.already_has?(current_user) + - if @game.startable? + = button_to 'Start', start_game_path(@game) + + - if @game.winner_player_id || @game.nil? + = button_to 'New Game', new_game_table_path(@table) -# .players %b Players: diff --git a/config/routes.rb b/config/routes.rb index fc100a2f..07204230 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,9 @@ Rails.application.routes.draw do resources :players resources :tables do + member do + post 'new_game' + end resources :seats do member do patch 'sit'