From b3ab53ff75c79efc47ca525d441d41cd24e3c158 Mon Sep 17 00:00:00 2001 From: noracami Date: Wed, 16 Oct 2024 04:30:13 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20#17=20create=20bot=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/ai_player.rb | 8 ++++++++ app/models/bot.rb | 15 ++++++++++++++ app/models/visitor.rb | 2 ++ db/migrate/20241015190410_create_bots.rb | 12 +++++++++++ ...15194901_create_bot_players_join_tables.rb | 8 ++++++++ db/schema.rb | 20 ++++++++++++++++++- spec/models/bot_players_join_table_spec.rb | 5 +++++ spec/models/bot_spec.rb | 5 +++++ 8 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 app/models/ai_player.rb create mode 100644 app/models/bot.rb create mode 100644 db/migrate/20241015190410_create_bots.rb create mode 100644 db/migrate/20241015194901_create_bot_players_join_tables.rb create mode 100644 spec/models/bot_players_join_table_spec.rb create mode 100644 spec/models/bot_spec.rb diff --git a/app/models/ai_player.rb b/app/models/ai_player.rb new file mode 100644 index 0000000..d9e4017 --- /dev/null +++ b/app/models/ai_player.rb @@ -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 diff --git a/app/models/bot.rb b/app/models/bot.rb new file mode 100644 index 0000000..ff56316 --- /dev/null +++ b/app/models/bot.rb @@ -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 diff --git a/app/models/visitor.rb b/app/models/visitor.rb index fe39115..c92145d 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -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 diff --git a/db/migrate/20241015190410_create_bots.rb b/db/migrate/20241015190410_create_bots.rb new file mode 100644 index 0000000..047226c --- /dev/null +++ b/db/migrate/20241015190410_create_bots.rb @@ -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 diff --git a/db/migrate/20241015194901_create_bot_players_join_tables.rb b/db/migrate/20241015194901_create_bot_players_join_tables.rb new file mode 100644 index 0000000..497c4c1 --- /dev/null +++ b/db/migrate/20241015194901_create_bot_players_join_tables.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 669e8c0..c2aa9fc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,10 +10,27 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_09_29_170448) do +ActiveRecord::Schema[7.1].define(version: 2024_10_15_194901) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "ai_players", id: false, force: :cascade do |t| + t.bigint "bot_id", null: false + t.bigint "player_id", null: false + t.index ["bot_id"], name: "index_ai_players_on_bot_id" + t.index ["player_id"], name: "index_ai_players_on_player_id" + end + + create_table "bots", force: :cascade do |t| + t.string "name", null: false + t.bigint "visitor_id", null: false + t.string "webhook_url", null: false + t.integer "concurrent_number", default: 1, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["visitor_id"], name: "index_bots_on_visitor_id" + end + create_table "game_steps", force: :cascade do |t| t.bigint "game_id", null: false t.integer "step_number", null: false @@ -65,6 +82,7 @@ t.index ["visitor_id"], name: "index_visitors_rooms_on_visitor_id" end + add_foreign_key "bots", "visitors" add_foreign_key "game_steps", "games" add_foreign_key "visitors_rooms", "rooms" add_foreign_key "visitors_rooms", "visitors" diff --git a/spec/models/bot_players_join_table_spec.rb b/spec/models/bot_players_join_table_spec.rb new file mode 100644 index 0000000..ece67f6 --- /dev/null +++ b/spec/models/bot_players_join_table_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe BotPlayersJoinTable, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/bot_spec.rb b/spec/models/bot_spec.rb new file mode 100644 index 0000000..c66d9e2 --- /dev/null +++ b/spec/models/bot_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Bot, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end