This repository was archived by the owner on Jan 9, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +54
-6
lines changed
assets/javascripts/battletype Expand file tree Collapse file tree 6 files changed +54
-6
lines changed Original file line number Diff line number Diff line change 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" : {
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11class 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
Original file line number Diff line number Diff line change 33RSpec . 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 ) }
Original file line number Diff line number Diff line change 33RSpec . 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' }
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
7082end
Original file line number Diff line number Diff line change 11require "rails_helper"
22
33RSpec . 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 ) }
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
172197end
You can’t perform that action at this time.
0 commit comments