From 38f3a39221869483e3468e9f4d8cab5450a70f89 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sat, 22 May 2021 15:39:16 -0400 Subject: Equiping and unequiping items --- app/controllers/characters/items_controller.rb | 21 ++++++++++++- app/errors/equipment_error.rb | 2 ++ app/models/character.rb | 25 +++++++++++++++ app/models/equipment.rb | 11 +++++++ app/models/item.rb | 9 ++++++ app/views/characters/items/index.html.erb | 42 ++++++++++++++++++++------ 6 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 app/errors/equipment_error.rb create mode 100644 app/models/equipment.rb (limited to 'app') 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 @@ -

Inventory

+

Inventory

- +

Equipment

+ +
+ + + + + + + + + <% @character.equipment.each do |eq| %> + + + + + + <% end %> + +
SlotItemUnequip
<%= eq.slot.to_s %><%= eq.item.name %> + <%= button_to "Unequip", character_item_unequip_path(slot: eq.slot ) %> +
+ + + +

Inventory

+ + - - <% @character_items.ordered_by_item_name.each do |ci| %> + <% @character.character_items.ordered_by_item_name.each do |ci| %> - <% end %> -- cgit v1.2.3
Amount Item Equip UseDestroy
<%= ci.quantity %> <%= ci.item.name %> - <% 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 %> @@ -25,10 +51,6 @@ <%= link_to "[Use]", "#" %> <% end %> - <%= link_to "[Destroy]", "#" %> - <%= link_to "[Destroy All]", "#" %> -