Quantcast

Update DeathManager to support delays.

F16Gaming [09-13-12 - 23:05]
Update DeathManager to support delays.
Filename
DeathManager.lua
diff --git a/DeathManager.lua b/DeathManager.lua
index 7c899d4..70ccc8f 100644
--- a/DeathManager.lua
+++ b/DeathManager.lua
@@ -26,6 +26,7 @@ local type = type

 -- API Upvalues
 local RepopMe = RepopMe
+local CreateFrame = CreateFrame
 local HasSoulstone = HasSoulstone
 local UseSoulstone = UseSoulstone
 local AcceptResurrect = AcceptResurrect
@@ -40,6 +41,15 @@ C.DeathManager = {}
 local L = C.LocaleManager
 local DM = C.DeathManager
 local CM
+local CEN = C.Extensions.Number
+
+local DEFAULT_RELEASE_DELAY = 5
+local DEFAULT_RESURRECT_DELAY = 5
+local MAX_RELEASE_DELAY = 60
+local MAX_RESURRECT_DELAY = 55
+
+local DeathAnnouncePending = false
+local RessAnnouncePending = false

 local SelfRessType = {
 	None = 0,
@@ -64,14 +74,70 @@ function DM:LoadSavedVars()
 	if type(self.Settings.RELEASE_ENABLED) ~= "boolean" then
 		self.Settings.RELEASE_ENABLED = true
 	end
+	if type(self.Settings.RELEASE_ANNOUNCE) ~= "boolean" then
+		self.Settings.RELEASE_ANNOUNCE = true
+	end
+	if type(self.Settings.RELEASE_DELAY) ~= "number" then
+		self.Settings.RELEASE_DELAY = DEFAULT_RELEASE_DELAY
+	end
 	if type(self.Settings.RESURRECT_ENABLED) ~= "boolean" then
 		self.Settings.RESURRECT_ENABLED = true
 	end
+	if type(self.Settings.RESURRECT_ANNOUNCE) ~= "boolean" then
+		self.Settings.RESURRECT_ANNOUNCE = true
+	end
+	if type(self.Settings.RESURRECT_DELAY) ~= "number" then
+		self.Settings.RESURRECT_DELAY = DEFAULT_RESURRECT_DELAY
+	end
 end

 function DM:OnDeath()
-	if not self.Settings.ENABLED then return end
+	if not self.Settings.ENABLED or not self.Settings.RELEASE_ENABLED or not self.Settings.RELEASE_ANNOUNCE or DeathAnnouncePending then return end
 	self.Dead = true
+	if self.Settings.RELEASE_DELAY > 0 then
+		DeathAnnouncePending = true
+		local frame = CreateFrame("Frame")
+		frame.Time = 0
+		frame.Delay = self.Settings.RELEASE_DELAY
+		frame:SetScript("OnUpdate", function(self, elapsed)
+			self.Time = self.Time + elapsed
+			if self.Time >= self.Delay then
+				self:SetScript("OnUpdate", nil)
+				self.Time = 0
+				DeathAnnouncePending = false
+				DM:AnnounceDeath()
+			end
+		end)
+	else
+		self:AnnounceDeath()
+	end
+end
+
+function DM:OnResurrect(resser)
+	if not self.Settings.ENABLED or not self.Settings.RESURRECT_ENABLED or not self.Settings.RESURRECT_ANNOUNCE or RessAnnouncePending then return end
+	self.Resurrection = true
+	if self.Settings.RESURRECT_DELAY > 0 then
+		RessAnnouncePending = true
+		local frame = CreateFrame("Frame")
+		frame.Time = 0
+		frame.Delay = self.Settings.RESURRECT_DELAY
+		frame.Resser = resser
+		frame:SetScript("OnUpdate", function(self, elapsed)
+			self.Time = self.Time + elapsed
+			if self.Time >= self.Delay then
+				self:SetScript("OnUpdate", nil)
+				self.Time = 0
+				RessAnnouncePending = false
+				DM:AnnounceResurrect(self.Resser)
+			end
+		end)
+	else
+		self:AnnounceResurrect(resser)
+	end
+end
+
+function DM:AnnounceDeath()
+	if not self.Dead then return end
 	local t = self:HasSelfRess()
 	if t == SelfRessType.SoulStone then
 		CM:SendMessage(L("DM_ONDEATH_SOULSTONE"), "SMART")
@@ -84,9 +150,8 @@ function DM:OnDeath()
 	end
 end

-function DM:OnResurrect(resser)
-	if not self.Settings.ENABLED or not self.Settings.RESURRECT_ENABLED then return end
-	self.Resurrection = true
+function DM:AnnounceResurrect(resser)
+	if not self.Resurrection then return end
 	CM:SendMessage(L("DM_ONRESS"):format(resser), "SMART")
 end

@@ -199,6 +264,44 @@ function DM:ToggleRelease()
 	return self:EnableRelease()
 end

+function DM:IsReleaseAnnounceEnabled()
+	return self.Settings.RELEASE_ANNOUNCE
+end
+
+function DM:EnableReleaseAnnounce()
+	self.Settings.RELEASE_ANNOUNCE = true
+	return "DM_RELEASE_ANNOUNCE_ENABLED"
+end
+
+function DM:DisableReleaseAnnounce()
+	self.Settings.RELEASE_ANNOUNE = false
+	return "DM_RELEASE_ANNOUNCE_DISABLED"
+end
+
+function DM:ToggleReleaseAnnounce()
+	if self:IsReleaseAnnounceEnabled() then
+		return self:DisableReleaseAnnounce()
+	end
+	return self:EnableReleaseAnnounce()
+end
+
+function DM:GetReleaseDelay()
+	return self.Settings.RELEASE_DELAY
+end
+
+function DM:SetReleaseDelay(amount)
+	if amount < 0 then
+		amount = 0
+	elseif amount > MAX_RELEASE_DELAY then
+		amount = MAX_RELEASE_DELAY
+	end
+	self.Settings.RELEASE_DELAY = amount
+	if amount > 0 then
+		return "DM_RELEASE_SETDELAY_SUCCESS", {CEN:FormatSeconds(amount)}
+	end
+	return "DM_RELEASE_SETDELAY_INSTANT"
+end
+
 function DM:IsResurrectEnabled()
 	return self.Settings.RESURRECT_ENABLED
 end
@@ -219,3 +322,41 @@ function DM:ToggleResurrect()
 	end
 	return self:EnableResurrect()
 end
+
+function DM:IsResurrectAnnounceEnabled()
+	return self.Settings.RESURRECT_ANNOUNCE
+end
+
+function DM:EnableResurrectAnnounce()
+	self.Settings.RESURRECT_ANNOUNCE = true
+	return "DM_RESURRECT_ANNOUNCE_ENABLED"
+end
+
+function DM:DisableResurrectAnnounce()
+	self.Settings.RESURRECT_ANNOUNE = false
+	return "DM_RESURRECT_ANNOUNCE_DISABLED"
+end
+
+function DM:ToggleResurrectAnnounce()
+	if self:IsResurrectAnnounceEnabled() then
+		return self:DisableResurrectAnnounce()
+	end
+	return self:EnableResurrectAnnounce()
+end
+
+function DM:GetResurrectDelay()
+	return self.Settings.RESURRECT_DELAY
+end
+
+function DM:SetResurrectDelay(amount)
+	if amount < 0 then
+		amount = 0
+	elseif amount > MAX_RESURRECT_DELAY then
+		amount = MAX_RESURRECT_DELAY
+	end
+	self.Settings.RESURRECT_DELAY = amount
+	if amount > 0 then
+		return "DM_RESURRECT_SETDELAY_SUCCESS", {CEN:FormatSeconds(amount)}
+	end
+	return "DM_RESURRECT_SETDELAY_INSTANT"
+end