Adds Bot button and start of Bot Players

This commit is contained in:
Jesse C. Fisher 2021-11-07 21:48:48 -08:00
parent 6b5be166f8
commit fe136d1764
9 changed files with 44 additions and 4 deletions

View File

@ -1,5 +1,5 @@
class SeatsController < ApplicationController 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 respond_to :html, :json
@ -80,6 +80,11 @@ class SeatsController < ApplicationController
redirect_to @seat.table, notice: @message redirect_to @seat.table, notice: @message
end end
def add_bot
@message = @seat.add_bot
redirect_to @seat.table, notice: @message
end
private private
# Use callbacks to share common setup or constraints between actions. # Use callbacks to share common setup or constraints between actions.
def set_seat def set_seat

View File

@ -16,6 +16,7 @@ class Player < ActiveRecord::Base
def display_name def display_name
return user.email if self.user return user.email if self.user
return "BotPlayer" if self.is_bot?
return ("Guest" + self.soft_token)[0..9] if self.soft_token return ("Guest" + self.soft_token)[0..9] if self.soft_token
return "Empty" return "Empty"
end end

View File

@ -43,6 +43,24 @@ class Seat < ActiveRecord::Base
end end
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? def occupied?
self.user_id.present? || self.user_soft_token.present? self.user_id.present? || self.user_soft_token.present?
end end

View File

@ -8,6 +8,8 @@
- else - else
= button_to sit_seat_path(seat) do = button_to sit_seat_path(seat) do
- "Sit" - "Sit"
= button_to add_bot_seat_path(seat) do
- "+ Bot"
- if seat.player - if seat.player
.player{id: "player-#{seat.player.id}", class: @game.controlling_player == seat.player ? "taking-turn" : "", .player{id: "player-#{seat.player.id}", class: @game.controlling_player == seat.player ? "taking-turn" : "",

View File

@ -34,4 +34,4 @@
:javascript :javascript
var soft_token = "#{current_user.soft_token}"; var soft_token = "#{current_user.soft_token}";
= javascript_include_tag 'table-loader' -# = javascript_include_tag 'table-loader'

View File

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

View File

@ -0,0 +1,5 @@
class AddIsBotToPlayer < ActiveRecord::Migration[5.2]
def change
add_column :players, :is_bot, :boolean, default: false
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -53,6 +53,7 @@ ActiveRecord::Schema.define(version: 2019_10_24_023437) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "soft_token" t.string "soft_token"
t.boolean "is_bot", default: false
t.index ["game_id"], name: "index_players_on_game_id" t.index ["game_id"], name: "index_players_on_game_id"
t.index ["user_id"], name: "index_players_on_user_id" t.index ["user_id"], name: "index_players_on_user_id"
end end

View File

@ -2,11 +2,18 @@
# As a user # As a user
# I want to add a bot to a table # I want to add a bot to a table
# So I can play against the computer # 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 # Scenario: User can add a bot if there is room at the table
# Given I can edit the table # Given I can edit the table
# When I see the table with an empty seat # When I see the table with an empty seat
# Then I see a button to add a bot # 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 scenario 'user can add a bot if the table is not full' do
skip "To be implemented" skip "To be implemented"
#signin('test@example.com', 'please123') #signin('test@example.com', 'please123')