diff --git a/app/channels/room_channel.rb b/app/channels/room_channel.rb index e542ec7..7534360 100644 --- a/app/channels/room_channel.rb +++ b/app/channels/room_channel.rb @@ -157,13 +157,15 @@ def dispatch_to_lobby(event, room) { id: room.id, name: room.name, + owner_id: room.owner_id, players: room.players.map do |player| { id: player.id, nickname: player.nickname, character: player.character, is_ready: player.ready?, - role: player.role + role: player.role, + is_owner: player.id == room.owner_id } end }, @@ -179,10 +181,11 @@ def dispatch_to_room(event, room) nickname: rp.nickname, character: rp.character, is_ready: rp.ready?, - role: rp.role + role: rp.role, + is_owner: rp.id == room.owner_id } end - broadcast_to(room, { event:, players:, status: room.status }) + broadcast_to(room, { event:, owner_id: room.owner_id, players:, status: room.status }) end end diff --git a/app/controllers/api/v1/rooms_controller.rb b/app/controllers/api/v1/rooms_controller.rb index bb375ba..1f8fc0b 100644 --- a/app/controllers/api/v1/rooms_controller.rb +++ b/app/controllers/api/v1/rooms_controller.rb @@ -19,12 +19,8 @@ def show # POST /api/v1/rooms def create - @room = Room.create!(name: params.fetch(:name, 'New Room')) - - # Deprecated: User is automatically joined to the room - # Reason: User should join the room explicitly by calling /api/v1/rooms/:id/join or with websocket call - # user = Visitor.find(@jwt_request['sub']) - # room.players << user + current_user = Visitor.find(@jwt_request['sub']) + @room = Room.create!(name: params.fetch(:name, 'New Room'), owner_id: current_user.id) Domain::CreateRoomEvent.new(room_id: @room.id).dispatch @@ -33,7 +29,8 @@ def create # DELETE /api/v1/rooms/:id/close # 關閉房間 - # issue #10: while closing room, call gaas end game if possible + # https://github.com/side-project-at-SPT/split-rails/issues/10 + # While closing room, call gaas end game if possible # situation: # 1. room is not hosted by gaas player # => won't call gaas end game diff --git a/app/views/api/v1/rooms/_room.json.jbuilder b/app/views/api/v1/rooms/_room.json.jbuilder index 2ea608d..a87d654 100644 --- a/app/views/api/v1/rooms/_room.json.jbuilder +++ b/app/views/api/v1/rooms/_room.json.jbuilder @@ -1,13 +1,13 @@ json.id room.id json.name room.name +json.owner_id room.owner_id json.players room.players do |player| json.id player.id json.nickname player.nickname json.character player.character json.is_ready player.ready? json.role player.role + json.is_owner player.id == room.owner_id end json.status room.status -if room.status == 'playing' - json.game_id room.games.last.id -end \ No newline at end of file +json.game_id room.games.last.id if room.status == 'playing' diff --git a/db/migrate/20240929170448_add_owner_to_rooms.rb b/db/migrate/20240929170448_add_owner_to_rooms.rb new file mode 100644 index 0000000..ede33ae --- /dev/null +++ b/db/migrate/20240929170448_add_owner_to_rooms.rb @@ -0,0 +1,6 @@ +# https://github.com/side-project-at-SPT/split-rails/issues/15 +class AddOwnerToRooms < ActiveRecord::Migration[7.1] + def change + add_column :rooms, :owner_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 8c8783f..669e8c0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_07_25_163449) do +ActiveRecord::Schema[7.1].define(version: 2024_09_29_170448) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -42,6 +42,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.datetime "closed_at" + t.integer "owner_id" end create_table "visitors", force: :cascade do |t|