Quantcast

Added DuelManager.

F16Gaming [08-14-12 - 09:27]
Added DuelManager.

Provides AcceptDuel, DeclineDuel and StartDuelAdded DuelManager.
Filename
Command.lua
CommandManager.lua
DuelManager.lua
Events.lua
load.xml
locales/enUS.lua
locales/svSE.lua
diff --git a/Command.lua b/Command.lua
index 12bdde8..6ae5665 100644
--- a/Command.lua
+++ b/Command.lua
@@ -57,6 +57,7 @@ local AC
 local DM
 local SM
 local IM
+local CDM
 local log

 --- Initialize Command.
@@ -74,6 +75,7 @@ function C:Init()
 	DM = self.DeathManager
 	SM = self.SummonManager
 	IM = self.InviteManager
+	CDM = self.DuelManager
 	log = self.Logger
 	self:LoadSavedVars()
 	log:Normal(L("ADDON_LOAD"))
@@ -114,6 +116,7 @@ function C:LoadSavedVars()
 	DM:Init()
 	SM:Init()
 	IM:Init()
+	CDM:Init()
 	Cmd:Init()
 	log:SetDebug(self.Settings.DEBUG)
 	self.Global.VERSION = self.VarVersion
diff --git a/CommandManager.lua b/CommandManager.lua
index 8205742..8ce386f 100644
--- a/CommandManager.lua
+++ b/CommandManager.lua
@@ -69,6 +69,7 @@ local AM = C.AuthManager
 local DM = C.DeathManager
 local SM = C.SummonManager
 local IM = C.InviteManager
+local CDM = C.DuelManager
 local Chat
 local CES = C.Extensions.String
 local CET = C.Extensions.Table
@@ -246,7 +247,7 @@ CM:Register({"set", "s"}, PM.Access.Groups.Admin.Level, function(args, sender, i
 			return false, "CM_ERR_NOCMDCHAR"
 		end
 		return Chat:SetCmdChar(args[2])
-	elseif mod:match("^d") then -- DeathManager
+	elseif mod:match("^de") then -- DeathManager
 		if #args < 2 then
 			if C.DeathManager:IsEnabled() then
 				return "CM_SET_DM_ISENABLED"
@@ -370,6 +371,36 @@ CM:Register({"set", "s"}, PM.Access.Groups.Admin.Level, function(args, sender, i
 			return IM:ToggleGroup()
 		end
 		return false, "CM_SET_IM_USAGE"
+	elseif mod:match("^d") then -- DuelManager
+		if #args < 2 then
+			if CDM:IsEnabled() then
+				return "CM_SET_CDM_ISENABLED"
+			end
+			return "CM_SET_CDM_ISDISABLED"
+		end
+		if isChat then -- Players are only allowed to check status of DuelManager
+			return false, "CM_ERR_NOCHAT"
+		end
+		local setting = args[2]:lower()
+		if setting:match("^[eay].*a") then -- Enable Announce
+			return CDM:EnableAnnounce()
+		elseif setting:match("^[dn].*a") then -- Disable Announce
+			return CDM:DisableAnnounce()
+		elseif setting:match("^a.*t") then -- Toggle Announce
+			return CDM:ToggleAnnounce()
+		elseif setting:match("^s.*d") or setting:match("^de") then -- Set Delay
+			if #args < 3 then
+				return "CM_SET_CDM_DELAY_CURRENT", {CDM:GetDelay()}
+			end
+			local newDelay = tonumber(args[3])
+			if not newDelay then return false, "CM_SET_CDM_DELAY_USAGE" end
+			return CDM:SetDelay(newDelay)
+		elseif setting:match("^[eay]") then -- Enable
+			return CDM:Enable()
+		elseif setting:match("^[dn]") then -- Disable
+			return CDM:Disable()
+		end
+		return false, "CM_SET_CDM_USAGE"
 	end
 	return false, "CM_SET_USAGE"
 end, "CM_SET_HELP")
@@ -1034,6 +1065,30 @@ CM:Register({"declinesummon", "ds", "declinesumm", "dsumm", "cancelsummon", "csu
 	return SM:DeclineSummon()
 end, "CM_DECLINESUMMON_HELP")

+CM:Register({"acceptduel", "acceptd"}, PM.Access.Groups.User.Level, function(args, sender, isChat)
+	if not CDM:IsEnabled() then
+		return false, "CM_ERR_DISABLED"
+	end
+	return CDM:AcceptDuel()
+end, "CM_ACCEPTDUEL_HELP")
+
+CM:Register({"declineduel", "declined"}, PM.Access.Groups.User.Level, function(args, sender, isChat)
+	if not CDM:IsEnabled() then
+		return false, "CM_ERR_DISABLED"
+	end
+	return CDM:DeclineDuel()
+end, "CM_DECLINEDUEL_HELP")
+
+CM:Register({"startduel", "startd", "challenge"}, PM.Access.Groups.User.Level, function(args, sender, isChat)
+	if not CDM:IsEnabled() then
+		return false, "CM_ERR_DISABLED"
+	end
+	if #args < 1 then
+		return false, "CM_STARTDUEL_USAGE"
+	end
+	return CDM:Challenge(args[1])
+end, "CM_STARTDUEL_HELP")
+
 for i,v in ipairs(CM.Slash) do
 	_G["SLASH_" .. C.Name:upper() .. i] = "/" .. v
 end
diff --git a/DuelManager.lua b/DuelManager.lua
new file mode 100644
index 0000000..374c531
--- /dev/null
+++ b/DuelManager.lua
@@ -0,0 +1,201 @@
+--[[
+	* 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
+local ceil = math.ceil
+
+-- API Upvalues
+local StartDuel = StartDuel
+local AcceptDuel = AcceptDuel
+local CancelDuel = CancelDuel
+local CreateFrame = CreateFrame
+local StaticPopup_Hide = StaticPopup_Hide
+local StaticPopup_Visible = StaticPopup_Visible
+
+local C = Command
+
+C.DuelManager = {}
+
+local L = C.LocaleManager
+local DM = C.DuelManager
+local PM
+local CM
+
+local DEFAULT_DELAY = 5
+
+local MAX_DELAY = 50
+
+function DM:Init()
+	PM = C.PlayerManager
+	CM = C.ChatManager
+	self:LoadSavedVars()
+end
+
+function DM:LoadSavedVars()
+	if type(C.Global["DUEL_MANAGER"]) ~= "table" then
+		C.Global["DUEL_MANAGER"] = {}
+	end
+
+	self.Settings = C.Global["DUEL_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 DM:OnDuel(sender)
+	if self.Settings.DELAY > 0 then
+		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)
+				DM:Announce(self.Sender)
+			end
+		end)
+	else
+		self:Announce(sender)
+	end
+end
+
+function DM:Announce(sender)
+	if not self:HasDuel() then return end
+	local locale = PM:GetOrCreatePlayer(sender).Settings.locale
+	local msg = L(locale, "CDM_ANNOUNCE", true)
+	CM:SendMessage(msg, "WHISPER", sender)
+end
+
+function DM:AcceptDuel()
+	if not self:HasDuel() then
+		return false, "CDM_ERR_NODUEL"
+	end
+
+	AcceptDuel()
+
+	if StaticPopup_Visible("DUEL_REQUESTED") then
+		StaticPopup_Hide("DUEL_REQUESTED")
+	end
+
+	return "CDM_ACCEPTED"
+end
+
+function DM:DeclineDuel()
+	if not DM:HasDuel() then
+		return self:CancelDuel()
+	end
+
+	CancelDuel()
+
+	if StaticPopup_Visible("DUEL_REQUESTED") then
+		StaticPopup_Hide("DUEL_REQUESTED")
+	end
+
+	return "CDM_DECLINED"
+end
+
+function DM:CancelDuel()
+	CancelDuel()
+
+	return "CDM_CANCELLED"
+end
+
+function DM:Challenge(target)
+	StartDuel(target)
+
+	return "CDM_CHALLENGED", {target}
+end
+
+function DM:HasDuel()
+	return StaticPopup_Visible("DUEL_REQUESTED")
+end
+
+function DM:Enable()
+	self.Settings.ENABLED = true
+	return "CDM_ENABLED"
+end
+
+function DM:Disable()
+	self.Settings.ENABLED = false
+	return "CDM_DISABLED"
+end
+
+function DM:Toggle()
+	if self:IsEnabled() then
+		return self:Disable()
+	end
+	return self:Enable()
+end
+
+function DM:IsEnabled()
+	return self.Settings.ENABLED
+end
+
+function DM:EnableAnnounce()
+	self.Settings.ANNOUNCE = true
+	return "CDM_ANNOUNCE_ENABLED"
+end
+
+function DM:DisableAnnounce()
+	self.Settings.ANNOUNCE = false
+	return "CDM_ANNOUNCE_DISABLED"
+end
+
+function DM:ToggleAnnounce()
+	if self:IsAnnounceEnabled() then
+		return self:DisableAnnounce()
+	end
+	return self:EnableAnnounce()
+end
+
+function DM:IsAnnounceEnabled()
+	return self.Settings.ANNOUNCE
+end
+
+function DM:SetDelay(delay)
+	if type(delay) ~= "number" then
+		return false, "CDM_DELAY_NUM"
+	end
+	delay = ceil(delay)
+	if delay < 0 or delay > MAX_DELAY then
+		return false, "CDM_DELAY_OUTOFRANGE", {MAX_DELAY}
+	end
+	self.Settings.DELAY = delay
+	if self.Settings.DELAY > 0 then
+		return "CDM_DELAY_SET", {self.Settings.DELAY}
+	end
+	return "CDM_DELAY_DISABLED"
+end
+
+function DM:GetDelay()
+	return self.Settings.DELAY
+end
+
+function DM:DisableDelay()
+	return DM:SetDelay(0)
+end
diff --git a/Events.lua b/Events.lua
index 625071f..4bc3c08 100644
--- a/Events.lua
+++ b/Events.lua
@@ -36,6 +36,7 @@ local AC = C.AddonComm
 local DM = C.DeathManager
 local SM = C.SummonManager
 local IM = C.InviteManager
+local CDM = C.DuelManager

 --- Event handler for ADDON_LOADED
 -- @name Command.Events.ADDON_LOADED
@@ -155,3 +156,7 @@ end
 function C.Events.CONFIRM_SUMMON(self, ...)
 	SM:OnSummon()
 end
+
+function C.Events.DUEL_REQUESTED(self, ...)
+	CDM:OnDuel((select(1, ...)))
+end
diff --git a/load.xml b/load.xml
index e404cd7..e52cdbb 100644
--- a/load.xml
+++ b/load.xml
@@ -35,6 +35,7 @@
 	<Script file="DeathManager.lua" />
 	<Script file="SummonManager.lua" />
 	<Script file="InviteManager.lua" />
+	<Script file="DuelManager.lua" />
 	<Script file="CommandManager.lua" />
 	<Script file="ChatManager.lua" />
 	<Script file="Events.lua" />
diff --git a/locales/enUS.lua b/locales/enUS.lua
index 373aea6..fbb9852 100644
--- a/locales/enUS.lua
+++ b/locales/enUS.lua
@@ -116,10 +116,10 @@ local L = {
 	CM_VERSION = "%s",

 	CM_SET_HELP = "Control the settings of Command.",
-	CM_SET_USAGE = "Usage: set cmdchar|deathmanager|summonmanager|invitemanager",
+	CM_SET_USAGE = "Usage: set cmdchar|deathmanager|summonmanager|invitemanager|duelmanager",
 	CM_SET_DM_ISENABLED = "DeathManager is enabled.",
 	CM_SET_DM_ISDISABLED = "DeathManager is disabled.",
-	CM_SET_DM_USAGE = "Usage: set dm [enable|disable|toggle|enableress|disableress|toggleress|enablerel|disablerel|togglerel]",
+	CM_SET_DM_USAGE = "Usage: set deathmanager [enable|disable|toggle|enableress|disableress|toggleress|enablerel|disablerel|togglerel]",
 	CM_SET_SM_ISENABLED = "SummonManager is enabled.",
 	CM_SET_SM_ISDISABLED = "SummonManager is disabled.",
 	CM_SET_SM_DELAY_CURRENT = "The current delay for summon announcements is %s.",
@@ -132,6 +132,11 @@ local L = {
 	CM_SET_IM_GUILD_DELAY_CURRENT = "Guild announce delay is set to %d second(s).",
 	CM_SET_IM_GUILD_DELAY_USAGE = "Usage: set im guilddelay [delay]",
 	CM_SET_IM_USAGE = "Usage: set im [enable|disable|toggle|groupenable|groupdisable|grouptoggle|groupenableannounce|groupdisableannounce|grouptoggleannounce|groupdelay|groupdisabledelay|guildenable|guilddisable|guildtoggle|guildenableannounce|guilddisableannounce|guildtoggleannounce|guildenableoverride|guilddisableoverride|guildtoggleoverride|guilddelay|guilddisabledelay]",
+	CM_SET_CDM_ISENABLED = "DuelManager is enabled.",
+	CM_SET_CDM_ISDISABLED = "DuelManager is disabled.",
+	CM_SET_CDM_DELAY_CURRENT = "Announce delay is set to %d second(s).",
+	CM_SET_CDM_DELAY_USAGE = "Usage: set duelmanager delay [delay]",
+	CM_SET_CDM_USAGE = "Usage: set duelmanager [enable|disable|toggle|enableannounce|disableannounce|toggleannounce|delay]",

 	CM_LOCALE_HELP = "Change locale settings.",
 	CM_LOCALE_USAGE ="Usage: locale [set|reset|usemaster|playerindependent]",
@@ -291,6 +296,13 @@ local L = {

 	CM_DECLINESUMMON_HELP = "Player will decline a pending summon request.",

+	CM_ACCEPTDUEL_HELP = "Accepts a pending duel request.",
+
+	CM_DECLINEDUEL_HELP = "Declines a pending duel request or cancels an active duel.",
+
+	CM_STARTDUEL_HELP = "Challenges another player to a duel.",
+	CM_STARTDUEL_USAGE = "Usage: startduel <target>",
+
 	------------
 	-- Events --
 	------------
@@ -541,6 +553,34 @@ local L = {
 	IM_GUILD_ACCEPTED = "Accepted guild invite!",
 	IM_GUILD_DECLINED = "Declined guild invite.",

+	-----------------
+	-- DuelManager --
+	-----------------
+
+	CDM_ERR_NODUEL = "I do not currently have an active duel request.",
+
+	CDM_ANNOUNCE = "Type !acceptduel to make me accept the duel request or !decline duel to decline it!",
+
+	CDM_ACCEPETED = "Accepted duel request!",
+
+	CDM_DECLINED = "Declined duel request.",
+
+	CDM_CANCELLED = "Cancelled active duel (if any).",
+
+	CDM_CHALLENGED = "Sent a duel request to %s!",
+
+	CDM_ENABLED = "DuelManager has been enabled!",
+
+	CDM_DISABLED = "DuelManager has been disabled.",
+
+	CDM_ANNOUNCE_ENABLED = "DuelManager announce has been enabled!",
+
+	CDM_ANNOUNCE_DISABLED = "DuelManager announce has been disabled.",
+
+	CDM_DELAY_NUM = "Delay has to be a number.",
+	CDM_DELAY_OUTOFRANGE = "Delay has to be between 0 and %d seconds.",
+	CDM_DELAY_SET = "Announce delay set to %d second(s)!",
+	CDM_DELAY_DISABLED = "Announce delay has been disabled, will now announce immediately.",

 	-----------------
 	-- AuthManager --
diff --git a/locales/svSE.lua b/locales/svSE.lua
index 9f03e9f..850648e 100644
--- a/locales/svSE.lua
+++ b/locales/svSE.lua
@@ -116,7 +116,7 @@ local L = {
 	CM_VERSION = "%s",

 	CM_SET_HELP = "Ändra inställningarna i Command.",
-	CM_SET_USAGE = "Användning: set cmdchar|deathmanager|summonmanager|invitemanager",
+	CM_SET_USAGE = "Användning: set cmdchar|deathmanager|summonmanager|invitemanager|duelmanager",
 	CM_SET_DM_ISENABLED = "DeathManager is enabled.",
 	CM_SET_DM_ISDISABLED = "DeathManager is disabled.",
 	CM_SET_DM_USAGE = "Användning: set dm [enable|disable|toggle|enableress|disableress|toggleress|enablerel|disablerel|togglerel]",
@@ -132,6 +132,11 @@ local L = {
 	CM_SET_IM_GUILD_DELAY_CURRENT = "Guild announce delay is set to %d second(s).",
 	CM_SET_IM_GUILD_DELAY_USAGE = "Användning: set im guilddelay [delay]",
 	CM_SET_IM_USAGE = "Användning: set im [enable|disable|toggle|groupenable|groupdisable|grouptoggle|groupenableannounce|groupdisableannounce|grouptoggleannounce|groupdelay|groupdisabledelay|guildenable|guilddisable|guildtoggle|guildenableannounce|guilddisableannounce|guildtoggleannounce|guildenableoverride|guilddisableoverride|guildtoggleoverride|guilddelay|guilddisabledelay]",
+	CM_SET_CDM_ISENABLED = "DuelManager is enabled.",
+	CM_SET_CDM_ISDISABLED = "DuelManager is disabled.",
+	CM_SET_CDM_DELAY_CURRENT = "Announce delay is set to %d second(s).",
+	CM_SET_CDM_DELAY_USAGE = "Usage: set duelmanager delay [delay]",
+	CM_SET_CDM_USAGE = "Usage: set duelmanager [enable|disable|toggle|enableannounce|disableannounce|toggleannounce|delay]",

 	CM_LOCALE_HELP = "Change locale settings.",
 	CM_LOCALE_USAGE ="Användning: locale [set|reset|usemaster|playerindependent]",
@@ -291,6 +296,13 @@ local L = {

 	CM_DECLINESUMMON_HELP = "Player will decline a pending summon request.",

+	CM_ACCEPTDUEL_HELP = "Accepts a pending duel request.",
+
+	CM_DECLINEDUEL_HELP = "Declines a pending duel request or cancels an active duel.",
+
+	CM_STARTDUEL_HELP = "Challenges another player to a duel.",
+	CM_STARTDUEL_USAGE = "Usage: startduel <target>",
+
 	------------
 	-- Events --
 	------------
@@ -542,6 +554,35 @@ local L = {
 	IM_GUILD_DECLINED = "Declined guild invite.",

 	-----------------
+	-- DuelManager --
+	-----------------
+
+	CDM_ERR_NODUEL = "I do not currently have an active duel request.",
+
+	CDM_ANNOUNCE = "Type !acceptduel to make me accept the duel request or !decline duel to decline it!",
+
+	CDM_ACCEPETED = "Accepted duel request!",
+
+	CDM_DECLINED = "Declined duel request.",
+
+	CDM_CANCELLED = "Cancelled active duel (if any).",
+
+	CDM_CHALLENGED = "Sent a duel request to %s!",
+
+	CDM_ENABLED = "DuelManager has been enabled!",
+
+	CDM_DISABLED = "DuelManager has been disabled.",
+
+	CDM_ANNOUNCE_ENABLED = "DuelManager announce has been enabled!",
+
+	CDM_ANNOUNCE_DISABLED = "DuelManager announce has been disabled.",
+
+	CDM_DELAY_NUM = "Delay has to be a number.",
+	CDM_DELAY_OUTOFRANGE = "Delay has to be between 0 and %d seconds.",
+	CDM_DELAY_SET = "Announce delay set to %d second(s)!",
+	CDM_DELAY_DISABLED = "Announce delay has been disabled, will now announce immediately.",
+
+	-----------------
 	-- AuthManager --
 	-----------------