diff options
author | David Gay <david@davidgay.org> | 2021-06-06 19:06:46 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-06-06 19:06:46 -0400 |
commit | e37402ff309311a14d7dd666d0d8b29517504017 (patch) | |
tree | 3d6604805e9004bc0c37130f451376e79a68c989 /app/models | |
parent | 3622126380278d9bed8ea0e1e05a0bd1ea040596 (diff) |
Leviathans
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/location.rb | 1 | ||||
-rw-r--r-- | app/models/monster_spawn.rb | 35 | ||||
-rw-r--r-- | app/models/monster_spawn_combat.rb | 6 |
3 files changed, 42 insertions, 0 deletions
diff --git a/app/models/location.rb b/app/models/location.rb index b7c5bf5..e008270 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -2,5 +2,6 @@ class Location < ApplicationRecord include HasWhatnot has_many :activities + has_many :monster_spawns validates :gid, :name, presence: true end diff --git a/app/models/monster_spawn.rb b/app/models/monster_spawn.rb new file mode 100644 index 0000000..d34e390 --- /dev/null +++ b/app/models/monster_spawn.rb @@ -0,0 +1,35 @@ +class MonsterSpawn < ApplicationRecord + belongs_to :monster + belongs_to :location + has_many :monster_spawn_combats + + after_update :check_hp + after_create :send_chat_message + + def alive? + self.remaining_hp > 0 + end + + def remaining_hp + self.monster.max_hp - MonsterSpawnCombat.where(monster_spawn: self).sum(:hp_lost) + end + + private + def send_chat_message + chat_message = ChatMessage.new(body: "A leviathan has appeared in #{location.name}!", + chat_room: ChatRoom.find_by_gid("news")) + if chat_message.save + ChatRoomChannel.broadcast_chat_message(chat_message) + end + end + + def check_hp + if alive? + chat_message = ChatMessage.new(body: "The #{monster.name} in #{location.name} has been slain!", + chat_room: ChatRoom.find_by_gid("news")) + if chat_message.save + ChatRoomChannel.broadcast_chat_message(chat_message) + end + end + end +end diff --git a/app/models/monster_spawn_combat.rb b/app/models/monster_spawn_combat.rb new file mode 100644 index 0000000..50a565b --- /dev/null +++ b/app/models/monster_spawn_combat.rb @@ -0,0 +1,6 @@ +class MonsterSpawnCombat < ApplicationRecord + belongs_to :monster_spawn + belongs_to :character + + validates :hp_lost, numericality: { greater_than_or_equal_to: 0, only_integer: true } +end |