From ef46165f7a96e24aaf39c841931692b17bd04404 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Sun, 7 Nov 2021 21:48:48 -0800 Subject: [PATCH] Adds Bot button and start of Bot Players --- app/controllers/seats_controller.rb | 7 ++++++- app/models/player.rb | 1 + app/models/seat.rb | 18 ++++++++++++++++++ app/views/tables/_seat.html.haml | 2 ++ app/views/tables/show.html.haml | 2 +- config/routes.rb | 1 + .../20211108045859_add_is_bot_to_player.rb | 5 +++++ db/schema.rb | 3 ++- spec/features/bots/add_bot_players_spec.rb | 9 ++++++++- 9 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20211108045859_add_is_bot_to_player.rb diff --git a/app/controllers/seats_controller.rb b/app/controllers/seats_controller.rb index eb9e0439..8273e2e9 100644 --- a/app/controllers/seats_controller.rb +++ b/app/controllers/seats_controller.rb @@ -1,5 +1,5 @@ class SeatsController < ApplicationController - before_action :set_seat, only: [:show, :edit, :update, :destroy, :sit, :stand] + before_action :set_seat, only: [:show, :edit, :update, :destroy, :sit, :stand, :add_bot] respond_to :html, :json @@ -80,6 +80,11 @@ class SeatsController < ApplicationController redirect_to @seat.table, notice: @message end + def add_bot + @message = @seat.add_bot + redirect_to @seat.table, notice: @message + end + private # Use callbacks to share common setup or constraints between actions. def set_seat diff --git a/app/models/player.rb b/app/models/player.rb index 896aa7f3..330af995 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -16,6 +16,7 @@ class Player < ActiveRecord::Base def display_name return user.email if self.user + return "BotPlayer" if self.is_bot? return ("Guest" + self.soft_token)[0..9] if self.soft_token return "Empty" end diff --git a/app/models/seat.rb b/app/models/seat.rb index 525f5c38..7fda5ef2 100644 --- a/app/models/seat.rb +++ b/app/models/seat.rb @@ -43,6 +43,24 @@ class Seat < ActiveRecord::Base end end + def add_bot + if self.occupied? + return self + else + # self.reload + if self.player == nil + # add player to game + @bot_player = self.table.current_game.players.new(is_bot: true) + self.player = @bot_player + self.player.save + self.save + return "Added Bot" + end + + return "Failed to add Bot" + end + end + def occupied? self.user_id.present? || self.user_soft_token.present? end diff --git a/app/views/tables/_seat.html.haml b/app/views/tables/_seat.html.haml index d35e790d..a2fb843d 100644 --- a/app/views/tables/_seat.html.haml +++ b/app/views/tables/_seat.html.haml @@ -8,6 +8,8 @@ - else = button_to sit_seat_path(seat) do - "Sit" + = button_to add_bot_seat_path(seat) do + - "+ Bot" - if seat.player .player{id: "player-#{seat.player.id}", class: @game.controlling_player == seat.player ? "taking-turn" : "", diff --git a/app/views/tables/show.html.haml b/app/views/tables/show.html.haml index 7b009d2b..a4f3db9f 100644 --- a/app/views/tables/show.html.haml +++ b/app/views/tables/show.html.haml @@ -34,4 +34,4 @@ :javascript var soft_token = "#{current_user.soft_token}"; - = javascript_include_tag 'table-loader' + -# = javascript_include_tag 'table-loader' diff --git a/config/routes.rb b/config/routes.rb index adb9ee58..0867ae66 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ Rails.application.routes.draw do member do post 'sit' post 'stand' + post 'add_bot' end end diff --git a/db/migrate/20211108045859_add_is_bot_to_player.rb b/db/migrate/20211108045859_add_is_bot_to_player.rb new file mode 100644 index 00000000..bdd60c4d --- /dev/null +++ b/db/migrate/20211108045859_add_is_bot_to_player.rb @@ -0,0 +1,5 @@ +class AddIsBotToPlayer < ActiveRecord::Migration[5.2] + def change + add_column :players, :is_bot, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 948d8d90..1befbadb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_10_24_023437) do +ActiveRecord::Schema.define(version: 2021_11_08_045859) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -53,6 +53,7 @@ ActiveRecord::Schema.define(version: 2019_10_24_023437) do t.datetime "created_at" t.datetime "updated_at" t.string "soft_token" + t.boolean "is_bot", default: false t.index ["game_id"], name: "index_players_on_game_id" t.index ["user_id"], name: "index_players_on_user_id" end diff --git a/spec/features/bots/add_bot_players_spec.rb b/spec/features/bots/add_bot_players_spec.rb index 12a320ee..8f3b2f96 100644 --- a/spec/features/bots/add_bot_players_spec.rb +++ b/spec/features/bots/add_bot_players_spec.rb @@ -2,11 +2,18 @@ # As a user # I want to add a bot to a table # So I can play against the computer -feature 'Add bot', type: :feature, js: true do +feature 'Add bot', type: :feature do # Scenario: User can add a bot if there is room at the table # Given I can edit the table # When I see the table with an empty seat # Then I see a button to add a bot + scenario 'visitor can add a bot if the seat is empty' do + @table = FactoryGirl.create :table + visit table_path @table + @button = page.find_button('+ Bot', match: :first).click + expect(page).to have_content "BotPlayer" + end + scenario 'user can add a bot if the table is not full' do skip "To be implemented" #signin('test@example.com', 'please123')