diff --git a/Heimdall.lua b/Heimdall.lua index f901173..835decb 100644 --- a/Heimdall.lua +++ b/Heimdall.lua @@ -31,6 +31,7 @@ local function init() ---@field GetOrDefault fun(table: table, keys: string[], default: any): any ---@field Split fun(input: string, deliminer: string): string[] ---@field IsStinky fun(name: string): boolean + ---@field Memoize fun(f: function): function ---@field WhoQueryService WhoQueryService ---@field Whoer InitTable ---@field Messenger InitTable @@ -88,6 +89,7 @@ local function init() ---@field notes table ---@field locale string ---@field echoToRussian boolean + ---@field debug boolean ---@class HeimdallSpotterConfig ---@field enabled boolean @@ -315,6 +317,7 @@ local function init() --/run Heimdall_Data.config.who.queries="g-\"БеспредеЛ\"|ally" Heimdall_Data.config = { + debug = shared.GetOrDefault(Heimdall_Data, { "config", "debug" }, false), spotter = { enabled = shared.GetOrDefault(Heimdall_Data, { "config", "spotter", "enabled" }, true), debug = shared.GetOrDefault(Heimdall_Data, { "config", "spotter", "debug" }, false), @@ -622,6 +625,33 @@ local function init() return Heimdall_Data.config.stinkies[name] ~= nil or shared.StinkyCache[name] ~= nil end + ---@param f function + ---@return function + shared.Memoize = function(f) + local mem = {} -- memoizing table + setmetatable(mem, { __mode = "kv" }) -- make it weak + return function(x) -- new version of ’f’, with memoizing + if Heimdall_Data.config.debug then + print(string.format("[Heimdall] Memoize %s", tostring(x))) + end + local r = mem[x] + if r == nil then -- no previous result? + if Heimdall_Data.config.debug then + print(string.format("[Heimdall] Memoize %s is nil, calling original function", tostring(x))) + end + r = f(x) -- calls original function + if Heimdall_Data.config.debug then + print(string.format("[Heimdall] Memoized result for %s: %s", tostring(x), tostring(r))) + end + mem[x] = r -- store result for reuse + end + if Heimdall_Data.config.debug then + print(string.format("[Heimdall] Memoize %s is %s", tostring(x), tostring(r))) + end + return r + end + end + shared.Messenger.Init() shared.StinkyTracker.Init() shared.AgentTracker.Init() diff --git a/Modules/Config.lua b/Modules/Config.lua index 2d8f365..0177269 100644 --- a/Modules/Config.lua +++ b/Modules/Config.lua @@ -530,7 +530,16 @@ function shared.Config.Init() configFrame:Add(scale, 2, 2) local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal") title:SetText(string.format("%s - v%s", shared.L[Heimdall_Data.config.locale].config.heimdallConfig, shared.VERSION)) - configFrame:Add(title, 2, 8) + configFrame:Add(title, 2, 7) + + local debug = CreateBasicButton("HeimdallConfigDebug", configFrame.frame, + shared.L[Heimdall_Data.config.locale].config.debug, function() + Heimdall_Data.config.debug = not Heimdall_Data.config.debug + return Heimdall_Data.config.debug + end) + debug:UpdateColor(Heimdall_Data.config.debug) + configFrame:Add(debug, 2, 1) + local russian = nil local english = CreateBasicButton("HeimdallConfigEnglish", configFrame.frame, shared.L.en.config.english, function() Heimdall_Data.config.locale = "en" diff --git a/Modules/Whoer.lua b/Modules/Whoer.lua index be5b284..0770b8f 100644 --- a/Modules/Whoer.lua +++ b/Modules/Whoer.lua @@ -169,7 +169,7 @@ function shared.Whoer.Init() return nil end, ---@param query WHOQuery - ---@return string + ---@return string WhoQueryToString = function(query) local ret = "" ret = ret .. query.query @@ -226,6 +226,26 @@ function shared.Whoer.Init() } shared.WhoQueryService.queries = shared.WhoQueryService.WhoQueriesFromString(Heimdall_Data.config.who.queries) + ---@param inputZone string + ---@return boolean + local ShouldNotifyForZone = shared.Memoize(function(inputZone) + if not Heimdall_Data.config.who.debug then + print(string.format("[%s] ShouldNotifyForZone %s", ModuleName, inputZone)) + end + for zone, _ in pairs(Heimdall_Data.config.who.zoneNotifyFor) do + if string.find(inputZone, zone) then + if not Heimdall_Data.config.who.debug then + print(string.format("[%s] ShouldNotifyForZone %s is true thanks to %s", ModuleName, inputZone, zone)) + end + return true + end + end + if not Heimdall_Data.config.who.debug then + print(string.format("[%s] ShouldNotifyForZone %s is false", ModuleName, inputZone)) + end + return false + end) + -----@type WHOQuery[] --local whoQueries = { -- WHOQuery.new("g-\"БеспредеЛ\"", {}), @@ -271,7 +291,8 @@ function shared.Whoer.Init() return string.format("Cannot notify for nil player %s", tostring(player)) end - if not Heimdall_Data.config.who.zoneNotifyFor[player.zone] then + if not ShouldNotifyForZone(player.zone) then + --if not Heimdall_Data.config.who.zoneNotifyFor[player.zone] then if Heimdall_Data.config.who.debug then print(string.format("[%s] Skipping notification - Zone '%s' not in notify list", ModuleName, player.zone)) end @@ -341,8 +362,9 @@ function shared.Whoer.Init() local function NotifyZoneChanged(player, zone) if not Heimdall_Data.config.who.enabled then return end if not player then return string.format("Cannot notify for nil player %s", tostring(player)) end - if not Heimdall_Data.config.who.zoneNotifyFor[zone] - and not Heimdall_Data.config.who.zoneNotifyFor[player.zone] then + --if not Heimdall_Data.config.who.zoneNotifyFor[zone] + -- and not Heimdall_Data.config.who.zoneNotifyFor[player.zone] then + if not ShouldNotifyForZone(zone) and not ShouldNotifyForZone(player.zone) then return string.format("Not notifying for zones %s and %s", tostring(zone), tostring(player.zone)) end local text = string.format(shared.L.en.whoerMoved, @@ -405,9 +427,9 @@ function shared.Whoer.Init() local function NotifyGone(player) if not Heimdall_Data.config.who.enabled then return end if not player then return string.format("Cannot notify for nil player %s", tostring(player)) end - if not Heimdall_Data.config.who.zoneNotifyFor[player.zone] then - return string.format("Not notifying for zone %s", - tostring(player.zone)) + --if not Heimdall_Data.config.who.zoneNotifyFor[player.zone] then + if not ShouldNotifyForZone(player.zone) then + return string.format("Not notifying for zone %s", tostring(player.zone)) end local text = string.format(shared.L.en.whoerGone, @@ -517,6 +539,10 @@ function shared.Whoer.Init() print(string.format("[%s] Ignoring blacklisted player: %s", ModuleName, name)) end continue = true + else + if Heimdall_Data.config.who.debug then + print(string.format("[%s] Player %s is not blacklisted", ModuleName, name)) + end end if not continue then @@ -572,6 +598,9 @@ function shared.Whoer.Init() player.lastSeenInternal = GetTime() if player.zone ~= zone then + if Heimdall_Data.config.who.debug then + print(string.format("[%s] Player %s zone changed from %s to %s", ModuleName, name, player.zone, zone)) + end local err = NotifyZoneChanged(player, zone) if err then print(string.format("Error notifying for %s: %s", tostring(name), tostring(err)))