Adds Bot button and start of Bot Players
This commit is contained in:
parent
fd8ee73fad
commit
ef46165f7a
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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" : "",
|
||||
|
||||
@ -34,4 +34,4 @@
|
||||
|
||||
:javascript
|
||||
var soft_token = "#{current_user.soft_token}";
|
||||
= javascript_include_tag 'table-loader'
|
||||
-# = javascript_include_tag 'table-loader'
|
||||
|
||||
@ -21,6 +21,7 @@ Rails.application.routes.draw do
|
||||
member do
|
||||
post 'sit'
|
||||
post 'stand'
|
||||
post 'add_bot'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
5
db/migrate/20211108045859_add_is_bot_to_player.rb
Normal file
5
db/migrate/20211108045859_add_is_bot_to_player.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class AddIsBotToPlayer < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :players, :is_bot, :boolean, default: false
|
||||
end
|
||||
end
|
||||
@ -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
|
||||
|
||||
@ -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')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user