Guest User

Broker Garrison - compact tooltip

a guest
Nov 19th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 49.05 KB | None | 0 0
  1. local ADDON_NAME, _ = ...
  2. local _G = getfenv(0)
  3.  
  4. local LibStub = _G.LibStub
  5.  
  6. local BrokerGarrison = LibStub('AceAddon-3.0'):NewAddon(ADDON_NAME, 'AceConsole-3.0', "AceHook-3.0", 'AceEvent-3.0', 'AceTimer-3.0', "LibSink-2.0")
  7. local Garrison = BrokerGarrison
  8.  
  9. Garrison.versionString = GetAddOnMetadata(ADDON_NAME, "Version");
  10. Garrison.cleanName = "Broker Garrison"
  11.  
  12. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  13. local L = LibStub:GetLibrary( "AceLocale-3.0" ):GetLocale(ADDON_NAME)
  14. local LibQTip = LibStub('LibQTip-1.0')
  15. local AceConfigDialog = LibStub("AceConfigDialog-3.0")
  16. local Toast, ToastVersion = LibStub("LibToast-1.0")
  17. local LSM = LibStub:GetLibrary("LibSharedMedia-3.0")
  18.  
  19. -- LUA
  20. local math, string, table, print, pairs, ipairs, unpack = _G.math, _G.string, _G.table, _G.print, _G.pairs, _G.ipairs, _G.unpack
  21. local tonumber, strupper, select, time = _G.tonumber, _G.strupper, _G.select, _G.time
  22. -- Blizzard
  23. local BreakUpLargeNumbers, C_Garrison, GetCurrencyInfo = _G.BreakUpLargeNumbers, _G.C_Garrison, _G.GetCurrencyInfo
  24. -- UI Elements
  25. local InterfaceOptionsFrameAddOns, UIParentLoadAddOn, GarrisonLandingPage = _G.InterfaceOptionsFrameAddOns, _G.UIParentLoadAddOn, _G.GarrisonLandingPage
  26. local GarrisonMissionFrame, GarrisonLandingPageMinimapButton = _G.GarrisonMissionFrame ,_G.GarrisonLandingPageMinimapButton
  27. -- UI Functions
  28. local ShowUIPanel, HideUIPanel, CreateFont, PlaySoundFile = _G.ShowUIPanel, _G.HideUIPanel, _G.CreateFont, _G.PlaySoundFile
  29. -- UI Hooks
  30. local OptionsListButtonToggle_OnClick = _G.OptionsListButtonToggle_OnClick
  31.  
  32. local garrisonDb, configDb, globalDb, DEFAULT_FONT, colors
  33.  
  34. -- Constants
  35. local TYPE_BUILDING = "building"
  36. local TYPE_MISSION = "mission"
  37. local TYPE_SHIPMENT = "shipment"
  38.  
  39. Garrison.TYPE_BUILDING = TYPE_BUILDING
  40. Garrison.TYPE_MISSION = TYPE_MISSION
  41. Garrison.TYPE_SHIPMENT = TYPE_SHIPMENT
  42.  
  43. local addonInitialized = false
  44. local delayedInit = false
  45. local dependencyLoaded = false
  46. local CONFIG_VERSION = 2
  47.  
  48. local timers = {}
  49. Garrison.timers = timers
  50. local atlas = {}
  51. Garrison.atlas = atlas
  52. local iconCache = {}
  53. Garrison.iconCache = iconCache
  54.  
  55. -- Garrison Functions
  56. local debugPrint, pairsByKeys, formatRealmPlayer, tableSize, getColoredString, getColoredUnitName, formattedSeconds, getIconString
  57.  
  58.  
  59. local TOAST_MISSION_COMPLETE = "BrokerGarrisonMissionComplete"
  60. local TOAST_BUILDING_COMPLETE = "BrokerGarrisonBuildingComplete"
  61. local TOAST_SHIPMENT_COMPLETE = "BrokerGarrisonShipmentComplete"
  62.  
  63. local DB_DEFAULTS = {
  64.     profile = {
  65.         general = {
  66.             mission = {
  67.                 ldbTemplate = "M1",
  68.                 hideCharactersWithoutMissions = true,
  69.                 ldbLabelText = L["Garrison: Missions"],
  70.                 showOnlyCurrentRealm = false,
  71.                 collapseOtherCharsOnLogin = false,
  72.             },
  73.             building = {
  74.                 hideBuildingWithoutShipments = false,
  75.                 hideHeader = false,
  76.                 ldbTemplate = "B1",
  77.                 ldbLabelText = L["Garrison: Buildings"],
  78.                 showOnlyCurrentRealm = false,
  79.                 collapseOtherCharsOnLogin = false,
  80.             },         
  81.             hideGarrisonMinimapButton = false,
  82.         },
  83.         tooltip = {
  84.             building = {
  85.                 sort = {
  86.                     [1] = {
  87.                         value = "b.canActivate",
  88.                         ascending = true,
  89.                     },
  90.                     [2] = {
  91.                         value = "b.isBuilding",
  92.                         ascending = false,
  93.                     },
  94.                     [3] = {
  95.                         value = "b.shipmentsReady",
  96.                         ascending = false,
  97.                     },
  98.                     [4] = {
  99.                         value = "b.shipmentCapacity",
  100.                         ascending = false,
  101.                     },
  102.                     [5] = {
  103.                         value = "b.name",
  104.                         ascending = true,
  105.                     },
  106.                     ['*'] = {
  107.                         value = "-",
  108.                         ascending = false,
  109.                     },
  110.                 },
  111.                 group = {
  112.                     [1] = {
  113.                         value = "b.size",
  114.                         ascending = true,
  115.                     },
  116.                 },
  117.             },
  118.             mission = {
  119.                 sort = {
  120.                     [1] = {
  121.                         value = "m.timeLeft",
  122.                         ascending = true,
  123.                     },
  124.                     [2] = {
  125.                         value = "m.level",
  126.                         ascending = false,
  127.                     },
  128.                     [3] = {
  129.                         value = "m.name",
  130.                         ascending = true,
  131.                     },                 
  132.                     ['*'] = {
  133.                         value = "-",
  134.                         ascending = false,
  135.                     },                 
  136.                 },
  137.                 group = {
  138.                     [1] = {
  139.                         value = "m.missionState",
  140.                         ascending = true,
  141.                     },
  142.                 },             
  143.             },
  144.         },
  145.         notification = {
  146.             sink = {},
  147.             ['**'] = {
  148.                 enabled = true,
  149.                 repeatOnLoad = false,
  150.                 toastEnabled = true,
  151.                 toastPersistent = true,
  152.                 hideBlizzardNotification = true,
  153.                 hideMinimapPulse = false,
  154.             }
  155.         },
  156.         display = {
  157.             scale = 1,
  158.             autoHideDelay = 0.25,
  159.             iconSize = 16,
  160.             fontSize = 12,
  161.             showIcon = true,
  162.         },
  163.         debugPrint = false,
  164.     },
  165.     global = {
  166.         data = {}
  167.     }
  168. }
  169.  
  170. -- Player info
  171. local charInfo = {
  172.     playerName = UnitName("player"),
  173.     playerClass = select(2, UnitClass("player")),
  174.     playerFaction = UnitFactionGroup("player"),
  175.     realmName = GetRealmName(),
  176. }
  177. Garrison.charInfo = charInfo
  178.  
  179. local location = {
  180.     garrisonMapName = _G.GetMapNameByID(976),
  181.     zoneName = nil,
  182.     inGarrison = nil,
  183. }
  184. Garrison.location = location
  185.  
  186. -- LDB init
  187. local ldb_object_mission = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject(ADDON_NAME.."Mission",
  188.   { type = "data source",
  189.    label = L["Garrison: Missions"],
  190.     icon = "Interface\\Icons\\Inv_Garrison_Resource",
  191.     text = L["Garrison: Missions"],
  192.    })
  193.  
  194. local ldb_object_building = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject(ADDON_NAME.."Building",
  195.   { type = "data source",
  196.    label = L["Garrison: Buildings"],
  197.     icon = "Interface\\Icons\\Inv_Garrison_Resource",
  198.     text = L["Garrison: Buildings"],
  199.    })
  200.  
  201.  
  202. function Garrison:OnDependencyLoaded()
  203.     if not dependencyLoaded then
  204.         dependencyLoaded = true
  205.    
  206.         GarrisonLandingPage = _G.GarrisonLandingPage
  207.         GarrisonMissionFrame = _G.GarrisonMissionFrame
  208.  
  209.         debugPrint("DependencyLoaded")
  210.  
  211.         Garrison:ScheduleTimer("RegisterEvents", 5)
  212.  
  213.         self:Hook("GarrisonCapacitiveDisplayFrame_Update", true)
  214.     end
  215. end
  216.  
  217. function Garrison:RegisterEvents()
  218.     debugPrint("RegisterEvents")
  219.  
  220.     local fullUpdateRet = Garrison:FullUpdateBuilding(TYPE_BUILDING)   
  221.  
  222.     timers.notify_update = Garrison:ScheduleRepeatingTimer("QuickUpdate", 5)
  223.     timers.ldb_update = Garrison:ScheduleRepeatingTimer("LDBUpdate", 1)
  224.     timers.icon_update = Garrison:ScheduleRepeatingTimer("SlowUpdate", 60) 
  225.  
  226.     self:RegisterEvent("GARRISON_BUILDING_PLACED", "BuildingUpdate")
  227.     self:RegisterEvent("GARRISON_BUILDING_REMOVED", "BuildingUpdate")
  228.     self:RegisterEvent("GARRISON_BUILDING_UPDATE", "BuildingUpdate")
  229.     self:RegisterEvent("GARRISON_BUILDING_ACTIVATED", "BuildingUpdate")
  230.     self:RegisterEvent("GARRISON_UPDATE", "BuildingUpdate")
  231.     self:RegisterEvent("SHIPMENT_UPDATE", "ShipmentStatusUpdate")
  232.  
  233. end
  234.  
  235. function Garrison:LoadDependencies()
  236.     if not GarrisonMissionFrame or not GarrisonLandingPage then
  237.         debugPrint("Loading Blizzard_GarrisonUI...")
  238.         if UIParentLoadAddOn("Blizzard_GarrisonUI") then
  239.             Garrison:OnDependencyLoaded()
  240.         end
  241.     end
  242.  
  243.     if not dependencyLoaded and _G.IsAddOnLoaded("Blizzard_GarrisonUI") then
  244.         Garrison:OnDependencyLoaded()
  245.     end
  246. end
  247. -- Helper Functions
  248.  
  249. local function toastCallback (callbackType, mouseButton, buttonDown, payload)
  250.  
  251.     local missionData = payload[1]
  252.  
  253.     if callbackType == "primary" then
  254.         debugPrint("OK: "..payload[1].id.." ("..payload[1].name..")")
  255.     end
  256.     if callbackType == "secondary" then
  257.         debugPrint("Dismiss: "..payload[1].id.." ("..payload[1].name..")")
  258.         missionData.notification = 2 -- Mission dismissed, never show again
  259.         missionData.notificationDismissed = true
  260.     end
  261. end
  262.  
  263. local function toastMissionComplete (toast, text, missionData)
  264.     if configDb.notification.mission.toastPersistent then
  265.         toast:MakePersistent()
  266.     end
  267.     toast:SetTitle(L["Garrison: Mission complete"])
  268.     toast:SetFormattedText(getColoredString(text, colors.green))
  269.  
  270.     if ToastVersion >= 8 and missionData.typeAtlas then
  271.         toast:SetIconAtlas(missionData.typeAtlas)
  272.     else
  273.         toast:SetIconTexture([[Interface\Icons\Inv_Garrison_Resource]])
  274.     end
  275.  
  276.     if configDb.notification.mission.extendedToast then
  277.         toast:SetPrimaryCallback(_G.OKAY, toastCallback)
  278.         toast:SetSecondaryCallback(L["Dismiss"], toastCallback)
  279.         toast:SetPayload(missionData)
  280.     end
  281. end
  282.  
  283. local function toastBuildingComplete (toast, text, buildingData)
  284.     if configDb.notification.building.toastPersistent then
  285.         toast:MakePersistent()
  286.     end
  287.     toast:SetTitle(L["Garrison: Building complete"])
  288.     toast:SetFormattedText(getColoredString(text, colors.green))
  289.     toast:SetIconTexture(Garrison.GetIconPath(buildingData.icon))
  290.     if configDb.notification.building.extendedToast then
  291.         toast:SetPrimaryCallback(_G.OKAY, toastCallback)
  292.         toast:SetSecondaryCallback(L["Dismiss"], toastCallback)
  293.         toast:SetPayload(buildingData)
  294.     end
  295. end
  296.  
  297. local function toastShipmentComplete (toast, text, shipmentData)
  298.     if configDb.notification.shipment.toastPersistent then
  299.         toast:MakePersistent()
  300.     end
  301.     toast:SetTitle(L["Garrison: Shipment complete"])
  302.     toast:SetFormattedText(getColoredString(text, colors.green))
  303.     toast:SetIconTexture(Garrison.GetIconPath(shipmentData.texture))
  304.     if configDb.notification.shipment.extendedToast then
  305.         toast:SetPrimaryCallback(_G.OKAY, toastCallback)
  306.         toast:SetSecondaryCallback(L["Dismiss"], toastCallback)
  307.         toast:SetPayload(shipmentData)
  308.     end
  309. end
  310.  
  311.  
  312.  
  313.  
  314. function Garrison:SendNotification(paramCharInfo, data, notificationType)
  315.  
  316.     local retVal = false
  317.  
  318.     local playerNotificationEnabled = globalDb.data[paramCharInfo.realmName][paramCharInfo.playerName].notificationEnabled
  319.  
  320.     if delayedInit then
  321.         if configDb.notification[notificationType].enabled and (playerNotificationEnabled == nil or playerNotificationEnabled) then
  322.             if  (not data.notification or
  323.                 (data.notification == 0) or
  324.                 (not addonInitialized and configDb.notification[notificationType].repeatOnLoad and not data.notificationDismissed) or
  325.                 (notificationType == TYPE_SHIPMENT and (not data.notificationValue or data.shipmentsReadyEstimate > data.notificationValue))
  326.             ) then
  327.  
  328.                 local notificationText, toastName, toastText, soundName, toastEnabled, playSound
  329.  
  330.                 toastText = ("%s\n\n%s"):format(formatRealmPlayer(paramCharInfo, true), data.name)
  331.                 toastEnabled = configDb.notification[notificationType].toastEnabled
  332.                 playSound = configDb.notification[notificationType].PlaySound
  333.                 soundName = configDb.notification[notificationType].SoundName or "None"
  334.  
  335.                 if (notificationType == TYPE_MISSION) then
  336.                     notificationText = (L["Mission complete (%s): %s"]):format(formatRealmPlayer(paramCharInfo, false), data.name)
  337.                     toastName = TOAST_MISSION_COMPLETE
  338.                 elseif (notificationType == TYPE_BUILDING) then
  339.                     notificationText = (L["Building complete (%s): %s"]):format(formatRealmPlayer(paramCharInfo, false), data.name)
  340.                     toastName = TOAST_BUILDING_COMPLETE
  341.                 elseif (notificationType == TYPE_SHIPMENT) then
  342.                     toastText = ("%s\n\n%s (%s / %s)"):format(formatRealmPlayer(paramCharInfo, true), data.name, data.shipmentsReadyEstimate, data.shipmentsTotal)
  343.                     notificationText = (L["Shipment complete (%s): %s (%s / %s)"]):format(formatRealmPlayer(paramCharInfo, false), data.name, data.shipmentsReadyEstimate, data.shipmentsTotal)
  344.                     toastName = TOAST_SHIPMENT_COMPLETE
  345.  
  346.                     data.notificationValue = data.shipmentsReadyEstimate
  347.                 end
  348.  
  349.                 debugPrint(notificationText)
  350.  
  351.                 self:Pour(notificationText, colors.green.r, colors.green.g, colors.green.b)
  352.  
  353.                 if toastEnabled then
  354.                     Toast:Spawn(toastName, toastText, data)
  355.                 end
  356.  
  357.                 if playSound then
  358.                     PlaySoundFile(LSM:Fetch("sound", soundName))
  359.                 end
  360.  
  361.                 data.notification = 1
  362.  
  363.                 retVal = true
  364.             end
  365.         end
  366.     end
  367.  
  368.     return retVal
  369. end
  370.  
  371. function Garrison:GetPlayerMissionCount(paramCharInfo, missionCount, missions)
  372.     local now = time()
  373.  
  374.     local numMissionsPlayer = tableSize(missions)
  375.  
  376.     if numMissionsPlayer > 0 then
  377.         missionCount.total = missionCount.total + numMissionsPlayer
  378.  
  379.         for missionID, missionData in pairs(missions) do
  380.             local timeLeft = missionData.duration - (now - missionData.start)
  381.  
  382.             -- Do mission handling while we are at it
  383.             if  (timeLeft < 0 and missionData.start == -1) then
  384.                 -- Detect completed mission
  385.  
  386.  
  387.                 -- Deprecated - should be detected on finished event
  388.                 local parsedTimeLeft = string.match(missionData.timeLeft, Garrison.COMPLETED_PATTERN)
  389.                 if (parsedTimeLeft == "0") then
  390.                     -- 1 * 0 found in string -> assuming mission complete
  391.                     missionData.start = 0
  392.                 end
  393.             end
  394.  
  395.             -- Count
  396.             if missionData.start > 0 then
  397.                 if (timeLeft <= 0) then
  398.                     missionCount.complete = missionCount.complete + 1
  399.                     missionData.statusComplete = true
  400.                 else
  401.                     if missionCount.nextTime == -1 or timeLeft < missionCount.nextTime then
  402.                         missionCount.nextTime = timeLeft
  403.                         missionCount.nextData = missionData
  404.                         missionCount.nextChar = paramCharInfo
  405.                     end                
  406.  
  407.                     missionCount.inProgress = missionCount.inProgress + 1
  408.                 end
  409.             else
  410.                 if missionData.start == 0 then
  411.                     missionCount.complete = missionCount.complete + 1
  412.                     missionData.statusComplete = true
  413.                 else
  414.                     missionCount.inProgress = missionCount.inProgress + 1
  415.                 end
  416.             end
  417.  
  418.             missionData.missionState = missionData.statusComplete and Garrison.STATE_MISSION_COMPLETE or Garrison.STATE_MISSION_INPROGRESS
  419.  
  420.             missionData.timeLeftCalc = math.max(0, timeLeft)
  421.  
  422.             if (timeLeft < 0 and missionData.start >= 0) then
  423.                 Garrison:SendNotification(paramCharInfo, missionData, TYPE_MISSION)
  424.             end
  425.         end
  426.  
  427.     end
  428. end
  429.  
  430.  
  431. function Garrison:DoShipmentMagic(shipmentData, paramCharInfo)
  432.     local now = time()
  433.  
  434.     local shipmentsReady, shipmentsInProgress, shipmentsAvailable
  435.     local timeLeftNext = 0
  436.     local timeLeftTotal = 0
  437.     local shipmentsAvailable = shipmentData.shipmentCapacity
  438.  
  439.     if shipmentData and shipmentData.shipmentsTotal then
  440.         local openShipments = shipmentData.shipmentsTotal - shipmentData.shipmentsReady
  441.  
  442.         local timeDiff = (now - shipmentData.creationTime)
  443.         local shipmentsReadyByTime = 0
  444.         if shipmentData.duration and shipmentData.duration > 0 then
  445.             shipmentsReadyByTime = math.floor(timeDiff / shipmentData.duration)
  446.         end
  447.  
  448.         --if isCurrentChar(paramCharInfo) then
  449.         --  shipmentsReady = shipmentData.shipmentsReady
  450.         --else
  451.             -- Only for other chars
  452.             shipmentsReady = math.min(shipmentData.shipmentsReady + shipmentsReadyByTime, shipmentData.shipmentsTotal)
  453.         --end
  454.         shipmentsInProgress = shipmentData.shipmentsTotal - shipmentsReady
  455.         shipmentsAvailable = shipmentData.shipmentCapacity - shipmentData.shipmentsTotal
  456.  
  457.         timeLeftNext = shipmentData.duration - timeDiff
  458.  
  459.         if shipmentsInProgress == 0 then
  460.             timeLeftNext = 0
  461.         else
  462.             timeLeftNext = timeLeftNext + (shipmentData.duration * shipmentsReadyByTime)
  463.             timeLeftTotal = timeLeftNext + (shipmentData.duration * (shipmentsInProgress - 1))
  464.         end
  465.  
  466.         return shipmentsReady, shipmentsInProgress, shipmentsAvailable, timeLeftNext, timeLeftTotal
  467.     else
  468.         return 0, 0, shipmentsAvailable, 0, 0
  469.     end
  470. end
  471.  
  472. function Garrison:GetPlayerBuildingCount(paramCharInfo, buildingCount, buildings)
  473.     local now = time()
  474.  
  475.     local numBuildingsPlayer = tableSize(buildings)
  476.  
  477.     if numBuildingsPlayer > 0 then
  478.         buildingCount.building.total = buildingCount.building.total + numBuildingsPlayer
  479.  
  480.         for plotID, buildingData in pairs(buildings) do
  481.  
  482.             if buildingData.isBuilding or buildingData.canActivate then
  483.                 -- Check for building complete
  484.                 local timeLeft = buildingData.buildTime - (now - buildingData.timeStart)
  485.  
  486.                 if buildingData.canActivate or timeLeft < 0 then
  487.                     Garrison:SendNotification(paramCharInfo, buildingData, TYPE_BUILDING)
  488.                     buildingCount.building.complete = buildingCount.building.complete + 1
  489.                     buildingData.canActivate = true
  490.                     buildingData.buildingState = Garrison.STATE_BUILDING_COMPLETE
  491.                 else
  492.                     buildingCount.building.building = buildingCount.building.building + 1
  493.                     buildingData.buildingState = Garrison.STATE_BUILDING_BUILDING
  494.                 end
  495.             else
  496.                 buildingCount.building.active = buildingCount.building.active + 1
  497.                 buildingData.buildingState = Garrison.STATE_BUILDING_ACTIVE
  498.  
  499.                 local shipmentData = buildingData.shipment
  500.  
  501.                 -- Check for work orders
  502.                 if shipmentData and shipmentData.name and shipmentData.shipmentsTotal then
  503.  
  504.                     local shipmentsReady, shipmentsInProgress, shipmentsAvailable, timeLeftNext = Garrison:DoShipmentMagic(shipmentData, paramCharInfo)
  505.  
  506.                     shipmentData.shipmentsReadyEstimate = shipmentsReady
  507.                     shipmentData.shipmentsInProgress = shipmentsInProgress
  508.                     shipmentData.shipmentsAvailable = shipmentsAvailable
  509.  
  510.                     if shipmentData.shipmentsReadyEstimate > 0 then                    
  511.                         Garrison:SendNotification(paramCharInfo, shipmentData, TYPE_SHIPMENT)
  512.                     end
  513.  
  514.                     if shipmentData.notificationValue and shipmentData.shipmentsReadyEstimate < shipmentData.notificationValue then
  515.                             shipmentData.notificationValue = shipmentData.shipmentsReadyEstimate
  516.                     end
  517.  
  518.                     buildingCount.shipment.inProgress = buildingCount.shipment.inProgress + shipmentsInProgress
  519.                     buildingCount.shipment.ready = buildingCount.shipment.ready + shipmentsReady
  520.                     buildingCount.shipment.total = buildingCount.shipment.total + shipmentData.shipmentsTotal
  521.                     buildingCount.shipment.available = buildingCount.shipment.available + shipmentsAvailable
  522.  
  523.                     if timeLeftNext > 0 and (buildingCount.shipment.nextTime == -1 or timeLeftNext < buildingCount.shipment.nextTime) then
  524.                         --debugPrint(("Update %s (%s)"):format(timeLeftNext, paramCharInfo.playerName))
  525.  
  526.                         buildingCount.shipment.nextTime = timeLeftNext
  527.                         buildingCount.shipment.nextData = shipmentData
  528.                         buildingCount.shipment.nextChar = paramCharInfo
  529.                     end
  530.                 elseif shipmentData and shipmentData.name then
  531.                     buildingCount.shipment.available = buildingCount.shipment.available + shipmentData.shipmentCapacity
  532.                 end
  533.             end
  534.         end
  535.     end
  536. end
  537.  
  538.  
  539. function Garrison:GetMissionCount(paramCharInfo)
  540.     local missionCount = {
  541.         total = 0,
  542.         inProgress = 0,
  543.         complete = 0,
  544.         nextTime = -1,
  545.         nextName = nil,
  546.         nextChar = nil,
  547.     }
  548.     local missionCountCurrent = nil
  549.  
  550.     if paramCharInfo then
  551.         Garrison:GetPlayerMissionCount(paramCharInfo, missionCount, globalDb.data[paramCharInfo.realmName][paramCharInfo.playerName].missions)
  552.         --missionCountCurrent = missionCount
  553.     else
  554.         Garrison:GetPlayerMissionCount(charInfo, missionCount, globalDb.data[charInfo.realmName][charInfo.playerName].missions)
  555.  
  556.         missionCountCurrent = Garrison.deepcopy(missionCount, nil)
  557.        
  558.         for realmName, realmData in pairs(globalDb.data) do
  559.             for playerName, playerData in pairs(realmData) do
  560.                 if not Garrison.isCurrentChar(playerData.info) then
  561.                     Garrison:GetPlayerMissionCount(playerData.info, missionCount, playerData.missions)
  562.                 end
  563.             end
  564.         end
  565.     end
  566.  
  567.     return missionCount, missionCountCurrent
  568. end
  569.  
  570. function Garrison:GetBuildingCount(paramCharInfo)
  571.     local buildingCount = {
  572.         building = {
  573.             total = 0,
  574.             building = 0,
  575.             complete = 0,
  576.             active = 0,
  577.         },     
  578.         shipment = {
  579.             inProgress = 0,
  580.             ready = 0,
  581.             total = 0,
  582.             available = 0,
  583.             nextTime = -1,
  584.             nextName = nil,
  585.             nextChar = nil,
  586.         }  
  587.     }
  588.     local buildingCountCurrent
  589.  
  590.  
  591.     if paramCharInfo then
  592.         Garrison:GetPlayerBuildingCount(paramCharInfo, buildingCount, globalDb.data[paramCharInfo.realmName][paramCharInfo.playerName].buildings)      
  593.         --buildingCountCurrent = buildingCount
  594.     else
  595.         Garrison:GetPlayerBuildingCount(charInfo, buildingCount, globalDb.data[charInfo.realmName][charInfo.playerName].buildings)     
  596.  
  597.         buildingCountCurrent = Garrison.deepcopy(buildingCount, nil)
  598.  
  599.         for realmName, realmData in pairs(globalDb.data) do
  600.             for playerName, playerData in pairs(realmData) do
  601.                 if not Garrison.isCurrentChar(playerData.info) then
  602.                     Garrison:GetPlayerBuildingCount(playerData.info, buildingCount, playerData.buildings)
  603.                 end
  604.             end
  605.         end
  606.     end
  607.  
  608.     return buildingCount, buildingCountCurrent
  609. end
  610.  
  611.  
  612. function Garrison:UpdateConfig()
  613.     if GarrisonLandingPageMinimapButton then
  614.         if GarrisonLandingPageMinimapButton:IsShown() then
  615.             if configDb.general.hideGarrisonMinimapButton then
  616.                 GarrisonLandingPageMinimapButton:Hide()
  617.             end
  618.         else
  619.             if not configDb.general.hideGarrisonMinimapButton then
  620.                 GarrisonLandingPageMinimapButton:Show()
  621.             end
  622.         end
  623.     end
  624. end
  625.  
  626. local updater
  627.  
  628. local DrawTooltip
  629. do
  630.     local NUM_TOOLTIP_COLUMNS = 5
  631.     local tooltipRegistry = {} 
  632.     local LDB_anchor
  633.     local tooltipType
  634.     local tooltip
  635.     local locked = false
  636.     local tooltipTypeNew
  637.  
  638.     local last_update = 0
  639.  
  640.     updater = _G.CreateFrame("Frame", nil, _G.UIParent)
  641.  
  642.     updater:SetScript("OnUpdate",
  643.         function(self, elapsed)
  644.             last_update = last_update + elapsed
  645.  
  646.             if last_update < 0.1 then
  647.                 return
  648.             end
  649.  
  650.             --tooltipRegistry[TYPE_BUILDING].tooltip
  651.            
  652.             if tooltip and tooltipType then            
  653.                 --debugPrint("onupdate tooltip: "..tooltipType)
  654.                 if tooltip:IsMouseOver() or (LDB_anchor and LDB_anchor:IsMouseOver()) then                                      
  655.                     --debugPrint("mouseover")
  656.                     self.elapsed = (self.elapsed or 0) + last_update
  657.  
  658.                     if self.elapsed >= 1 then
  659.                         --debugPrint("redraw")
  660.                         self.elapsed = 0
  661.                         DrawTooltip(LDB_anchor, tooltipType)
  662.                     end
  663.                 end
  664.             end
  665.  
  666.             last_update = 0
  667.         end)   
  668.  
  669.     local function ExpandButton_OnMouseUp(tooltip_cell, param, button)
  670.         local realm_and_character, paramType = unpack(param)
  671.  
  672.         local realm, character_name = (":"):split(realm_and_character, 2)
  673.  
  674.         --debugPrint(LDB_anchor)
  675.  
  676.         if paramType == TYPE_MISSION then
  677.             globalDb.data[realm][character_name].missionsExpanded = not globalDb.data[realm][character_name].missionsExpanded
  678.         else
  679.             globalDb.data[realm][character_name].buildingsExpanded = not globalDb.data[realm][character_name].buildingsExpanded
  680.         end
  681.  
  682.         DrawTooltip(tooltipRegistry[paramType].anchor, paramType)
  683.     end
  684.  
  685.     local function ExpandButton_OnMouseDown(tooltip_cell, param, button)
  686.         local is_expanded, paramType = unpack(param)
  687.  
  688.         local line, column = tooltip_cell:GetPosition()
  689.         tooltip:SetCell(line, column, is_expanded and Garrison.ICON_CLOSE_DOWN or Garrison.ICON_OPEN_DOWN)
  690.     end
  691.  
  692.     local function Tooltip_OnRelease_Mission(arg)
  693.         tooltipRegistry[TYPE_MISSION].tooltip = nil
  694.         tooltipRegistry[TYPE_MISSION].anchor = nil
  695.  
  696.         if tooltipType == TYPE_MISSION then
  697.             LDB_anchor = nil
  698.             tooltipType = nil
  699.         else
  700.             debugPrint("OnReleaseDraw: "..tooltipType)
  701.             DrawTooltip(LDB_anchor, tooltipType)
  702.         end
  703.     end
  704.  
  705.     local function Tooltip_OnRelease_Building(arg)
  706.         tooltipRegistry[TYPE_BUILDING].tooltip = nil
  707.         tooltipRegistry[TYPE_BUILDING].anchor = nil
  708.  
  709.         if tooltipType == TYPE_BUILDING then
  710.             LDB_anchor = nil
  711.             tooltipType = nil      
  712.         else
  713.             debugPrint("OnReleaseDraw: "..tooltipType)
  714.             DrawTooltip(LDB_anchor, tooltipType)
  715.         end
  716.     end
  717.  
  718.  
  719.     local function AddSeparator(tooltip)
  720.         tooltip:AddSeparator(1, colors.lineGrey.r, colors.lineGrey.g, colors.lineGrey.b, colors.lineGrey.a)
  721.     end
  722.  
  723.     local function AddRow(tooltip, ...)
  724.         local color = ...
  725.         local row = tooltip:AddLine(" ")
  726.         if color then
  727.             tooltip:SetLineColor(row, color.r, color.g, color.b, 1)
  728.         end
  729.  
  730.         return row
  731.     end
  732.  
  733.     local function AddEmptyRow(tooltip, ...)
  734.         -- This is where you could easily toggle whether calling AddEmptyRow actually adds a row at all.
  735.         -- In my local hack, I'll just comment out the next line.
  736.         AddRow(tooltip, ...)
  737.     end
  738.  
  739.     function DrawTooltip(anchor_frame, paramTooltipType)
  740.         if not anchor_frame then
  741.             return
  742.         end
  743.         LDB_anchor = anchor_frame      
  744.  
  745.         if tooltipType ~= paramTooltipType then
  746.             -- mission <> building switch
  747.             debugPrint("Switched tooltip")
  748.             tooltipTypeNew = paramTooltipType
  749.         end
  750.  
  751.         tooltipType = paramTooltipType
  752.         if not tooltipRegistry[tooltipType] then
  753.             tooltipRegistry[tooltipType] = {}
  754.         end
  755.  
  756.         tooltip = tooltipRegistry[tooltipType].tooltip
  757.         tooltipRegistry[tooltipType].anchor = LDB_anchor
  758.         --LDB_anchor = tooltipRegistry[tooltipType].tooltip
  759.  
  760.         locked = true
  761.  
  762.         if not tooltip then
  763.             tooltipRegistry[tooltipType].tooltip = LibQTip:Acquire("BrokerGarrisonTooltip-"..paramTooltipType, NUM_TOOLTIP_COLUMNS, "LEFT", "LEFT", "LEFT", "LEFT", "LEFT")
  764.             tooltip = tooltipRegistry[tooltipType].tooltip
  765.  
  766.             if tooltipType == TYPE_MISSION then
  767.                 tooltip.OnRelease = Tooltip_OnRelease_Mission
  768.             elseif tooltipType == TYPE_BUILDING then
  769.                 tooltip.OnRelease = Tooltip_OnRelease_Building
  770.             end
  771.             tooltip:EnableMouse(true)
  772.             tooltip:SmartAnchorTo(anchor_frame)
  773.             tooltip:SetAutoHideDelay(configDb.display.autoHideDelay or 0.25, LDB_anchor)
  774.             tooltip:SetScale(configDb.display.scale or 1)
  775.             local font = LSM:Fetch("font", configDb.display.fontName or DEFAULT_FONT)
  776.             local fontSize = configDb.display.fontSize or 12
  777.             local headerFontSize = fontSize + 2
  778.  
  779.             local tmpFont = CreateFont("BrokerGarrisonTooltipFont")
  780.             tmpFont:SetFont(font, fontSize)
  781.  
  782.             local tmpHeaderFont = CreateFont("BrokerGarrisonTooltipHeaderFont")
  783.             tmpHeaderFont:SetFont(font, headerFontSize)
  784.  
  785.             tooltip:SetFont(tmpFont)
  786.             tooltip:SetHeaderFont(tmpHeaderFont)
  787.         end
  788.  
  789.         local now = time()
  790.         local name, row, realmName, realmData, playerName, playerData, missionID, missionData
  791.  
  792.         tooltip:Clear()
  793.         tooltip:SetCellMarginH(0)
  794.  
  795.         tooltip:SetCellMarginV(0)
  796.  
  797.         local realmNum = 0
  798.  
  799.         if tooltipType == TYPE_MISSION then
  800.             local sortOptions, groupBy = Garrison.getSortOptions(Garrison.TYPE_MISSION, "name")
  801.  
  802.             for realmName, realmData in pairsByKeys(globalDb.data) do
  803.                 realmNum = realmNum + 1
  804.  
  805.                 -- Preview building/player count
  806.                 local playerCount = 0
  807.                 local missionCountTable = {}
  808.  
  809.                 for playerName, playerData in pairsByKeys(realmData) do
  810.                     missionCountTable[playerName] = Garrison:GetMissionCount(playerData.info)
  811.  
  812.                     if (playerData.tooltipEnabled == nil or playerData.tooltipEnabled) and (missionCountTable[playerName].total > 0 or (not configDb.general.mission.hideCharactersWithoutMissions)) then
  813.                         playerCount = playerCount + 1
  814.                     end
  815.                 end
  816.  
  817.                 if playerCount > 0 and not (configDb.general.mission.showOnlyCurrentRealm and realmName ~= charInfo.realmName) then
  818.  
  819.                     row = tooltip:AddHeader()
  820.                     tooltip:SetCell(row, 1, ("%s"):format(getColoredString(("%s"):format(realmName), colors.lightGray)), nil, "LEFT", 3)
  821.  
  822.                     AddEmptyRow(tooltip)
  823.                     AddSeparator(tooltip)
  824.  
  825.                     for playerName, playerData in pairsByKeys(realmData) do
  826.                         --local missionCount = Garrison:GetMissionCount(playerData.info)
  827.                         local missionCount = missionCountTable[playerName]
  828.  
  829.                         if playerData.tooltipEnabled == nil or playerData.tooltipEnabled
  830.                             and (missionCount.total > 0 or (not configDb.general.mission.hideCharactersWithoutMissions)) then
  831.                            
  832.                             AddEmptyRow(tooltip)
  833.  
  834.                             row = AddRow(tooltip)
  835.                             tooltip:SetCell(row, 1, playerData.missionsExpanded and Garrison.ICON_CLOSE or Garrison.ICON_OPEN)
  836.                             tooltip:SetCell(row, 2, ("%s"):format(getColoredUnitName(playerData.info.playerName, playerData.info.playerClass)))
  837.                             --tooltip:SetCell(row, 3, ("%s %s %s %s"):format(Garrison.ICON_CURRENCY, BreakUpLargeNumbers(playerData.currencyAmount or 0), Garrison.ICON_CURRENCY_APEXIS, BreakUpLargeNumbers(playerData.currencyApexisAmount or 0)))
  838.                             tooltip:SetCell(row, 3, getColoredString((L["Total: %s | In Progress: %s | Complete: %s"]):format(missionCount.total, missionCount.inProgress, missionCount.complete), colors.lightGray), nil, "RIGHT", 1)
  839.                         --ooltip:SetCell(row, 4, getColoredString((L["Total: %s"]):format(missionCount.total), colors.lightGray))
  840.                             --tooltip:SetCell(row, 5, getColoredString((L["In Progress: %s"]):format(missionCount.inProgress), colors.lightGray))
  841.                             --tooltip:SetCell(row, 6, getColoredString((L["Complete: %s"]):format(missionCount.complete), colors.lightGray))
  842.                             tooltip:SetCellScript(row, 1, "OnMouseUp", ExpandButton_OnMouseUp, {("%s:%s"):format(realmName, playerName), Garrison.TYPE_MISSION})
  843.                             tooltip:SetCellScript(row, 1, "OnMouseDown", ExpandButton_OnMouseDown, {playerData.missionsExpanded, Garrison.TYPE_MISSION})
  844.  
  845.                             AddEmptyRow(tooltip)
  846.                             AddSeparator(tooltip)
  847.  
  848.                             if playerData.missionsExpanded and missionCount.total > 0 then
  849.  
  850.                                 AddEmptyRow(tooltip, colors.darkGray)
  851.  
  852.                                 --debugPrint(groupBy)
  853.                                 local sortedMissionTable = Garrison.sort(playerData.missions, unpack(sortOptions))
  854.                                 local lastGroupValue = nil
  855.  
  856.                                 for missionID, missionData in sortedMissionTable do
  857.  
  858.                                     if groupBy and #groupBy > 0 then
  859.                                         local groupByValue = Garrison.getTableValue(missionData, unpack(groupBy))
  860.                                         if lastGroupValue == nil then
  861.                                             lastGroupValue = groupByValue
  862.                                         else
  863.                                             if lastGroupValue == groupByValue then
  864.                                                 -- OK
  865.                                             else
  866.                                                 AddEmptyRow(tooltip, colors.darkGray)
  867.                                                 AddSeparator(tooltip)
  868.                                                 AddEmptyRow(tooltip, colors.darkGray)
  869.  
  870.                                                 lastGroupValue = groupByValue
  871.                                             end
  872.                                         end
  873.                                     end
  874.  
  875.                                     --debugPrint(("%s: %s => %s"):format(missionData.name, groupByValue or '-', _G.tostring(isGrouped)))
  876.  
  877.                                     local timeLeft = missionData.duration - (now - missionData.start)
  878.  
  879.                                     row = AddRow(tooltip, colors.darkGray)
  880.                                     if configDb.display.showIcon then
  881.                                         tooltip:SetCell(row, 1, getIconString(missionData.typeAtlas, configDb.display.iconSize, true), nil, "LEFT", 1)
  882.                                     end
  883.                                     tooltip:SetCell(row, 2, missionData.name, nil, "LEFT", 1)
  884.  
  885.                                     if (missionData.start == -1) then
  886.                                         local formattedTime = ("~%s %s"):format(
  887.                                             missionData.timeLeft,
  888.                                             getColoredString("("..formattedSeconds(missionData.duration)..")", colors.lightGray)
  889.                                         )
  890.                                         tooltip:SetCell(row, 3, formattedTime, nil, "RIGHT", 1)
  891.                                     elseif (missionData.start == 0 or timeLeft < 0) then
  892.                                         tooltip:SetCell(row, 3, getColoredString(L["Complete!"], colors.green), nil, "RIGHT", 1)
  893.                                     else
  894.                                         local formattedTime = ("%s %s"):format(
  895.                                             formattedSeconds(timeLeft),
  896.                                             getColoredString("("..formattedSeconds(missionData.duration)..")", colors.lightGray)
  897.                                         )
  898.  
  899.                                         tooltip:SetCell(row, 3, formattedTime, nil, "RIGHT", 1)
  900.                                     end
  901.  
  902.                                 end
  903.  
  904.                                 AddEmptyRow(tooltip, colors.darkGray)
  905.  
  906.                                 AddSeparator(tooltip)
  907.                             end
  908.                         else
  909.                             debugPrint("Hide "..playerData.info.playerName)
  910.                         end
  911.                     end
  912.                 else
  913.                     debugPrint(("[%s]: No players for realm - hiding"):format(realmName))
  914.                 end
  915.                 AddEmptyRow(tooltip)
  916.             end
  917.         elseif tooltipType == TYPE_BUILDING then
  918.             local sortOptions, groupBy = Garrison.getSortOptions(Garrison.TYPE_BUILDING, "name")           
  919.            
  920.             for realmName, realmData in pairsByKeys(globalDb.data) do
  921.                 realmNum = realmNum + 1
  922.  
  923.                 local playerCount = 0
  924.  
  925.                 -- Preview building/player count
  926.                 local buildingCountTable = {}
  927.  
  928.                 for playerName, playerData in pairsByKeys(realmData) do
  929.                     buildingCountTable[playerName] = Garrison:GetBuildingCount(playerData.info)
  930.  
  931.                     if playerData.tooltipEnabled == nil or playerData.tooltipEnabled and (buildingCountTable[playerName].building.total > 0) then
  932.                         playerCount = playerCount + 1
  933.                     end
  934.                 end
  935.  
  936.                 if playerCount > 0 and not (configDb.general.building.showOnlyCurrentRealm and realmName ~= charInfo.realmName) then
  937.  
  938.                     if realmNum > 1 then
  939.                         AddEmptyRow(tooltip)                                   
  940.                     end
  941.  
  942.                     row = tooltip:AddHeader()
  943.                     tooltip:SetCell(row, 1, ("%s"):format(getColoredString(("%s"):format(realmName), colors.lightGray)), nil, "LEFT", 4)
  944.  
  945.                     AddEmptyRow(tooltip)
  946.  
  947.                     AddSeparator(tooltip)
  948.  
  949.                     for playerName, playerData in pairsByKeys(realmData) do
  950.  
  951.                         --local buildingCount = Garrison:GetBuildingCount(playerData.info)
  952.                         local buildingCount = buildingCountTable[playerName]
  953.  
  954.                         if playerData.tooltipEnabled == nil or playerData.tooltipEnabled and (buildingCount.building.total > 0) then
  955.                             playerCount = playerCount + 1
  956.  
  957.                             AddEmptyRow(tooltip)
  958.  
  959.                             row = AddRow(tooltip)
  960.                             tooltip:SetCell(row, 1, playerData.buildingsExpanded and Garrison.ICON_CLOSE or Garrison.ICON_OPEN, nil, "LEFT", 1, nil, 0, 0, 20, 20)
  961.                             tooltip:SetCell(row, 2, ("%s"):format(getColoredUnitName(playerData.info.playerName, playerData.info.playerClass)), nil, "LEFT", 3)
  962.                             tooltip:SetCell(row, 5, ("%s %s %s %s"):format(Garrison.ICON_CURRENCY_TOOLTIP, BreakUpLargeNumbers(playerData.currencyAmount or 0), Garrison.ICON_CURRENCY_APEXIS_TOOLTIP, BreakUpLargeNumbers(playerData.currencyApexisAmount or 0)), nil, "RIGHT", 1)
  963.                             tooltip:SetCellScript(row, 1, "OnMouseUp", ExpandButton_OnMouseUp, {("%s:%s"):format(realmName, playerName), Garrison.TYPE_BUILDING})
  964.                             tooltip:SetCellScript(row, 1, "OnMouseDown", ExpandButton_OnMouseDown, {playerData.buildingsExpanded, Garrison.TYPE_BUILDING})
  965.  
  966.                             AddEmptyRow(tooltip)
  967.  
  968.                             AddSeparator(tooltip)
  969.  
  970.                             if not (playerData.buildingsExpanded) then
  971.                                 tooltip:SetCell(row, 2, ("%s"):format(getColoredUnitName(playerData.info.playerName, playerData.info.playerClass)), nil, "LEFT", 1)
  972.  
  973.                                 local formattedShipment = ""
  974.  
  975.                                 if (buildingCount.building.complete > 0 or buildingCount.building.building > 0) then
  976.                                     local isBuildingIcon = ""
  977.                                     local displayCount = 0
  978.                                     if buildingCount.building.complete > 0 then
  979.                                         isBuildingIcon = Garrison.ICON_ARROW_UP
  980.                                         displayCount = "("..buildingCount.building.complete..")"
  981.                                     else
  982.                                         isBuildingIcon = Garrison.ICON_ARROW_UP_WAITING
  983.                                         displayCount = "("..buildingCount.building.building..")"
  984.                                     end
  985.  
  986.                                     --formattedShipment = formattedShipment..isBuildingIcon
  987.                                     tooltip:SetCell(row, 3, ("%s %s"):format(isBuildingIcon, getColoredString(displayCount, colors.lightGray)), nil, "LEFT", 1)
  988.                                 end
  989.  
  990.                                 if (buildingCount.shipment.inProgress > 0 or buildingCount.shipment.ready > 0) then
  991.                                     formattedShipment = formattedShipment..("%s/%s"):format(
  992.                                         buildingCount.shipment.inProgress,
  993.                                         getColoredString(buildingCount.shipment.ready, colors.green)
  994.  
  995.                                     )                          
  996.                                 end                        
  997.  
  998.                                 tooltip:SetCell(row, 4, formattedShipment, nil, "LEFT", 1)
  999.  
  1000.                             elseif playerData.buildingsExpanded and buildingCount.building.total > 0 then
  1001.                                 AddEmptyRow(tooltip, colors.darkGray)
  1002.                                
  1003.                                 if not configDb.general.building.hideHeader then
  1004.                                     row = AddRow(tooltip, colors.darkGray)
  1005.                                     tooltip:SetCell(row, 4, getColoredString(L["SHIPMENT"], colors.lightGray), nil, "CENTER", 1)
  1006.                                     tooltip:SetCell(row, 5, getColoredString(L["TIME"], colors.lightGray), nil, "CENTER", 1)
  1007.                                     AddEmptyRow(tooltip, colors.darkGray)
  1008.                                 end
  1009.  
  1010.                                 local sortedBuildingTable = Garrison.sort(playerData.buildings, unpack(sortOptions))
  1011.                                 local lastGroupValue = nil
  1012.                                 --local sortedBuildingTable = Garrison.sort(playerData.buildings, "name,a")
  1013.  
  1014.                                 for plotID, buildingData in sortedBuildingTable do
  1015.  
  1016.                                     if not configDb.general.building.hideBuildingWithoutShipments or
  1017.                                         (buildingData.isBuilding or buildingData.canActivate) or
  1018.                                         (buildingData.shipment and buildingData.shipment.shipmentCapacity) then                            
  1019.  
  1020.                                         if groupBy and #groupBy > 0 then
  1021.                                             local groupByValue = Garrison.getTableValue(buildingData, unpack(groupBy))
  1022.                                            
  1023.  
  1024.                                             if lastGroupValue == nil then
  1025.                                                 lastGroupValue = groupByValue
  1026.                                             else
  1027.                                                 if lastGroupValue == groupByValue then
  1028.                                                     -- OK
  1029.                                                 else
  1030.                                                     AddEmptyRow(tooltip, colors.darkGray)
  1031.                                                     AddSeparator(tooltip)
  1032.                                                     AddEmptyRow(tooltip, colors.darkGray)
  1033.                                                    
  1034.                                                     if not configDb.general.building.hideHeader then
  1035.                                                         row = AddRow(tooltip, colors.darkGray)
  1036.                                                         tooltip:SetCell(row, 4, getColoredString(L["SHIPMENT"], colors.lightGray), nil, "CENTER", 1)
  1037.                                                         tooltip:SetCell(row, 5, getColoredString(L["TIME"], colors.lightGray), nil, "CENTER", 1)
  1038.                                                         AddEmptyRow(tooltip, colors.darkGray)
  1039.                                                     end
  1040.  
  1041.                                                     lastGroupValue = groupByValue
  1042.                                                 end
  1043.                                             end
  1044.  
  1045.                                             --debugPrint("groupBy: "..tostring(unpack(groupBy)))
  1046.                                             --debugPrint(("%s: %s => %s"):format(buildingData.name, groupByValue or '-', tostring(isGrouped)))
  1047.                                         end
  1048.  
  1049.                                         -- Display building and Workorder data                             
  1050.                                         row = AddRow(tooltip, colors.darkGray)
  1051.  
  1052.                                         local timeLeftBuilding = 0
  1053.                                         if buildingData.isBuilding then
  1054.                                             timeLeftBuilding = buildingData.buildTime - (now - buildingData.timeStart)
  1055.                                         end
  1056.  
  1057.                                         local rank, isBuildingIcon
  1058.                                         if buildingData.isBuilding or buildingData.canActivate then                                
  1059.  
  1060.                                             if (buildingData.isBuilding and timeLeftBuilding > 0) then
  1061.                                                 isBuildingIcon = Garrison.ICON_ARROW_UP_WAITING
  1062.                                             else
  1063.                                                 isBuildingIcon = Garrison.ICON_ARROW_UP
  1064.                                             end
  1065.  
  1066.                                             --debugPrint(("[%s] isBuilding: %s, timeLeftBuilding: %s"):format(buildingData.name, _G.tostring(buildingData.isBuilding), _G.tostring(timeLeftBuilding)))
  1067.  
  1068.                                             if buildingData.rank > 1 then
  1069.                                                 rank = getColoredString("("..(buildingData.rank - 1)..") "..isBuildingIcon, colors.lightGray)
  1070.                                             else
  1071.                                                 rank = isBuildingIcon
  1072.                                             end
  1073.                                         else
  1074.                                             isBuildingIcon = ""
  1075.                                             rank = getColoredString("("..buildingData.rank..")", colors.lightGray)
  1076.                                         end
  1077.  
  1078.                                         if configDb.display.showIcon then
  1079.                                             --tooltip:SetCell(row, 1, getIconString(, configDb.display.iconSize, false, false), nil, "LEFT", 1)
  1080.                                             tooltip:SetCell(row, 2, "", nil, "LEFT", 1, Garrison.iconProvider, 0, 0, nil, nil, Garrison.GetIconPath(buildingData.icon), configDb.display.iconSize)
  1081.                                         end
  1082.  
  1083.                                         tooltip:SetCell(row, 3, ("%s %s"):format(buildingData.name, rank), nil, "LEFT", 1)
  1084.                                         --tooltip:SetCell(row, 3, isBuildingIcon, nil, "LEFT", 1)
  1085.                                        
  1086.                                         if buildingData.hasFollowerSlot then
  1087.                                             local followerTexture, iconSize                                
  1088.                                             if buildingData.follower and buildingData.follower.followerName then
  1089.                                                 followerTexture = buildingData.follower.portraitIconID
  1090.                                                 iconSize = configDb.display.iconSize - 2
  1091.                                             else
  1092.                                                 followerTexture = Garrison.ICON_PATH_FOLLOWER_NO_PORTRAIT
  1093.                                                 iconSize = configDb.display.iconSize
  1094.                                             end
  1095.  
  1096.                                             tooltip:SetCell(row, 1, "", nil, "LEFT", 1, Garrison.iconProvider, 0, 0, nil, nil, followerTexture, iconSize)
  1097.                                         end
  1098.  
  1099.                                         if ((buildingData.isBuilding and timeLeftBuilding <= 0) or buildingData.canActivate) then
  1100.                                             tooltip:SetCell(row, 5, getColoredString(L["Can be activated"], colors.green), nil, "LEFT", 1)
  1101.                                         elseif buildingData.isBuilding then
  1102.  
  1103.                                             local formattedTime = ("%s %s"):format(
  1104.                                                 formattedSeconds(timeLeftBuilding),
  1105.                                                 getColoredString("("..formattedSeconds(buildingData.buildTime)..")", colors.lightGray)
  1106.                                             )
  1107.  
  1108.                                             tooltip:SetCell(row, 5, formattedTime, nil, "LEFT", 1)
  1109.  
  1110.                                         elseif buildingData.shipment and buildingData.shipment.name then
  1111.                                             local shipmentData = buildingData.shipment
  1112.  
  1113.                                             local shipmentsReady, shipmentsInProgress, shipmentsAvailable, timeLeftNext, timeLeftTotal = Garrison:DoShipmentMagic(shipmentData, playerData.info)
  1114.  
  1115.                                             local formattedShipment = ("%s/%s %s"):format(
  1116.                                                 shipmentsInProgress,
  1117.                                                 getColoredString(shipmentsReady, colors.green),
  1118.                                                 getColoredString("("..shipmentsAvailable..")", colors.lightGray)
  1119.                                                 )
  1120.  
  1121.  
  1122.                                             tooltip:SetCell(row, 4, formattedShipment, nil, "LEFT", 1)
  1123.  
  1124.                                             if timeLeftNext > 0 and timeLeftTotal > 0 then
  1125.                                                 local formattedTime = ("%s %s"):format(
  1126.                                                     formattedSeconds(timeLeftNext),
  1127.                                                     getColoredString("("..formattedSeconds(timeLeftTotal)..")", colors.lightGray)
  1128.                                                 )
  1129.  
  1130.                                                 tooltip:SetCell(row, 5, formattedTime, nil, "LEFT", 1)
  1131.                                             end
  1132.  
  1133.  
  1134.                                         else
  1135.                                             tooltip:SetCell(row, 4, "-", nil, "LEFT", 1)
  1136.                                         end
  1137.  
  1138.                                     end                
  1139.                                 end
  1140.                            
  1141.                                 AddEmptyRow(tooltip, colors.darkGray)
  1142.                                 AddSeparator(tooltip)
  1143.                             end
  1144.                         else
  1145.                             debugPrint("Hide "..playerData.info.playerName)
  1146.                         end
  1147.                     end
  1148.                 else --playerCount <= 0
  1149.                     debugPrint(("[%s]: No players for realm - hiding"):format(realmName))
  1150.                 end
  1151.             end
  1152.             AddEmptyRow(tooltip)
  1153.  
  1154.         end
  1155.  
  1156.         tooltip:Show()
  1157.  
  1158.         tooltip:UpdateScrolling()
  1159.  
  1160.         locked = false
  1161.     end
  1162.  
  1163.     function ldb_object_building:OnEnter()
  1164.         DrawTooltip(self, TYPE_BUILDING)
  1165.     end
  1166.  
  1167.     function ldb_object_building:OnLeave()
  1168.         --updater.elapsed = 0
  1169.     end
  1170.  
  1171.     function ldb_object_mission:OnEnter()
  1172.         DrawTooltip(self, TYPE_MISSION)
  1173.     end
  1174.  
  1175.     local function onclick(button)
  1176.         if button == "LeftButton" then
  1177.             Garrison:LoadDependencies()
  1178.  
  1179.             if GarrisonLandingPage then
  1180.                 if (not GarrisonLandingPage:IsShown()) then
  1181.                     ShowUIPanel(GarrisonLandingPage)
  1182.                 else
  1183.                     HideUIPanel(GarrisonLandingPage)
  1184.                 end
  1185.             end
  1186.         else
  1187.             for i, button in ipairs(InterfaceOptionsFrameAddOns.buttons) do
  1188.                 if button.element and button.element.name == ADDON_NAME and button.element.collapsed then
  1189.                     OptionsListButtonToggle_OnClick(button.toggle)
  1190.                 end
  1191.             end
  1192.             if AceConfigDialog.OpenFrames[ADDON_NAME] then
  1193.                 AceConfigDialog:Close(ADDON_NAME)
  1194.             else
  1195.                 AceConfigDialog:Open(ADDON_NAME)
  1196.             end
  1197.         end
  1198.     end
  1199.  
  1200.     function ldb_object_mission:OnClick(button)
  1201.         onclick(button)
  1202.     end
  1203.  
  1204.     function ldb_object_building:OnClick(button)
  1205.         onclick(button)
  1206.     end
  1207. end
  1208.  
  1209.  
  1210. function Garrison:UpdateUnknownMissions(missionsLoaded)
  1211.     local activeMissions = {}
  1212.  
  1213.     for key,garrisonMission in pairs(C_Garrison.GetInProgressMissions()) do
  1214.         activeMissions[garrisonMission.missionID] = true
  1215.         -- Mission not found in Database
  1216.         if not globalDb.data[charInfo.realmName][charInfo.playerName].missions[garrisonMission.missionID]
  1217.             or globalDb.data[charInfo.realmName][charInfo.playerName].missions[garrisonMission.missionID].start == -1 then
  1218.             local mission = {
  1219.                 id = garrisonMission.missionID,
  1220.                 name = garrisonMission.name,
  1221.                 start = -1,
  1222.                 duration = garrisonMission.durationSeconds,
  1223.                 notification = 0,
  1224.                 timeLeft = garrisonMission.timeLeft,
  1225.                 type = garrisonMission.type,
  1226.                 typeAtlas = garrisonMission.typeAtlas,
  1227.                 level = garrisonMission.level,
  1228.             }
  1229.             globalDb.data[charInfo.realmName][charInfo.playerName].missions[garrisonMission.missionID] = mission
  1230.  
  1231.             -- debugPrint("Update untracked Mission: "..garrisonMission.missionID)
  1232.         end
  1233.     end
  1234.  
  1235.     if missionsLoaded then
  1236.         -- cleanup unknown missions
  1237.         local missionID
  1238.         for missionID, _ in pairs(globalDb.data[charInfo.realmName][charInfo.playerName].missions) do
  1239.             if not activeMissions[missionID] then
  1240.                 globalDb.data[charInfo.realmName][charInfo.playerName].missions[missionID] = nil
  1241.                 debugPrint("Removed unknown Mission: "..missionID)
  1242.             end
  1243.         end
  1244.     end
  1245.  
  1246.     for key,garrisonMission in pairs(C_Garrison.GetCompleteMissions()) do
  1247.         if (globalDb.data[charInfo.realmName][charInfo.playerName].missions[garrisonMission.missionID]) then
  1248.             if globalDb.data[charInfo.realmName][charInfo.playerName].missions[garrisonMission.missionID].start ~= 0 then
  1249.                 debugPrint("Finished Mission (Loop): "..garrisonMission.missionID)
  1250.                 globalDb.data[charInfo.realmName][charInfo.playerName].missions[garrisonMission.missionID].start = 0
  1251.             end
  1252.         else
  1253.             debugPrint("Unknown Mission (Loop): "..garrisonMission.missionID)
  1254.         end
  1255.     end
  1256.  
  1257. end
  1258.  
  1259.  
  1260. function Garrison:Update()
  1261.     Garrison:FullUpdateBuilding(TYPE_SHIPMENT)
  1262.  
  1263.     Garrison:UpdateUnknownMissions(false)
  1264.  
  1265.     Garrison:UpdateLDB()
  1266.  
  1267.     -- First update
  1268.     if delayedInit then
  1269.         addonInitialized = true
  1270.     end
  1271. end
  1272.  
  1273. function Garrison:UpdateLDB()
  1274.     -- LDB Text
  1275.     local missionCount, missionCountCurrent = Garrison:GetMissionCount(nil)
  1276.     local buildingCount, buildingCountCurrent = Garrison:GetBuildingCount(nil) 
  1277.  
  1278.     local currencyTotal, currencyAmount = 0, 0
  1279.     local currencyApexisAmount, currencyApexisTotal = 0, 0
  1280.  
  1281.     for realmName, realmData in pairs(globalDb.data) do
  1282.         for playerName, playerData in pairs(realmData) do
  1283.             if Garrison.isCurrentChar(playerData.info) then
  1284.                 currencyAmount = (playerData.currencyAmount or 0)
  1285.                 currencyApexisAmount = playerData.currencyApexisAmount or 0
  1286.             end
  1287.             currencyTotal = currencyTotal + (playerData.currencyAmount or 0)
  1288.             currencyApexisTotal = currencyApexisTotal + (playerData.currencyApexisAmount or 0)
  1289.         end
  1290.     end
  1291.  
  1292.     local data = {
  1293.         missionCount = missionCount,
  1294.         buildingCount = buildingCount,
  1295.         missionCountCurrent = missionCountCurrent,
  1296.         buildingCountCurrent = buildingCountCurrent,       
  1297.         currencyAmount = currencyAmount,
  1298.         currencyApexisAmount = currencyApexisAmount,
  1299.         currencyApexisTotal = currencyApexisTotal,
  1300.         currencyTotal = currencyTotal,
  1301.         currencyIcon = Garrison.ICON_CURRENCY,
  1302.         currencyApexisIcon = Garrison.ICON_CURRENCY_APEXIS,
  1303.     }
  1304.  
  1305.  
  1306.     ldb_object_mission.text = Garrison.replaceVariables(Garrison:GetLDBText(Garrison.TYPE_MISSION), data)
  1307.     ldb_object_mission.label = configDb.general.mission.ldbLabelText
  1308.  
  1309.     ldb_object_building.text = Garrison.replaceVariables(Garrison:GetLDBText(Garrison.TYPE_BUILDING), data)
  1310.     ldb_object_building.label = configDb.general.building.ldbLabelText
  1311.  
  1312. end
  1313.  
  1314.  
  1315. function Garrison:OnInitialize()
  1316.     garrisonDb = LibStub("AceDB-3.0"):New(ADDON_NAME .. "DB", DB_DEFAULTS, true)
  1317.     globalDb = garrisonDb.global
  1318.     configDb = garrisonDb.profile
  1319.  
  1320.     self.DB = garrisonDb
  1321.  
  1322.     -- Data migration
  1323.     if _G.Broker_GarrisonDB.data then
  1324.         globalDb.data = _G.Broker_GarrisonDB.data
  1325.         _G.Broker_GarrisonDB.data = nil
  1326.     end
  1327.  
  1328.     -- DB
  1329.     if not globalDb.data[charInfo.realmName] then
  1330.         globalDb.data[charInfo.realmName] = {}
  1331.     end
  1332.  
  1333.     if (not globalDb.data[charInfo.realmName][charInfo.playerName]) then
  1334.         globalDb.data[charInfo.realmName][charInfo.playerName] = {
  1335.             missions = {},
  1336.             buildings = {},
  1337.             missionsExpanded = true,
  1338.             buildingsExpanded = true,
  1339.             info = charInfo,
  1340.             currencyAmount = 0,
  1341.             tooltipEnabled = true,
  1342.             notificationEnabled = true,
  1343.         }
  1344.     end
  1345.  
  1346.     if (not globalDb.data[charInfo.realmName][charInfo.playerName]["missions"]) then
  1347.         globalDb.data[charInfo.realmName][charInfo.playerName]["missions"] = {}
  1348.     end
  1349.     if (not globalDb.data[charInfo.realmName][charInfo.playerName]["buildings"]) then
  1350.         globalDb.data[charInfo.realmName][charInfo.playerName]["buildings"] = {}
  1351.     end
  1352.  
  1353.     -- Upgrade Code, 1 => 2
  1354.     if not globalDb.data[charInfo.realmName][charInfo.playerName].configVersion then
  1355.         globalDb.data[charInfo.realmName][charInfo.playerName].configVersion = CONFIG_VERSION
  1356.  
  1357.         globalDb.data[charInfo.realmName][charInfo.playerName].tooltipEnabled = true
  1358.         globalDb.data[charInfo.realmName][charInfo.playerName].notificationEnabled = true
  1359.     end
  1360.  
  1361.     self:InitHelper()
  1362.     self:InitEvent()
  1363.  
  1364.     -- Assign functions
  1365.     debugPrint, pairsByKeys, formatRealmPlayer, tableSize = Garrison.debugPrint, Garrison.pairsByKeys, Garrison.formatRealmPlayer, Garrison.tableSize
  1366.     getColoredString, getColoredUnitName, formattedSeconds  = Garrison.getColoredString, Garrison.getColoredUnitName, Garrison.formattedSeconds
  1367.     getIconString = Garrison.getIconString
  1368.  
  1369.     colors = Garrison.colors
  1370.  
  1371.     self:SetupOptions()
  1372.  
  1373.     Garrison:SetSinkStorage(configDb.notification.sink)
  1374.  
  1375.     Toast:Register(TOAST_MISSION_COMPLETE, toastMissionComplete)
  1376.     Toast:Register(TOAST_BUILDING_COMPLETE, toastBuildingComplete)
  1377.     Toast:Register(TOAST_SHIPMENT_COMPLETE, toastShipmentComplete)
  1378.  
  1379.     Garrison:UpdateConfig()
  1380.  
  1381.     self:RegisterEvent("GARRISON_MISSION_STARTED", "GARRISON_MISSION_STARTED")
  1382.     self:RegisterEvent("GARRISON_MISSION_COMPLETE_RESPONSE", "GARRISON_MISSION_COMPLETE_RESPONSE")
  1383.     self:RegisterEvent("GARRISON_MISSION_FINISHED", "GARRISON_MISSION_FINISHED")
  1384.  
  1385.     self:RegisterEvent("GARRISON_SHOW_LANDING_PAGE", "GARRISON_SHOW_LANDING_PAGE")
  1386.     self:RegisterEvent("GARRISON_MISSION_NPC_OPENED", "GARRISON_MISSION_NPC_OPENED")
  1387.  
  1388.     self:RegisterEvent("ZONE_CHANGED_NEW_AREA", "ZoneUpdate")
  1389.     self:RegisterEvent("ZONE_CHANGED", "ZoneUpdate")
  1390.  
  1391.     self:RegisterEvent("ADDON_LOADED", "CheckAddonLoaded")
  1392.     --self:RegisterEvent("VIGNETTE_REMOVED", "VignetteEvent")  
  1393.  
  1394.     self:RawHook("GarrisonMissionAlertFrame_ShowAlert", true)
  1395.     self:RawHook("GarrisonBuildingAlertFrame_ShowAlert", true)
  1396.  
  1397.     self:RawHook("GarrisonMinimapBuilding_ShowPulse", true)
  1398.     self:RawHook("GarrisonMinimapShipmentCreated_ShowPulse", true)
  1399.     self:RawHook("GarrisonMinimapMission_ShowPulse", true)         
  1400.  
  1401.     timers.init_update = Garrison:ScheduleTimer("DelayedUpdate", 5)
  1402.  
  1403.     ldb_object_mission.icon = Garrison.ICON_PATH_MISSION
  1404.     ldb_object_building.icon = Garrison.ICON_PATH_BUILDING
  1405.  
  1406.     -- collapse other characters (if option is enabled)
  1407.     for realmName, realmData in pairs(globalDb.data) do
  1408.         for playerName, playerData in pairs(realmData) do
  1409.             local isCurrentChar = Garrison.isCurrentChar(playerData.info)
  1410.             if configDb.general.building.collapseOtherCharsOnLogin then
  1411.                 playerData.buildingsExpanded = isCurrentChar
  1412.             end
  1413.             if configDb.general.mission.collapseOtherCharsOnLogin then
  1414.                 playerData.missionsExpanded = isCurrentChar
  1415.             end
  1416.         end
  1417.     end
  1418.  
  1419. end
  1420.  
  1421. function Garrison:DelayedUpdate()
  1422.     Garrison:LoadDependencies()
  1423.  
  1424.     delayedInit = true
  1425.     Garrison:UpdateCurrency()  
  1426.     Garrison:UpdateLocation()
  1427.  
  1428.     --self:RegisterEvent("SHIPMENT_CRAFTER_CLOSED", "ShipmentUpdate")
  1429.     self:RegisterEvent("CURRENCY_DISPLAY_UPDATE", "UpdateCurrency")
  1430.  
  1431. end
Advertisement
Add Comment
Please, Sign In to add comment