Skip to content

Commit

Permalink
✨ 🚧 #15, add owner_id column to rooms table
Browse files Browse the repository at this point in the history
  • Loading branch information
noracami committed Sep 29, 2024
1 parent ac14335 commit d70fedb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
9 changes: 6 additions & 3 deletions app/channels/room_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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
11 changes: 4 additions & 7 deletions app/controllers/api/v1/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/views/api/v1/rooms/_room.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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
json.game_id room.games.last.id if room.status == 'playing'
6 changes: 6 additions & 0 deletions db/migrate/20240929170448_add_owner_to_rooms.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion db/schema.rb

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

0 comments on commit d70fedb

Please sign in to comment.