Skip to content

Commit

Permalink
Add items and display them in the surroundings.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristandunn committed Sep 5, 2024
1 parent 668b48f commit efc0002
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 5 deletions.
11 changes: 11 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Item < ApplicationRecord
MINIMUM_NAME_LENGTH = 3
MAXIMUM_NAME_LENGTH = 24

belongs_to :owner, polymorphic: true, optional: true

validates :name, presence: true,
length: { in: MINIMUM_NAME_LENGTH..MAXIMUM_NAME_LENGTH }
end
3 changes: 2 additions & 1 deletion app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class Room < ApplicationRecord
DEFAULT_COORDINATES = { x: 0, y: 0, z: 0 }.freeze

has_many :characters, dependent: :restrict_with_exception
has_many :spawns, dependent: :destroy
has_many :items, foreign_key: :owner_id, inverse_of: :owner, dependent: :destroy
has_many :monsters, dependent: :destroy
has_many :spawns, dependent: :destroy

validates :description, presence: true
validates :x, numericality: { only_integer: true },
Expand Down
6 changes: 6 additions & 0 deletions app/views/game/_surroundings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<% end %>
</ol>

<ol id="surrounding-items">
<% room.items.sort_by(&:name).each do |item| %>
<%= render "game/surroundings/item", item: item %>
<% end %>
</ol>

<ol id="surrounding-characters">
<% room.characters.active.sort_by(&:name).each do |character| %>
<%= render "game/surroundings/character", character: character %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/game/surroundings/_item.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%# locals: (item:) %>
<li id="surrounding_item_<%= item.id %>">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=">
<%= item.name %>
</li>
13 changes: 13 additions & 0 deletions db/migrate/20240825015139_create_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateItems < ActiveRecord::Migration[7.2]
def change
create_table :items do |t|
t.references :owner, null: false, polymorphic: true

t.string :name, null: false, limit: 24

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
DESCRIPTION
}
)

Item.find_or_create_by(owner: room).tap do |item|
item.update(name: "Empty Jug")
end
end

Room.find_or_initialize_by(x: -1, y: 0, z: -1).tap do |room|
Expand Down
11 changes: 11 additions & 0 deletions spec/factories/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

FactoryBot.define do
factory :item do
name

trait :room do
owner { association(:room) }
end
end
end
8 changes: 8 additions & 0 deletions spec/features/characters/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
expect(page).to have_surrounding_monster(monster)
end

it "displays items in surroundings", js: false do
item = create(:item, owner: character.room)

click_on character.name

expect(page).to have_surrounding_item(item)
end

it "broadcasts an enter message to the room" do
using_session(:nearby_character) do
sign_in_as_character create(:character, room: character.room)
Expand Down
22 changes: 22 additions & 0 deletions spec/features/commands/move_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@
expect(page).not_to have_surrounding_monster(north_monster)
end

it "adds new surrounding items to the sender" do
north_room = create(:room, x: 0, y: 1, z: 0)
north_item = create(:item, owner: north_room)

send_command(:move, :north)

expect(page).to have_surrounding_item(north_item)
end

it "removes old surrounding items from the sender" do
north_room = create(:room, x: 0, y: 1, z: 0)
north_item = create(:item, owner: north_room)

send_command(:move, :north)

wait_for(have_surrounding_item(north_item)) do
send_command(:move, :south)
end

expect(page).not_to have_surrounding_item(north_item)
end

it "broadcasts the exit message to the source room" do
create(:room, x: 0, y: 1, z: 0)

Expand Down
23 changes: 23 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

describe Item do
describe "associations" do
subject(:item) { create(:item, :room) }

it { is_expected.to belong_to(:owner).optional(true) }
end

describe "validations" do
subject(:item) { create(:item, :room) }

it { is_expected.to validate_presence_of(:name) }

it do
expect(item).to validate_length_of(:name)
.is_at_least(described_class::MINIMUM_NAME_LENGTH)
.is_at_most(described_class::MAXIMUM_NAME_LENGTH)
end
end
end
13 changes: 10 additions & 3 deletions spec/models/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
describe "associations" do
subject(:room) { build(:room) }

it { is_expected.to have_many(:monsters).dependent(:destroy) }
it { is_expected.to have_many(:spawns).dependent(:destroy) }

it "has many :characters dependent: :restrict_with_exception" do
expect(room).to have_many(:characters).dependent(:restrict_with_exception)
end

it "has many :items inverse_of: owner, dependent: destroy" do
expect(room).to have_many(:items)
.with_foreign_key(:owner_id)
.inverse_of(:owner)
.dependent(:destroy)
end
end

describe "constants" do
Expand All @@ -20,9 +30,6 @@
describe "validations" do
subject(:room) { build(:room) }

it { is_expected.to have_many(:monsters).dependent(:destroy) }
it { is_expected.to have_many(:spawns).dependent(:destroy) }

it { is_expected.to validate_presence_of(:description) }

it { is_expected.to validate_numericality_of(:x).only_integer }
Expand Down
4 changes: 4 additions & 0 deletions spec/support/helpers/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def have_surrounding_character(character)
have_css("#surrounding-characters li", text: character.name)
end

def have_surrounding_item(item)
have_css("#surrounding-items li", text: item.name)
end

def have_surrounding_monster(monster)
have_css("#surrounding-monsters li", text: monster.name)
end
Expand Down
23 changes: 23 additions & 0 deletions spec/views/game/_surroundings.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@
end
end

context "with items" do
let(:room) { create(:room) }

it "renders the surrounding items ordered by name" do
item_first = create(:item, owner: room, name: "Dagger")
item_second = create(:item, owner: room, name: "Knife")

expect(html).to have_css(
"#surrounding-items #surrounding_item_#{item_first.id}"
).and(
have_css(
"#surrounding-items #surrounding_item_#{item_second.id}"
)
).and(
have_css(
"#surrounding-items " \
"#surrounding_item_#{item_first.id} + " \
"#surrounding_item_#{item_second.id}"
)
)
end
end

context "with monsters" do
let(:monster_first) { create(:monster, room: room, name: "Blob") }
let(:monster_second) { create(:monster, room: room, name: "Rat") }
Expand Down
18 changes: 18 additions & 0 deletions spec/views/game/surroundings/_item.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "rails_helper"

describe "game/surroundings/_item.html.erb" do
subject(:html) do
render template: "game/surroundings/_item",
locals: { item: item }

rendered
end

let(:item) { build_stubbed(:item) }

it "renders the surrounding item" do
expect(html).to have_css("#surrounding_item_#{item.id}", text: item.name)
end
end

0 comments on commit efc0002

Please sign in to comment.