Skip to content

Commit

Permalink
🚧 #17 create bot model
Browse files Browse the repository at this point in the history
  • Loading branch information
noracami committed Oct 15, 2024
1 parent 35a7aa0 commit b3ab53f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/models/ai_player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AiPlayer < ApplicationRecord
belongs_to :bot
belongs_to :player, class_name: 'Visitor', foreign_key: 'player_id'

def nickname
"#{bot.name} (#{bot.owner.nickname})"
end
end
15 changes: 15 additions & 0 deletions app/models/bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Bot < ApplicationRecord
belongs_to :owner, class_name: 'Visitor', foreign_key: 'visitor_id'
has_many :ai_players
has_many :players, through: :ai_players, class_name: 'Visitor'

def generate_player
bot = Visitor.new_visitor(name: "#{name} #{SecureRandom.base36(4)}", role: :ai)
players << bot
bot
end

def join_room(room)
Domain::SplitRoom::Command::AddAi.new(room:, ai_player: generate_player).call
end
end
2 changes: 2 additions & 0 deletions app/models/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def read_preferences
end

def nickname
return AiPlayer.find_by(player_id: id)&.nickname || 'none' if role_ai?

self.preferences&.dig('nickname') || 'none'
end

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20241015190410_create_bots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateBots < ActiveRecord::Migration[7.1]
def change
create_table :bots do |t|
t.string :name, null: false
t.belongs_to :visitor, null: false, foreign_key: true
t.string :webhook_url, null: false
t.integer :concurrent_number, null: false, default: 1

t.timestamps
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20241015194901_create_bot_players_join_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateBotPlayersJoinTables < ActiveRecord::Migration[7.1]
def change
create_join_table :bots, :players, table_name: :ai_players do |t|
t.index :bot_id
t.index :player_id
end
end
end
20 changes: 19 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/models/bot_players_join_table_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe BotPlayersJoinTable, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/bot_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Bot, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit b3ab53f

Please sign in to comment.