From 6330a6b4e15aaa445981b8e071c28b7ee37d9914 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Wed, 10 Nov 2021 22:14:43 -0800 Subject: [PATCH] Create button to remove bots --- app/controllers/seats_controller.rb | 7 +++++- app/models/seat.rb | 14 ++++++++++++ app/views/tables/_seat.html.haml | 3 +++ config/routes.rb | 1 + spec/features/bots/remove_bot_players_spec.rb | 22 +++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 spec/features/bots/remove_bot_players_spec.rb diff --git a/app/controllers/seats_controller.rb b/app/controllers/seats_controller.rb index 8273e2e..13805d2 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, :add_bot] + before_action :set_seat, only: [:show, :edit, :update, :destroy, :sit, :stand, :add_bot, :remove_bot] respond_to :html, :json @@ -85,6 +85,11 @@ class SeatsController < ApplicationController redirect_to @seat.table, notice: @message end + def remove_bot + @message = @seat.remove_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/seat.rb b/app/models/seat.rb index 6842b22..461f5e1 100644 --- a/app/models/seat.rb +++ b/app/models/seat.rb @@ -61,6 +61,20 @@ class Seat < ActiveRecord::Base end end + def remove_bot + if !self.occupied? + return "Failed to remove Bot seat empty" + else + if self.player.is_bot? + self.player.is_bot = false + self.player.save + return "Removed Bot" + end + + return "Failed to remove Bot" + end + end + def occupied? self.user_id.present? || self.user_soft_token.present? || self.try("player").try("is_bot?") == true end diff --git a/app/views/tables/_seat.html.haml b/app/views/tables/_seat.html.haml index a2fb843..5be0f5a 100644 --- a/app/views/tables/_seat.html.haml +++ b/app/views/tables/_seat.html.haml @@ -2,6 +2,9 @@ .seat{class: "position-#{@seats.index(seat) + 1}"} .seat-controls - if seat.occupied? + - if seat.player.is_bot? + = button_to remove_bot_seat_path(seat) do + - "- Bot" - if seat.occupied_by? current_user = button_to stand_seat_path(seat), :data => { :confirm => 'Are you sure you want to leave this seat?' } do - "Stand" diff --git a/config/routes.rb b/config/routes.rb index 0867ae6..fc11115 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,6 +22,7 @@ Rails.application.routes.draw do post 'sit' post 'stand' post 'add_bot' + post 'remove_bot' end end diff --git a/spec/features/bots/remove_bot_players_spec.rb b/spec/features/bots/remove_bot_players_spec.rb new file mode 100644 index 0000000..1b4e4c8 --- /dev/null +++ b/spec/features/bots/remove_bot_players_spec.rb @@ -0,0 +1,22 @@ +# Feature: Remove bot from seat +# As a user +# I want to remove a bot from a sea +feature 'Remove bot', type: :feature do + before(:each) do + @table = FactoryGirl.create :table + visit table_path @table + end + # Scenario: Visitor can remove a bot from seat + # Given I can edit the table + # When I see the seat with a bot + # Then I see a button to remove a bot + scenario 'visitor can remove a bot from a seat' do + @button = page.find_button('+ Bot', match: :first).click + page.assert_selector('button', text: '+ Bot', count: 3) + expect(page).to have_content "BotPlayer" + @button = page.find_button('- Bot', match: :first).click + expect(page).to have_content "Removed Bot" + page.assert_selector('button', text: '+ Bot', count: 4) + end + +end