From 8f1396b6e0b3ad080aab27fe5baee898ecbcb055 Mon Sep 17 00:00:00 2001 From: F16Gaming Date: Fri, 10 Feb 2012 18:47:48 +0100 Subject: [PATCH] Implemented addon communication. Checks for newer versions. Prevents two AddOn instances in the same group/guild from handling a command twice. --- AddonComm.lua | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ChatManager.lua | 14 ++++++++ Command.lua | 14 ++++++++ CommandManager.lua | 15 ++++---- Events_Chat.lua | 9 +++++ GroupTools.lua | 2 +- RollManager.lua | 11 ++++++ load.xml | 1 + 8 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 AddonComm.lua diff --git a/AddonComm.lua b/AddonComm.lua new file mode 100644 index 0000000..74d64a3 --- /dev/null +++ b/AddonComm.lua @@ -0,0 +1,101 @@ +--[[ + * Copyright (c) 2011 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 . +--]] + +local C = Command + +local GT = C.GroupTools +local CES = C.Extensions.String +local CET = C.Extensions.Table + +local log = C.Logger + +C.AddonComm = { + GroupMaster = true, -- Whether or not we are currently the "master" instance in group + GuildMaster = true, -- Whether or not we are currently the "master" instance in guild + Type = { + VersionUpdate = "COMM_VU", + HandleCommand = "COMM_DO" + }, + Format = { + VersionUpdate = "%s", + HandleCommand = "%s;&%s;&%s" + }, + Last = { + Sender = nil, + Message = nil, + Channel = nil + } +} + +local AC = C.AddonComm + +function AC:Init() + --self:LoadSavedVars() + for _,v in pairs(self.Type) do + RegisterAddonMessagePrefix(v) + end +end + +function AC:LoadSavedVars() + +end + +function AC:Receive(msgType, msg, channel, sender) + if msgType == self.Type.VersionUpdate then + local t = CES:Split(msg, ":") + if #t < 1 then return end + local ver = tonumber(t[1]) + if type(t[1]) ~= "number" then return end + C:CheckVersion(ver) + elseif msgType == self.Type.HandleCommand then + local t = CES:Split(msg, ";&") + if #t < 3 then return end + local name = tostring(t[1]) + local sent = tostring(t[2]) + local chan = tostring(t[3]) + if type(t[1]) ~= "string" or type(t[2]) ~= "string" or type(t[3]) ~= "string" then return end + self.Last.Sender = name + self.Last.Message = sent + self.Last.Channel = chan + end +end + +function AC:Send(msgType, msg, channel, target) + channel = channel or "RAID" + if not CET:HasValue(self.Type, msgType) then + error("Invalid Message Type specified: " .. tostring(msgType)) + return + end + SendAddonMessage(msgType, msg, channel, target) +end + +function AC:Handled(msg, sender, channel) + if channel == "WHISPER" then return end + if self:IsHandled(msg, sender, channel) then return false end + self:Send(self.Type.HandleCommand, self.Format.HandleCommand:format(sender, msg, channel), channel) + return true +end + +function AC:IsHandled(msg, sender, channel) + if channel == "WHISPER" then return false end + if tostring(msg) == self.Last.Message and tostring(sender) == self.Last.Sender and tostring(channel) == self.Last.Channel then + return true + end + return false +end diff --git a/ChatManager.lua b/ChatManager.lua index 11950c0..e4a006c 100644 --- a/ChatManager.lua +++ b/ChatManager.lua @@ -41,6 +41,7 @@ C.ChatManager = { local CM = C.ChatManager local PM = C.PlayerManager local GT = C.GroupTools +local AC = C.AddonComm local CCM = C.CommandManager local CES = C.Extensions.String @@ -167,6 +168,7 @@ function CM:HandleMessage(msg, sender, channel, target, isBN) C.Logger:Normal("Battle.Net convos/whispers are not supported yet") return end + local raw = msg msg = CES:Trim(msg) local args = self:ParseMessage(msg) if not self:IsCommand(args[1]) then return end @@ -174,12 +176,24 @@ function CM:HandleMessage(msg, sender, channel, target, isBN) self.LastTarget = target local cmd = self:ParseCommand(args[1]) if not CCM:HasCommand(cmd) then return end + if channel ~= "WHISPER" and not AC:Handled(raw, sender, channel) then + C.Logger:Normal("Request already handled by another instance of Command, aborting...") + return + end local t = {} if #args > 1 then for i=2,#args do table.insert(t, args[i]) end end + + --[[ + if channel ~= "WHISPER" and AC:IsHandled(msg, sender) then + C.Logger:Normal("Request already handled by other instance of Command, aborting...") + return + end + ]] + local player = PM:GetOrCreatePlayer(sender) local result, err = CCM:HandleCommand(cmd, t, true, player) if result then diff --git a/Command.lua b/Command.lua index 2df62f7..90208fb 100644 --- a/Command.lua +++ b/Command.lua @@ -32,6 +32,8 @@ Command = { Name = "Command", Version = GetAddOnMetadata("Command", "Version"), + VersionNum = 1, -- Increment on every release + VersionChecked = false, -- Prevent spam of "New Version" notice Loaded = false, VarVersion = 2, Global = {}, @@ -45,6 +47,7 @@ local Cmd local CM local PM local RM +local AC local log --- Initialize Command. @@ -57,6 +60,7 @@ function C:Init() CM = self.ChatManager PM = self.PlayerManager RM = self.RollManager + AC = self.AddonComm log = self.Logger self:LoadSavedVars() log:Normal("AddOn loaded! Use /cmd help or !help for help. !!NYI!!") @@ -90,11 +94,21 @@ function C:LoadSavedVars() CM:Init() PM:Init() RM:Init() + AC:Init() Cmd:Init() log:SetDebug(self.Settings.DEBUG) self.Global.VERSION = self.VarVersion end +function C:CheckVersion(ver) + if self.VersionChecked then return end + ver = ver or 0 + if ver > self.VersionNum then + log:Normal("A new version of " .. self.Name .. " is available! Check the site you downloaded from for the updated version.") + self.VersionChecked = true + end +end + --- Control AddOn state. -- @param enabled Boolean indicating enabled or disabled state. -- diff --git a/CommandManager.lua b/CommandManager.lua index 28a47b6..b99678e 100644 --- a/CommandManager.lua +++ b/CommandManager.lua @@ -34,7 +34,6 @@ C.CommandManager = { Commands = {} } - local CM = C.CommandManager local PM = C.PlayerManager local QM = C.QueueManager @@ -110,7 +109,9 @@ end function CM:GetCommands(all) -- NOTE: Only returns the NAMES, not the actual command function local t = {} for k,v in pairs(self.Commands) do - table.insert(t, k) + if k ~= "__DEFAULT__" or all then + table.insert(t, k) + end if all and #v.Alias > 0 then for _,a in pairs(v.Alias) do table.insert(t, a) @@ -155,15 +156,15 @@ CM:Register({"__DEFAULT__", "help", "h"}, PM.Access.Local, function(args, sender return "End of help message." end, "Prints this help message.") -CM:Register({"commands", "cmds", "cmdlist", "listcmds", "listcommands", "commandlist", PM.Access.Groups.User.Level, function(args, sender, isChat) +CM:Register({"commands", "cmds", "cmdlist", "listcmds", "listcommands", "commandlist"}, PM.Access.Groups.User.Level, function(args, sender, isChat) local all if #args > 0 then all = args[1] == "all" end - local cmds = self:GetCommands(all) + local cmds = CM:GetCommands(all) local msg = "" for _,v in pairs(cmds) do - msg = msg .. ", " .. v + msg = msg .. v .. ", " end return CES:Cut(msg, 240) end, "Print all registered commands.") @@ -529,6 +530,8 @@ CM:Register({"roll", "r"}, PM.Access.Groups.Op.Level, function(args, sender, isC return RM:SetMin(tonumber(args[3])) elseif args[2] == "max" then return RM:SetMax(tonumber(args[3])) + elseif args[2] == "time" then + return RM:SetTime(tonumber(args[3])) else return false, "Usage: roll set " end @@ -550,7 +553,7 @@ SlashCmdList[C.Name:upper()] = function(msg, editBox) table.insert(t, args[i]) end end - local result, err = CM:HandleCommand(cmd, t, false, nil) + local result, err = CM:HandleCommand(cmd, t, false, PM:GetOrCreatePlayer(UnitName("player"))) if result then C.Logger:Normal(tostring(result)) else diff --git a/Events_Chat.lua b/Events_Chat.lua index 25ac1aa..ae33714 100644 --- a/Events_Chat.lua +++ b/Events_Chat.lua @@ -19,6 +19,7 @@ local C = Command local CM = C.ChatManager +local AC = C.AddonComm function C.Events.CHAT_MSG_SYSTEM(self, event, ...) if C.RollManager.Running then @@ -26,6 +27,14 @@ function C.Events.CHAT_MSG_SYSTEM(self, event, ...) end end +function C.Events.CHAT_MSG_ADDON(self, event, ...) + local msgType = (select(1, ...)) + local msg = (select(2, ...)) + local channel = (select(3, ...)) + local sender = (select(4, ...)) + AC:Receive(msgType, msg, channel, sender) +end + --[[ function C.Events.CHAT_MSG_BATTLEGROUND(self, event, ...) local chan = CM:GetRespondChannelByEvent(event) diff --git a/GroupTools.lua b/GroupTools.lua index dd8da07..9181e9e 100644 --- a/GroupTools.lua +++ b/GroupTools.lua @@ -82,7 +82,7 @@ function GT:IsGroupFull() local num = 0 local max = self.RaidMax if self:IsRaid() then - num = GetNumRaidMembers() + 1 + num = GetNumRaidMembers() elseif self:IsGroup() then num = GetNumPartyMembers() + 1 max = self.PartyMax diff --git a/RollManager.lua b/RollManager.lua index a7bccc0..338f001 100644 --- a/RollManager.lua +++ b/RollManager.lua @@ -108,6 +108,17 @@ function RM:SetMax(amount) return "Sucessfully set maximum roll number to " .. amount .. "!" end +function RM:SetTime(amount) + if type(amount) ~= "number" then + return false, "Invalid amount passed: " .. tostring(amount) + end + if amount <= 0 then + return false, "Amount must be larger than zero (0)." + end + self.Settings.DEFAULT_TIME = amount + return "Successfully set default roll time to " .. amount .. "!" +end + function RM:StartRoll(sender, item, time) time = tonumber(time) or self.Settings.DEFAULT_TIME RollTimer.Time = time diff --git a/load.xml b/load.xml index d2d81c6..cada1d3 100644 --- a/load.xml +++ b/load.xml @@ -27,6 +27,7 @@