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

Commit e8b84f9

Browse files
cvenezianir3trofitted
authored andcommitted
Disable defense on finihed game
1 parent 7f4bff8 commit e8b84f9

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

app/assets/javascripts/battletype/logs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"already_destroyed": "Ship is already destroyed!",
3939
"bomb_already_dropped": "Bomb already dropped",
4040
"player_ship": "It's your own ship!",
41+
"not_running": "Game is not running!",
4142
"ship_not_found": "Ship not found",
4243
"wrong_case": "Words are case sensitive"
4344
}

app/dispatches/defenses/launch.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ def shot_down_ship
3232
end
3333

3434
def failed_payload
35-
{ code: 'failed_defense', player_id: player.id, word: word, error_codes: defense.errors[:word] }
35+
{ code: 'failed_defense', player_id: player.id, word: word, error_codes: error_codes }
36+
end
37+
38+
def error_codes
39+
defense.errors.messages.values.flatten
3640
end
3741

3842
def successful_payload

app/models/defense.rb

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

4-
validate :ship_exists, :matching_case_sensitive, :not_matching_own_ship, :ship_not_destroyed_yet, :mission_not_accomplished_yet
4+
validate :game_running,
5+
:ship_exists,
6+
:matching_case_sensitive,
7+
:not_matching_own_ship,
8+
:ship_not_destroyed_yet,
9+
:mission_not_accomplished_yet
510

611
private
712
attr_reader :player, :perfect_typing, :word
@@ -35,6 +40,12 @@ def unlocked_strike
3540

3641
private
3742

43+
def game_running
44+
unless player.game.state == 'running'
45+
errors.add(:game, "not_running")
46+
end
47+
end
48+
3849
def ship_not_destroyed_yet
3950
if ship&.state == 'destroyed'
4051
errors.add(:word, "already_destroyed")

spec/dispatches/defenses/launch_spec.rb

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

6-
let(:game) { Game.create! }
6+
let(:game) { Game.create!(state: 'running') }
77
let(:attacker) { Player.create!(game: game) }
88
let(:attacker_ship) { attacker.ships.last }
99
let(:attacker_word) { Word.create!(value: 'attacker', game: game) }

spec/models/defense_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe "Defense", type: :model do
44
subject(:defense) { Defense.new(player: player, word: word, perfect_typing: perfect_typing) }
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: 'HaCkeR', game: game) }
99
let(:perfect_typing) { '1' }
@@ -162,5 +162,15 @@
162162
expect(defense.valid?).to eq(true)
163163
end
164164
end
165+
166+
context 'when game is finished' do
167+
before :each do
168+
game.update(state: 'finished')
169+
end
170+
171+
it 'returns false' do
172+
expect(defense.valid?).to eq(false)
173+
end
174+
end
165175
end
166176
end

spec/requests/defenses_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "rails_helper"
22

33
RSpec.describe "Defenses", type: :request do
4-
let(:game) { Game.create!(name: 'Starship Battle') }
4+
let(:game) { Game.create!(name: 'Starship Battle', state: 'running') }
55
let(:attacker) { game.players.create! }
66
let(:player) { game.players.create! }
77

@@ -201,5 +201,32 @@
201201
)
202202
end
203203
end
204+
205+
context 'when game is finished' do
206+
before :each do
207+
attacker_word = Word.create!(value: 'finished')
208+
attacker.ships.create!(word: attacker_word)
209+
game.update(state: 'finished')
210+
end
211+
212+
it "returns 200 HTTP status" do
213+
post "/defenses", params: { word: 'finished', perfect_typing: '0' }
214+
expect(response).to have_http_status(200)
215+
end
216+
217+
it 'broadcasts a failed defense payload' do
218+
allow(ActionCable.server).to receive(:broadcast)
219+
post "/defenses", params: { word: 'finished' }
220+
expect(ActionCable.server).to have_received(:broadcast).with(
221+
anything,
222+
{
223+
code: 'failed_defense',
224+
player_id: player.id,
225+
word: 'finished',
226+
error_codes: ['not_running']
227+
}
228+
)
229+
end
230+
end
204231
end
205232
end

0 commit comments

Comments
 (0)