class Characters::ItemsController < ApplicationController def index @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 def use @item = Item.find(params[:item_id]) unless @item.usable? flash[:alert] = "You can't use #{@item.name}." redirect_to character_items_path(current_char) and return end unless current_char.character_items.exists?(item: @item) flash[:alert] = "You don't have #{@item.name}." redirect_to character_items_path(current_char) and return end @item.whatnot[:use_effects]&.each do |effect| case effect[:type] when "change_wounds" # This is basically duplicated in HearthAmenityController Character.transaction do wounds_change = [effect[:value], -current_char.wounds].max current_char.shift_item(@item, -1) current_char.wounds = current_char.wounds + wounds_change current_char.save! flash[:notice] = "#{effect[:message]}" heal_or_gain = wounds_change.positive? ? "gain" : "heal" flash[:notice] += " You #{heal_or_gain} #{wounds_change.abs} wound(s)." end when "activity" start_activity(Activity.find_by_gid(effect[:gid])) and return else raise "Invalid use effect type string (#{effect[:type]})" end redirect_to character_items_path(current_char) end end end