diff options
author | David Gay <david@davidgay.org> | 2021-06-07 20:25:56 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-06-07 20:25:56 -0400 |
commit | bafe2ca25dcb347bc00cbae5cb82d5658a92ec4f (patch) | |
tree | db260c9d503fe7071183ece7fdb7fdcef118fe9e /app | |
parent | 54473e0e8d2db24e4c1dd12aa309d610e62ed09d (diff) |
Record and reference MonsterSpawn starting HP in the database
Diffstat (limited to 'app')
-rw-r--r-- | app/models/monster_spawn.rb | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/app/models/monster_spawn.rb b/app/models/monster_spawn.rb index 4423557..5162cc6 100644 --- a/app/models/monster_spawn.rb +++ b/app/models/monster_spawn.rb @@ -3,17 +3,24 @@ class MonsterSpawn < ApplicationRecord belongs_to :location has_many :monster_spawn_combats + after_initialize :set_starting_hp after_create :send_chat_message + validates :starting_hp, presence: true, numericality: { greater_than_or_equal_to: 1, only_integer: true } + def alive? self.remaining_hp > 0 end def remaining_hp - self.monster.max_hp - MonsterSpawnCombat.where(monster_spawn: self).sum(:hp_lost) + self.starting_hp - MonsterSpawnCombat.where(monster_spawn: self).sum(:hp_lost) end private + def set_starting_hp + self.starting_hp = monster.max_hp + end + def send_chat_message chat_message = ChatMessage.new(body: "A leviathan has appeared in #{location.name}!", chat_room: ChatRoom.find_by_gid("news")) |