From 2cb92046ebe697d6e17c78e968ec1bcf528487f0 Mon Sep 17 00:00:00 2001 From: David Gay Date: Thu, 3 Jun 2021 19:55:55 -0400 Subject: Only allow one food condition per character --- app/models/concerns/has_whatnot.rb | 6 +++++- app/models/state.rb | 14 +++++++++++--- data/conditions.yml | 4 ++++ data/hearth_amenities.yml | 2 +- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/models/concerns/has_whatnot.rb b/app/models/concerns/has_whatnot.rb index 289fe40..a0934ef 100644 --- a/app/models/concerns/has_whatnot.rb +++ b/app/models/concerns/has_whatnot.rb @@ -7,7 +7,11 @@ module HasWhatnot end def tags - whatnot[:tags] if whatnot + whatnot[:tags] ? whatnot[:tags] : [] + end + + def where_has_tag(tag) + where("whatnot->'tags' ? :tag", tag: tag) end end end diff --git a/app/models/state.rb b/app/models/state.rb index 27c8741..29b3f42 100644 --- a/app/models/state.rb +++ b/app/models/state.rb @@ -2,7 +2,7 @@ class State < ApplicationRecord belongs_to :character belongs_to :condition - after_create :destroy_duplicates + after_create :overwrite_old_states def expired? self.expires_at < Time.now @@ -17,7 +17,15 @@ class State < ApplicationRecord end private - def destroy_duplicates - self.character.states.where(condition: self.condition).where.not(id: self.id).destroy_all + 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 diff --git a/data/conditions.yml b/data/conditions.yml index 42dc03a..d7c9aef 100644 --- a/data/conditions.yml +++ b/data/conditions.yml @@ -43,6 +43,8 @@ mudtub_mash: name: "mudtub mash" description: "Incrases max HP by 1." whatnot: + tags: + - "food" effects: - type: "stat_change" gid: "max_hp" @@ -51,6 +53,8 @@ midoras_mudtub_mash: name: "midoras mudtub mash" description: "Incrases max HP and accuracy by 1." whatnot: + tags: + - "food" effects: - type: "stat_change" gid: "max_hp" diff --git a/data/hearth_amenities.yml b/data/hearth_amenities.yml index e53e583..a2e2e89 100644 --- a/data/hearth_amenities.yml +++ b/data/hearth_amenities.yml @@ -72,7 +72,7 @@ loamspire: gid: "planting_spots" modifier: 5 spicebench: - name: "spicebench" + name: "Spicebench" description: >- An array of tools, containers, and shelves for practicing spicework. Also includes a small stone oven. whatnot: -- cgit v1.2.3