summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/characters/items_controller.rb21
-rw-r--r--app/errors/equipment_error.rb2
-rw-r--r--app/models/character.rb25
-rw-r--r--app/models/equipment.rb11
-rw-r--r--app/models/item.rb9
-rw-r--r--app/views/characters/items/index.html.erb42
6 files changed, 99 insertions, 11 deletions
diff --git a/app/controllers/characters/items_controller.rb b/app/controllers/characters/items_controller.rb
index e1a30f8..8df23c8 100644
--- a/app/controllers/characters/items_controller.rb
+++ b/app/controllers/characters/items_controller.rb
@@ -1,5 +1,24 @@
class Characters::ItemsController < ApplicationController
def index
- @character_items = Character.find(params[:character_id]).character_items
+ @character = Character.find(params[:character_id])
+ end
+
+ def equip
+ @item = Item.find(params[:item_id])
+ current_char.equip(@item)
+ flash[:notice] = "Equipped #{@item.name}."
+ rescue EquipmentError
+ flash[:alert] = "Couldn't equip #{@item.name}."
+ ensure
+ redirect_to character_items_path(current_char)
+ end
+
+ def unequip
+ current_char.unequip(params[:slot].to_sym)
+ flash[:notice] = "Unequipped item."
+ rescue EquipmentError
+ flash[:alert] = "Couldn't unequip item."
+ ensure
+ redirect_to character_items_path(current_char)
end
end
diff --git a/app/errors/equipment_error.rb b/app/errors/equipment_error.rb
new file mode 100644
index 0000000..0d24e8a
--- /dev/null
+++ b/app/errors/equipment_error.rb
@@ -0,0 +1,2 @@
+class EquipmentError < StandardError
+end
diff --git a/app/models/character.rb b/app/models/character.rb
index 73d2c4b..59ed5ae 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -5,6 +5,7 @@ class Character < ApplicationRecord
has_many :titles, through: :title_awards
belongs_to :active_title, class_name: "Title", optional: true
has_one :hearth
+ has_many :equipment
has_many :character_items
has_many :learned_activities
has_many :items, through: :character_items
@@ -44,6 +45,30 @@ class Character < ApplicationRecord
ci && ci.quantity >= quantity
end
+ def open_slots_for(item)
+ full_slots = self.equipment.map { |e| e.slot }
+ item.equip_slots.reject { |slot| full_slots.include?(slot) }
+ end
+
+ def equip(item)
+ Character.transaction do
+ open_slots = self.open_slots_for(item)
+ raise EquipmentError unless open_slots.any?
+ self.shift_item(item, -1)
+ self.equipment.create(item: item, slot: open_slots.first)
+ end
+ end
+
+ def unequip(slot)
+ Character.transaction do
+ equipment = self.equipment.find_by(slot: slot)
+ raise EquipmentError unless equipment
+ item = equipment.item
+ equipment.destroy
+ self.shift_item(item, 1)
+ end
+ end
+
def add_skill_xp(skill, amount)
CharacterSkill.find_by(skill: skill).increment!(:xp, amount)
end
diff --git a/app/models/equipment.rb b/app/models/equipment.rb
new file mode 100644
index 0000000..11030fd
--- /dev/null
+++ b/app/models/equipment.rb
@@ -0,0 +1,11 @@
+class Equipment < ApplicationRecord
+ belongs_to :character
+ belongs_to :item
+ enum slot: [:mainhand, :offhand, :head, :neck, :back, :torso, :grip,
+ :left_ring, :right_ring, :waist, :legs, :feet, :curio]
+ validates :slot, presence: true, uniqueness: { scope: :character }
+
+ def slot
+ self[:slot].to_sym
+ end
+end
diff --git a/app/models/item.rb b/app/models/item.rb
index d42460b..5e60f04 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -5,4 +5,13 @@ class Item < ApplicationRecord
:left_ring, :right_ring, :waist, :legs, :feet, :curio]
validates :gid, :name, :description, presence: true
validates :usable, inclusion: { in: [true, false] }
+
+ def equipment?
+ self.whatnot && self.whatnot[:equip_slots]&.any?
+ end
+
+ def equip_slots
+ return [] unless self.equipment?
+ self.whatnot[:equip_slots].map { |data| data.to_sym }
+ end
end
diff --git a/app/views/characters/items/index.html.erb b/app/views/characters/items/index.html.erb
index e313f7e..a7c932d 100644
--- a/app/views/characters/items/index.html.erb
+++ b/app/views/characters/items/index.html.erb
@@ -1,23 +1,49 @@
-<h1 class="text-xl">Inventory</h1>
+<h1 class="text-3xl mb-4">Inventory</h1>
-<table class="table-auto">
+<h2 class="text-xl mb-4">Equipment</h2>
+
+<table class="table-auto mb-8">
+ <thead>
+ <tr>
+ <th class="table-header-padded">Slot</th>
+ <th class="table-header-padded">Item</th>
+ <th class="table-header-padded">Unequip</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @character.equipment.each do |eq| %>
+ <tr>
+ <td class="table-cell-padded"><%= eq.slot.to_s %></td>
+ <td class="table-cell-padded"><%= eq.item.name %></td>
+ <td class="table-cell-padded">
+ <%= button_to "Unequip", character_item_unequip_path(slot: eq.slot ) %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+
+
+
+<h2 class="text-xl mb-4">Inventory</h2>
+
+<table class="table-auto mb-8">
<thead>
<tr>
<th class="table-header-padded">Amount</th>
<th class="table-header-padded">Item</th>
<th class="table-header-padded">Equip</th>
<th class="table-header-padded">Use</th>
- <th class="table-header-padded">Destroy</th>
</tr>
</thead>
<tbody>
- <% @character_items.ordered_by_item_name.each do |ci| %>
+ <% @character.character_items.ordered_by_item_name.each do |ci| %>
<tr>
<td class="table-cell-padded text-right"><%= ci.quantity %></td>
<td class="table-cell-padded"><%= ci.item.name %></td>
<td class="table-cell-padded">
- <% if ci.item.equip_slot %>
- <%= link_to "[Equip]", "#" %>
+ <% if ci.item.equipment? %>
+ <%= button_to "Equip", character_item_equip_path(item_id: ci.item.id) %>
<% end %>
</td>
<td class="table-cell-padded">
@@ -25,10 +51,6 @@
<%= link_to "[Use]", "#" %>
<% end %>
</td>
- <td class="table-cell-padded">
- <%= link_to "[Destroy]", "#" %>
- <%= link_to "[Destroy All]", "#" %>
- </td>
</tr>
<% end %>
</tbody>