Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/assets/javascripts/battletype.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(function () {
this.Battletype = {
attacking: true,
running: true,

_eventsRelay: Object.create(PS2EventRelay),

Expand Down Expand Up @@ -108,6 +109,7 @@
}
break;
case "game_won":
this.running = false;
this._logs.displayMessage(payload.code);

if (payload.player_id == this.playerId) {
Expand Down Expand Up @@ -139,6 +141,8 @@
}
},
scanForShip: function (text) {
if (!this.running) { return }

if (!this.attacking) {
var target = this.$combatZone.find("[id^='" + Ship._shipIdFromWord(text) + "']");
if (target) {
Expand All @@ -150,6 +154,8 @@
}
},
transmitEntry: function (entry) {
if (!this.running) { return }

this.attacking ? this._transmitAttack(entry) : this._transmitDefense(entry);
this._stdin.reset();
},
Expand All @@ -165,13 +171,17 @@
$(this.defenseFrequency).trigger("submit.rails");
},
_transmitBombing: function (ship) {
if (!this.running) { return }

ship.className += " leaving"; // The bombing ship leaves the scene
this.mothership.hit = true; // The mothership takes a hit

this.bombingFrequency.elements["word"].value = ship.word;
$(this.bombingFrequency).trigger("submit.rails");
},
switchMode: function () {
if (!this.running) { return }

Battletype.attacking = !Battletype.attacking;

if (Battletype.attacking) {
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/battletype/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
"attack": {
"english_word": "Must be an English word",
"long_enough": "Word is not long enough",
"not_running": "Game is not running!",
"unique_case_insensitive_word": "Word must be unique"
},
"bomb": {
"attacked_player_ship": "You tried to bomb your mothership!!",
"not_running": "Game is not running!",
"ship_not_found": "Ship to bomb not found"
},
"defense": {
"already_destroyed": "Ship is already destroyed!",
"bomb_already_dropped": "Bomb already dropped",
"player_ship": "It's your own ship!",
"not_running": "Game is not running!",
"ship_not_found": "Ship not found",
"wrong_case": "Words are case sensitive"
}
Expand Down
6 changes: 5 additions & 1 deletion app/dispatches/attacks/launch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def call
private

def failed_payload
{ code: 'failed_attack', player_id: player.id, word: word, error_codes: attack.errors[:word] }
{ code: 'failed_attack', player_id: player.id, word: word, error_codes: error_codes }
end

def error_codes
attack.errors.messages.values.flatten
end

def save_word
Expand Down
6 changes: 5 additions & 1 deletion app/dispatches/bombs/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ def failed_payloads
code: 'failed_bombing',
player_id: player.id,
word: word,
error_codes: bomb.errors[:word]
error_codes: error_codes
}]
end

def error_codes
bomb.errors.messages.values.flatten
end

def game_won?
attacked_player.life == 0
end
Expand Down
6 changes: 5 additions & 1 deletion app/dispatches/defenses/launch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def shot_down_ship
end

def failed_payload
{ code: 'failed_defense', player_id: player.id, word: word, error_codes: defense.errors[:word] }
{ code: 'failed_defense', player_id: player.id, word: word, error_codes: error_codes }
end

def error_codes
defense.errors.messages.values.flatten
end

def successful_payload
Expand Down
8 changes: 7 additions & 1 deletion app/models/attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Attack
include ActiveModel::Validations
MIN_WORD_SIZE = 2

validate :unique_case_insensitive_word?, :english_word?, :long_enough?
validate :game_running?, :unique_case_insensitive_word?, :english_word?, :long_enough?

private
attr_reader :game, :word, :player
Expand Down Expand Up @@ -30,6 +30,12 @@ def initialize(game:, word:, player:)

private

def game_running?
unless game.running?
errors.add(:game, "not_running")
end
end

def unique_case_insensitive_word?
unless Word.where(game: game).where('LOWER(value) = LOWER(?)', word).empty?
errors.add(:word, "unique_case_insensitive_word")
Expand Down
8 changes: 7 additions & 1 deletion app/models/bomb.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Bomb
include ActiveModel::Validations

validate :ship_exists, :not_matching_attacked_player_ship
validate :game_running, :ship_exists, :not_matching_attacked_player_ship

private
attr_reader :player, :word
Expand All @@ -18,6 +18,12 @@ def ship

private

def game_running
unless player.game.state == 'running'
errors.add(:game, "not_running")
end
end

def not_matching_attacked_player_ship
matching = ships.where.not(player_id: player.id).where(words: { value: word }).exists?

Expand Down
13 changes: 12 additions & 1 deletion app/models/defense.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class Defense
include ActiveModel::Validations

validate :ship_exists, :matching_case_sensitive, :not_matching_own_ship, :ship_not_destroyed_yet, :mission_not_accomplished_yet
validate :game_running,
:ship_exists,
:matching_case_sensitive,
:not_matching_own_ship,
:ship_not_destroyed_yet,
:mission_not_accomplished_yet

private
attr_reader :player, :perfect_typing, :word
Expand Down Expand Up @@ -35,6 +40,12 @@ def unlocked_strike

private

def game_running
unless player.game.state == 'running'
errors.add(:game, "not_running")
end
end

def ship_not_destroyed_yet
if ship&.state == 'destroyed'
errors.add(:word, "already_destroyed")
Expand Down
10 changes: 7 additions & 3 deletions spec/dispatches/attacks/launch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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

describe ".call" do
context 'when word is valid' do
let(:word) { 'curry' }
before { allow_words(word) }

before :each do
allow_words(word)
end

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

before :each do
allow_words(word)
Word.create!(game: game, value: word)
end

Expand All @@ -82,7 +86,7 @@
end

it "returns a payload with the invalid word and the attacker's id" do
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"])
expect(dispatch.call(player: player, word: word)).to include(code: 'failed_attack', word: "duplicate", player_id: player.id, error_codes: ["unique_case_insensitive_word"])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dispatches/bombs/drop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe "Bombs::Drop", type: :dispatch do
subject(:dispatch) { Bombs::Drop }

let(:game) { Game.create! }
let(:game) { Game.create!(state: 'running') }
let(:attacked_player) { Player.create!(game: game, life: 10) }
let(:attacked_word) { Word.create!(game: game, value: 'go') }
let(:attacker) { Player.create!(game: game) }
Expand Down
2 changes: 1 addition & 1 deletion spec/dispatches/defenses/launch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe "Defenses::Launch", type: :dispatch do
subject(:dispatch) { Defenses::Launch }

let(:game) { Game.create! }
let(:game) { Game.create!(state: 'running') }
let(:attacker) { Player.create!(game: game) }
let(:attacker_ship) { attacker.ships.last }
let(:attacker_word) { Word.create!(value: 'attacker', game: game) }
Expand Down
17 changes: 16 additions & 1 deletion spec/models/attack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
let(:game) { Game.create! }
let(:word) { 'attack' }
let(:player) { Player.new }
before { allow_words("attack") }

before do
allow_words("attack")
game.running!
end

describe '.reward_for' do
describe 'word between 0 and 1 letters' do
Expand Down Expand Up @@ -97,5 +101,16 @@
it { expect(Attack.new(game: game, word: "AniMal", player: player).valid?).to be true }
it { expect(Attack.new(game: game, word: "animal12", player: player).valid?).to be false }
end

context 'when game is finished' do
before :each do
allow_words("finished")
game.finished!
end

it 'returns false' do
expect(attack.valid?).to eq(false)
end
end
end
end
14 changes: 13 additions & 1 deletion spec/models/bomb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe "Bomb", type: :model do
subject(:bomb) { Bomb.new(player: attacker, word: word) }

let(:game) { Game.create! }
let(:game) { Game.create!(state: 'running') }
let(:attacker) { Player.create!(game: game) }
let(:attacker_word) { Word.create!(value: 'BOMB', game: game) }
let(:perfect_typing) { '1' }
Expand Down Expand Up @@ -66,5 +66,17 @@
expect(bomb.valid?).to eq(true)
end
end

context "when game is finished" do
let(:word) { 'BOMB' }

before :each do
game.update(state: 'finished')
end

it 'returns false' do
expect(bomb.valid?).to eq(false)
end
end
end
end
12 changes: 11 additions & 1 deletion spec/models/defense_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe "Defense", type: :model do
subject(:defense) { Defense.new(player: player, word: word, perfect_typing: perfect_typing) }

let(:game) { Game.create! }
let(:game) { Game.create!(state: 'running') }
let(:attacker) { Player.create!(game: game) }
let(:attacker_word) { Word.create!(value: 'HaCkeR', game: game) }
let(:perfect_typing) { '1' }
Expand Down Expand Up @@ -162,5 +162,15 @@
expect(defense.valid?).to eq(true)
end
end

context 'when game is finished' do
before :each do
game.update(state: 'finished')
end

it 'returns false' do
expect(defense.valid?).to eq(false)
end
end
end
end
22 changes: 21 additions & 1 deletion spec/requests/attacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:player) { game.players.create!(nickname: "Rico") }

before :each do
game.running!
allow_any_instance_of(ApplicationController).to receive(:current_player).and_return(player)
end

Expand Down Expand Up @@ -54,6 +55,7 @@

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

Expand All @@ -65,7 +67,25 @@
it 'broadcasts an error message' do
allow(ActionCable.server).to receive(:broadcast)
post "/attacks", params: { word: 'battletype' }
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"])
expect(ActionCable.server).to have_received(:broadcast).with(anything, code: 'failed_attack', player_id: player.id, word: 'battletype', error_codes: ["unique_case_insensitive_word"])
end
end

context 'when game is finished' do
before :each do
allow_words("finished")
game.update(state: 'finished')
end

it 'returns 200 HTTP status' do
post "/attacks", params: { word: 'finished' }
expect(response).to have_http_status(200)
end

it 'broadcasts an error message' do
allow(ActionCable.server).to receive(:broadcast)
post "/attacks", params: { word: 'finished' }
expect(ActionCable.server).to have_received(:broadcast).with(anything, code: 'failed_attack', word: 'finished', player_id: player.id, error_codes: ['not_running'] )
end
end
end
Expand Down
Loading