From b02694815dfbf25178fdae8da27b003c0072d9a1 Mon Sep 17 00:00:00 2001 From: F16Gaming Date: Tue, 14 Aug 2012 11:27:28 +0200 Subject: [PATCH] Added DuelManager. Provides AcceptDuel, DeclineDuel and StartDuelAdded DuelManager. --- Command.lua | 3 + CommandManager.lua | 57 ++++++++++++++- DuelManager.lua | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Events.lua | 5 ++ load.xml | 1 + locales/enUS.lua | 44 +++++++++++- locales/svSE.lua | 43 ++++++++++- 7 files changed, 350 insertions(+), 4 deletions(-) create mode 100644 DuelManager.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 . +--]] + +-- 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 @@