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

Commit 9e2619e

Browse files
committed
Disable attack on finished game
1 parent 5a2c516 commit 9e2619e

File tree

6 files changed

+54
-8
lines changed

6 files changed

+54
-8
lines changed

app/assets/javascripts/battletype/logs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"attack": {
2828
"english_word": "Must be an English word",
2929
"long_enough": "Word is not long enough",
30+
"not_running": "Game is not running!",
3031
"unique_case_insensitive_word": "Word must be unique"
3132
},
3233
"bomb": {

app/dispatches/attacks/launch.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ def call
2828
private
2929

3030
def failed_payload
31-
{ code: 'failed_attack', player_id: player.id, word: word, error_codes: attack.errors[:word] }
31+
{ code: 'failed_attack', player_id: player.id, word: word, error_codes: error_codes }
32+
end
33+
34+
def error_codes
35+
attack.errors.messages.values.flatten
3236
end
3337

3438
def save_word

app/models/attack.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Attack
22
include ActiveModel::Validations
33
MIN_WORD_SIZE = 2
44

5-
validate :unique_case_insensitive_word?, :english_word?, :long_enough?
5+
validate :game_running?, :unique_case_insensitive_word?, :english_word?, :long_enough?
66

77
private
88
attr_reader :game, :word, :player
@@ -30,6 +30,12 @@ def initialize(game:, word:, player:)
3030

3131
private
3232

33+
def game_running?
34+
unless game.state == 'running'
35+
errors.add(:game, "not_running")
36+
end
37+
end
38+
3339
def unique_case_insensitive_word?
3440
unless Word.where(game: game).where('LOWER(value) = LOWER(?)', word).empty?
3541
errors.add(:word, "unique_case_insensitive_word")

spec/dispatches/attacks/launch_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
RSpec.describe "Attacks::Launch", type: :dispatch do
44
subject(:dispatch) { Attacks::Launch }
5-
let(:game) { Game.create! }
5+
let(:game) { Game.create!(state: 'running') }
66
let(:player) { Player.create!(game: game, nickname: "Rico") }
77

88
describe ".call" do
99
context 'when word is valid' do
1010
let(:word) { 'curry' }
11-
before { allow_words(word) }
11+
12+
before :each do
13+
allow_words(word)
14+
end
1215

1316
it 'saves the Word' do
1417
expect { dispatch.call(player: player, word: word) }.to change { Word.where(value: word, game: game).count }.from(0).to(1)
@@ -64,6 +67,7 @@
6467
let(:word) { 'duplicate' }
6568

6669
before :each do
70+
allow_words(word)
6771
Word.create!(game: game, value: word)
6872
end
6973

@@ -82,7 +86,7 @@
8286
end
8387

8488
it "returns a payload with the invalid word and the attacker's id" do
85-
expect(dispatch.call(player: player, word: word)).to include(code: 'failed_attack', word: "duplicate", player_id: player.id, error_codes: ["unique_case_insensitive_word", "english_word"])
89+
expect(dispatch.call(player: player, word: word)).to include(code: 'failed_attack', word: "duplicate", player_id: player.id, error_codes: ["unique_case_insensitive_word"])
8690
end
8791
end
8892
end

spec/models/attack_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
RSpec.describe "Attack", type: :model do
44
subject(:attack) { Attack.new(game: game, word: word, player: player) }
5-
let(:game) { Game.create! }
5+
let(:game) { Game.create!(state: 'running') }
66
let(:word) { 'attack' }
77
let(:player) { Player.new }
8+
89
before { allow_words("attack") }
910

1011
describe '.reward_for' do
@@ -97,5 +98,16 @@
9798
it { expect(Attack.new(game: game, word: "AniMal", player: player).valid?).to be true }
9899
it { expect(Attack.new(game: game, word: "animal12", player: player).valid?).to be false }
99100
end
101+
102+
context 'when game is finished' do
103+
before :each do
104+
allow_words("finished")
105+
game.update(state: 'finished')
106+
end
107+
108+
it 'returns false' do
109+
expect(attack.valid?).to eq(false)
110+
end
111+
end
100112
end
101113
end

spec/requests/attacks_spec.rb

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

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

77
before :each do
@@ -54,6 +54,7 @@
5454

5555
context 'when provided word has already been played with a different case' do
5656
before :each do
57+
allow_words("battletype")
5758
game.words.create!(value: 'BattleType')
5859
end
5960

@@ -65,7 +66,25 @@
6566
it 'broadcasts an error message' do
6667
allow(ActionCable.server).to receive(:broadcast)
6768
post "/attacks", params: { word: 'battletype' }
68-
expect(ActionCable.server).to have_received(:broadcast).with(anything, code: 'failed_attack', player_id: player.id, word: 'battletype', error_codes: ["unique_case_insensitive_word", "english_word"])
69+
expect(ActionCable.server).to have_received(:broadcast).with(anything, code: 'failed_attack', player_id: player.id, word: 'battletype', error_codes: ["unique_case_insensitive_word"])
70+
end
71+
end
72+
73+
context 'when game is finished' do
74+
before :each do
75+
allow_words("finished")
76+
game.update(state: 'finished')
77+
end
78+
79+
it 'returns 200 HTTP status' do
80+
post "/attacks", params: { word: 'finished' }
81+
expect(response).to have_http_status(200)
82+
end
83+
84+
it 'broadcasts an error message' do
85+
allow(ActionCable.server).to receive(:broadcast)
86+
post "/attacks", params: { word: 'finished' }
87+
expect(ActionCable.server).to have_received(:broadcast).with(anything, code: 'failed_attack', word: 'finished', player_id: player.id, error_codes: ['not_running'] )
6988
end
7089
end
7190
end

0 commit comments

Comments
 (0)