Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit fe10485

Browse files
committed
Broadcast the update of player's nickname
1 parent f9764cd commit fe10485

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

app/controllers/players_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ def update
55
return render json: { error: "Cannot update another user" }, status: 401 if @player != current_player
66

77
@player.update(player_params)
8-
render json: { status: :ok }, status: 200
8+
9+
payload = {
10+
code: "successful_player_nickname_update",
11+
player_id: @player.id,
12+
nickname: @player.nickname
13+
}
14+
15+
ActionCable.server.broadcast "game_#{current_player.game_id}", payload
16+
head 200
917
end
1018

1119
private

app/views/games/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<li class="life_bar"><%= render "games/hud/life" %></li>
2121
</ul>
2222
<div id="current_player" data-player-id="<%= current_player.id %>" data-player-nickname="<%= current_player.nickname %>"></div>
23-
<%= form_for(current_player, url: player_path(current_player), class: "player") do |f|%>
23+
<%= form_for(current_player, url: player_path(current_player), data: { remote: true }, class: "player") do |f| %>
2424
<%= f.text_field :nickname, id: 'current_player_nickname' %>
2525
<% end %>
2626
<ul id="life_opponent">

spec/requests/players_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,26 @@
77
context 'update current_player' do
88
before :each do
99
allow_any_instance_of(ApplicationController).to receive(:current_player).and_return(player)
10+
end
11+
12+
it "returns 200 HTTP status" do
1013
put "/players/#{player.id}", params: { player: { nickname: "Zim" }}
14+
expect(response).to have_http_status(200)
1115
end
1216

13-
it { expect(response).to have_http_status(200) }
14-
it { expect(player.reload.nickname).to eq("Zim") }
17+
it "updates player nickname" do
18+
put "/players/#{player.id}", params: { player: { nickname: "Zim" }}
19+
expect(player.reload.nickname).to eq("Zim")
20+
end
21+
22+
it 'broadcasts a player nickname updated payload' do
23+
allow(ActionCable.server).to receive(:broadcast)
24+
put "/players/#{player.id}", params: { player: { nickname: "Zim" }}
25+
expect(ActionCable.server).to have_received(:broadcast).with(
26+
anything,
27+
code: 'successful_player_nickname_update', player_id: player.id, nickname: 'Zim'
28+
)
29+
end
1530
end
1631

1732
context 'update another player' do

0 commit comments

Comments
 (0)