View Single Post
10-14-19, 09:16 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
It occurs to me to post more code, so this is my entire opening and closing of faction headers, plus setting of the faction by factionID. If anyone wants to see all of the code, I'll be happy to post that too.
Lua Code:
  1. local repsCollapsed = {} -- Obey user's settings about headers opened or closed
  2. -- Open all faction headers
  3. function RepByZone:OpenAllFactionHeaders()
  4.     local i = 1
  5.     while i <= GetNumFactions() do
  6.         local name, _, _, _, _, _, _, _, isHeader, isCollapsed = GetFactionInfo(i)
  7.         if isHeader then
  8.             repsCollapsed[name] = isCollapsed
  9.             if name == FACTION_INACTIVE then
  10.                 if not isCollapsed then
  11.                     CollapseFactionHeader(i)
  12.                 end
  13.                 break
  14.             elseif isCollapsed then
  15.                 ExpandFactionHeader(i)
  16.             end
  17.         end
  18.         i = i + 1
  19.     end
  20. end
  21.  
  22. -- Close all faction headers
  23. function RepByZone:CloseAllFactionHeaders()
  24.     local i = 1
  25.     while i <= GetNumFactions() do
  26.         local name, _, _, _, _, _, _, _, isHeader, isCollapsed = GetFactionInfo(i)
  27.         if isHeader then
  28.             if isCollapsed and not repsCollapsed[name] then
  29.                 ExpandFactionHeader(i)
  30.             elseif repsCollapsed[name] and not isCollapsed then
  31.                 CollapseFactionHeader(i)
  32.             end
  33.         end
  34.         i = i + 1
  35.     end
  36.     wipe(repsCollapsed)
  37. end
  38.  
  39. function RepByZone:GetAllFactions()
  40.     -- Will not return factions the user has marked as inactive
  41.     self:OpenAllFactionHeaders()
  42.     local factionList = {}
  43.  
  44.     for i = 1, GetNumFactions() do
  45.         local name, _, _, _, _, _, _, _, isHeader, _, _, _, _, factionID = GetFactionInfo(i)
  46.         if not isHeader then
  47.             factionList[factionID] = name
  48.         end
  49.     end
  50.  
  51.     self:CloseAllFactionHeaders()
  52.     return factionList
  53. end
  54.  
  55. -- Blizzard sets watched faction by index, not by factionID so create our own API
  56. function RepByZone:SetWatchedFactionByFactionID(id)
  57.     if type(id) == "table" then id = tonumber(id) end
  58.     if type(id) ~= "number" then return end
  59.  
  60.     self:OpenAllFactionHeaders()
  61.     for i = 1, GetNumFactions() do
  62.         local name, _, standingID, _, _, _, _, _, isHeader, _, _, isWatched, _, factionID = GetFactionInfoByID(id)
  63.         if id == factionID then
  64.             self:Print("DEBUG: SetWatchedFactionByFactionID name:", name)
  65.             self:Print("DEBUG: SetWatchedFactionByFactionID index:", i)
  66.             if not isWatched then
  67.                 SetWatchedFactionIndex(i)
  68.             end
  69.             self:CloseAllFactionHeaders()
  70.             return name, id
  71.         else
  72.             break
  73.         end
  74.     end
  75.     self:CloseAllFactionHeaders()
  76. end
  77.  
  78. -- Player switched zones, set watched faction
  79. function RepByZone:SwitchedZones()
  80.     local UImapID = IsInInstance() and select(8, GetInstanceInfo()) or C_Map.GetBestMapForUnit("player")
  81.     local locationsAndFactions = IsInInstance() and self:InstanceAndFactionList() or self:ZoneAndFactionList()
  82.     -- local mapID = C_Map.GetMapInfo(UImapID).mapID
  83.     self:Print("DEBUG: Current UImapID is", UImapID)
  84.     -- self:Print("DEBUG: Current mapID is", mapID)
  85.     self:SetWatchedFactionByFactionID(db.defaultRepID)
  86.     for zoneID, factionID in pairs(locationsAndFactions) do
  87.         if zoneID == UImapID then
  88.             self:Print("DEBUG: zoneID and UImapID match")
  89.             self:SetWatchedFactionByFactionID(factionID)
  90.             break
  91.         end
  92.     end
  93. end

Last edited by myrroddin : 10-14-19 at 09:43 PM. Reason: Modified SwitchedZones() function
  Reply With Quote