diff WorldQuests.lua @ 72:6db0b9696936

do bounty criteria checking in the QuestButtonUpdate segment
author Nenue
date Fri, 07 Apr 2017 01:07:48 -0400
parents d6c0bed32c51
children 25f7dbc20a72
line wrap: on
line diff
--- a/WorldQuests.lua	Wed Apr 05 12:44:19 2017 -0400
+++ b/WorldQuests.lua	Fri Apr 07 01:07:48 2017 -0400
@@ -2,10 +2,17 @@
 -- WorldQuests.lua
 -- Created: 11/2/2016 3:40 PM
 -- %file-revision%
+
+local print = DEVIAN_WORKSPACE and function(...) _G.print('WorldQuests', ...) end or function() end
+local rprint = DEVIAN_WORKSPACE and function(...) _G.print('WQRefresh', ...) end or function() end
+local qprint = DEVIAN_WORKSPACE and function(...) _G.print('POI', ...) end or function() end
+local wprint = DEVIAN_WORKSPACE and function(...) _G.print('WP', ...) end or function() end
+local mprint = DEVIAN_WORKSPACE and function(...) _G.print('Canvas', ...) end or function() end
 local _, db = ...
 local Module = WorldPlanQuestsMixin
 
 local _G = _G
+local type, tostring, tonumber, pairs, ipairs = type, tostring, tonumber, pairs, ipairs
 local MC_GetNumZones, MC_GetZoneInfo = C_MapCanvas.GetNumZones, C_MapCanvas.GetZoneInfo
 local TQ_GetQuestsForPlayerByMapID = C_TaskQuest.GetQuestsForPlayerByMapID -- This function is not yet documented
 local TQ_GetQuestZoneID = C_TaskQuest.GetQuestZoneID
@@ -17,13 +24,8 @@
 local tonumber, abs = tonumber, math.abs
 local GetQuestLogRewardInfo = GetQuestLogRewardInfo
 local GetCurrentMapAreaID, GetMapInfo, GetMapNameByID = GetCurrentMapAreaID, GetMapInfo, GetMapNameByID
-
-
-local print = DEVIAN_WORKSPACE and function(...) _G.print('WorldQuests', ...) end or function() end
-local rprint = DEVIAN_WORKSPACE and function(...) _G.print('WQRefresh', ...) end or function() end
-local qprint = DEVIAN_WORKSPACE and function(...) _G.print('POI', ...) end or function() end
-local wprint = DEVIAN_WORKSPACE and function(...) _G.print('WP', ...) end or function() end
-local mprint = DEVIAN_WORKSPACE and function(...) _G.print('Canvas', ...) end or function() end
+local GetQuestBountyInfoForMapID, GetQuestLogTitle, GetQuestLogIndexByID, IsQuestComplete = GetQuestBountyInfoForMapID, GetQuestLogTitle, GetQuestLogIndexByID, IsQuestComplete
+local IsQuestCriteriaForBounty = IsQuestCriteriaForBounty
 
 local ToggleButton = {}
 local callbacks = {}
@@ -37,10 +39,13 @@
 local REWARD_GEAR = WORLD_QUEST_REWARD_TYPE_FLAG_EQUIPMENT
 local REWARD_CURRENCY = WORLD_QUEST_REWARD_TYPE_FLAG_ORDER_RESOURCES
 local REWARD_REAGENT = WORLD_QUEST_REWARD_TYPE_FLAG_MATERIALS
-local SCALE_FACTORS = {
-  0.25, 0.7, 1
-}
+local SCALE_FACTORS = { 0.25, 0.7, 1 }
 
+local BountyBoard = WorldMapFrame.UIElementsFrame.BountyBoard
+local ActionButton = WorldMapFrame.UIElementsFrame.ActionButton
+
+local layoutDirty = true
+local bountiesDirty = true
 local currentScale = WorldMapDetailFrame:GetScale()
 local canTargetQuests
 local numShown = 0
@@ -66,7 +71,7 @@
   end
   if #db.UpdatedPins >= 1 then
     print('|cFF00FF88pending updates', #db.UpdatedPins)
-    self:UpdateQueuedPins()
+    self:RefreshIfQueued()
   end
 
 end
@@ -121,6 +126,9 @@
   for k,v in pairs( ToggleButton) do
     self.Toggle:SetScript(k,v)
   end
+
+  self:UpdateBounties('SETUP')
+
   self:Show()
 end
 callbacks.ClickWorldMapActionButton = function(WorldQuests)
@@ -135,67 +143,16 @@
 callbacks.WorldMapScrollFrame_ReanchorQuestPOIs = function (WorldQuests)
   WorldQuests:RefreshIfChanged()
 end
-callbacks[WorldMapFrame.UIElementsFrame.BountyBoard] = {
-  SetSelectedBountyIndex = function(WorldQuests)
-    WorldQuests:UpdateBountyInfo()
-    WorldQuests:Refresh(true)
-  end
-}
-callbacks[WorldMapFrame.UIElementsFrame.ActionButton] = {
-  UpdateCastingState = function(WorldQuests)
-    WorldQuests:Refresh(true)
-  end
-}
 
+callbacks[BountyBoard] = {}
+callbacks[BountyBoard].SetSelectedBountyIndex = function(WorldQuests)
+  WorldQuests:UpdateBounties('BOUNTY_SELECTED')
+  WorldQuests:RefreshIfChanged()
+end
 
-local GetQuestBountyInfoForMapID, GetQuestLogTitle, GetQuestLogIndexByID, IsQuestFlaggedCompleted = GetQuestBountyInfoForMapID, GetQuestLogTitle, GetQuestLogIndexByID, IsQuestFlaggedCompleted
-function Module:UpdateBountyInfo()
-  print('|cFF00FF88BountyInfo()|r')
-  wipe(db.BountyInfo)
-  db.selectedBounty = nil
-
-  local selectedBounty = WorldMapFrame.UIElementsFrame.BountyBoard:GetSelectedBountyIndex()
-  local bounties, numBounties = GetQuestBountyInfoForMapID(db.currentMapID)
-
-  for index, data in ipairs(bounties) do
-    if data.factionID then
-      data.title = GetQuestLogTitle(GetQuestLogIndexByID(data.questID))
-      data.complete =  IsQuestFlaggedCompleted(data.questID)
-      if not data.complete then
-        db.BountyInfo[data.factionID] = data
-        print('bounty', data.factionID, data.title, data.complete)
-        if index == selectedBounty then
-          db.selectedBounty = data
-        end
-      end
-
-    end
-  end
-
-  for questID, pin in pairs(db.QuestsByID) do
-    local doUpdate
-    if pin.factionID then
-      if db.BountyInfo[pin.factionID] then
-        print(pin.questID, pin.factionID, db.BountyInfo[pin.factionID], pin.isCriteria)
-        pin.isCriteria = true
-        doUpdate = true
-      else
-        doUpdate = true
-        pin.isCriteria = nil
-      end
-
-    end
-
-
-    if doUpdate then
-      if pin:IsVisible() then
-        pin:Refresh()
-      else
-        pin.isStale = true
-      end
-    end
-  end
-  db.BountyUpdate = nil
+callbacks[ActionButton] = {}
+callbacks[ActionButton].UpdateCastingState = function(WorldQuests)
+  WorldQuests:Refresh(true)
 end
 
 function Module:OnConfigUpdate()
@@ -269,23 +226,7 @@
   self:RegisterEvent('ARTIFACT_UPDATE')
 end
 
-function Module:OnMapInfo(isBrokenIsle, isZoomedOut, mapAreaID, isNewMap, isMapOpen)
-
-  print('|cFFFFFF00'..self:GetName()..':OnMapInfo()|r, mapAreaID =', mapAreaID,'visible =', isMapOpen, 'changed =', isNewMap)
-
-  if db.BountyUpdate then
-    self:UpdateBountyInfo()
-  end
-  if isNewMap then
-    print('|cFF00FF88refreshing for changed map')
-    if isMapOpen then
-      self:Refresh(true)
-    else
-      self.isStale = true
-    end
-  end
-
-end
+local artifactKnowledgeMultiplier
 local superTrackedQuestID
 function Module:OnEvent (event, ...)
 
@@ -296,6 +237,7 @@
     else
       self.isStale = true
     end
+
     print('WorldMapFrame', WorldMapFrame:IsVisible(), 'hasUpdates:', self.isStale)
   elseif event == 'WORLD_QUEST_COMPLETED_BY_SPELL' then
     local questID = ...
@@ -304,17 +246,24 @@
       rprint('|cFFFF4400release|r', questID)
     end
   elseif event == 'SKILL_LINES_CHANGED' or event == 'CURRENT_SPELL_CAST_CHANGED' then
-    self:Refresh()
+    self:Refresh(event)
   elseif event == 'ARTIFACT_UPDATE' then
-    local ak = C_ArtifactUI.GetArtifactKnowledgeMultiplier()
-    if ak and (ak ~= self.akLevel) then
-      print('push artifact knowledge update', self.akLevel, 'to', ak)
-      self.akLevel = ak
-      for index, pin in pairs( db.QuestsByID) do
-        if pin.rewardType == REWARD_ARTIFACT_POWER then
-          pin.dataLoaded = nil
+    local akCheck = C_ArtifactUI.GetArtifactKnowledgeMultiplier()
+    if akCheck and (akCheck ~= artifactKnowledgeMultiplier) then
+      if artifactKnowledgeMultiplier then
+        print('push artifact knowledge update', artifactKnowledgeMultiplier, 'to', akCheck)
+        for index, pin in pairs( db.QuestsByID) do
+          if pin.rewardType == REWARD_ARTIFACT_POWER then
+            db.log(pin.questID .. ' ' .. tostring(pin.title) .. ' Flagged for artifact power.')
+            pin.itemNumber = 0
+            pin.dataLoaded = nil
+          end
         end
+      else
+
+        print('artifact knowledge multiplier is known', akCheck)
       end
+      artifactKnowledgeMultiplier = akCheck
     end
   elseif event == 'SUPER_TRACKED_QUEST_CHANGED' then
     if superTrackedQuestID and db.QuestsByID[superTrackedQuestID] then
@@ -327,6 +276,58 @@
   end
 end
 
+
+
+
+local bountyInfo = {}
+local bountyDisplayLocation, bountyLockedQuestID, selectedBountyIndex, selectedBountyQuestID
+function Module:UpdateBounties(...)
+  print('|cFF00FF88BountyInfo()|r', ...)
+  wipe(db.BountiesByFactionID)
+
+  bountiesDirty = nil
+  db.selectedBounty = nil
+  selectedBountyIndex = BountyBoard:GetSelectedBountyIndex()
+  bountyInfo, bountyDisplayLocation, bountyLockedQuestID = GetQuestBountyInfoForMapID(db.currentMapID, bountyInfo)
+  local numBounties = 0
+  for index, data in ipairs(bountyInfo) do
+    if data.factionID then
+      numBounties = numBounties + 1
+      data.index = index
+      data.complete =  IsQuestComplete(data.questID)
+      if not data.complete then
+        db.BountiesByFactionID[data.factionID] = data
+        if index == selectedBountyIndex then
+          db.selectedBounty = data
+          selectedBountyQuestID = data.questID
+        end
+        print(' ', index, data.factionID, GetQuestLogTitle(GetQuestLogIndexByID(data.questID)), data.complete, (index == selectedBountyIndex) and 'SELECTED' or '')
+      end
+    end
+  end
+
+  local numUpdated = 0
+  local numHidden = 0
+  for questID, pin in pairs(db.QuestsByID) do
+  end
+  --print(numUpdated, 'changes from Bounty Board ('..tostring(numHidden)..' hidden)')
+end
+
+function Module:OnMapInfo(isBrokenIsle, isZoomedOut, mapAreaID, isNewMap, isMapOpen)
+  if isNewMap then
+    print('|cFF00FF88'..self:GetName()..':OnMapInfo()|r, mapAreaID =', mapAreaID,'visible =', isMapOpen, 'changed =', isNewMap)
+    if isMapOpen then
+      self:Refresh(true)
+    else
+      self.isStale = true
+    end
+  else
+
+    rprint('|cFFFFFF00'..self:GetName()..':OnMapInfo()|r, mapAreaID =', mapAreaID,'visible =', isMapOpen, 'changed =', isNewMap)
+  end
+end
+
+
 local totalPins = 0
 local TQ_GetQuestLocation = C_TaskQuest.GetQuestLocation
 function Module:AcquirePin (info)
@@ -416,111 +417,9 @@
   print('|cFF00FF00-'.. (pin.mapID and GetMapNameByID(pin.mapID) or '???') ..'|r', id, pin.title)
 end
 
-
-function Module:GetBonusObjectives()
-
-
-  local tasksTable = GetTasksTable()
-  if tasksTable ~= nil then
-    print('|cFF00FF88'..self:GetName()..':BonusObjectives()|r ')
-    self.numTasks = #tasksTable
-    for i, taskID in ipairs(tasksTable) do
-      if not QuestUtils_IsQuestWorldQuest(taskID) then
-      local info = db.TasksByID[taskID]
-        if not info then
-          local isInArea, isOnMap, numObjectives, taskName, displayAsObjective = GetTaskInfo(taskID)
-          if isOnMap then
-            print(' * '..i, taskID, GetTaskInfo(taskID))
-            info = {
-              questID = taskID,
-              numObjectives = numObjectives,
-              title = taskName,
-              isInArea = isInArea,
-              isOnMap = isOnMap,
-              displayAsObjective = displayAsObjective,
-              worldQuest = false,
-              isPending = false,
-              isNew = true,
-            }
-
-
-            db.TasksByID[taskID] = info
-
-            local pin = self:AcquirePin(taskID)
-            for k,v in pairs(info) do
-              pin[k] = v
-            end
-            pin:GetBonusObjectiveInfo(info)
-          end
-        end
-      end
-
-
-    end
-  end
-end
-
-
--- use tooltip object to extract item details
-function Module:GetRewardHeader(questID)
-  local name, icon, quantity, quality, _, itemID = GetQuestLogRewardInfo(1, questID)
-  local scanner = _G.WorldPlanTooltip
-  local print = qprint
-  if not itemID then
-    return
-  end
-  qprint('GetRewardHeader', questID)
-
-  scanner:SetOwner(WorldPlan, "ANCHOR_NONE")
-  scanner:SetItemByID(itemID)
-  scanner:Show()
-  local ttl1 = _G['WorldPlanTooltipTextLeft1']
-  local ttl2 = _G['WorldPlanTooltipTextLeft2']
-  local ttl3 = _G['WorldPlanTooltipTextLeft3']
-  local ttl4 = _G['WorldPlanTooltipTextLeft4']
-  --print(ttl2, ttl3, ttl4)
-  if ttl2 then
-    local text = ttl2:GetText()
-    -- Artifact Power
-    --print(text)
-    if text then
-      if text:match("|cFFE6CC80") then
-        --print('AP token!', text)
-        local power
-        if ttl4 then
-          local text = ttl4:GetText()
-          --print('tip line 4', text)
-          if text then
-            power = text:gsub("%p", ""):match("%d+")
-            power = tonumber(power)
-          end
-
-        end
-        return REWARD_ARTIFACT_POWER, "Interface\\ICONS\\inv_7xp_inscription_talenttome01", power, name, itemID, quality
-      elseif text:match("Item Level") then
-        --print('equipment!', text)
-        quantity = text:match("Item Level ([%d\+]+)")
-        return REWARD_GEAR, icon, quantity, name, itemID, quality
-      elseif  text:match("Crafting Reagent") then
-        qprint('|cFFFF4400it is a reagent', text)
-        return REWARD_REAGENT, icon, quantity, name, itemID, quality
-      end
-    end
-  end
-
-  if ttl3 then
-    local text = ttl3:GetText()
-    if text and text:match("Crafting Reagent") then
-      qprint('|cFFFF4400it is a reagent', text)
-      return REWARD_REAGENT, icon, quantity, name, itemID, quality
-    end
-  end
-  return 128, icon, quantity, name, itemID, quality
-end
-
--- pins are queued by their OnUpdate and are ostensibly already visible, we just need to fix the zoom and anchor
-function Module:UpdateQueuedPins()
-  print('|cFF00FF88UpdateQueuedPins()')
+-- re-anchors and scales pins that have had either of these changed due to data loading delays
+function Module:RefreshIfQueued()
+  print('|cFF00FF88RefreshIfQueued()')
   local pin = tremove(db.UpdatedPins)
   while pin do
     pin:CheckFilterRules()
@@ -539,83 +438,12 @@
   end
 end
 
--- create of update quest pins for a map and its underlying zones
-function Module:UpdateWorldQuests (mapID)
 
-  mapID = mapID or db.currentMapID
-  if not mapID then
-    -- info not available yet
-    return
-  end
+local msg = '|cFF00FF88WorldQuests:Refresh()|r|cFF00FFFF'
+function Module:Refresh(...)
 
-  local scalingConstant = 1
-  local style = DEFAULT_STYLE
-  if self.dataLoaded then
-    style = REWARD_TYPE_STYLES[self.rewardType]
-    scalingConstant = db.isContinentMap and 2 or 3
-  end
-
-  local pinScale = SCALE_FACTORS[scalingConstant]
-  print(pinScale)
-  self:SetScale(pinScale)
-  self.Overlay:SetScale(pinScale)
-
-    print('|cFF00FF88'..self:GetName()..':UpdateWorldQuests()|r', 'map:', mapID, 'realMap:', db.currentMapID)
-
-
-  self.isStale = nil
-  print('|cFF00FFFFContinent:|r', BROKEN_ISLES_ID, GetMapNameByID(BROKEN_ISLES_ID))
-  self.isRecursed = true
-  for i = 1, MC_GetNumZones(BROKEN_ISLES_ID) do
-    local submapID, name, depth = MC_GetZoneInfo(BROKEN_ISLES_ID, i)
-    local taskInfo = TQ_GetQuestsForPlayerByMapID(submapID, BROKEN_ISLES_ID)
-    if taskInfo then
-      local zoneName = GetMapNameByID(submapID)
-      print('|cFF00FFFF  Zone:|r', submapID, zoneName, #taskInfo)
-      db.QuestsByZone[submapID] = db.QuestsByZone[submapID] or {}
-      for i, info in ipairs(taskInfo) do
-        if HaveQuestData(info.questId) then
-          rprint('|cFF44FF44update|r', info.questId, zoneName)
-          local questID = info.questId
-          local pin = self:AcquirePin(questID)
-          local pin = db.QuestsByID[questID]
-          if pin then
-            pin.isStale = true
-            if pin.isPending then
-              self.isPending = true
-            end
-          end
-        else
-          rprint('|cFFFF4400no data|r', info.questId, zoneName)
-        end
-      end
-    end
-  end
-
-  self:GetBonusObjectives()
-
-  print('  hasUpdate:', self.isStale, 'isPending:', self.isPending, 'timer:', (self.OnNext and 'waiting' or ''))
-  --WorldPlan.isStale = (self.isStale or WorldPlan.isStale)
-  if self.isStale and self:IsVisible() then
-    self:Refresh()
-  end
-
-end
-
-function Module:Report()
-  for i, pin in ipairs(db.UsedPins) do
-    db:print(i, pin.questID, pin.title)
-  end
-
-  for id, pin in pairs(db.QuestsByID) do
-    db:print(id, pin.worldQuestType, pin.rewardType, pin.title)
-  end
-end
-
-local bountiesInitialized
-function Module:Refresh(...)
-  rprint('|cFF00FF88'..self:GetName()..':Refresh()|r', ...)
-  print('|cFF00FF88'..self:GetName()..':Refresh()|r', ...)
+  rprint(msg, ...)
+  print('|cFF00FF88WorldQuests:Refresh()|r', ...)
   if not self:IsVisible() then
     print('  not visible, flag for later')
     self.isStale = true
@@ -625,17 +453,23 @@
     numShown = 0
     return
   end
+  wprint('  '..msg)
 
+  layoutDirty = nil
+  scaleConstant = db.isContinentMap and 2 or 3
+  canTargetQuests = SpellCanTargetQuest()
 
-  wprint('  |cFF00FF88'..self:GetName()..':Refresh()|r')
-
-  scaleConstant = db.isContinentMap and 2 or 3
   for index, pin in pairs(db.QuestsByID) do
     pin.used = nil
   end
 
-  canTargetQuests = SpellCanTargetQuest()
   self:UpdateAnchors()
+  if bountiesDirty then
+    print('  bounties dirty, pushing that')
+    self:UpdateBounties()
+    bountiesDirty = nil
+  end
+
 
   -- calculate quests shown
   numShown = 0
@@ -663,9 +497,8 @@
   end
 
 
-  self:UpdateBountyInfo()
 
-  print(numShown, 'shown', numLoaded, 'loaded')
+  print(numShown, 'shown,', numLoaded, 'with data')
   if numShown > numLoaded then
     self.Status:Show()
   end
@@ -679,11 +512,11 @@
 function Module:RefreshIfChanged()
   local scaleCheck = WorldMapDetailFrame:GetScale()
   if scaleCheck ~= currentScale then
-    print('|cFF00FF88scale changed from', currentScale, 'to', scaleCheck)
-    self:Refresh()
+    print('|cFF00FF88RefreshIfChanged()|r map scale updated')
+    self:Refresh('WORLD_MAP_SCALE_CHANGED')
     currentScale = scaleCheck
-  elseif self.isStale then
-    print('|cFF00FF88isStale flag was set')
+  elseif self.isStale or layoutDirty then
+    print('|cFF00FF88RefreshIfChanged()|r layout is dirty')
     self:Refresh()
   end
 end
@@ -715,6 +548,33 @@
     --tinsert(self.UsedPositions, pin)
   end
 
+
+  local isCriteria, isBounty
+  if pin.factionID then
+    if db.BountiesByFactionID[pin.factionID] then
+      isCriteria = true
+    end
+    if selectedBountyQuestID then
+      isBounty = IsQuestCriteriaForBounty(pin.questID, selectedBountyQuestID) and true or nil
+    end
+  end
+
+  if (pin.isBounty ~= isBounty) or (pin.isCriteria ~= isCriteria) then
+    print(pin.isBounty, '~=', isBounty)
+    print(pin.isCriteria, '~=', isCriteria)
+    pin.isBounty = isBounty
+    pin.isCriteria = isCriteria
+    if pin:IsVisible() then
+      --print(' changed', pin.title)
+      --numUpdated = numUpdated + 1
+      pin:Refresh('BOUNTY_UPDATE')
+    else
+      --numHidden = numHidden + 1
+      pin.isStale = true
+    end
+    layoutDirty = true
+  end
+
   if self:IsVisible() and (pin.isStale) then
     pin:Refresh()
   end