summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-29 17:19:56 -0400
committerDavid Gay <david@davidgay.org>2021-05-29 17:19:56 -0400
commitaac7563767c5fbc5ef67f4d615833e7523a46df7 (patch)
tree807e708c32d6d8710bcc1b6f96862ed34b320072 /app/models
parent8f9dcbf33d5cd3222e4d8e0cfa6f72b1596b917c (diff)
Conditions and states (boons & banes), with `quarrying_draught`
Diffstat (limited to 'app/models')
-rw-r--r--app/models/character.rb9
-rw-r--r--app/models/condition.rb5
-rw-r--r--app/models/state.rb23
3 files changed, 36 insertions, 1 deletions
diff --git a/app/models/character.rb b/app/models/character.rb
index a5c871b..7e80d65 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -10,6 +10,8 @@ class Character < ApplicationRecord
has_many :learned_activities
has_many :items, through: :character_items
has_many :character_skills
+ has_many :conditions, through: :states
+ has_many :states
has_many :chat_messages
has_many :bazaar_orders
validates :name, presence: true
@@ -91,6 +93,10 @@ class Character < ApplicationRecord
end
end
+ def active_states
+ self.states.all.select { |s| !s.expired? }
+ end
+
def pay_cost_for(activity)
CharacterItem.transaction do
activity.whatnot[:cost]&.each do |cost|
@@ -230,7 +236,8 @@ class Character < ApplicationRecord
# TODO: Review this filter_map to see if it can be simplified
hearth_amenity_effects = self.hearth.built_hearth_amenities.filter_map { |a| a.effects if a.effects }
equipment_effects = self.equipment.filter_map { |a| a.effects if a.effects }
- (hearth_amenity_effects + equipment_effects).flatten
+ state_effects = self.states.filter_map { |a| a.effects if a.effects && !a.expired? }
+ (hearth_amenity_effects + equipment_effects + state_effects).flatten
end
def total_stat_change(gid)
diff --git a/app/models/condition.rb b/app/models/condition.rb
new file mode 100644
index 0000000..f2b048f
--- /dev/null
+++ b/app/models/condition.rb
@@ -0,0 +1,5 @@
+class Condition < ApplicationRecord
+ include HasWhatnot
+
+ validates :gid, :name, :description, presence: true
+end
diff --git a/app/models/state.rb b/app/models/state.rb
new file mode 100644
index 0000000..27c8741
--- /dev/null
+++ b/app/models/state.rb
@@ -0,0 +1,23 @@
+class State < ApplicationRecord
+ belongs_to :character
+ belongs_to :condition
+
+ after_create :destroy_duplicates
+
+ def expired?
+ self.expires_at < Time.now
+ end
+
+ def remaining_duration
+ (self.expires_at - Time.now).to_i
+ end
+
+ def effects
+ self.condition.whatnot[:effects]
+ end
+
+ private
+ def destroy_duplicates
+ self.character.states.where(condition: self.condition).where.not(id: self.id).destroy_all
+ end
+end