summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/character.rb6
-rw-r--r--app/models/hearth.rb1
-rw-r--r--app/models/location.rb1
-rw-r--r--app/models/monster_spawn.rb8
4 files changed, 16 insertions, 0 deletions
diff --git a/app/models/character.rb b/app/models/character.rb
index f6ddb92..b3d8c25 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -16,6 +16,7 @@ class Character < ApplicationRecord
has_many :states
has_many :chat_messages
has_many :monster_kills
+ has_many :monster_spawn_combats
has_many :bazaar_orders
validates :name, presence: true
# TODO: Make defaults better. This has to allow nil so the `attribute` default works, and I don't like that.
@@ -459,6 +460,11 @@ class Character < ApplicationRecord
base
end
+ def monster_spawns_attacked_in_past_24_hours
+ # TODO: Don't load into memory
+ monster_spawn_combats.where(created_at: 24.hours.ago..).map { |msc| msc.monster_spawn }.uniq
+ end
+
private
def create_skills
Skill.all.each { |skill| self.character_skills.create(skill: skill, xp: 0) }
diff --git a/app/models/hearth.rb b/app/models/hearth.rb
index ee00c0c..cbd98dc 100644
--- a/app/models/hearth.rb
+++ b/app/models/hearth.rb
@@ -1,5 +1,6 @@
class Hearth < ApplicationRecord
belongs_to :character
+ belongs_to :location
has_many :built_hearth_amenities
has_many :hearth_amenities, through: :built_hearth_amenities
has_many :hearth_plantings
diff --git a/app/models/location.rb b/app/models/location.rb
index 7bd1386..e0aaf9b 100644
--- a/app/models/location.rb
+++ b/app/models/location.rb
@@ -3,6 +3,7 @@ class Location < ApplicationRecord
has_many :activities
has_many :characters
+ has_many :hearths
has_many :monster_spawns
validates :gid, :name, presence: true
end
diff --git a/app/models/monster_spawn.rb b/app/models/monster_spawn.rb
index 5162cc6..42db98a 100644
--- a/app/models/monster_spawn.rb
+++ b/app/models/monster_spawn.rb
@@ -7,6 +7,7 @@ class MonsterSpawn < ApplicationRecord
after_create :send_chat_message
validates :starting_hp, presence: true, numericality: { greater_than_or_equal_to: 1, only_integer: true }
+ validate :one_living_leviathan_per_location, on: :create
def alive?
self.remaining_hp > 0
@@ -28,4 +29,11 @@ class MonsterSpawn < ApplicationRecord
ChatRoomChannel.broadcast_chat_message(chat_message)
end
end
+
+ def one_living_leviathan_per_location
+ # TODO: Don't load into memory
+ if location.monster_spawns.find { |ms| ms.alive? && ms != self }
+ errors.add(:chat, "A location can only have one monster spawn at a time.")
+ end
+ end
end