Skip to content

Commit

Permalink
Support external listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Anonomit committed Feb 4, 2023
1 parent 451605d commit 4491f8c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Comm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions LootReserve.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ LootReserveGlobalSave =
};

LootReserve.BagCache = nil;
LootReserve.Listeners = {
RESERVES = { },
};

StaticPopupDialogs["LOOTRESERVE_GENERIC_ERROR"] =
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4491f8c

Please sign in to comment.