diff options
author | David Gay <david@davidgay.org> | 2021-05-23 20:15:03 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-05-23 20:15:03 -0400 |
commit | 82eec1f68172857b3b39e6ab3e3b837de16c1198 (patch) | |
tree | a1edb49f55ad01c3acaf41070bf09f207fcfaf22 /app/controllers/characters | |
parent | 6bf84733db22f6c5d5105acc3565b075fcc65917 (diff) |
Item usage
Diffstat (limited to 'app/controllers/characters')
-rw-r--r-- | app/controllers/characters/items_controller.rb | 29 |
1 files changed, 29 insertions, 0 deletions
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 |