From 82eec1f68172857b3b39e6ab3e3b837de16c1198 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sun, 23 May 2021 20:15:03 -0400 Subject: Item usage --- app/controllers/characters/items_controller.rb | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'app/controllers') diff --git a/app/controllers/characters/items_controller.rb b/app/controllers/characters/items_controller.rb index 8df23c8..4d47ebb 100644 --- a/app/controllers/characters/items_controller.rb +++ b/app/controllers/characters/items_controller.rb @@ -21,4 +21,33 @@ class Characters::ItemsController < ApplicationController 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" + 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 + else + raise "Invalid use effect type string (#{effect[:type]})" + end + redirect_to character_items_path(current_char) + end + end end -- cgit v1.2.3