summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/location.rb1
-rw-r--r--app/models/monster_spawn.rb35
-rw-r--r--app/models/monster_spawn_combat.rb6
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