class Characters::ItemsController < ApplicationController before_action :set_character, only: :index def index end def equip @item = Item.find(params[:item_id]) unless current_char.can_equip? @item flash[:alert] = "Couldn't equip #{@item.name}" flash[:alert] += " (requires #{@item.equip_requirements&.join(", ")})" if @item.equip_requirements.any? return end current_char.equip(@item) flash[:notice] = "Equipped #{@item.name}." rescue EquipmentError flash[:alert] = "Couldn't equip #{@item.name}. Make sure the slot is free." 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 when "condition" condition = Condition.find_by_gid(effect[:gid]) Character.transaction do current_char.shift_item(@item, -1) current_char.states.create!(condition: condition, expires_at: Time.now + effect[:duration]) flash[:notice] = "#{effect[:message]}" flash[:notice] += " You gain the #{condition.name} condition." end else raise "Invalid use effect type string (#{effect[:type]})" end redirect_to character_items_path(current_char) end end private def set_character @character = Character.find(params[:character_id]) unless current_char == @character flash[:alert] = "You can only look at your own items." redirect_to character_path(@character) end end end