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

Commit 884c496

Browse files
cvenezianir3trofitted
authored andcommitted
Disable bombing on finihed game
1 parent e8b84f9 commit 884c496

File tree

6 files changed

+54
-6
lines changed

6 files changed

+54
-6
lines changed

app/assets/javascripts/battletype/logs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"bomb": {
3434
"attacked_player_ship": "You tried to bomb your mothership!!",
35+
"not_running": "Game is not running!",
3536
"ship_not_found": "Ship to bomb not found"
3637
},
3738
"defense": {

app/dispatches/bombs/drop.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ def failed_payloads
4646
code: 'failed_bombing',
4747
player_id: player.id,
4848
word: word,
49-
error_codes: bomb.errors[:word]
49+
error_codes: error_codes
5050
}]
5151
end
5252

53+
def error_codes
54+
bomb.errors.messages.values.flatten
55+
end
56+
5357
def game_won?
5458
attacked_player.life == 0
5559
end

app/models/bomb.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Bomb
22
include ActiveModel::Validations
33

4-
validate :ship_exists, :not_matching_attacked_player_ship
4+
validate :game_running, :ship_exists, :not_matching_attacked_player_ship
55

66
private
77
attr_reader :player, :word
@@ -18,6 +18,12 @@ def ship
1818

1919
private
2020

21+
def game_running
22+
unless player.game.state == 'running'
23+
errors.add(:game, "not_running")
24+
end
25+
end
26+
2127
def not_matching_attacked_player_ship
2228
matching = ships.where.not(player_id: player.id).where(words: { value: word }).exists?
2329

spec/dispatches/bombs/drop_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe "Bombs::Drop", type: :dispatch do
44
subject(:dispatch) { Bombs::Drop }
55

6-
let(:game) { Game.create! }
6+
let(:game) { Game.create!(state: 'running') }
77
let(:attacked_player) { Player.create!(game: game, life: 10) }
88
let(:attacked_word) { Word.create!(game: game, value: 'go') }
99
let(:attacker) { Player.create!(game: game) }

spec/models/bomb_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe "Bomb", type: :model do
44
subject(:bomb) { Bomb.new(player: attacker, word: word) }
55

6-
let(:game) { Game.create! }
6+
let(:game) { Game.create!(state: 'running') }
77
let(:attacker) { Player.create!(game: game) }
88
let(:attacker_word) { Word.create!(value: 'BOMB', game: game) }
99
let(:perfect_typing) { '1' }
@@ -66,5 +66,17 @@
6666
expect(bomb.valid?).to eq(true)
6767
end
6868
end
69+
70+
context "when game is finished" do
71+
let(:word) { 'BOMB' }
72+
73+
before :each do
74+
game.update(state: 'finished')
75+
end
76+
77+
it 'returns false' do
78+
expect(bomb.valid?).to eq(false)
79+
end
80+
end
6981
end
7082
end

spec/requests/bombings_spec.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
require "rails_helper"
22

33
RSpec.describe "Bombings", type: :request do
4-
let(:game) { Game.create!(name: 'Starship Battle') }
5-
let(:game) { Game.create! }
4+
let(:game) { Game.create!(name: 'Starship Battle', state: 'running') }
65
let(:attacked_player) { Player.create!(game: game, life: 10) }
76
let(:attacked_word) { Word.create!(game: game, value: 'go') }
87
let(:attacker) { Player.create!(game: game, life: 7) }
@@ -168,5 +167,31 @@
168167
)
169168
end
170169
end
170+
171+
context "when game is finished" do
172+
before :each do
173+
game.update(state: 'finished')
174+
end
175+
176+
it "returns 200 HTTP status" do
177+
post "/bombings", params: { word: 'BOMB' }
178+
expect(response).to have_http_status(200)
179+
end
180+
181+
it 'broadcasts a failed bombing payload' do
182+
allow(ActionCable.server).to receive(:broadcast)
183+
post "/bombings", params: { word: 'BOMB' }
184+
185+
expect(ActionCable.server).to have_received(:broadcast).with(
186+
anything,
187+
{
188+
code: 'failed_bombing',
189+
player_id: attacker.id,
190+
word: 'BOMB',
191+
error_codes: ['not_running']
192+
}
193+
)
194+
end
195+
end
171196
end
172197
end

0 commit comments

Comments
 (0)