summaryrefslogtreecommitdiff
path: root/app/models/state.rb
blob: 52767e71e66604a9b8930538464347f5fe632211 (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
32
33
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 and one drink condition. Destroy any old ones.
      %w[food drink].each do |tag|
        if condition.tags.include?(tag)
          character.states.each do |state|
            state.destroy if state.id != id && state.condition.tags.include?(tag)
          end
        end
      end
    end
end