summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-23 20:15:03 -0400
committerDavid Gay <david@davidgay.org>2021-05-23 20:15:03 -0400
commit82eec1f68172857b3b39e6ab3e3b837de16c1198 (patch)
treea1edb49f55ad01c3acaf41070bf09f207fcfaf22 /app
parent6bf84733db22f6c5d5105acc3565b075fcc65917 (diff)
Item usage
Diffstat (limited to 'app')
-rw-r--r--app/controllers/characters/items_controller.rb29
-rw-r--r--app/models/character.rb2
-rw-r--r--app/models/item.rb5
-rw-r--r--app/views/characters/items/index.html.erb2
4 files changed, 35 insertions, 3 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
diff --git a/app/models/character.rb b/app/models/character.rb
index a6e8ff6..ef8768e 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -145,7 +145,7 @@ class Character < ApplicationRecord
def equipment_stats
stats = {}
self.equipment.each do |eq|
- eq.item.whatnot[:effects]&.each do |effect|
+ eq.item.whatnot[:equip_effects]&.each do |effect|
if effect[:type] == "stat"
stats[effect[:gid]] ||= 0
stats[effect[:gid]] += effect[:modifier]
diff --git a/app/models/item.rb b/app/models/item.rb
index 5e60f04..1ef5d7d 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -4,12 +4,15 @@ class Item < ApplicationRecord
enum equip_slot: [:mainhand, :offhand, :head, :neck, :back, :torso, :grip,
: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 usable?
+ self.whatnot && self.whatnot[:use_effects]&.any?
+ end
+
def equip_slots
return [] unless self.equipment?
self.whatnot[:equip_slots].map { |data| data.to_sym }
diff --git a/app/views/characters/items/index.html.erb b/app/views/characters/items/index.html.erb
index a7c932d..20b43c0 100644
--- a/app/views/characters/items/index.html.erb
+++ b/app/views/characters/items/index.html.erb
@@ -48,7 +48,7 @@
</td>
<td class="table-cell-padded">
<% if ci.item.usable? %>
- <%= link_to "[Use]", "#" %>
+ <%= button_to "Use", character_item_use_path(item_id: ci.item.id) %>
<% end %>
</td>
</tr>