diff options
author | David Gay <david@davidgay.org> | 2021-05-19 19:04:13 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-05-19 19:04:13 -0400 |
commit | 045867220ea68b912f74b74a1476ac08bd19dbfd (patch) | |
tree | 302e9740a54ad0b9147a6995db8ebd3409e2b9b3 /app/models | |
parent | 9fec79398a34d26be1042e35cae429b88f8b96d0 (diff) |
More work on getting amenity construction working
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 |