view ObjectiveTracker/Achievements.lua @ 37:e84d645c8ab8

- revised the tracker update function to build its complete data list up front and use the values as points of comparison for determining possible out of place blocks, which will be iterated over afterward to remove what wasn't re-used - also entailed revising the exact role of global event handlers and function hooks, limiting their directions of communication so one doesn't end up calling the other multiple or inifinity times - schema handling polish
author Nenue
date Mon, 18 Apr 2016 07:56:23 -0400
parents 69d03f8e293e
children 1f8f9cc3d956
line wrap: on
line source
--- ${PACKAGE_NAME}
-- @file-author@
-- @project-revision@ @project-hash@
-- @file-revision@ @file-hash@
-- Created: 4/13/2016 7:48 PM
local B = select(2,...).frame
local T = B:RegisterModule("ObjectiveTracker", _G.VeneerObjectiveWrapper, 'BuffFrame')
local band = bit.band
local RemoveTrackedAchievement, GetAchievementLink = RemoveTrackedAchievement, GetAchievementLink
local AchievementFrame_ToggleAchievementFrame, AchievementFrame_SelectAchievement = AchievementFrame_ToggleAchievementFrame, AchievementFrame_SelectAchievement
local GetTime, GetAchievementNumCriteria, GetAchievementCriteriaInfo = GetTime, GetAchievementNumCriteria, GetAchievementCriteriaInfo
local GetNumTrackedAchievements, GetTrackedAchievements, GetAchievementInfo = GetNumTrackedAchievements, GetTrackedAchievements, GetAchievementInfo
local Default, Cheevs = T.DefaultHandler, T.Cheevs
local wipe = table.wipe
local print, bprint, lprint, iprint = B.print('Tracker'), B.print('Block'), B.print('Line'), B.print('iprint')

--- Data retrieval
Cheevs.GetNumWatched = function(self, targetID, isNew)
  local trackedList = {GetTrackedAchievements() }
  local numWatched, numAll = #trackedList, #self.WatchList
  print('    |cFF00FF88GetNumWatched:|r',self.name, numWatched, ' ('..numAll..' recorded)')
  local start = 1
  local limit = max(numAll, #trackedList)
  if targetID then
    if not isNew and self.Info[targetID] then
      -- if it's only an update, we can limit the scope
      start = self.Info[targetID].watchIndex
      print('    |cFF0088FFGetNumWatched: limit selection to['..start..'-'..limit..']')
      if self.InfoBlock[targetID] then
        self.InfoBlock[targetID]:Hide()
      end

    end
  end

  for index = start, limit do
    if index > numWatched then
      self.WatchList[index] = nil
      print('      % dropping watch entry #'..index)
    else
      local cheevID = trackedList[index]
      if not self.Info[cheevID] then
        self.Info[cheevID] = self:GetInfo(cheevID, index)
      else
        print('    |cFF00FFBBGetInfo:', cheevID, 'already pulled')
      end
      self:GetObjectives(cheevID)

      local info = self.Info[cheevID]
      local watchBlock = self.WatchBlock[cheevID]

      self.Info[cheevID].watchIndex = index
      self.WatchList[index] = info
    end
  end

  numAll = #self.WatchList
  return numWatched, numAll, self.WatchList
end
Cheevs.GetInfo = function(self, cheevID, watchIndex)
  --- step 1: confirm primary data and begin an entry if needed
  local id, name, points, completed, month, day, year, description, flags, icon, rewardText, isGuildAch, wasEarnedByMe, earnedBy = GetAchievementInfo(cheevID)
  if not id then return nil end

  print('      |cFF44AAFFGetInfo: pulling', id..',', name, earnedBy)


  self.Info[cheevID] = self.Info[cheevID] or {}
  local c = self.Info[cheevID]
  local rewards = {}

  c.schema = 'achievement'
  c.rewardInfo = rewards
  c.type = 'Cheevs'
  c.title = name
  c.points = points
  c.completed = completed
  c.month = month
  c.day = day
  c.year = year
  c.description = description
  c.flags = flags
  c.icon = icon
  c.rewardText = rewardText
  c.isGuildAch = isGuildAch
  c.wasEarnedByMe = wasEarnedByMe
  c.earnedBy = earnedBy

  local tagInfo = {}
  c.tagInfo = tagInfo
  c.watchIndex = watchIndex
  c.id = cheevID
  c.cheevID = cheevID

  self.Info[cheevID] = c
  self.WatchInfo[watchIndex] = c
  return c
end

Cheevs.GetObjectives = function(self, cheevID)
  local c = self.Info[cheevID]
  c.numObjectives = GetAchievementNumCriteria(cheevID)
  c.objectives = c.objectives or {}
  for i = 1, c.numObjectives do
    local description, type, completed, quantity, requiredQuantity, characterName, flags, assetID, quantityString, criteriaID = GetAchievementCriteriaInfo(cheevID, i)
    local line = c.objectives[i] or {}
    line.objectiveIndex = i
    line.cheevID = cheevID
    line.text = description
    line.type = type
    line.finished = completed
    line.value = quantity
    line.maxValue = requiredQuantity
    line.characterName = characterName
    line.flags = flags
    line.assetID = assetID
    line.quantityString = quantityString
    line.criteriaID = criteriaID
    c.objectives[i] = line

    print('        |cFF44FFDDGetObjectives:|r', i, type, description, quantityString)
  end
end

--- Content handlers
Cheevs.UpdateLine = function(handler, block, line, data)
  local print = B.print('CheevsLine')
  local attachment
  local text
  line.progress = 0
  print('  ', data.objectiveIndex,'|cFF0088FF-|r', data.objectiveType, data.text)
  if data.flags then
    if band(data.flags, 0x00000001) > 0 then
      line.format = "%d/%d"
      attachment = T.SetWidget(line, data, 'ProgressBar', data.criteriaID)
      attachment.value = data.value
      attachment.maxValue = data.maxValue
      attachment:SetParent(handler.frame)

      print(attachment:GetNumPoints())
      for i = 1, attachment:GetNumPoints() do
        print('  ',attachment:GetPoint(i))
      end
      attachment.status:SetFormattedText("%d/%d", data.value, data.maxValue)
      attachment:SetPoint('TOP', line, 'TOP')
      line.height = attachment.height
    elseif band(data.flags, 0x00000002) then
      line.widget = nil
      text = line.text
    else
      line.widget = nil
      line.displayColor = 'FFFFFF'
      text = line.text

    end
  else

    line.displayText = data.text
  end
  print('  |cFF00DD22UpdateLine:|r', data.type, data.quantityString, 'qty:', data.quantity, 'assetID:', data.assetID)
  return text, attachment, 'default'
end

Cheevs.Select = function(self, block)
  Cheevs.Link(self, block)
  T:Update(self.updateReasonModule, block.info.cheevID)
end

Cheevs.Remove = function(self, block)

  RemoveTrackedAchievement(block.info.cheevID)
end
Cheevs.OnMouseUp = function(self, button)

  Default.OnMouseUp(self, button)
end
Cheevs.Link = function(self, block)
  local achievementLink = GetAchievementLink(block.info.cheevID);
  if ( achievementLink ) then
    _G.ChatEdit_InsertLink(achievementLink);
  end
end

Cheevs.Open = function(self, block)

  if ( not _G.AchievementFrame ) then
    _G.AchievementFrame_LoadUI();
  end
  if ( not _G.AchievementFrame:IsShown() ) then
    _G.AchievementFrame_ToggleAchievementFrame();
  end
  _G.AchievementFrame_SelectAchievement(block.info.cheevID);
end