Optmizations and keep parameter

Leandro Silva [01-22-20 - 23:41]
Optmizations and keep parameter
Filename
LeoGuildManager.lua
LeoGuildManager.txt
LeoGuildManagerInit.lua
Settings.lua
diff --git a/LeoGuildManager.lua b/LeoGuildManager.lua
index 7da3f3f..b28bbcd 100644
--- a/LeoGuildManager.lua
+++ b/LeoGuildManager.lua
@@ -7,6 +7,7 @@ LeoGuildManager.numEvents = {}
 LeoGuildManager.inCombat = false
 LeoGuildManager.nextGuildScan = 1
 LeoGuildManager.isDebug = false
+LeoGuildManager.lastScannedEvent = {}

 LeoGuildManager.manualScan = false

@@ -49,6 +50,24 @@ function LeoGuildManager.GetPurgePurchases(guildName)
     end
     return LeoGuildManager.globalData.settings.guilds[guildName].purchases
 end
+function LeoGuildManager.SetKeepBankHistory(guildName, value)
+    LeoGuildManager.globalData.settings.guilds[guildName].keepBankHistory = value
+end
+function LeoGuildManager.GetKeepBankHistory(guildName)
+    if LeoGuildManager.globalData.settings.guilds[guildName].keepBankHistory == nil then
+        LeoGuildManager.globalData.settings.guilds[guildName].keepBankHistory = 30
+    end
+    return LeoGuildManager.globalData.settings.guilds[guildName].keepBankHistory
+end
+function LeoGuildManager.SetKeepRosterHistory(guildName, value)
+    LeoGuildManager.globalData.settings.guilds[guildName].keepRosterHistory = value
+end
+function LeoGuildManager.GetKeepRosterHistory(guildName)
+    if LeoGuildManager.globalData.settings.guilds[guildName].keepRosterHistory == nil then
+        LeoGuildManager.globalData.settings.guilds[guildName].keepRosterHistory = 30
+    end
+    return LeoGuildManager.globalData.settings.guilds[guildName].keepRosterHistory
+end

 function LeoGuildManager.TimeAgo(timestamp)
     local diff = GetTimeStamp() - timestamp
@@ -483,9 +502,17 @@ local function normalizeGuilds()
                 purchases = 0,
                 inactivity = 30,
                 blacklist = {},
-                tooltipRoster = false
+                tooltipRoster = false,
+                keepBankHistory = 30,
+                keepRosterHistory = 30
             }
         end
+        if LeoGuildManager.globalData.settings.guilds[guildName].keepBankHistory == nil then
+            LeoGuildManager.globalData.settings.guilds[guildName].keepBankHistory = 30
+        end
+        if LeoGuildManager.globalData.settings.guilds[guildName].keepRosterHistory == nil then
+            LeoGuildManager.globalData.settings.guilds[guildName].keepRosterHistory = 30
+        end
     end

     for _, guildName in pairs(LeoGuildManager.guilds) do
@@ -634,26 +661,23 @@ function LeoGuildManager.ProcessEvent(guildId, category, eventIndex)

     local evType, evTime, param1, param2 = GetGuildEventInfo(guildId, category, eventIndex)
     local displayName = param1
-    local timeStamp = GetTimeStamp() - evTime
+    local now = GetTimeStamp()
+    local timeStamp = now - evTime

     if timeStamp < 0 then
         return false
     end
-    -- Fetch bank history for a month only
-    if category == GUILD_HISTORY_BANK and timeStamp < 31 * LeoGuildManager.SECONDS_IN_HOUR then
-        LeoGuildManager.debug("Gold event older than 31 days for guild " .. GetGuildName(guildId) .. " " .. LeoGuildManager.TimeAgo(timeStamp))
+
+    local keepFor = LeoGuildManager.GetKeepBankHistory(guildName)
+    if category == GUILD_HISTORY_BANK and keepFor > 0 and timeStamp < now - keepFor * LeoGuildManager.SECONDS_IN_DAY then
         return false
     end
-    -- Already scanned this far
-    LeoGuildManager.debug("New event for " .. GetGuildName(guildId) .. " on " .. LeoGuildManager.TimeAgo(timeStamp) .. " first " .. LeoGuildManager.TimeAgo(LeoGuildManager.scanData[guildName][category].firstEvent) .. " last " .. LeoGuildManager.TimeAgo(LeoGuildManager.scanData[guildName][category].lastEvent))
-    local limit = math.min(LeoGuildManager.scanData[guildName][category].lastEvent or 0, LeoGuildManager.scanData[guildName][category].fistEvent or 0)
-    if category == GUILD_HISTORY_GENERAL and timeStamp < limit then
-        LeoGuildManager.debug("General event older than " .. limit .. " days for guild " .. GetGuildName(guildId))
+
+    keepFor = LeoGuildManager.GetKeepRosterHistory(guildName)
+    if category == GUILD_HISTORY_GENERAL and keepFor > 0 and timeStamp < now - keepFor * LeoGuildManager.SECONDS_IN_DAY then
         return false
     end

-    LeoGuildManager.scanData[guildName][category].numAdded = LeoGuildManager.scanData[guildName][category].numAdded + 1
-
     if LeoGuildManager.scanData[guildName][category].lastEvent == 0 or
             LeoGuildManager.scanData[guildName][category].lastEvent < timeStamp then
         LeoGuildManager.scanData[guildName][category].lastEvent = timeStamp
@@ -665,10 +689,12 @@ function LeoGuildManager.ProcessEvent(guildId, category, eventIndex)

     if (category == GUILD_HISTORY_GENERAL) then
         if (evType == GUILD_EVENT_GUILD_JOIN) then
+            LeoGuildManager.scanData[guildName][category].numAdded = LeoGuildManager.scanData[guildName][category].numAdded + 1
             initMemberScanData(guildName, displayName)
             LeoGuildManager.scanData[guildName].members[displayName].joined = timeStamp
             LeoGuildManager.debug("Added guild event (".. displayName .. " joined) for guild " .. GetGuildName(guildId) .. " from " .. LeoGuildManager.TimeAgo(timeStamp))
         elseif (evType == GUILD_EVENT_GUILD_INVITE) then
+            LeoGuildManager.scanData[guildName][category].numAdded = LeoGuildManager.scanData[guildName][category].numAdded + 1
             initMemberScanData(guildName, param2)
             LeoGuildManager.scanData[guildName].members[param2].invited = timeStamp
             LeoGuildManager.scanData[guildName].members[param2].invitedBy = param1
@@ -687,6 +713,8 @@ function LeoGuildManager.ProcessEvent(guildId, category, eventIndex)
             }
         end

+        LeoGuildManager.scanData[guildName][category].numAdded = LeoGuildManager.scanData[guildName][category].numAdded + 1
+
         if evType == GUILD_EVENT_BANKGOLD_ADDED then
             LeoGuildManager.debug("Added bank event (".. displayName .. " deposited " .. param2 .. " gold) for guild " .. GetGuildName(guildId) .. " from " .. LeoGuildManager.TimeAgo(timeStamp))
         elseif evType == GUILD_EVENT_BANKGOLD_REMOVED then
@@ -702,8 +730,7 @@ local function onGuildHistoryResponseReceived(eventCode, guildId, category)
         return
     end

-    LeoGuildManager.nextGuildHistoryScanIndex = LeoGuildManager.nextGuildHistoryScanIndex or {}
-    LeoGuildManager.nextGuildHistoryScanIndex[guildId] = LeoGuildManager.nextGuildHistoryScanIndex[guildId] or 1
+    LeoGuildManager.lastScannedEvent[guildId] = LeoGuildManager.lastScannedEvent[guildId] or 1

     local guildName = GetGuildName(guildId)
     initGuildScanData(guildName)
@@ -711,24 +738,28 @@ local function onGuildHistoryResponseReceived(eventCode, guildId, category)
     local numEvents = GetNumGuildEvents(guildId, category)
     if numEvents > 0 then
         LeoGuildManager.debug("Handling " .. numEvents .. " events for guild " .. GetGuildName(guildId))
+    else
+        -- LeoGuildManager.log("No events for guild " .. guildName)
+        return
     end

-    for i = LeoGuildManager.nextGuildHistoryScanIndex[guildId], numEvents do
+    LeoGuildManager.scanData[guildName][GUILD_HISTORY_GENERAL].numAdded = 0
+    LeoGuildManager.scanData[guildName][GUILD_HISTORY_BANK].numAdded = 0
+
+    for i = LeoGuildManager.lastScannedEvent[guildId], numEvents do
         LeoGuildManager.ProcessEvent(guildId, category, i)
     end

     local guildEvents = LeoGuildManager.scanData[guildName][GUILD_HISTORY_GENERAL].numAdded or 0
     local bankEvents = LeoGuildManager.scanData[guildName][GUILD_HISTORY_BANK].numAdded or 0
     if guildEvents > 0 then
-        LeoGuildManager.log("Added " .. guildEvents .. " guild events for guild " .. guildName)
-        -- RequestMoreGuildHistoryCategoryEvents(guildId, GUILD_HISTORY_GENERAL)
+        LeoGuildManager.debug("Added " .. guildEvents .. " guild events for guild " .. guildName)
     end
     if bankEvents > 0 then
-        LeoGuildManager.log("Added " .. bankEvents .. " bank events for guild " .. guildName)
-        -- RequestMoreGuildHistoryCategoryEvents(guildId, GUILD_HISTORY_BANK)
+        LeoGuildManager.debug("Added " .. bankEvents .. " bank events for guild " .. guildName)
     end

-    LeoGuildManager.nextGuildHistoryScanIndex[guildId] = numEvents + 1
+    LeoGuildManager.lastScannedEvent[guildId] = numEvents + 1
 end

 function LeoGuildManager.HasIntegrationAddonsLoaded(integration)
@@ -774,7 +805,7 @@ end

 function LeoGuildManager.ScanGuilds()
     if LeoGuildManager.inCombat then
-        zo_callLater(LeoGuildManager.ScanGuilds, shortScanInterval)
+        zo_callLater(LeoGuildManager.ScanGuilds, 10000)
         return
     end

@@ -785,7 +816,7 @@ function LeoGuildManager.ScanGuilds()
         guildId = GetGuildId(i)

         if LeoGuildManager.GuildNeedsScan(guildId) then
-            local guildName = GetGuildName(guildId)
+            -- local guildName = GetGuildName(guildId)

             -- if not HasGuildHistoryCategoryEverBeenRequested(guildId, GUILD_HISTORY_GENERAL) then
                 -- LeoGuildManager.debug("Starting scan for general events on " .. guildName .. " ...")
@@ -806,6 +837,39 @@ function LeoGuildManager.ScanGuilds()
     zo_callLater(LeoGuildManager.ScanGuilds, 10000)
 end

+function LeoGuildManager:DeleteOldData()
+    local numGuilds = GetNumGuilds()
+    for i = 1, numGuilds do
+        local guildId = GetGuildId(i)
+        local guildName = GetGuildName(guildId)
+        local old = LeoGuildManager.GetKeepRosterHistory(guildName) * LeoGuildManager.SECONDS_IN_DAY
+
+        if old > 0 then
+            local oldTimeStamp = GetTimeStamp() - old
+            for name, memberTable in pairs(LeoGuildManager.scanData[guildName].members) do
+                if memberTable.joined ~= nil and memberTable.joined < oldTimeStamp then
+                    LeoGuildManager.scanData[guildName].members[name] = nil
+                end
+                if memberTable.invited ~= nil and memberTable.invited < oldTimeStamp then
+                    LeoGuildManager.scanData[guildName].members[name] = nil
+                end
+            end
+
+        end
+
+        old = LeoGuildManager.GetKeepBankHistory(guildName) * LeoGuildManager.SECONDS_IN_DAY
+
+        if old > 0 then
+            local oldTimeStamp = GetTimeStamp() - old
+            for index, event in pairs(LeoGuildManager.scanData[guildName][GUILD_HISTORY_BANK].events) do
+                if event.timeStamp ~= nil and event.timeStamp < oldTimeStamp then
+                    LeoGuildManager.scanData[guildName][GUILD_HISTORY_BANK].events[index] = nil
+                end
+            end
+        end
+    end
+end
+
 function LeoGuildManager:OnUpdate()
     LeoGuildManager.AutoKick(true)
 end
@@ -839,6 +903,10 @@ local function onPlayerActivated(eventCode)
     end, 5000)
 end

+local function OnPlayerDeactivated()
+    LeoGuildManager:DeleteOldData()
+end
+
 function LeoGuildManager.OnAddOnLoaded(event, addonName)
     if addonName == LeoGuildManager.name then
         EVENT_MANAGER:UnregisterForEvent(LeoGuildManager.Name, EVENT_ADD_ON_LOADED)
@@ -850,6 +918,7 @@ function LeoGuildManager.OnAddOnLoaded(event, addonName)
             LeoGuildManager.OnUpdate()
         end)
         EVENT_MANAGER:RegisterForEvent(LeoGuildManager.name, EVENT_PLAYER_ACTIVATED, onPlayerActivated)
+        EVENT_MANAGER:RegisterForEvent(LeoGuildManager.name, EVENT_PLAYER_DEACTIVATED, OnPlayerDeactivated)
         CALLBACK_MANAGER:RegisterCallback("LAM-PanelControlsCreated", onSettingsControlsCreated)
         EVENT_MANAGER:RegisterForEvent(LeoGuildManager.name, EVENT_NEW_MOVEMENT_IN_UI_MODE, onNewMovementInUIMode)
         CHAMPION_PERKS_SCENE:RegisterCallback('StateChange', onChampionPerksSceneStateChange)
diff --git a/LeoGuildManager.txt b/LeoGuildManager.txt
index 4470307..a454f4f 100644
--- a/LeoGuildManager.txt
+++ b/LeoGuildManager.txt
@@ -1,7 +1,7 @@
 ## Title: Leo's Guild Manager
 ## APIVersion: 100029 100030
-## Version: 1.2.1
-## AddOnVersion: 1201
+## Version: 1.2.2
+## AddOnVersion: 1202
 ## Author: |c39B027@LeandroSilva|r
 ## SavedVariables: LeoGuildManagerGlobalData LeoGuildManagerScanData
 ## DependsOn: LibStub LibFeedback LibAddonMenu-2.0
diff --git a/LeoGuildManagerInit.lua b/LeoGuildManagerInit.lua
index 2bf7d87..bb2db96 100644
--- a/LeoGuildManagerInit.lua
+++ b/LeoGuildManagerInit.lua
@@ -4,7 +4,7 @@ LeoGuildManagerUI = LeoGuildManagerUI or {}

 LeoGuildManager.name = "LeoGuildManager"
 LeoGuildManager.displayName = "Leo's Guild Manager"
-LeoGuildManager.version = "1.2.1"
+LeoGuildManager.version = "1.2.2"
 LeoGuildManager.chatPrefix = "|c39B027" .. LeoGuildManager.name .. "|r: "

 LeoGuildManager.TAB_PURGE = "Purge"
diff --git a/Settings.lua b/Settings.lua
index be7aa8d..546647f 100644
--- a/Settings.lua
+++ b/Settings.lua
@@ -67,6 +67,8 @@ function LeoGuildManagerSettings:CreatePanel()

     for guildId, guild in pairs(LeoGuildManager.guilds) do

+        local guildName = GetGuildName(guildId)
+
         if LeoGuildManager.globalData.settings.guilds[guild].enabled == true then

             local ranks = LeoGuildManager.GetGuildRanks(guildId)
@@ -98,6 +100,27 @@ function LeoGuildManagerSettings:CreatePanel()
                         getFunc = function() return LeoGuildManager.UseTooltipRoster(guild) end,
                         setFunc = function(value) LeoGuildManager.SetTooltipRoster(guild, value) end,
                     },{
+                        type = "slider",
+                        name = "Keep Bank history for (days)",
+                        tooltip = "Will clean old data. 0 will disable it and will keep the data indefinitely",
+                        width = "half",
+                        disabled = function() return not LeoGuildManager.CanScanBankHistory(guildId) end,
+                        getFunc = function() return LeoGuildManager.GetKeepBankHistory(guildName) end,
+                        setFunc = function(value) LeoGuildManager.SetKeepBankHistory(guildName, value) end,
+                        min = 0,
+                        max = 90,
+                        default = 30,
+                    },{
+                        type = "slider",
+                        name = "Keep Roster history for (days)",
+                        tooltip = "Will clean old data. 0 will disable it and will keep the data indefinitely",
+                        width = "half",
+                        getFunc = function() return LeoGuildManager.GetKeepRosterHistory(guildName) end,
+                        setFunc = function(value) LeoGuildManager.SetKeepRosterHistory(guildName, value) end,
+                        min = 0,
+                        max = 90,
+                        default = 30,
+                    },{
                         type = "submenu",
                         name = "|c3f7fffPurge|r",
                         controls = {