Create button to remove bots

This commit is contained in:
Jesse C. Fisher 2021-11-10 22:14:43 -08:00
parent 72a26675b5
commit 2a088661a1
5 changed files with 46 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@ -22,6 +22,7 @@ Rails.application.routes.draw do
post 'sit'
post 'stand'
post 'add_bot'
post 'remove_bot'
end
end

View File

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