From 4491f8c709785d4a9f3868457ffb481dc217f07a Mon Sep 17 00:00:00 2001 From: Anonomit Date: Fri, 3 Feb 2023 19:34:05 -0500 Subject: [PATCH] Support external listeners --- Comm.lua | 1 + LootReserve.lua | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ Server.lua | 2 ++ 3 files changed, 68 insertions(+) diff --git a/Comm.lua b/Comm.lua index 5789d17..67a7ded 100644 --- a/Comm.lua +++ b/Comm.lua @@ -232,6 +232,7 @@ end -- SessionInfo function LootReserve.Comm:BroadcastSessionInfo(starting) + LootReserve:NotifyListeners("RESERVES"); local session = LootReserve.Server.CurrentSession; if session.Settings.Blind then for player in pairs(session.Members) do diff --git a/LootReserve.lua b/LootReserve.lua index 9ba2915..1c72579 100644 --- a/LootReserve.lua +++ b/LootReserve.lua @@ -45,6 +45,9 @@ LootReserveGlobalSave = }; LootReserve.BagCache = nil; +LootReserve.Listeners = { + RESERVES = { }, +}; StaticPopupDialogs["LOOTRESERVE_GENERIC_ERROR"] = { @@ -138,6 +141,68 @@ end function LootReserve:OnDisable() end +-- Other addons may use this to be notified of things that happen in LootReserve. +-- Return value is boolean, whether registration succeeded. +function LootReserve:RegisterListener(category, id, callback) + local success, result = pcall(function() + if not category or not self.Listeners[category] then return false; end + self.Listeners[category][id] = callback; + return true; + end); + return result or false; +end + +function LootReserve:UnregisterListener(category, id) + local success, result = pcall(function() + if not category or not self.Listeners[category] then return false; end + self.Listeners[category][id] = nil; + return true; + end); + return result or false; +end + +-- Other addons may use this to request data manually. +-- Return value is boolean, whether the notification was successful. +function LootReserve:PromptListener(category, id) + if id then + return self:NotifyListeners(category, id); + end + return false; +end + +function LootReserve:NotifyListeners(category, whiteID) + local GetPackage; + if category == "RESERVES" then + local pkg; + GetPackage = function() + if not pkg then + pkg = { }; + local session = LootReserve.Server.CurrentSession; + if session then + for member, memberData in pairs(session.Members) do + pkg[member] = { }; + for _, reserve in ipairs(memberData.ReservedItems) do + table.insert(pkg[member], reserve) + end + end + end + end + return pkg; + end + end + + local success = false; + if GetPackage then + for id, callback in pairs(self.Listeners.RESERVES) do + if not whiteID or id == whiteID then + pcall(function() callback(GetPackage()); end); + success = true; + end + end + end + return success; +end + function LootReserve:ShowError(fmt, ...) StaticPopup_Show("LOOTRESERVE_GENERIC_ERROR", "|cFFFFD200LootReserve|r|n|n" .. format(fmt, ...) .. "|n "); end diff --git a/Server.lua b/Server.lua index d9be7be..0009bed 100644 --- a/Server.lua +++ b/Server.lua @@ -2223,6 +2223,7 @@ function LootReserve.Server:Reserve(player, itemID, count, chat, skipChecks) else LootReserve.Comm:BroadcastReserveInfo(itemID, reserve.Players); end + LootReserve:NotifyListeners("RESERVES"); -- Send chat messages if self.CurrentSession.Settings.ChatFallback then @@ -2378,6 +2379,7 @@ function LootReserve.Server:CancelReserve(player, itemID, count, chat, forced, w else LootReserve.Comm:BroadcastReserveInfo(itemID, reserve.Players); end + LootReserve:NotifyListeners("RESERVES"); -- Remove the item entirely if all reserves were cancelled if #reserve.Players == 0 then