diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/character.rb | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/app/models/character.rb b/app/models/character.rb index a55230e..1d24f93 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -13,11 +13,22 @@ class Character < ApplicationRecord after_create :create_skills after_create { Hearth.create(character: self) } - def shift_item(gid, amount) + def shift_item(item, amount) + item = Item.find_by_gid(item) if item.is_a? String CharacterItem.transaction do - item = self.character_items.find_or_initialize_by(item: Item.find_by_gid(gid.to_s)) - item.increment(:quantity, amount) - item.save + ci = self.character_items.find_or_initialize_by(item: item) + ci.increment(:quantity, amount) + ci.save + end + end + + def pay_cost_for(activity) + CharacterItem.transaction do + if activity.whatnot[:cost] + activity.whatnot[:cost][:items]&.each do |item_gid, quantity| + self.shift_item(item_gid, -quantity) + end + end end end |