summaryrefslogtreecommitdiff
path: root/app/models/state.rb
blob: 29b3f4249404a89ebe8b66efbd39477776b9ec45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class State < ApplicationRecord
  belongs_to :character
  belongs_to :condition

  after_create :overwrite_old_states

  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 overwrite_old_states
      # Don't allow duplicate conditions; just destroy the old one.
      character.states.where(condition: condition).where.not(id: id).destroy_all

      # Only allow one food condition. Destroy the old one.
      if condition.tags.include?("food")
        character.states.each do |state|
          state.destroy if state.id != id && state.condition.tags.include?("food")
        end
      end
    end
end