From 045867220ea68b912f74b74a1476ac08bd19dbfd Mon Sep 17 00:00:00 2001 From: David Gay Date: Wed, 19 May 2021 19:04:13 -0400 Subject: More work on getting amenity construction working --- app/models/character.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'app/models') 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 -- cgit v1.2.3