view ClassPlan.lua @ 1:232617b8bcd5

New Order Hall mission tracker: - record active missions and shipments across characters
author Nenue
date Thu, 13 Oct 2016 05:32:43 -0400
parents
children b8a19781f79b
line wrap: on
line source
SLASH_CLASSPLAN1 = "/classplan"
SLASH_CLASSPLAN2 = "/cp"
SlashCmdList.CLASSPLAN = function(args)
  if ClassOrderPlan:IsVisible() then
    ClassOrderPlan:Hide()
  else
    ClassOrderPlan:Show()
    DEFAULT_CHAT_FRAME:AddMessage('|cFF88FF00WorldPlan|r: Order Hall Panel')
  end
end

ClassOrderPlanCore = {
  blocks = {},
  playerFirst = true,
  timers = {}
}
ClassPlanBlockMixin = {
  followers = {},
  blocks = {},
}
local core, block = ClassOrderPlanCore, ClassPlanBlockMixin
local print = DEVIAN_WORKSPACE and function(...) print('ClassPlan', ...) end or nop

function core:OnLoad ()
  self:RegisterUnitEvent('UNIT_PORTRAIT_UPDATE', 'player')
  self:RegisterEvent('GARRISON_MISSION_STARTED')
  self:RegisterEvent('GARRISON_MISSION_FINISHED')
  self:RegisterEvent('PLAYER_LOGIN')
  self:RegisterEvent('PLAYER_ENTERING_WORLD')
end


function core:OnEvent (event, ...)
  if event == 'UNIT_PORTRAIT_UPDATE' then
    SetPortraitTexture(self.portrait, 'player')
  elseif event == 'PLAYER_LOGIN' then
    if not self.initialized then
      if IsLoggedIn() then
        WorldPlanData.OrderHall = WorldPlanData.OrderHall or {}
        self.data = WorldPlanData.OrderHall


        local name, realm = UnitName('player')
        realm  = realm or GetRealmName()
        self.profileKey = name .. '-' .. realm
        if not self.data[self.profileKey] then
          self.data[self.profileKey] = {}
        end
        self.profile = self.data[self.profileKey]

        self.initialized = true
        print('initialized')
      end
    end
  else
    self:Refresh ()
  end
end

function core:UpdateList()
  self.sortedMissions = self.sortedMissions or {}
  table.wipe(self.sortedMissions)

  for name, profile in pairs(self.data) do
    local isMine = (profile == self.profile)
    for index, data in pairs(profile.missions) do

      data.classColor = profile.classColor or {r = 0.7, g = 0.7, b =0.7}

      data.profileKey = name
      data.isMine = (profile == self.profile)
      tinsert(self.sortedMissions, data)
    end
  end

  for i, v in ipairs(self.sortedMissions) do
    print(i, v.missionEndTime, v.name)
  end


  table.sort(self.sortedMissions, function(a,b)
    local result = false
    if not a or not b then
      result = true
    else

      if (a.isMine ~= b.isMine) and self.playerFirst then
        result =  a.isMine
      else
        if (not b.missionEndTime) or (not a.missionEndTime) then
          print('missing article', b.missionEndTime, a.missionEndTime)
        end

        result = ( b.missionEndTime > a.missionEndTime)
      end
    end

    print('cmp', (b and (b.missionEndTime .. ' ' .. tostring(b.isMine)) or '-'), '>', (a and (a.missionEndTime .. ' ' .. tostring(a.isMine)) or '-'), result, n)
    return result
  end)
  self.isStale = nil

  local lastProfile
  local numItems = #self.sortedMissions
  for i, data in ipairs(self.sortedMissions) do
    local block = self.blocks[i]
    if not block then
      block = CreateFrame('Frame', nil, self, 'ClassPlanBlock')
      block:SetID(i)
      self.numBlocks = (self.numBlocks or 0) + 1

      if self.lastBlock then
        block:SetPoint('TOPLEFT', self.lastBlock, 'BOTTOMLEFT', 0, 0)
      else
        block:SetPoint('TOPLEFT', self.portrait, 'TOPRIGHT', 0, 0)
      end
      self.lastBlock = block
      self.blocks[i] = block
    end

    local r,g,b = 1, 1, 1
    if data.isRare then
      r,g,b = 0.1, 0.4, 1
    end
    if data.isMine then
      block.Icon:SetVertexColor(0,1,0,1)
    else
      block.Icon:SetVertexColor(1,1,1)
    end


    --block.missionData = data
    block.missionID = data.missionID
    block.missionEndTime = data.missionEndTime
    block.Icon:SetAtlas(data.typeAtlas, false)
    block.Label:SetText(data.name)
    block.Label:SetTextColor(r, g, b)

    if lastProfile ~= data.profileKey then
      block.Owner:SetText(data.profileKey)
      block.Owner:SetTextColor(data.classColor.r, data.classColor.g, data.classColor.b)
    else
      block.Owner:SetText(nil)
    end
    block.Background:SetColorTexture(data.classColor.r, data.classColor.g, data.classColor.b, 0.5)

    block:Show()
    lastProfile = data.profileKey
  end
  for i = numItems + 1, self.numBlocks do
    if self.blocks[i] then
      self.blocks[i]:Hide()
    end
  end
end

function core:OnShow()
  if self.isStale then
    print('updating items on show')
    self:UpdateList()
  end
end

function core:Refresh ()
  if not self.profile then
    return
  end

  self.items = C_Garrison.GetLandingPageItems(LE_GARRISON_TYPE_7_0)

  self.profile.missions = self.profile.missions or {}

  self.profile.classColor = RAID_CLASS_COLORS[select(2, UnitClass('player'))]


  print('|cFF0088FFLocal Scoop|r:', self.profileKey)
  if #self.items >= 1 then
    table.wipe(self.profile.missions)
    for index, data in ipairs(self.items) do
      print('', data.name)
      print('  |cFF00FF00', data.timeLeft .. '|r', date("%A %I:%m %p", data.missionEndTime))
      tinsert(self.profile.missions, data)
    end
  end

  if self:IsVisible() then
    self:UpdateList()
  else
    print('items update pending')
    self.isStale = true
  end
end

function block:OnUpdate()
  if self.missionEndTime then
    local timeLeft = self.missionEndTime - time()
    if timeLeft < 0 then
      self.TimeLeft:SetText('Complete!')
    else
      local days = floor(timeLeft/(24*3600))
      local hours = floor(mod(timeLeft, (24*3600)) / 3600)
      local minutes = floor(mod(timeLeft, 3600) / 60)
      local seconds = mod(timeLeft, 60)
      self.TimeLeft:SetText(
        ((days > 0) and (days .. ' d') or '') ..
            ((hours > 0) and (' '.. hours .. ' hr') or '')..
            ((minutes > 0) and (' ' .. minutes .. ' min' or ''))
        , 1,1,1)
    end
  else
    self.TimeLeft:SetText(self.missionEndTime)
  end
end