Advertisement
naeshy

SexyReputation.lua

Aug 19th, 2020
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.03 KB | None | 0 0
  1. local SexyReputations = LibStub("AceAddon-3.0"):NewAddon("Sexy Reputations", "AceEvent-3.0", "AceTimer-3.0", "AceConsole-3.0")
  2. local mod = SexyReputations
  3. local tooltip
  4.  
  5. local GetNumFactions = GetNumFactions
  6. local GetFactionInfo = GetFactionInfo
  7. local fmt = string.format
  8. local floor = math.floor
  9. local IsAltKeyDown = IsAltKeyDown
  10. local IsControlKeyDown = IsControlKeyDown
  11. local IsShiftKeyDown = IsShiftKeyDown
  12. local tconcat = table.concat
  13. local GetFriendshipReputation = GetFriendshipReputation
  14. local FL
  15.  
  16. local L        = LibStub("AceLocale-3.0"):GetLocale("SexyReputation", false)
  17. local LD       = LibStub("LibDropdown-1.0")
  18. local QTIP     = LibStub("LibQTip-1.0")
  19. local BAR      = LibStub("LibSimpleBar-1.0")
  20.  
  21. local ldb = LibStub("LibDataBroker-1.1"):NewDataObject(L["Sexy Reputation"],
  22.                                {
  23.                               type =  "data source",
  24.                               label = L["Sexy Reputation"],
  25.                               text = L["Factions"],
  26.                               icon = (UnitFactionGroup("player") == "Horde" and
  27.                                    [[Interface\Addons\SexyReputation\hordeicon]] or
  28.                                    [[Interface\Addons\SexyReputation\allianceicon]]),
  29.                                })
  30.  
  31.  
  32. mod.repTitles = {
  33.    FACTION_STANDING_LABEL1, -- Hated
  34.    FACTION_STANDING_LABEL2, -- Hostile
  35.    FACTION_STANDING_LABEL3, -- Unfriendly
  36.    FACTION_STANDING_LABEL4, -- Neutral
  37.    FACTION_STANDING_LABEL5, -- Friendly
  38.    FACTION_STANDING_LABEL6, -- Honored
  39.    FACTION_STANDING_LABEL7, -- Revered
  40.    FACTION_STANDING_LABEL8, -- Exalted
  41. }
  42.  
  43. -- names, used for looking up colors
  44. mod.colorIds = {
  45.    hated = 1, hostile = 2, unfriendly = 3, neutral = 4, friendly = 5, honored = 6, revered = 7, exalted = 8
  46. }
  47.  
  48. local minReputationValues =  {
  49.    [1] = -42000, -- Hated
  50.    [2] =  -6000, -- Hostile
  51.    [3] =  -3000, -- Unfriendly
  52.    [4] =      0, -- Neutral
  53.    [5] =   3000, -- Friendly
  54.    [6] =   9000, -- Honored
  55.    [7] =  21000, -- Revered
  56.    [8] =  42000, -- Exalted
  57. }
  58.  
  59.  
  60. -- table recycling
  61. local new, del, newHash, newSet, deepDel
  62. do
  63.    local list = setmetatable({}, {__mode='k'})
  64.    function new(...)
  65.       local t = next(list)
  66.       if t then
  67.      list[t] = nil
  68.      for i = 1, select('#', ...) do
  69.         t[i] = select(i, ...)
  70.      end
  71.      return t
  72.       else
  73.      return { ... }
  74.       end
  75.    end
  76.  
  77.    function newHash(...)
  78.       local t = next(list)
  79.       if t then
  80.      list[t] = nil
  81.       else
  82.      t = {}
  83.       end
  84.       for i = 1, select('#', ...), 2 do
  85.      t[select(i, ...)] = select(i+1, ...)
  86.       end
  87.       return t
  88.    end
  89.  
  90.    function del(t)
  91.       if type(t) ~= table then
  92.      return nil
  93.       end
  94.       for k,v in pairs(t) do
  95.      t[k] = nil
  96.       end
  97.       list[t] = true
  98.       return nil
  99.    end
  100.  
  101.    function deepDel(t)
  102.       if type(t) ~= "table" then
  103.      return nil
  104.       end
  105.       for k,v in pairs(t) do
  106.      t[k] = deepDel(v)
  107.       end
  108.       return del(t)
  109.    end
  110. end
  111.  
  112. function mod:OnInitialize()
  113.    mod.db = LibStub("AceDB-3.0"):New("SexyRepDB", mod.defaults, "Default")
  114.    mod.gdb = mod.db.global
  115.    mod.cdb = mod.db.char
  116.    FL = mod.gdb.factionLookup
  117.  
  118.    mod.sessionFactionChanges = new()
  119.    mod.factionGainsCache = new()
  120.    mod:SetDefaultColors()
  121. end
  122.  
  123. function mod:OnEnable()
  124.    mod:RegisterEvent("COMBAT_TEXT_UPDATE")
  125.    mod:RegisterEvent("QUEST_TURNED_IN")
  126.    mod:ScheduleTimer("UpdateLDBText", 3)
  127.    mod:ScheduleTimer("ScanFactions", 5)
  128. end
  129.  
  130. function mod:OnDisable()
  131.    mod:UnregisterEvent("COMBAT_TEXT_UPDATE");
  132. end
  133.  
  134. -- This transforms the faction name to an ID which is cached.
  135. -- This means the data storage will be smaller. The faction
  136. -- ID is unique to a computer and cannot be shared with others.
  137. function mod:FactionID(name)
  138.    if type(name) == "number" then return name end
  139.    local id = FL[name]
  140.    if not id then
  141.       id = (mod.gdb.numFactions or 0) + 1
  142.       mod.gdb.numFactions = id
  143.       FL[name] = id
  144.    end
  145.    return id
  146. end
  147.  
  148. function mod:ScanFactions(toggleActiveId)
  149.    local foldedHeaders = new()
  150.    
  151.    mod.allFactions = deepDel(mod.allFactions) or new()
  152.    mod.factionIdToIdx = del(mod.factionIdToIdx) or new()
  153.    mod.factionGainsCache = deepDel(mod.factionGainsCache) or new()
  154.  
  155.    -- Iterate through the factions until we run out. We need to unfold
  156.    -- any folded header, which changes the number of factions, so we just
  157.    -- keep iterating until GetFactionInfo return nil
  158.    local fr = mod.cdb.fr
  159.  
  160.    for idx = 1, 500 do
  161.       local name, description, standingId, bottomValue, topValue, earnedValue, atWarWith,
  162.       canToggleAtWar, isHeader, isCollapsed, hasRep, isWatched, isChild, factionId = GetFactionInfo(idx)
  163.      
  164.       local isParagon, paraVal, paraThres, paraRewardPending
  165.      
  166.       --check if paragon and grab info
  167.       if factionId then
  168.           isParagon = C_Reputation.IsFactionParagon(factionId)
  169.          
  170.           if isParagon then    
  171.             paraVal, paraThres, _, paraRewardPending, _ = C_Reputation.GetFactionParagonInfo(factionId)      
  172.           end
  173.       end
  174.      
  175.       local nextName = GetFactionInfo(idx + 1)
  176.       if name == nextName then break end -- bugfix
  177.       if not name then  break end -- last one reached
  178.       local friendId, friendRep, friendMaxRep, _, friendshipText, _, friendTextLevel, friendThresh, nextFriendThresh = GetFriendshipReputation and GetFriendshipReputation(factionId);
  179.       local isCapped
  180.       if (friendId ~= nil) then
  181.          if nextFriendThresh then
  182.             bottomValue = friendThresh
  183.             topValue = nextFriendThresh
  184.             earnedValue = friendRep
  185.          else
  186.             bottomValue, topValue, earnedValue = 0, 1, 1
  187.             isCapped = true
  188.          end
  189.       end
  190.      
  191.       local faction = newHash("name", name,
  192.                   "desc", description,
  193.                   "bottomValue", bottomValue,
  194.                   "topValue", topValue,
  195.                   "reputation", earnedValue,
  196.                   "isHeader", isHeader,
  197.                   "standingId", standingId,
  198.                   "hasRep", hasRep or earnedValue ~= 0,
  199.                   "isParagon", isParagon or false,
  200.                   "paraVal", paraVal or nil,
  201.                   "paraThresh", paraThres or nil,
  202.                   "paraRewardPending", paraRewardPending or nil,
  203.                   "isChild", isChild,
  204.                               "friendId", friendId,
  205.                               "friendshipText", friendshipText,
  206.                               "friendTextLevel", friendTextLevel,
  207.                               "friendIsCapped", isCapped,
  208.                   "id", mod:FactionID(name))
  209.       mod.allFactions[idx] = faction
  210.       mod.factionIdToIdx[faction.id] = idx
  211.  
  212.       if faction.id == toggleActiveId then
  213.      local isInActive = IsFactionInactive(idx)
  214.      if isInActive then
  215.         SetFactionActive(idx)
  216.      else
  217.         SetFactionInactive(idx)
  218.      end
  219.      mod:ScanFactions() -- we need to rescan fully..
  220.      return
  221.       end
  222.  
  223.       if isHeader and isCollapsed then
  224.      foldedHeaders[idx] = true
  225.      ExpandFactionHeader(idx)
  226.       end
  227.       if fr and faction.name == FACTION_INACTIVE then
  228.      mod.cdb.hf[faction.id] = true
  229.       end
  230.    end
  231.  
  232.    mod.cdb.fr = false
  233.    
  234.    -- Restore factions folded states
  235.    for id = #mod.allFactions, 1, -1 do
  236.       if foldedHeaders[idx] then
  237.      CollapseFactionHeader(idx)
  238.       end
  239.    end
  240.    del(foldedHeaders)
  241. end
  242.  
  243. function mod:GetDate(delta)
  244.    local dt = date("*t", time()-(delta or 0))
  245.    return dt.year * 10000 + dt.month * 100 + dt.day
  246. end
  247.  
  248. function mod:ReputationLevelDetails(faction)
  249.    local reputation, standingId, friendId = faction.reputation, faction.standingId, faction.friendId
  250.    local sc, color, rep, title, colorId
  251.    rep = reputation - faction.bottomValue
  252.    if friendId then
  253.       title = faction.friendTextLevel
  254.       colorId = mod.colorIds.friendly
  255.    else
  256.       title = mod.repTitles[standingId]
  257.       colorId = standingId
  258.    end
  259.    sc = mod.gdb.colors[colorId]
  260.    if mod.gdb.colorFactions then
  261.       color = fmt("%02x%02x%02x", floor(sc.r*255), floor(sc.g*255), floor(sc.b*255))
  262.    else
  263.       color = "ffffff"
  264.    end
  265.    return color, rep, title, colorId
  266. end
  267.  
  268. function mod:GetGainsSummary(id)
  269.    local today = mod:GetDate()
  270.    local newlyCalculated = false
  271.    local fc = mod.factionGainsCache[today]
  272.    if not fc then
  273.       -- Either we changed day, in which case we need to recalculate
  274.       -- or it's new and it doesn't matter
  275.       mod.factionGainsCache = deepDel(mod.factionGainsCache) or new()
  276.       mod.factionGainsCache[today] = new()
  277.       fc = mod.factionGainsCache[today]
  278.    end
  279.    if not fc[id] then
  280.       newlyCalculated = true
  281.       local todayDate = mod:GetDate()
  282.       local yesterDate = mod:GetDate(86400)
  283.       local fh = mod.cdb.factionHistory
  284.       local todayChange = fh[todayDate] and fh[todayDate][id] or 0
  285.       local yesterChange = fh[yesterDate] and fh[yesterDate][id] or 0
  286.       local weekChange = (todayChange or 0) + (yesterChange or 0)
  287.       for day = 2,6 do
  288.      local dayChange = fh[mod:GetDate(day*86400)] -- going back in time
  289.      if dayChange and dayChange[id] then
  290.         weekChange = weekChange + dayChange[id]
  291.      end
  292.       end
  293.       local monthChange = weekChange
  294.       for day = 7, 29 do
  295.      local dayChange = fh[mod:GetDate(day*86400)] -- going back in time
  296.      if dayChange and dayChange[id] then
  297.         monthChange = monthChange + dayChange[id]
  298.      end
  299.       end
  300.       fc[id] = newHash("today", todayChange,
  301.                "yesterday", yesterChange,
  302.                "week", weekChange,
  303.                "month", monthChange,
  304.                "changed", todayChange ~= 0 or yesterChange ~= 0
  305.               or weekChange ~= 0 or monthChange ~= 0)
  306.    end
  307.    return fc[id], newlyCalculated
  308. end
  309.  
  310. ---------------------------------------------------
  311. -- LDB Display and display utility methods
  312.  
  313. local function _addIndentedCell(tooltip, icon, text, indentation, font, func, arg)
  314.    local y, x = tooltip:AddLine(icon)
  315.    tooltip:SetCell(y, 2, text, font or tooltip:GetFont(), "LEFT", 1, nil, indentation)
  316.    if func then
  317.       tooltip:SetLineScript(y, "OnMouseUp", func, arg)
  318.    end
  319.    return y, x
  320. end
  321.  
  322. local function c(text, color)
  323.    text = text or ""
  324.    return fmt("|cff%s%s|r", color, text)
  325. end
  326. local function delta(number, zero)
  327.    if not number or (not zero and number == 0) then
  328.       return ""
  329.    end
  330.    if number < 0 then
  331.       return fmt("|cffff2020%d|r", number)
  332.    elseif number > 0 then
  333.       return fmt("|cff00af00+%d|r", number)
  334.    else
  335.       return "|cffcfcfcf0|r"
  336.    end
  337. end
  338.  
  339. local function _plusminus(folded)
  340.    return fmt("|TInterface\\Buttons\\UI-%sButton-Up:18|t", folded and "Plus" or "Minus")
  341. end
  342.  
  343. local function _showFactionInfoTooltip(frame, faction)
  344.    if mod.gdb.showTooltips then
  345.       local tooltip = QTIP:Acquire("SexyRepFactionTooltip")
  346.       if faction.hasRep or (faction.desc and faction.desc ~= '') then
  347.      local y
  348.      tooltip:SetColumnLayout(faction.hasRep and 2 or 1, "LEFT", "RIGHT")
  349.      tooltip:Clear()
  350.          local header = faction.name
  351.          if faction.friendTextLevel then
  352.             header = header .. " - "..faction.friendTextLevel.." ("..faction.standingId.." / 8)"           
  353.          end
  354.      tooltip:AddHeader(c(header, "ffd200"))
  355.      if faction.desc and faction.desc ~= '' then
  356.         tooltip:SetCell((tooltip:AddLine()), 1, faction.desc, tooltip:GetFont(), "LEFT", 1, nil, nil, 0, 300, 50)
  357.         tooltip:AddLine(" ")
  358.      end
  359.      if faction.friendshipText and faction.friendshipText ~= '' then
  360.         tooltip:SetCell((tooltip:AddLine()), 1, faction.friendshipText, tooltip:GetFont(), "LEFT", 1, nil, nil, 0, 300, 50)
  361.         tooltip:AddLine(" ")
  362.      end
  363.            
  364.      if faction.hasRep then
  365.         -- Show recent reputtion history
  366.         local sessionChange = mod.sessionFactionChanges[faction.id] or 0
  367.         local gs = mod:GetGainsSummary(faction.id)
  368.         y = tooltip:AddHeader()
  369.         if sessionChange ~= 0 or gs.changed then
  370.            tooltip:SetCell(y, 1, c(L["Recent reputation changes"], "ffd200"), "CENTER", 2)
  371.            tooltip:AddSeparator(1)
  372.            tooltip:AddLine(L["Session"], delta(sessionChange, true))
  373.            tooltip:AddLine(L["Today"], delta(gs.today, true))
  374.            tooltip:AddLine(L["Yesterday"], delta(gs.yesterday, true))
  375.            tooltip:AddLine(L["Last Week"], delta(gs.week, true))
  376.            tooltip:AddLine(L["Last Month"], delta(gs.month, true))
  377.  
  378.                local color, rep, repTitle = mod:ReputationLevelDetails(faction)
  379.                if not faction.friendId then
  380.                   local remaining = 42999 - faction.bottomValue - rep
  381.                   if remaining > 0 then
  382.                      tooltip:AddLine(L["Remaining"], remaining)
  383.                   end
  384.                   local repetitions
  385.                   if gs.today > 0 then
  386.                      repetitions = remaining/gs.today
  387.                   elseif gs.yesterday > 0 then
  388.                      repetitions = remaining/gs.yesterday
  389.                   end
  390.                   if repetitions then
  391.                      tooltip:AddLine(L["Repetitions"], string.format("%.2f", repetitions))
  392.                   end
  393.                end
  394.         else
  395.            tooltip:SetColumnLayout(1, "LEFT")
  396.            tooltip:SetCell(y, 1, c(L["Recent reputation changes"], "ffd200"))
  397.            tooltip:AddSeparator(1)
  398.            y = tooltip:AddLine(L["No changes recorded in the last 30 days."])
  399.         end
  400.      end
  401.      
  402.      if mod.cdb.watchedFaction == faction.id then
  403.         tooltip:AddLine(" ")
  404.         tooltip:AddSeparator(1)
  405.         tooltip:AddLine(c(L["This faction is currently being tracked."], "ffff00"))
  406.      end
  407.      tooltip:SetPoint("TOPLEFT", frame, "TOPRIGHT", 10, 0)
  408.      tooltip:SetFrameLevel(frame:GetFrameLevel()+1)
  409.      tooltip:SetClampedToScreen(true)
  410.      tooltip:Show()
  411.      tooltip:SetAutoHideDelay(0.25, frame)
  412.       else
  413.      QTIP:Release(tooltip)
  414.       end
  415.    end
  416. end
  417.  
  418. local function _factionOnClick(frame, faction, button)
  419.    if button == "LeftButton" then
  420.       if IsAltKeyDown() then
  421.      if faction.hasRep then
  422.         if mod.cdb.watchedFaction == faction.id then
  423.            mod.cdb.watchedFaction = nil
  424.         else
  425.            mod.cdb.watchedFaction = faction.id
  426.         end
  427.         mod:UpdateLDBText()
  428.      end
  429.       elseif IsControlKeyDown() and IsShiftKeyDown() then
  430.      mod:ScanFactions(faction.id)
  431.       elseif faction.isHeader then
  432.      mod.cdb.hf[faction.id] = not mod.cdb.hf[faction.id] or nil
  433.       end
  434.    end
  435.    ldb.OnEnter() -- redraw
  436. end
  437.  
  438. function ldb.OnEnter(frame)
  439.    tooltip = QTIP:Acquire("SexyRepTooltip")
  440.    tooltip:EnableMouse(true)
  441.  
  442.    local numCols = 2
  443.  
  444.    local showRep = mod.gdb.repTextStyle ~= mod.TEXT_STYLE_STANDING and mod.gdb.repStyle == mod.STYLE_TEXT
  445.    local showStanding = mod.gdb.repTextStyle ~= mod.TEXT_STYLE_REPUTATION and mod.gdb.repStyle == mod.STYLE_TEXT
  446.    local showRepBar = mod.gdb.repStyle == mod.STYLE_BAR
  447.    local showPercentage = mod.gdb.showPercentage
  448.    local showGains = mod.gdb.showGains
  449.    local colorFactions = mod.gdb.colorFactions
  450.    
  451.    if showRepBar then
  452.       numCols = numCols + 1
  453.    else
  454.       if showRep then numCols = numCols + 3 end
  455.       if showStanding then numCols = numCols + 1 end
  456.    end
  457.    if showPercentage then numCols = numCols + 1 end
  458.    if showGains then numCols = numCols + 2 end
  459.    
  460.    tooltip:Clear()
  461.    tooltip:SetColumnLayout(numCols, "LEFT")
  462.    
  463.    if frame then
  464.       tooltip:SetAutoHideDelay(0.5, frame)
  465.    end
  466.    
  467.    if not mod.allFactions or not #mod.allFactions then
  468.       mod:ScanFactions()
  469.    end
  470.    
  471.    local y, x
  472.  
  473.    y = tooltip:AddHeader("", c(L["Faction"], "ffff00"))
  474.    x = 3
  475.    if showRepBar then
  476.       tooltip:SetCell(y, x, c(L["Standing"], "ffff00"), "CENTER") x = x + 1
  477.    else
  478.       if showStanding then
  479.      tooltip:SetCell(y, x, c(L["Standing"], "ffff00"), "LEFT") x = x + 1
  480.       end
  481.       if showRep then
  482.      tooltip:SetCell(y, x, c(L["Reputation"], "ffff00"), "CENTER", 3) x = x + 3
  483.       end
  484.    end
  485.    if showPercentage then
  486.       tooltip:SetCell(y, x, c("%", "ffff00"), "CENTER") x = x + 1
  487.    end
  488.    if showGains then
  489.       tooltip:SetCell(y, x, c(L["Session"], "ffff00"), "CENTER") x = x + 1
  490.       tooltip:SetCell(y, x, c(L["Today"], "ffff00"), "CENTER") x = x + 1
  491.    end
  492.    tooltip:AddSeparator(1)
  493.  
  494.    local skipUntilHeader, skipUntilChildHeader
  495.    local isTopLevelHeader, isChildHeader
  496.    local todaysDate = mod:GetDate()
  497.    local showOnlyChanged = mod.gdb.showOnlyChanged
  498.    local hideExalted = mod.gdb.hideExalted
  499.    local watchedFaction = mod.cdb.watchedFaction
  500.    local gridLines = mod.gdb.gridLines
  501.    local indent, isTopLevelHeader, isChildHeader, sessionChange, today, showRow
  502.    for id, faction in ipairs(mod.allFactions) do
  503.       indent = 0
  504.       isTopLevelHeader = faction.isHeader and not faction.isChild
  505.       isChildHeader = faction.isHeader and faction.isChild
  506.      
  507.       sessionChange = mod.sessionFactionChanges[faction.id]
  508.       today = mod.cdb.factionHistory[todaysDate] and mod.cdb.factionHistory[todaysDate][faction.id];
  509.      
  510.       showRow = true
  511.       -- calculate whether this row should be displayed. Split out this way
  512.       -- so it's possible to understand what it's filtering and why
  513.      
  514.       if skipUntilHeader and not isTopLevelHeader then
  515.      showRow = false
  516.       elseif skipUntilChildHeader and not (isTopLevelHeader or isChildHeader) then
  517.      showRow = false
  518.       elseif showOnlyChanged and not (sessionChange or today) then
  519.      showRow = false
  520.       elseif hideExalted and faction.standingId == 8 then
  521.          showRow = false
  522.       end
  523.       if showRow then
  524.      local title, folded
  525.      if not showOnlyChanged then
  526.         if faction.isChild then indent = 20 end
  527.         if not faction.isHeader then indent = indent + 20 end
  528.         folded = faction.isHeader and mod.cdb.hf[faction.id]
  529.         local pm = _plusminus(folded)
  530.         title = faction.isHeader and fmt("%s %s", pm, faction.name) or c(faction.name, "ffd200")
  531.      else
  532.         title = faction.isHeader and faction.name or c(faction.name, "ffd200")
  533.      end
  534.      local color, rep, repTitle, colorId = mod:ReputationLevelDetails(faction)
  535.      local font
  536.  
  537.      local barColor = mod.gdb.colors[colorId]
  538.  
  539.      local icon = ""
  540.      if watchedFaction == faction.id then
  541.         icon = [[|TInterface\Icons\Spell_Shadow_EvilEye:20|t]]
  542.      end
  543.      y = _addIndentedCell(tooltip, icon, title, indent, font, _factionOnClick, faction)
  544.  
  545.      tooltip:SetLineScript(y, "OnEnter", _showFactionInfoTooltip, faction)
  546.      tooltip:SetLineScript(y, "OnLeave", nil)
  547.      
  548.      if not faction.isHeader or faction.hasRep then
  549.         x = 3          
  550.         local maxValue = faction.topValue-faction.bottomValue
  551.         local paraIcon = [[|TInterface\Icons\Inv_legioncircle_paragoncache_argussianreach:25|t]]               
  552.        
  553.         --Paragon adjustments
  554.         if faction.isParagon then
  555.             repTitle = "Paragon"               
  556.             barColor = {r=0, g=171/255, b=240/255}
  557.             color = "00ABF0"
  558.                    
  559.             rep = faction.paraVal % faction.paraThresh
  560.             maxValue = faction.paraThresh
  561.         end
  562.        
  563.         if maxValue == 0 then
  564.            maxValue = 21000 -- Exalted rep has no max value
  565.            rep = 21000
  566.         end
  567.  
  568.          -- "RIGHT", "CENTER", "RIGHT", "RIGHT")
  569.         if showStanding then
  570.             if faction.paraRewardPending then              
  571.                 tooltip:SetCell(y, x, paraIcon, "CENTER")
  572.             else
  573.                 tooltip:SetCell(y, x, c(repTitle, color), "LEFT")
  574.             end
  575.            x = x + 1           
  576.         end
  577.                
  578.         if showRep then
  579.             tooltip:SetCell(y, x, tostring(rep), "RIGHT") x = x + 1
  580.             tooltip:SetCell(y, x, "/", "CENTER") x = x + 1
  581.             tooltip:SetCell(y, x, tostring(maxValue), "RIGHT") x = x + 1           
  582.         end
  583.         if showRepBar then
  584.        
  585.             if faction.paraRewardPending then
  586.                 tooltip:SetCell(y, x, paraIcon, "CENTER")      
  587.             else           
  588.                 tooltip:SetCell(y, x, repTitle, "CENTER", mod.barProvider, barColor, rep, maxValue, 120, 12)       
  589.                 local xx, yy = x, y
  590.                
  591.                 tooltip:SetLineScript(y, "OnEnter", function(frame, factionid)
  592.                                       local idx = mod.factionIdToIdx[factionid]
  593.                                       local faction
  594.                                       if idx then
  595.                                         local faction = mod.allFactions[idx]
  596.                                         if faction then                            
  597.                                             tooltip:SetCell(yy, xx, fmt("%d / %d", rep, maxValue), "CENTER", mod.barProvider, barColor, rep, maxValue, 120, 12)                        
  598.                                            
  599.                                             _showFactionInfoTooltip(frame, faction)
  600.                                         end
  601.                                       end
  602.                                     end, faction.id)
  603.                 tooltip:SetLineScript(y, "OnLeave", function(frame, factionid)
  604.                                   local idx = mod.factionIdToIdx[factionid]
  605.                                   if idx then
  606.                                  local faction = mod.allFactions[idx]
  607.                                  if faction then
  608.                                     -- Breaks encapsulation but.. otherwise it breaks the code
  609.                                     local lines = tooltip.lines and tooltip.lines[yy]
  610.                                     if lines and lines.cells and lines.cells[xx] then
  611.                                        tooltip:SetCell(yy, xx, repTitle, "CENTER", mod.barProvider, barColor, rep, maxValue, 120, 12)
  612.                                     end
  613.                                  end
  614.                                   end
  615.                                end, faction.id)
  616.             end
  617.                                
  618.             x = x + 1
  619.         end
  620.         if showPercentage then
  621.            tooltip:SetCell(y, x, fmt("%.0f%%", (100.0*rep / maxValue)), "RIGHT") x = x + 1
  622.         end
  623.         if showGains then
  624.            tooltip:SetCell(y, x, delta(sessionChange), "CENTER") x = x + 1
  625.            tooltip:SetCell(y, x, delta(today), "CENTER") x = x + 1
  626.         end
  627.         if (sessionChange or today) and colorFactions and not showOnlyChanged then
  628.            tooltip:SetLineColor(y, 1, 1, 1, 0.2)
  629.         end
  630.         if gridLines then
  631.            tooltip:AddSeparator(0.5, 1, 1, 1, 0.5)
  632.         end
  633.      end
  634.      if folded then
  635.         if faction.isChild then
  636.            skipUntilChildHeader = true
  637.            skipUntilHeader = nil
  638.         else
  639.            skipUntilChildHeader = nil
  640.            skipUntilHeader = true
  641.         end
  642.      else
  643.         skipUntilChildHeader = nil
  644.         skipUntilHeader = nil
  645.      end
  646.       end
  647.    end
  648.  
  649.    tooltip:AddLine(" ")
  650.    tooltip:AddSeparator(1)
  651.    y = tooltip:AddLine()
  652.    tooltip:SetCell(y, 1, c(L["Using the faction tooltip:"], "ffff00"), "LEFT", numCols)
  653.    y = tooltip:AddLine("")
  654.    tooltip:SetCell(y, 2, c(L["Click:"], "eda55f") .. " "..c(L["Fold / unfold faction headers."], "ffd200"), "LEFT", numCols-1)
  655.    y = tooltip:AddLine("")
  656.    tooltip:SetCell(y, 2, c(L["Alt-Click:"], "eda55f").. " "..c(L["Toggle faction tracking state on and off."], "ffd200"), "LEFT", numCols-1)
  657.    y = tooltip:AddLine("")
  658.    tooltip:SetCell(y, 2, c(L["Shift+Ctrl-Click:"], "eda55f").. " "..c(L["Toggle faction inactive state."], "ffd200"), "LEFT", numCols-1)
  659.    
  660.    if frame then
  661.       tooltip:SmartAnchorTo(frame)
  662.    end
  663.    tooltip:SetScrollStep(100)
  664.    tooltip:UpdateScrolling(mod.gdb.maxHeight)
  665.    tooltip:Show()
  666. end
  667.  
  668. function ldb.OnClick(frame, button)
  669.    if button == "LeftButton" then
  670.       --mod:ToggleConfigDialog()
  671.    elseif button == "RightButton" then
  672.       -- First hide the tooltip
  673.       local tooltip = QTIP:Acquire("SexyRepTooltip")
  674.       QTIP:Release(tooltip)
  675.  
  676.       local menu = LD:OpenAce3Menu(mod.options)
  677.       menu:SetPoint("TOPLEFT", frame, "BOTTOMLEFT", 0, 0)
  678.       menu:SetFrameLevel(frame:GetFrameLevel() + 50)
  679.       menu:SetClampedToScreen(true)
  680.    end
  681. end
  682.  
  683. function ldb.OnLeave(frame)
  684.    --   if ldb.tooltip then
  685.    --      QTIP:Release(ldb.tooltip)
  686.    --      ldb.tooltip = nil
  687.    --   end
  688. end
  689.  
  690. function mod:UpdateLDBText()
  691.    local text = ""
  692.    local gdb = mod.gdb
  693.    if not mod.cdb.watchedFaction then
  694.       ldb.text = L["Factions"]
  695.       return
  696.    end
  697.    
  698.    if not mod.allFactions then
  699.       mod:ScanFactions()
  700.    end
  701.    local idx = mod.factionIdToIdx[mod.cdb.watchedFaction]
  702.    local faction =  mod.allFactions[idx]
  703.    if not faction then
  704.       ldb.text = L["Factions"]
  705.       return
  706.    end
  707.    
  708.    local fields = new()
  709.    
  710.    if gdb.trackName then      
  711.       fields[1] = faction.name
  712.    end
  713.    
  714.    local color, rep, repTitle = mod:ReputationLevelDetails(faction)
  715.    local maxValue = faction.topValue - faction.bottomValue
  716.    
  717.    if faction.isParagon then
  718.         repTitle = "Paragon"                       
  719.         color = "00ABF0"
  720.                
  721.         rep = faction.paraVal % faction.paraThresh
  722.         maxValue = faction.paraThresh
  723.     end
  724.    
  725.     if maxValue == 0 then
  726.        maxValue = 21000 -- Exalted rep has no max value
  727.        rep = 21000
  728.     end
  729.  
  730.    
  731.    if gdb.trackStanding then
  732.       fields[#fields+1] = c(repTitle, color)
  733.    end
  734.    
  735.    if gdb.trackRep then
  736.       fields[#fields+1] = fmt("%d/%d", rep, maxValue)
  737.    end
  738.    
  739.    if gdb.trackPercentage then
  740.       fields[#fields+1] = fmt("|cffffd200%.1f%%|r", 100.0 * rep / maxValue)
  741.    end
  742.    
  743.    if gdb.trackGains and mod.sessionFactionChanges[faction.id] then
  744.       fields[#fields+1] = delta(mod.sessionFactionChanges[faction.id], true)
  745.    end
  746.    
  747.    ldb.text = tconcat(fields, " - ")
  748. end
  749.  
  750. -----------------------
  751. --- EVENT HANDLING
  752. do
  753.    local factionScanTimer
  754.    
  755.    function mod:COMBAT_TEXT_UPDATE(event, type, faction, amount)
  756.         if type == "FACTION" then          
  757.        
  758.             if factionScanTimer then
  759.                 mod:CancelTimer(factionScanTimer, true)
  760.             end
  761.            
  762.             factionScanTimer = mod:ScheduleTimer("ScanForFactionChanges", 1)
  763.         end
  764.    end
  765.    
  766.     function mod:QUEST_TURNED_IN(event, questID, xp, money)
  767.         --rescan for paragon status change
  768.         mod:ScanFactions()
  769.     end
  770.  
  771.    function mod:ScanForFactionChanges()
  772.       local previousFactionData = mod.allFactions
  773.       factionScanTimer = nil
  774.       mod.allFactions = nil
  775.       mod:ScanFactions()
  776.      
  777.       if not previousFactionData then return end -- can't do anything, had no data before
  778.      
  779.       local date = mod:GetDate()
  780.  
  781.       local today =  mod.cdb.factionHistory[date] or new()
  782.       mod.cdb.factionHistory[date] = today
  783.  
  784.       for _,faction in ipairs(previousFactionData) do
  785.      local idx = mod.factionIdToIdx[faction.id] -- required since faction orders might have changed
  786.      if idx then
  787.         local newFaction = mod.allFactions[idx]
  788.         if newFaction.reputation ~= faction.reputation then
  789.            -- Rep change occurred
  790.            local amount = newFaction.reputation - faction.reputation
  791.            mod.sessionFactionChanges[faction.id] = (mod.sessionFactionChanges[faction.id] or 0) + amount
  792.            today[faction.id] = (today[faction.id] or 0) + amount
  793.  
  794.            -- Update the cached rep changes here, if needed.
  795.            local gs,upToDate = mod:GetGainsSummary(faction.id)
  796.            if not upToDate then
  797.           gs.changed = true
  798.           gs.today = gs.today + amount
  799.           gs.week  = gs.week + amount
  800.           gs.month = gs.month + amount
  801.            end
  802.            if faction.id == mod.cdb.watchedFaction then
  803.           mod:UpdateLDBText()
  804.            end
  805.         end
  806.      end
  807.       end
  808.       deepDel(previousFactionData)
  809.    end
  810. end
  811.  
  812.  
  813. -- Set up a custom provider for the bars
  814. local barProvider, barCellPrototype = QTIP:CreateCellProvider()
  815. mod.barProvider = barProvider
  816.  
  817. function barCellPrototype:InitializeCell()
  818.    self.bar = BAR:NewSimpleBar(self, 0, 0, 100, 10, BAR.LEFT_TO_RIGHT)
  819.    self.bar:SetAllPoints(self)
  820.    self.fontString = self.bar:CreateFontString()
  821.    self.fontString:SetAllPoints(self.bar)
  822.    self.fontString:SetFontObject(GameTooltipText)
  823.    self.fontString:SetJustifyV("CENTER")
  824. end
  825.  
  826. function barCellPrototype:SetupCell(tooltip, value, justification, font, color, rep, maxRep, width, height)
  827.    local fs = self.fontString
  828.    fs:SetFontObject(font or tooltip:GetFont())
  829.    fs:SetJustifyH(justification)
  830.    fs:SetText(tostring(value))
  831.    fs:Show()
  832.    
  833.    self.bar:SetValue(rep, maxRep)
  834.    self.bar:SetBackgroundColor(0, 0, 0, 0.4)
  835.    self.bar:SetColor(color.r, color.g, color.b, 0.8)
  836.    if width then
  837.       self.bar:SetLength(width)
  838.    end
  839.    if height then
  840.       self.bar:SetThickness(height)
  841.    end
  842.    self.bar.spark:Hide()
  843.    self:SetWidth(width)
  844.    return width, height
  845. end
  846.  
  847. function barCellPrototype:getContentHeight()
  848.    return self.bar:GetHeight()
  849. end
  850.  
  851. function barCellPrototype:ReleaseCell()
  852.    self.r, self.g, self.b = 1, 1, 1
  853. end
  854.  
  855.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement