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

Commit 5a2c516

Browse files
authored
Merge pull request #92 from ruby-nord/bug/game-finished
Fix game finished results
2 parents 5757a78 + 93cff2e commit 5a2c516

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

app/models/game.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class Game < ApplicationRecord
77
has_many :ships, through: :players
88
has_many :words, dependent: :destroy
99

10+
has_one :loser, -> { where(won: false ) }, class_name: 'Player'
11+
has_one :winner, -> { where(won: true ) }, class_name: 'Player'
12+
1013
def to_param
1114
slug
1215
end

app/views/games/finished.html.erb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
</div>
1010
</div>
1111
<div class="too_late">
12-
<% if @game.players.include?(current_player) && current_player.won == false %>
12+
<% case current_player
13+
when @game.winner %>
1314
<h1>Game Finished, Congratulations! You WON!</h1>
1415
<p>
1516
You're the master of the <strong><%= @game.name %></strong> room
1617
where you defeated <strong><%= @opponent.nickname %></strong>.
1718
</p>
18-
<% elsif @game.players.include?(current_player) %>
19+
<% when @game.loser %>
1920
<h1>Game Finished, You LOSE!</h1>
2021
<p>
2122
Today wasn't a good day for you in the <strong><%= @game.name %></strong> room.
@@ -29,7 +30,8 @@
2930
<strong><%= @game.players.last.nickname %></strong> had an epic fight deep in the space.
3031
</p>
3132
<% end %>
32-
<p>In total, the battle involved <strong><%= @game.ships.count %> ships</strong> and <strong><%= @game.ships.sum(:damage) %> damages</strong>.</p>
33+
34+
<p>In total, the battle involved <strong><%= @game.ships.count %> ships</strong> and <strong><%= @game.ships.sum(:damage) %> damages</strong>.</p>
3335

3436
<%= render 'components/social_links' %>
3537
</div>

spec/models/game_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Game", type: :model do
4+
subject(:game) { Game.new }
5+
6+
it { is_expected.to have_one(:loser).class_name(Player).conditions(won: false) }
7+
it { is_expected.to have_one(:winner).class_name(Player).conditions(won: true) }
8+
end

spec/requests/games_spec.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
let(:game) { Game.create!(name: 'Starship Battle', slug: 'starship-battle') }
55

66
describe "GET show" do
7-
context "url is not parametrized" do
7+
context "url is not parameterized" do
88
before :each do
99
get URI::encode("/games/#{game.name}")
1010
end
@@ -107,15 +107,42 @@
107107
end
108108

109109
context "when game is finished" do
110+
let(:rico) { Player.create!(nickname: "Rico") }
111+
let(:zim) { Player.create!(nickname: "Zim") }
112+
110113
before :each do
111114
game.update(state: "finished")
112-
Player.create!(nickname: "Zim", game: game)
113-
Player.create!(nickname: "Rico", game: game, won: true)
114115

116+
rico.update!(game: game, won: true)
117+
zim.update!(game: game, won: false)
118+
end
119+
120+
it 'tells that the game is finished' do
115121
get "/games/#{game.to_param}"
122+
expect(response.body).to include("finished")
123+
end
124+
125+
context 'when the signed-in player won the game' do
126+
before :each do
127+
allow_any_instance_of(ApplicationController).to receive(:current_player).and_return(rico)
128+
end
129+
130+
it 'tells the user that he won' do
131+
get "/games/#{game.to_param}"
132+
expect(response.body).to include("WON")
133+
end
116134
end
117135

118-
it { expect(response.body).to include("finished") }
136+
context 'when the signed-in player lost the game' do
137+
before :each do
138+
allow_any_instance_of(ApplicationController).to receive(:current_player).and_return(zim)
139+
end
140+
141+
it 'tells the user that he lost' do
142+
get "/games/#{game.to_param}"
143+
expect(response.body).to include("LOSE")
144+
end
145+
end
119146
end
120147
end
121148

0 commit comments

Comments
 (0)