From aba212e13062c43a1192ef885b05145ac1cc9fe7 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sun, 23 May 2021 17:06:34 -0400 Subject: Bazaar with buy orders, sell orders, and order cancellation --- app/models/character.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'app/models/character.rb') diff --git a/app/models/character.rb b/app/models/character.rb index bad2bb2..003f8af 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -11,6 +11,7 @@ class Character < ApplicationRecord has_many :items, through: :character_items has_many :character_skills has_many :chat_messages + has_many :bazaar_orders validates :name, presence: true validates_length_of :name, maximum: 15, message: "can't be longer than 15 characters" validates_uniqueness_of :name, message: "is already being used" @@ -35,12 +36,17 @@ class Character < ApplicationRecord def wildscour_level; skill_level("wildscour"); end def worldcall_level; skill_level("worldcall"); end + def vestige + vestige = self.character_items.find_by(item: Item.find_by_gid("vestige")) + vestige ? vestige.quantity : 0 + end + def shift_item(item, amount) item = Item.find_by_gid(item) if item.is_a? String CharacterItem.transaction do ci = self.character_items.find_or_initialize_by(item: item) - ci.increment(:quantity, amount) - ci.save + ci.increment!(:quantity, amount) + ci.save! end end @@ -71,7 +77,7 @@ class Character < ApplicationRecord open_slots = self.open_slots_for(item) raise EquipmentError unless open_slots.any? self.shift_item(item, -1) - self.equipment.create(item: item, slot: open_slots.first) + self.equipment.create!(item: item, slot: open_slots.first) end end @@ -80,7 +86,7 @@ class Character < ApplicationRecord equipment = self.equipment.find_by(slot: slot) raise EquipmentError unless equipment item = equipment.item - equipment.destroy + equipment.destroy! self.shift_item(item, 1) end end -- cgit v1.2.3