Quantcast

Added ReadyCheckManager (revamped version of ReadyCheck command).

F16Gaming [09-01-12 - 14:35]
Added ReadyCheckManager (revamped version of ReadyCheck command).
Filename
CommandManager.lua
Events.lua
ReadyCheckManager.lua
load.xml
locales/enUS.lua
locales/svSE.lua
diff --git a/CommandManager.lua b/CommandManager.lua
index b187cd2..7ca5765 100644
--- a/CommandManager.lua
+++ b/CommandManager.lua
@@ -886,40 +886,17 @@ CM:Register({"readycheck", "rc"}, PM.Access.Groups.User.Level, function(args, se
 	if #args <= 0 then
 		if PM:GetAccess(sender) > PM.Access.Groups.Op.Level then
 			return "CM_ERR_NOACCESS", {sender.Info.Name, PM.Access.Groups.Op.Level, PM:GetAccess(sender)}
-		elseif GT:IsGroupLeader() or GT:IsRaidLeaderOrAssistant() then
-			C.Data.ReadyCheckRunning = true
-			local name = tostring(sender.Info.Name)
-			DoReadyCheck()
-			return "CM_READYCHECK_ISSUED", {name}
 		else
-			return false, "CM_READYCHECK_NOPRIV"
+			return RCM:Start(sender.Info.Name)
 		end
 	end
-	local status = GetReadyCheckStatus("player")
-	if (status ~= "waiting" and status ~= nil) or GetReadyCheckTimeLeft() <= 0 or not C.Data.ReadyCheckRunning then
-		return false, "CM_READYCHECK_INACTIVE"
-	end
 	local arg = tostring(args[1]):lower()
-	if arg:match("^[ay]") then -- Accept
-		C.Data.ReadyCheckRunning = false
-		if ReadyCheckFrameYesButton then
-			ReadyCheckFrameYesButton:Click()
-		end
-		ConfirmReadyCheck(true)
-		status = GetReadyCheckStatus("player")
-		return "CM_READYCHECK_ACCEPTED"
+	if arg:match("^[ayr]") then -- Accept / Ready
+		return RCM:Accept()
 	elseif arg:match("^[dn]") then -- Decline
-		C.Data.ReadyCheckRunning = false
-		if ReadyCheckFrameNoButton then
-			ReadyCheckFrameNoButton:Click()
-		end
-		ConfirmReadyCheck(false)
-		status = GetReadyCheckStatus("player")
-		return "CM_READYCHECK_DECLINED"
-	else
-		return false, "CM_READYCHECK_INVALID", {tostring(arg)}
+		return RCM:Decline()
 	end
-	return false, "CM_READYCHECK_FAIL"
+	return false, "CM_READYCHECK_USAGE"
 end, "CM_READYCHECK_HELP")

 CM:Register({"loot", "l"}, PM.Access.Groups.Op.Level, function(args, sender, isChat, bnetInfo)
diff --git a/Events.lua b/Events.lua
index 9d22211..94610f3 100644
--- a/Events.lua
+++ b/Events.lua
@@ -38,6 +38,7 @@ local SM = C.SummonManager
 local IM = C.InviteManager
 local RM = C.RoleManager
 local CDM = C.DuelManager
+local RCM = C.ReadyCheckManager

 --- Event handler for ADDON_LOADED
 -- @name Command.Events.ADDON_LOADED
@@ -83,15 +84,11 @@ function C.Events.LFG_PROPOSAL_FAILED(self, ...)
 end

 function C.Events.READY_CHECK(self, ...)
-	if C.Data.ReadyCheckRunning then return end
-	local name = tostring(select(1, ...))
-	if name == UnitName("player") then return end
-	C.Data.ReadyCheckRunning = true
-	CM:SendMessage(L("E_READYCHECK"):format(name), "SMART")
+	RCM:OnReadyCheck(tostring(select(1, ...)))
 end

 function C.Events.READY_CHECK_FINISHED(self, ...)
-	C.Data.ReadyCheckRunning = false
+	RCM:OnReadyCheckEnd()
 end

 function C.Events.GROUP_ROSTER_UPDATE(self, ...)
diff --git a/ReadyCheckManager.lua b/ReadyCheckManager.lua
new file mode 100644
index 0000000..1773ba9
--- /dev/null
+++ b/ReadyCheckManager.lua
@@ -0,0 +1,203 @@
+--[[
+	* Copyright (c) 2011-2012 by Adam Hellberg.
+	*
+	* This file is part of Command.
+	*
+	* Command is free software: you can redistribute it and/or modify
+	* it under the terms of the GNU General Public License as published by
+	* the Free Software Foundation, either version 3 of the License, or
+	* (at your option) any later version.
+	*
+	* Command is distributed in the hope that it will be useful,
+	* but WITHOUT ANY WARRANTY; without even the implied warranty of
+	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	* GNU General Public License for more details.
+	*
+	* You should have received a copy of the GNU General Public License
+	* along with Command. If not, see <http://www.gnu.org/licenses/>.
+--]]
+
+-- Upvalues
+local type = type
+
+-- API Upvalues
+local UnitName = UnitName
+local CreateFrame = CreateFrame
+local DoReadyCheck = DoReadyCheck
+local ConfirmReadyCheck = ConfirmReadyCheck
+local GetReadyCheckStatus = GetReadyCheckStatus
+local GetReadyCheckTimeLeft = GetReadyCheckTimeLeft
+
+local C = Command
+
+C.ReadyCheckManager = {}
+
+local L = C.LocaleManager
+local RCM = C.ReadyCheckManager
+
+local MAX_DELAY = 55
+local DEFAULT_DELAY = 5
+
+local AnnouncePending = false
+
+function RCM:Init()
+	self:LoadSavedVars()
+end
+
+function RCM:LoadSavedVars()
+	if type(C.Global["READYCHECK_MANAGER"]) ~= "table" then
+		C.Global["READYCHECK_MANAGER"] = {}
+	end
+
+	self.Settings = C.Global["READYCHECK_MANAGER"]
+
+	if type(self.Settings.ENABLED) ~= "boolean" then
+		self.Settings.ENABLED = true
+	end
+
+	if type(self.Settings.ANNOUNCE) ~= "boolean" then
+		self.Settings.ANNOUNCE = true
+	end
+
+	if type(self.Settings.DELAY) ~= "number" then
+		self.Settings.DELAY = DEFAULT_DELAY
+	end
+end
+
+function RCM:OnReadyCheck(sender)
+	if not self.Settings.ANNOUNCE or AnnouncePending or not self.Settings.ENABLED then return end
+	if sender == UnitName("player") then return end
+	self.Active = true
+	if self.Settings.DELAY > 0 then
+		AnnouncePending = true
+		local frame = CreateFrame("Frame")
+		frame.Time = 0
+		frame.Delay = self.Settings.DELAY
+		frame.Sender = sender
+		frame:SetScript("OnUpdate", function(self, elapsed)
+			self.Time = self.Time + elapsed
+			if self.Time >= self.Delay then
+				self:SetScript("OnUpdate", nil)
+				AnnouncePending = false
+				RCM:Announce(self.Sender)
+			end
+		end)
+	else
+		self:Announce(sender)
+	end
+end
+
+function RCM:OnReadyCheckEnd()
+	self.Active = false
+end
+
+function RCM:Announce(sender)
+	if not self:ReadyCheckPending() then return end
+	CM:SendMessage(L("RCM_ANNOUNCE"):format(sender), "SMART")
+end
+
+function RCM:ReadyCheckPending()
+	return GetReadyCheckTimeLeft() > 0 or (ReadyCheckFrame and ReadyCheckFrame:IsShown())
+end
+
+function RCM:HasResponded()
+	local status = GetReadyCheckStatus("player")
+	return status ~= "waiting" and status ~= nil
+end
+
+function RCM:Accept()
+	if not self:ReadyCheckPending() then
+		return false, "RCM_INACTIVE"
+	elseif self:HasResponded() then
+		return false, "RCM_RESPONDED"
+	end
+	ConfirmReadyCheck(true)
+	self:HidePopup()
+	return "RCM_ACCEPTED"
+end
+
+function RCM:Decline()
+	if not self:ReadyCheckPending() then
+		return false, "RCM_INACTIVE"
+	elseif self:HasResponded() then
+		return false, "RCM_RESPONDED"
+	end
+	ConfirmReadyCheck(false)
+	self:HidePopup()
+	return "RCM_DECLINED"
+end
+
+function RCM:Start(sender)
+	if GT:IsGroupLeader() or GT:IsRaidLeaderOrAssistant() then
+		DoReadyCheck()
+		return "RCM_START_ISSUED", {sender}
+	end
+	return false, "RCM_START_NOPRIV"
+end
+
+function RCM:HidePopup()
+	if ReadyCheckFrame and ReadyCheckFrame:IsShown() then
+		ReadyCheckFrame:Hide()
+	end
+end
+
+function RCM:Enable()
+	self.Settings.ENABLED = true
+	return "RCM_ENABLED"
+end
+
+function RCM:Disable()
+	self.Settings.ENABLED = false
+	-- We have to reset the active state in case this was called during a ready check
+	self.Active = false
+	return "RCM_DISABLED"
+end
+
+function RCM:Toggle()
+	if self:IsEnabled() then
+		return self:Disable()
+	end
+	return self:Enable()
+end
+
+function RCM:IsEnabled()
+	return self.Settings.ENABLED
+end
+
+function RCM:EnableAnnounce()
+	self.Settings.ANNOUNCE = true
+	return "RCM_ANNOUNCE_ENABLED"
+end
+
+function RCM:DisableAnnounce()
+	self.Settings.ANNOUNCE = false
+	return "RCM_ANNOUNCE_DISABLED"
+end
+
+function RCM:ToggleAnnounce()
+	if self:IsAnnounceEnabled() then
+		return self:DisableAnnounce()
+	end
+	return self:EnableAnnounce()
+end
+
+function RCM:IsAnnounceEnabled()
+	return self.Settings.ANNOUNCE
+end
+
+function RCM:SetDelay(amount)
+	if amount < 0 then
+		amount = 0
+	elseif amount > MAX_DELAY then
+		amount = MAX_DELAY
+	end
+	self.Settings.DELAY = amount
+	if amount > 0 then
+		return "RCM_SETDELAY_SUCCESS", {self:GetDelay()}
+	end
+	return "RCM_SETDELAY_INSTANT"
+end
+
+function RCM:GetDelay()
+	return self.Settings.DELAY
+end
diff --git a/load.xml b/load.xml
index 4a6775c..9726f0a 100644
--- a/load.xml
+++ b/load.xml
@@ -38,6 +38,7 @@
 	<Script file="InviteManager.lua" />
 	<Script file="DuelManager.lua" />
 	<Script file="RoleManager.lua" />
+	<Script file="ReadyCheckManager.lua" />
 	<Script file="CommandManager.lua" />
 	<Script file="ChatManager.lua" />
 	<Script file="Events.lua" />
diff --git a/locales/enUS.lua b/locales/enUS.lua
index e2d27e6..bf601d1 100644
--- a/locales/enUS.lua
+++ b/locales/enUS.lua
@@ -262,13 +262,7 @@ local L = {
 	CM_TOGGLEDEBUG_HELP = "Toggle debugging mode on and off.",

 	CM_READYCHECK_HELP = "Respond to ready check or initiate a new one.",
-	CM_READYCHECK_ISSUED = "%s issued a ready check!",
-	CM_READYCHECK_NOPRIV = "Cannot initiate ready check when not leader or assistant.",
-	CM_READYCHECK_INACTIVE = "Ready check not running or I have already responded.",
-	CM_READYCHECK_ACCEPTED = "Accepted ready check.",
-	CM_READYCHECK_DECLINED = "Declined ready check.",
-	CM_READYCHECK_INVALID = "Invalid argument: %s",
-	CM_READYCHECK_FAIL = "Failed to accept or decline ready check.",
+	CM_READYCHECK_USAGE = "Usage: rc [accept|decline]",

 	CM_LOOT_HELP = "Provides various loot functions.",
 	CM_LOOT_USAGE = "Usage: loot type||threshold||master||pass",
@@ -627,6 +621,29 @@ local L = {
 	CRM_STARTDELAY_SUCCESS = "RoleManager announce delay set to %s!",
 	CRM_STARTDELAY_INSTANT = "RoleManager now announces instantly.",

+	-----------------------
+	-- ReadyCheckManager --
+	-----------------------
+	RCM_INACTIVE = "No ready check is currently running.",
+	RCM_RESPONDED = "I have already responded to the ready check.",
+
+	RCM_ENABLED = "ReadyCheckManager has been enabled.",
+	RCM_DISABLED = "ReadyCheckManager has been disabled.",
+
+	RCM_ANNOUNCE = "%s has started a ready check! Type !rc accept to make me accept or !rc deny to deny it.",
+
+	RCM_ACCEPTED = "Accepted the ready check!",
+	RCM_DECLINED = "Declined the ready check!",
+
+	RCM_START_ISSUED = "%s has started a ready check!",
+	RCM_START_NOPRIV = "Unable to start ready check, not leader or assistant.",
+
+	RCM_ANNOUNCE_ENABLED = "ReadyCheckManager will now announce received ready checks.",
+	RCM_ANNOUNCE_DISABLED = "ReadyCheckManager will no longer announce recevied ready checks.",
+
+	RCM_SETDELAY_SUCCESS = "ReadyCheckManager announce delay set to %d second(s)!",
+	RCM_SETDELAY_INSTANT = "ReadyCheckManager will now announce instantly!",
+
 	-----------------
 	-- AuthManager --
 	-----------------
diff --git a/locales/svSE.lua b/locales/svSE.lua
index ad663be..1527aaa 100644
--- a/locales/svSE.lua
+++ b/locales/svSE.lua
@@ -262,13 +262,7 @@ local L = {
 	CM_TOGGLEDEBUG_HELP = "Toggle debugging mode on and off.",

 	CM_READYCHECK_HELP = "Respond to ready check or initiate a new one.",
-	CM_READYCHECK_ISSUED = "%s issued a ready check!",
-	CM_READYCHECK_NOPRIV = "Cannot initiate ready check when not leader or assistant.",
-	CM_READYCHECK_INACTIVE = "Ready check not running or I have already responded.",
-	CM_READYCHECK_ACCEPTED = "Accepted ready check.",
-	CM_READYCHECK_DECLINED = "Declined ready check.",
-	CM_READYCHECK_INVALID = "Invalid argument: %s",
-	CM_READYCHECK_FAIL = "Failed to accept or decline ready check.",
+	CM_READYCHECK_USAGE = "Usage: rc [accept|decline]",

 	CM_LOOT_HELP = "Provides various loot functions.",
 	CM_LOOT_USAGE = "Användning: loot type||threshold||master||pass",
@@ -627,6 +621,29 @@ local L = {
 	CRM_STARTDELAY_SUCCESS = "RoleManager announce delay set to %s!",
 	CRM_STARTDELAY_INSTANT = "RoleManager now announces instantly.",

+	-----------------------
+	-- ReadyCheckManager --
+	-----------------------
+	RCM_INACTIVE = "No ready check is currently running.",
+	RCM_RESPONDED = "I have already responded to the ready check.",
+
+	RCM_ENABLED = "ReadyCheckManager has been enabled.",
+	RCM_DISABLED = "ReadyCheckManager has been disabled.",
+
+	RCM_ANNOUNCE = "%s has started a ready check! Type !rc accept to make me accept or !rc deny to deny it.",
+
+	RCM_ACCEPTED = "Accepted the ready check!",
+	RCM_DECLINED = "Declined the ready check!",
+
+	RCM_START_ISSUED = "%s has started a ready check!",
+	RCM_START_NOPRIV = "Unable to start ready check, not leader or assistant.",
+
+	RCM_ANNOUNCE_ENABLED = "ReadyCheckManager will now announce received ready checks.",
+	RCM_ANNOUNCE_DISABLED = "ReadyCheckManager will no longer announce recevied ready checks.",
+
+	RCM_SETDELAY_SUCCESS = "ReadyCheckManager announce delay set to %d second(s)!",
+	RCM_SETDELAY_INSTANT = "ReadyCheckManager will now announce instantly!",
+
 	-----------------
 	-- AuthManager --
 	-----------------