view ObjectiveTracker/BonusObjectiveData.lua @ 45:dd1ae565f559

Hooks and Handlers: - correct argument mix-ups for AcceptQuest/QUEST_ACCEPTED handlers; fixes auto-watch - respond to AcknowledgeAutoAcceptQuest; fixes lingering popups - include Popup and Quest trackers in the response code for CompleteQuest; fixes content artifacts following the rollover of repeating popups seen in Ashran - clean up wacky OnEvent header Layout - add alpha blend options QuestData - reset objectives data when a quest is in a completed state; keeps old data from ever reaching the Default.x code
author Nenue
date Tue, 26 Apr 2016 14:57:18 -0400
parents 9480bd904f4c
children
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 Default, Quest = T.DefaultHandler, T.Quest
local print = B.print('Tracker')
local lprint = B.print('Line')
local iprint = B.print('Info')
local Bonus = T.Bonus

local UnitName, GetRealmName, GetQuestObjectiveInfo, GetTasksTable, GetTaskInfo = UnitName, GetRealmName, GetQuestObjectiveInfo, GetTasksTable, GetTaskInfo
local GetMapNameByID, GetCurrentMapAreaID = GetMapNameByID, GetCurrentMapAreaID
local tinsert, ipairs, pairs, tostring, wipe = tinsert, ipairs, pairs, tostring, table.wipe
local GetQuestProgressBarPercent, PERCENTAGE_STRING, GetTime = GetQuestProgressBarPercent, PERCENTAGE_STRING, GetTime
local TASK_DISPLAY_TEST = 1 -- 1: normal (is nearby or on the map) 2: strict (is nearby) 3: data exists

--- Holds data for recently completed tasks
local completedTasks = {}

--- Returns a tasks table modified to include recently completed objectives
local InternalGetTasksTable = function()
  local print = Bonus.print
  local char = UnitName("player")
  local realm = GetRealmName()
  local tasks = GetTasksTable()

  for questID, data in pairs(Bonus.Info) do
    print('GetTasksTable', questID, #data.objectives)
    for i, o in ipairs(data.objectives) do
      print('GetTasksTable', questID, i, o.text)
    end
  end

  for questID, data in pairs(completedTasks) do
    if questID > 0  then
      local found = false
      for i = 1, #tasks do
        if tasks[i] == questID then
          found = true
          break
        end
      end
      -- if it's not part of the current table, then try to insert it where it was last found
      if not found then
        if data.watchIndex < #tasks then
          tinsert(tasks, data.watchIndex, data)
        else
          tinsert(tasks, data)
        end
      end
    end
  end
  return tasks
end

--- Returns an entry from the composed tasks table if possible, otherwise makes an API pull

local InternalGetTaskInfo = function(questID)
  if completedTasks[questID] then
    -- if it's a recently completed task, use the information stored for it
    return true, true, #completedTasks[questID].objectives
  else
    return GetTaskInfo(questID)
  end
end

--- Same as above but for the objective entries
local InternalGetQuestObjectiveInfo = function(questID, objectiveIndex)
  if ( completedTasks[questID] ) then
    print('using internal data')
    return completedTasks[questID].objectives[objectiveIndex], completedTasks[questID].objectiveType, true;
  else
    return GetQuestObjectiveInfo(questID, objectiveIndex, false);
  end
end

--- end redundant copy of silliness
------------------------------------------------------------------------------------------

Bonus.Completed = {}
Bonus.POI = {}
Bonus.Scenario = {}
Bonus.QuestBlock = {}
Bonus.WatchInfo = {}

local function CanShowTask(isInArea, isOnMap, existingTask, numObjectives)
  if TASK_DISPLAY_TEST == 1 then
    return (isInArea)
  elseif TASK_DISPLAY_TEST == 2 then
    return (isInArea and(isOnMap and existingTask))
  elseif TASK_DISPLAY_TEST == 3 then
    return true
  end
end

function Bonus:GetNumWatched ()

  local print = self.print
  print(self.name, self)

  local tasks = InternalGetTasksTable()
  local numWatched = 0
  local numAll = 0
  local existingTasks = {}
  wipe(self.WatchList)
  print('|cFF'..self.internalColor..'Bonus.GetNumWatched()|r', #tasks)
  print('InternalGetTaskInfo')

  for i, questID in ipairs(tasks) do
    local isInArea, isOnMap, numObjectives = InternalGetTaskInfo(questID)
    local existingTask = self.InfoBlock[questID]
    local displayObjectiveHeader = false;
    local displayTask = CanShowTask(isInArea, isOnMap, existingTask)
    if displayTask then
      print('TaskInfo', '|cFF00FF00showable objective list', questID)
      self.Info[questID] = self.Info[questID] or {}

      local t = self.Info[questID]
      if (isOnMap or isInArea) and existingTask then
        t.areaID = GetCurrentMapAreaID()
        local _
        t.mapName, _, _, t.isMicroDungeon, t.microDungeonMapName = GetMapInfo()
        print('InternalGetTaskInfo', 'map data', t.areaID, t.mapName)
      end

      local taskTitle
      t.id = questID
      t.objectives = {}
      local isComplete = true;
      for objectiveIndex = 1, numObjectives do
        local text, objectiveType, finished, displayAsObjective = InternalGetQuestObjectiveInfo(questID, objectiveIndex, false);
        displayObjectiveHeader = displayObjectiveHeader or displayAsObjective;
        if not taskTitle then
          if objectiveType == 'progressbar' and not text:match('^%d%+\\%d+') then
            taskTitle = text
            text = ''
          end
        end


        print('TaskObjective', text, objectiveType, finished, displayAsObjective)
        t.objectives[objectiveIndex] = t.objectives[objectiveIndex] or  {}
        local  o = t.objectives[objectiveIndex]

        o.index = objectiveIndex
        o.text = text
        o.type = objectiveType
        o.finished = finished
        o.displayAsObjective = displayAsObjective
        isComplete = (isComplete and finished)
      end

      T.SetRewards(t, questID)

      -- didn't get a name from progress bar? what about area name
      if not taskTitle then
        if isInArea then
          taskTitle = GetMapNameByID(GetCurrentMapAreaID())
        end
      end
      t.numObjectives = numObjectives
      t.isInArea = isInArea
      t.isOnMap = isOnMap
      t.existingTask = existingTask
      t.questID = questID
      t.id = questID
      t.taskIndex = i
      t.title = taskTitle
      t.isComplete = isComplete
      self.WatchList[i] = t
    elseif existingTask then
      print('TaskInfo', '|cFFFF4400hideable task', questID)
      existingTask:Hide()
    end


    print ('TaskInfo', i, '|cFFFFFF00'.. questID..'|r', '('..(isInArea and '|cFF88FF88' or '|cFF666666') .. 'isInArea|r', 'AND', (isOnMap and '|cFF88FF88' or '|cFF666666') .. 'isOnMap|r)', 'OR', (existingTask and '|cFF88FF88' or '|cFF666666') .. 'existingTask|r', (displayTask and '|cFF00FF00show|r' or '|cFFFF4400hide|r'))
  end


  self.numWatched = #self.WatchList
  self.numAll = #existingTasks
  return self.numWatched, self.numWatched, self.WatchList
end

Bonus.OnEvent = function(block, event, ...)
  if event == 'QUEST_LOG_UPDATE' then
    local info = block.info

    local isInArea, isOnMap, numObjectives = InternalGetTaskInfo(info.questID)
    if not CanShowTask(isInArea, isOnMap, block, numObjectives) then
      block:Hide()
    end
  end
end

Bonus.GetBlock = function(self, index)
  local block = Default.GetBlock(self, index)
  block:SetScript('OnEvent', self.OnEvent)
  block:RegisterEvent('QUEST_LOG_UPDATE')
  return block
end

--- info cleanup done when  turn-ins are detected
Bonus.OnTurnIn = function(self, block, questID, xp, money)
  --[=[
  local info = self.Info[questID]
  if info.rewardInfo and #info.rewardInfo >= 1 then
    for i, reward in ipairs(info.rewardInfo) do
      --[[
        type = 'item',
        index = i ,
        name = name,
        texture = texture,
        count = count,
        quality = quality,
        isUsable = isUsable
       ]]
      print(' reward ', i, ' ', reward.type, reward.name, reward.count)

    end
  end

  print('|cFFFF8800'..block:GetName()..':OnTurnIn call', questID, xp, money)
  local savedTasks = B.Conf.TasksLog or {}

  info.completedTime = GetTime()
  info.animate = true
  T.SetAnimate(self.updateReasonModule)
  savedTasks[questID] = {
    id = questID,
    title = info.title,
    finished = true,
    numObjectives = info.numObjectives,
    objectives = info.objectives,
    rewardInfo = info.rewardInfo,
  }
  B.Conf.TasksLog = savedTasks

  print(' ## CONF TASKLOG ##')
  for i, t in pairs(savedTasks[questID]) do
    print('    |cFFFFFF00'.. tostring(i)..'|r', t)

  end
  for o, j in ipairs(savedTasks[questID].objectives) do
    print('     |cFF00FFFF#'.. o ..'|r', j.type, j.finished)
  end

  print('adding', info.title, 'to cache')
  --]=]
end

Bonus.GetInfo = function(self, taskIndex)
  print(self.name, self)
  return self.WatchInfo[taskIndex]
end



--- Update hooks
Bonus.UpdateObjectives = function(handler, block, blockSchema)
  block.schema = blockSchema or 'default'
  local info = block.info
  block.title:SetText(info.title)


  Default.UpdateObjectives(handler, block)
  return blockSchema
end

Bonus.UpdateLine = function(handler, block, data)
  local info = block.info
  local print = lprint
  local text, attachment = '', nil
  if data.type == 'progressbar' then
    print('    |cFFFF44DDpercent='..tostring(GetQuestProgressBarPercent(info.questID)))
    local percent = 100
    if not data.finished then
      percent = GetQuestProgressBarPercent(info.questID)
    end
    data.value = percent
    data.maxValue = 100

    attachment = T.GetWidget(data, 'StatusBar', info.questID..'-'..data.index)
    attachment:SetParent(block)
    print(attachment:GetNumPoints())
    for i = 1, attachment:GetNumPoints() do
      print('  ',attachment:GetPoint(i))
    end

    attachment.value = percent
    attachment.maxValue = 100
    attachment.status:SetFormattedText(PERCENTAGE_STRING, percent)
    --attachment:SetParent(handler.frame)
    --print(attachment.status:GetText())
    print('    |cFFFF0022** text:|r', data.text, '|cFFFF0022value:|r', data.value, '|cFFFF0022max:|r', data.maxValue)
  end
  text = data.text
  return text, attachment, 'default'
end

Bonus.Select = function(handler, block)
  print(handler, block)
  handler:OnTurnIn(block, block.info.questID)
end
Bonus.Remove = function(self)

end