接下來這幾天會朝著完成第一次 happy path 邁進
移除之前建立的 GameRoom,並重新建立一次
rails d scaffold GameRoom
rails g scaffold GameRoom name game_data:json
db 推倒重新來吧
rails db:drop
rails db:prepare
# app/views/game_rooms/_game_room.html.erb
+ <p>
+ <button type="button" data-action="hello#join_the_room" data-hello-room_id-param="<%= game_room.id %>" class="rounded-lg my-10 py-3 px-5 bg-blue-400 text-white">join the room</button>
+ </p>
# app/views/game_rooms/index.html.erb
+ <div data-controller="hello">
+ <div id="game_rooms" class="min-w-full">
+ <%= render @game_rooms %>
+ </div>
+ <p class="font-bold text-2xl" data-hello-target="title"></p>
+ <button type="button" data-action="hello#ping_the_room" class="rounded-lg py-3 px-5 bg-emerald-600 text-white">ping to the room</button>
</div>
// app/javascript/controllers/hello_controller.js
join_the_room({ params: { roomId } }) {
this.channel.joinGame({
nickname: "Paul",
roomId,
});
}
// app/javascript/channels/chat_room_channels.js
joinGame(messageBody) {
this.perform("join_game", messageBody);
}
# app/channels/chat_room_channel.rb
def join_game(data)
game_room = GameRoom.find_by id: data['roomId']
return unless game_room
if uuid.in? game_room.game_data['players']
broadcast("#{uuid} has already joined room #{game_room.name}")
else
game_room.game_data['players'] << uuid
game_room.save
broadcast("#{uuid} has joined room #{game_room.name}")
end
end
game_data
初始化# app/controllers/game_rooms_controller.rb
# POST /game_rooms or /game_rooms.json
def create
- @game_room = GameRoom.new(game_room_params)
+ @game_room = GameRoom.new(game_room_params) { _1.game_data = {players: []} }
這時 玩家 還沒有建立模型,因此只要 websocket 重連,就會得到不同的 uuid
。
所以會新增以下 issues 要處理
identified_by :uuid
to current_user.id上述三個問題若沒處理,目前還是可以推進進度
因此明天先衝刺看能不能做到遊戲結束
Demo url -> https://ironman2023-hanabi.zeabur.app/