annotate CrossRealmAssist.lua @ 11:421508c17712

Started implementing new interface
author ShadowTheAge
date Tue, 10 Mar 2015 00:16:23 +0300
parents 9fa8442dd547
children 0deef890ec99
rev   line source
ShadowTheAge@0 1 CrossRealmAssist = LibStub("AceAddon-3.0"):NewAddon("CrossRealmAssist", "AceEvent-3.0", "AceConsole-3.0", "AceTimer-3.0")
ShadowTheAge@0 2 local AceGUI = LibStub("AceGUI-3.0")
ShadowTheAge@0 3 local addon = CrossRealmAssist;
ShadowTheAge@0 4 local wholib = LibStub:GetLibrary('LibWho-2.0'):Library()
ShadowTheAge@0 5
ShadowTheAge@11 6 local homeRealm, realmSep, gui, btRefresh, btQuick, btManual, lbRealm, lbStatus, playerName
ShadowTheAge@0 7
ShadowTheAge@11 8 local curRealmStat
ShadowTheAge@0 9 local scanstate=0
ShadowTheAge@0 10 local recentRealms={}
ShadowTheAge@0 11 local active=false
ShadowTheAge@0 12
ShadowTheAge@0 13 local lfgGroups={
ShadowTheAge@0 14 6, -- Custom
ShadowTheAge@0 15 10, -- Ashran
ShadowTheAge@0 16 1, -- Quests
ShadowTheAge@0 17 3, -- Raids
ShadowTheAge@0 18 8 -- BGs
ShadowTheAge@0 19 }
ShadowTheAge@0 20
ShadowTheAge@0 21 local lfgTabs
ShadowTheAge@0 22 local curLfgGroup
ShadowTheAge@0 23 local sheduledScan
ShadowTheAge@0 24 local wasInGroup
ShadowTheAge@11 25 local wasInInstance
ShadowTheAge@0 26 local lfgScanInProgress=false
ShadowTheAge@0 27
ShadowTheAge@11 28 local function setupWidget(widget, parameters, x, y)
ShadowTheAge@11 29 if parameters then
ShadowTheAge@11 30 for key,value in pairs(parameters) do widget[key](widget,value) end
ShadowTheAge@11 31 end
ShadowTheAge@11 32 if y then
ShadowTheAge@11 33 widget:SetPoint("TOPLEFT",widget:GetParent(),"TOPLEFT",x,-y)
ShadowTheAge@11 34 end
ShadowTheAge@11 35 return widget
ShadowTheAge@11 36 end
ShadowTheAge@11 37
ShadowTheAge@11 38 local function ShowTooltip(widget)
ShadowTheAge@11 39 GameTooltip:SetOwner(widget, widget.TooltipAnchor)
ShadowTheAge@11 40 GameTooltip:ClearLines()
ShadowTheAge@11 41 widget:TooltipFactory()
ShadowTheAge@11 42 GameTooltip:Show()
ShadowTheAge@11 43 end
ShadowTheAge@11 44
ShadowTheAge@11 45 local function HideTooltip(widget)
ShadowTheAge@11 46 GameTooltip:Hide()
ShadowTheAge@11 47 end
ShadowTheAge@11 48
ShadowTheAge@11 49 local function setupTooltip(widget, anchor, factory)
ShadowTheAge@11 50 widget.TooltipAnchor = anchor;
ShadowTheAge@11 51 widget.TooltipFactory = factory;
ShadowTheAge@11 52 widget:SetScript("OnEnter", ShowTooltip)
ShadowTheAge@11 53 widget:SetScript("OnLeave", HideTooltip)
ShadowTheAge@11 54 end
ShadowTheAge@11 55
ShadowTheAge@0 56 function addon:OnInitialize()
ShadowTheAge@0 57
ShadowTheAge@0 58 end
ShadowTheAge@0 59
ShadowTheAge@0 60 function addon:OnEnable()
ShadowTheAge@0 61 local tabCount = table.getn(lfgGroups)
ShadowTheAge@0 62 lfgTabs = {}
ShadowTheAge@0 63 for i=1,tabCount do
ShadowTheAge@0 64 local cat = lfgGroups[i]
ShadowTheAge@0 65 table.insert(lfgTabs,{value=cat,text=(C_LFGList.GetCategoryInfo(cat))})
ShadowTheAge@0 66 end
ShadowTheAge@0 67 realmSep = _G.REALM_SEPARATORS
ShadowTheAge@11 68 playerName = UnitName("player")
ShadowTheAge@0 69 homeRealm = GetRealmName()
ShadowTheAge@0 70 addon:RegisterChatCommand("cra", "Activate")
ShadowTheAge@0 71 addon:RegisterChatCommand("crossrealmassist", "Activate")
ShadowTheAge@11 72
ShadowTheAge@11 73 addon:ScheduleTimer("Activate", 0.5)
ShadowTheAge@0 74 end
ShadowTheAge@0 75
ShadowTheAge@0 76 function addon:OnDisable()
ShadowTheAge@0 77 addon:Deactivate()
ShadowTheAge@0 78 end
ShadowTheAge@0 79
ShadowTheAge@0 80 function addon:Activate()
ShadowTheAge@0 81 if active then return end
ShadowTheAge@0 82 active=true
ShadowTheAge@0 83 wasInGroup = IsInGroup()
ShadowTheAge@11 84 if not gui then addon:CreateUI() end
ShadowTheAge@11 85 addon:updateCurrentRealm();
ShadowTheAge@11 86 addon:ForceRefreshZone();
ShadowTheAge@11 87 gui:Show()
ShadowTheAge@11 88 addon:RegisterEvent("ZONE_CHANGED_NEW_AREA", "ForceRefreshZone")
ShadowTheAge@11 89 addon:RegisterEvent("SCENARIO_UPDATE", "ForceRefreshZone")
ShadowTheAge@11 90 --addon:RegisterEvent("LFG_LIST_SEARCH_RESULTS_RECEIVED", "LfgResponseData")
ShadowTheAge@11 91 --addon:RegisterEvent("LFG_LIST_SEARCH_FAILED", "LfgScanFailed")
ShadowTheAge@11 92 --addon:RegisterEvent("GROUP_ROSTER_UPDATE", "updatePartyInfo")
ShadowTheAge@11 93 --addon:RegisterEvent("LFG_LIST_APPLICATION_STATUS_UPDATED", "updateAppStatus")
ShadowTheAge@0 94 end
ShadowTheAge@0 95
ShadowTheAge@0 96 function addon:Deactivate()
ShadowTheAge@0 97 if not active then return end
ShadowTheAge@0 98 active = false
ShadowTheAge@0 99 scanstate = 0
ShadowTheAge@0 100 lfgScanInProgress = false
ShadowTheAge@0 101 addon:UnregisterEvent("ZONE_CHANGED_NEW_AREA")
ShadowTheAge@0 102 addon:UnregisterEvent("PLAYER_REGEN_ENABLED")
ShadowTheAge@0 103 addon:UnregisterEvent("LFG_LIST_SEARCH_RESULTS_RECEIVED")
ShadowTheAge@0 104 addon:UnregisterEvent("LFG_LIST_SEARCH_FAILED")
ShadowTheAge@0 105 addon:UnregisterEvent("GROUP_ROSTER_UPDATE")
ShadowTheAge@0 106 addon:UnregisterEvent("LFG_LIST_APPLICATION_STATUS_UPDATED")
ShadowTheAge@0 107 end
ShadowTheAge@0 108
ShadowTheAge@11 109 function addon:SetStatus(status)
ShadowTheAge@11 110 if status then
ShadowTheAge@11 111 lbStatus:SetText(status)
ShadowTheAge@11 112 else
ShadowTheAge@11 113 lbStatus:SetText("") -- TODO
ShadowTheAge@11 114 end
ShadowTheAge@11 115 end
ShadowTheAge@11 116
ShadowTheAge@0 117 function addon:CreateUI()
ShadowTheAge@11 118 gui = setupWidget(CreateFrame("Frame",nil,nil,"InsetFrameTemplate3"), {SetFrameStrata="DIALOG",SetWidth=208,SetHeight=60,EnableMouse=true,SetMovable=true})
ShadowTheAge@11 119 local title = setupWidget(CreateFrame("Frame",nil,gui), {SetWidth=190,SetHeight=18,EnableMouse=true,RegisterForDrag="LeftButton"}, 0, 6);
ShadowTheAge@11 120 title:SetScript("OnDragStart", function() gui:StartMoving() end)
ShadowTheAge@11 121 title:SetScript("OnDragStop", function() gui:StopMovingOrSizing() end)
ShadowTheAge@11 122 gui:SetScript("OnHide", addon.Deactivate)
ShadowTheAge@11 123 setupTooltip(title, "ANCHOR_TOP", addon.realmToolip)
ShadowTheAge@11 124
ShadowTheAge@11 125 lbRealm = setupWidget(gui:CreateFontString(nil,"BACKGROUND", "GameFontNormal"), {SetWidth=200,SetHeight=18}, 0, 6)
ShadowTheAge@11 126 lbStatus = setupWidget(gui:CreateFontString(nil,"BACKGROUND", "GameFontHighlightSmallLeft"), {SetWidth=200,SetHeight=10}, 6, 23)
ShadowTheAge@11 127
ShadowTheAge@11 128 btQuick = setupWidget(CreateFrame("Button",nil,gui,"UIMenuButtonStretchTemplate"),{EnableMouse=true,SetWidth=90,SetHeight=20,SetText="Quick join"},4,36)
ShadowTheAge@11 129 btManual = setupWidget(CreateFrame("Button",nil,gui,"UIMenuButtonStretchTemplate"),{EnableMouse=true,SetWidth=90,SetHeight=20,SetText="Manual join"},94,36)
ShadowTheAge@11 130 setupWidget(CreateFrame("Button",nil,gui,"UIPanelCloseButton"),{EnableMouse=true,SetWidth=20,SetHeight=20},188,0)
ShadowTheAge@11 131
ShadowTheAge@11 132 btRefresh = setupWidget(CreateFrame("Button",nil,gui,"UIPanelSquareButton"),{EnableMouse=true,SetWidth=20,SetHeight=20},184,36)
ShadowTheAge@11 133 btRefresh.icon:SetTexture("Interface/BUTTONS/UI-RefreshButton")
ShadowTheAge@11 134 btRefresh.icon:SetTexCoord(0,1,0,1);
ShadowTheAge@11 135 btRefresh:SetScript("OnClick",addon.RefreshZone)
ShadowTheAge@11 136
ShadowTheAge@11 137 gui:SetPoint("CENTER",0,0)
ShadowTheAge@11 138
ShadowTheAge@11 139 --[[gui = addon:AddUI(nil,"Window",{SetTitle="Cross Realm Assist",EnableResize=false,SetLayout="Flow"},true,{OnClose=addon.Deactivate})
ShadowTheAge@0 140
ShadowTheAge@0 141 local tabgroup = addon:AddUI(gui,"TabGroup",{SetFullHeight=true,SetTabs=lfgTabs,SetLayout="Fill"},true,{OnGroupSelected=addon.LfgScan})
ShadowTheAge@0 142 tabgroup:SelectTab(lfgGroups[1])
ShadowTheAge@0 143 lfgContainer = addon:AddUI(tabgroup,"ScrollFrame")
ShadowTheAge@0 144
ShadowTheAge@0 145 local hgroupc = addon:AddUI(gui,"InlineGroup",{SetLayout="Fill",SetFullHeight=true,SetTitle="Realm info"},true)
ShadowTheAge@0 146 hgroup = addon:AddUI(hgroupc,"ScrollFrame")
ShadowTheAge@0 147
ShadowTheAge@0 148 addon:AddUI(hgroup,"Heading",{SetText="Current realm"})
ShadowTheAge@0 149 currealm = addon:AddUI(hgroup,"SimpleGroup");
ShadowTheAge@0 150 addon:AddUI(hgroup,"Button",{SetText="Refresh",SetRelativeWidth=0.5,SetHeight=20},true,{OnClick=addon.ScanRealm})
ShadowTheAge@0 151
ShadowTheAge@0 152 addon:AddUI(hgroup,"Heading",{SetText="Party statistics"})
ShadowTheAge@0 153 partyrealm = addon:AddUI(hgroup,"SimpleGroup");
ShadowTheAge@0 154 leavebtn = addon:AddUI(hgroup,"Button",{SetText="Leave",SetRelativeWidth=0.5,SetHeight=20,SetDisabled=not IsInGroup()},true,{OnClick=LeaveParty})
ShadowTheAge@0 155
ShadowTheAge@0 156 addon:AddUI(hgroup,"Heading",{SetText="Recent realms"})
ShadowTheAge@0 157 recrealm = addon:AddUI(hgroup,"SimpleGroup");
ShadowTheAge@0 158 addon:AddUI(hgroup,"Button",{SetText="Clear",SetRelativeWidth=0.5,SetHeight=20},true,{OnClick=addon.ClearRecentRealms})
ShadowTheAge@0 159
ShadowTheAge@0 160 gui:DoLayout()
ShadowTheAge@0 161 gui:PauseLayout()
ShadowTheAge@0 162
ShadowTheAge@0 163 tabgroup:SetPoint("BOTTOMRIGHT", gui.content, "BOTTOMRIGHT", -200, 0)
ShadowTheAge@0 164 hgroupc:SetPoint("TOPLEFT", gui.content, "TOPRIGHT", -200, 0)
ShadowTheAge@0 165 hgroupc:SetPoint("BOTTOMRIGHT", gui.content, "BOTTOMRIGHT")
ShadowTheAge@0 166
ShadowTheAge@0 167 if curRealmStat then addon:updateCurrentRealm() end
ShadowTheAge@0 168 addon:updatePartyInfo()
ShadowTheAge@11 169 addon:updateRecentRealms()]]
ShadowTheAge@0 170 end
ShadowTheAge@0 171
ShadowTheAge@0 172 -- LFG scanning routine
ShadowTheAge@0 173
ShadowTheAge@11 174 --[[function addon.refreshLfgCurrent()
ShadowTheAge@0 175 addon.LfgScan(nil,nil,curLfgGroup)
ShadowTheAge@0 176 end
ShadowTheAge@0 177
ShadowTheAge@0 178 function addon.LfgScan(widget, callback, tab)
ShadowTheAge@0 179 if not tab then return end
ShadowTheAge@0 180 lfgScanInProgress = true
ShadowTheAge@0 181 if lfgContainer then lfgContainer:ReleaseChildren() end
ShadowTheAge@0 182 curLfgGroup = tab
ShadowTheAge@0 183 C_LFGList.Search(curLfgGroup,"")
ShadowTheAge@0 184 end
ShadowTheAge@0 185
ShadowTheAge@0 186 function addon:LfgScanFailed(event, reason)
ShadowTheAge@0 187 print("scan failed "..reason)
ShadowTheAge@0 188 if reason == "throttled" then
ShadowTheAge@0 189 addon:ScheduleTimer(addon.LfgScan, 2, nil, nil, curLfgGroup)
ShadowTheAge@0 190 end
ShadowTheAge@0 191 end
ShadowTheAge@0 192
ShadowTheAge@0 193 local function WeightLfgItem(item)
ShadowTheAge@0 194 local leaderRealm = 3
ShadowTheAge@0 195 if (item.realm ~= "???") then leaderRealm = (5 - (recentRealms[item.realm] or 0)) end
ShadowTheAge@0 196 local weight =
ShadowTheAge@0 197 (item.autoinv and 5 or 2) *
ShadowTheAge@0 198 ((item.friends > 0) and 2 or 3) *
ShadowTheAge@0 199 ((item.ilvl > 0) and 2 or 3) *
ShadowTheAge@0 200 ((item.voice ~= "") and 2 or 4)
ShadowTheAge@0 201 local count = item.pcount;
ShadowTheAge@0 202 local countWeight
ShadowTheAge@0 203 if count >= 39 or count <= 1 then countWeight = 1
ShadowTheAge@0 204 elseif count >= 35 or count <= 4 then countWeight = 2
ShadowTheAge@0 205 elseif count >= 30 or count <= 10 then countWeight = 3
ShadowTheAge@0 206 else countWeight = 4 end
ShadowTheAge@0 207 return weight * leaderRealm * countWeight;
ShadowTheAge@0 208 end
ShadowTheAge@0 209
ShadowTheAge@0 210 function addon:LfgResponseData()
ShadowTheAge@0 211 lfgScanInProgress = false;
ShadowTheAge@0 212 addon:UpdateCurrentLfgInfo()
ShadowTheAge@0 213 end
ShadowTheAge@0 214
ShadowTheAge@0 215 function addon:UpdateCurrentLfgInfo(repeated)
ShadowTheAge@0 216 if lfgScanInProgress then return end
ShadowTheAge@0 217 local count, list = C_LFGList.GetSearchResults()
ShadowTheAge@0 218 local lfgList = {}
ShadowTheAge@0 219 local lfgEntries = 0
ShadowTheAge@0 220 local hasUnknowns = false
ShadowTheAge@0 221 for i = 1,count do
ShadowTheAge@0 222 local rid = list[i];
ShadowTheAge@0 223 if not rid then break end
ShadowTheAge@0 224 local _, action, caption, desc, voice, ilvl, time, bnetfr, charfr, guild, delisted, fullname, pcount, autoinv = C_LFGList.GetSearchResultInfo(rid)
ShadowTheAge@0 225 if not fullname then
ShadowTheAge@0 226 fullname = "???-???"
ShadowTheAge@0 227 hasUnknowns = true
ShadowTheAge@0 228 end
ShadowTheAge@0 229 if not delisted then
ShadowTheAge@0 230 local pname, realm = strsplit(realmSep, fullname)
ShadowTheAge@0 231 realm = realm or homeRealm
ShadowTheAge@0 232 local item = {
ShadowTheAge@0 233 rid=rid,
ShadowTheAge@0 234 action=action,
ShadowTheAge@0 235 caption=caption,
ShadowTheAge@0 236 desc=desc,
ShadowTheAge@0 237 ilvl=ilvl,
ShadowTheAge@0 238 voice=voice,
ShadowTheAge@0 239 time=time,
ShadowTheAge@0 240 friends=bnetfr+charfr+guild,
ShadowTheAge@0 241 name=pname,
ShadowTheAge@0 242 realm = realm,
ShadowTheAge@0 243 pcount=pcount,
ShadowTheAge@0 244 autoinv=autoinv
ShadowTheAge@0 245 }
ShadowTheAge@0 246 item.weight = WeightLfgItem(item)
ShadowTheAge@0 247 table.insert(lfgList, item)
ShadowTheAge@0 248 lfgEntries = lfgEntries + 1
ShadowTheAge@0 249 end
ShadowTheAge@0 250 end
ShadowTheAge@0 251 addon:refreshLFGList(lfgList, lfgEntries);
ShadowTheAge@0 252 if hasUnknowns and repeated ~= true then addon:ScheduleTimer("UpdateCurrentLfgInfo", 1, true) end
ShadowTheAge@0 253 end
ShadowTheAge@0 254
ShadowTheAge@0 255 function addon:refreshLFGList(list, count)
ShadowTheAge@0 256 if not lfgContainer then return end
ShadowTheAge@0 257 lfgContainer:PauseLayout()
ShadowTheAge@0 258 lfgContainer:ReleaseChildren()
ShadowTheAge@0 259 table.sort(list, addon.SortLfgItems)
ShadowTheAge@0 260
ShadowTheAge@0 261 local canJoin = addon.canJoinGroup()
ShadowTheAge@0 262
ShadowTheAge@0 263 for i=1,count do
ShadowTheAge@0 264 local data = list[i];
ShadowTheAge@0 265 local renderer = addon:AddUI(lfgContainer,"SimpleGroup",{SetLayout="Flow",SetHeight=20,SetAutoAdjustHeight=false});
ShadowTheAge@0 266
ShadowTheAge@0 267 addon:AddUI(renderer,"Label",{SetText=data.caption,SetRelativeWidth=0.4},true)
ShadowTheAge@0 268 addon:AddUI(renderer,"Label",{SetText=data.pcount,SetRelativeWidth=0.05},true)
ShadowTheAge@0 269 local realm = addon:AddUI(renderer,"Label",{SetText=data.realm,SetRelativeWidth=0.25},true)
ShadowTheAge@0 270 if recentRealms[data.realm] then
ShadowTheAge@0 271 local r,g,b = GetItemQualityColor(recentRealms[data.realm]);
ShadowTheAge@0 272 realm:SetColor(r,g,b)
ShadowTheAge@0 273 end
ShadowTheAge@0 274
ShadowTheAge@0 275 local tooltip = {}
ShadowTheAge@0 276 if data.voice ~= "" then table.insert(tooltip,"Voice: "..data.voice) end
ShadowTheAge@0 277 if data.friends > 0 then table.insert(tooltip,"Friends: "..data.friends) end
ShadowTheAge@0 278 if data.ilvl > 0 then table.insert(tooltip,"Min. Ilvl: "..data.ilvl) end
ShadowTheAge@0 279 addon:AddIcon(renderer, "Interface/GossipFrame/ActiveQuestIcon", 16, tooltip[1] ~= nil, tooltip)
ShadowTheAge@0 280 addon:AddIcon(renderer, READY_CHECK_READY_TEXTURE, 16, data.autoinv, {"Auto invite!"})
ShadowTheAge@0 281 local btn = addon:AddUI(renderer,"Button",{SetText="Join",SetRelativeWidth=0.2,SetHeight=20,SetDisabled=not canJoin},true,{OnClick=addon.joinGroup})
ShadowTheAge@0 282 btn:SetUserData('rid',data.rid)
ShadowTheAge@0 283 end
ShadowTheAge@0 284 addon:AddUI(lfgContainer,"Button",{SetText="Refresh",SetWidth=100,SetHeight=20},true,{OnClick=addon.refreshLfgCurrent})
ShadowTheAge@0 285
ShadowTheAge@0 286 lfgContainer:ResumeLayout()
ShadowTheAge@0 287 lfgContainer:DoLayout()
ShadowTheAge@0 288 end
ShadowTheAge@0 289
ShadowTheAge@0 290 function addon.SortLfgItems(a,b)
ShadowTheAge@0 291 return a.weight > b.weight
ShadowTheAge@0 292 end
ShadowTheAge@0 293
ShadowTheAge@0 294 function addon.canJoinGroup()
ShadowTheAge@0 295 return (not IsInGroup()) or (UnitIsGroupLeader('player') and not IsInRaid())
ShadowTheAge@0 296 end
ShadowTheAge@0 297
ShadowTheAge@0 298 function addon.joinGroup(widget)
ShadowTheAge@0 299 if not addon.canJoinGroup() then return end
ShadowTheAge@0 300 local rid = widget:GetUserData('rid');
ShadowTheAge@0 301 widget:SetDisabled(true)
ShadowTheAge@0 302 C_LFGList.ApplyToGroup(rid, "", C_LFGList.GetAvailableRoles())
ShadowTheAge@0 303 end
ShadowTheAge@0 304
ShadowTheAge@0 305 function addon:updateAppStatus(event, id, status, oldstatus)
ShadowTheAge@0 306 if status == "invited" then
ShadowTheAge@0 307 LFGListInviteDialog_Accept(LFGListInviteDialog)
ShadowTheAge@0 308 end
ShadowTheAge@11 309 end]]
ShadowTheAge@0 310
ShadowTheAge@0 311 -- Zone scanning routine
ShadowTheAge@0 312
ShadowTheAge@0 313 function addon:LeaveCombat()
ShadowTheAge@0 314 addon:UnregisterEvent("PLAYER_REGEN_ENABLED")
ShadowTheAge@0 315 scanstate = 0
ShadowTheAge@0 316 addon:ScanRealm();
ShadowTheAge@0 317 end
ShadowTheAge@0 318
ShadowTheAge@11 319 function addon:ForceRefreshZone()
ShadowTheAge@11 320 addon:RefreshZone(true)
ShadowTheAge@11 321 end
ShadowTheAge@11 322
ShadowTheAge@11 323 function addon:RefreshZone(shedule)
ShadowTheAge@11 324 local inst, instType = IsInInstance()
ShadowTheAge@11 325 if (inst or instType ~= "none") then
ShadowTheAge@11 326 lbRealm:SetText("Instanced zone")
ShadowTheAge@11 327 curRealmStat = nil
ShadowTheAge@11 328 btQuick:Disable()
ShadowTheAge@11 329 else
ShadowTheAge@11 330 btQuick:Enable()
ShadowTheAge@11 331 if scanstate == 0 then
ShadowTheAge@11 332 addon:ScanRealm()
ShadowTheAge@11 333 elseif shedule == true and scanstate == 2 then
ShadowTheAge@11 334 addon:SetStatus("Rescan sheduled...")
ShadowTheAge@11 335 scanstate = 3
ShadowTheAge@11 336 end
ShadowTheAge@11 337 end
ShadowTheAge@11 338 end
ShadowTheAge@11 339
ShadowTheAge@0 340 function addon:ScanRealm()
ShadowTheAge@0 341 if scanstate > 0 then return end
ShadowTheAge@0 342 if InCombatLockdown() then
ShadowTheAge@0 343 addon:RegisterEvent("PLAYER_REGEN_ENABLED","LeaveCombat")
ShadowTheAge@0 344 scanstate = 1
ShadowTheAge@11 345 addon:SetStatus("Scan sheduled after combat...");
ShadowTheAge@0 346 return
ShadowTheAge@0 347 end
ShadowTheAge@0 348 scanstate = 2
ShadowTheAge@11 349 addon:SetStatus("Scanning current realm...");
ShadowTheAge@0 350 local searchString = _G.WHO_TAG_ZONE .. '"' .. GetZoneText() .. '"';
ShadowTheAge@0 351 wholib:Who(searchString, {callback = "ScanResult", handler=addon})
ShadowTheAge@0 352 end
ShadowTheAge@0 353
ShadowTheAge@0 354 function addon:ScanResult(query, results, complete)
ShadowTheAge@0 355 local realms = {}
ShadowTheAge@0 356 for _, player in ipairs(results) do
ShadowTheAge@0 357 addon:AddRealmStat(player.Name, realms);
ShadowTheAge@0 358 end
ShadowTheAge@0 359 curRealmStat = addon:GetRealmStat(realms);
ShadowTheAge@11 360 curRealmStat.complete = complete
ShadowTheAge@0 361 addon:updateCurrentRealm();
ShadowTheAge@0 362 local rescan = scanstate == 3
ShadowTheAge@0 363 scanstate = 0;
ShadowTheAge@0 364 if rescan then addon:ScanRealm() end
ShadowTheAge@0 365 end
ShadowTheAge@0 366
ShadowTheAge@0 367 function addon:AddRealmStat(name, realms)
ShadowTheAge@11 368 if (name == playerName) then return end
ShadowTheAge@11 369 local _, realm = strsplit(realmSep, name)
ShadowTheAge@0 370 realm = realm or homeRealm
ShadowTheAge@0 371 realms[realm] = (realms[realm] or 0) + 1
ShadowTheAge@0 372 end
ShadowTheAge@0 373
ShadowTheAge@0 374 function addon:AddUnitIdStat(unitid, realms)
ShadowTheAge@0 375 local name, realm = UnitName(unitid);
ShadowTheAge@0 376 if not name then return end
ShadowTheAge@0 377 if realm == "" then realm = homeRealm end
ShadowTheAge@0 378 realm = realm or homeRealm
ShadowTheAge@0 379 realms[realm] = (realms[realm] or 0) + 1
ShadowTheAge@0 380 end
ShadowTheAge@0 381
ShadowTheAge@0 382 function addon:GetRealmStat(realms)
ShadowTheAge@0 383 local rcount = 0;
ShadowTheAge@0 384 local stat = {};
ShadowTheAge@0 385 local pcount = 0;
ShadowTheAge@0 386 for realm,count in pairs(realms) do
ShadowTheAge@0 387 table.insert(stat,{realm=realm,count=count})
ShadowTheAge@0 388 rcount = rcount + 1;
ShadowTheAge@0 389 pcount = pcount + count;
ShadowTheAge@0 390 end
ShadowTheAge@0 391 if rcount > 1 then
ShadowTheAge@0 392 table.sort(stat, function(a,b) return a.count > b.count end)
ShadowTheAge@0 393 stat.max = stat[1].count
ShadowTheAge@0 394 else stat.max = 0 end
ShadowTheAge@0 395 stat.players = pcount;
ShadowTheAge@0 396 stat.realms = rcount;
ShadowTheAge@0 397 return stat;
ShadowTheAge@0 398 end
ShadowTheAge@0 399
ShadowTheAge@0 400 function addon:updateCurrentRealm()
ShadowTheAge@11 401 addon:SetStatus();
ShadowTheAge@11 402 if not curRealmStat or curRealmStat.realms < 1 then
ShadowTheAge@11 403 lbRealm:SetText("Unknown realm (No players)")
ShadowTheAge@11 404 return;
ShadowTheAge@0 405 end
ShadowTheAge@11 406
ShadowTheAge@11 407 local bestRealm = curRealmStat[1]
ShadowTheAge@11 408 local prevThreshold = math.min(bestRealm.count/3,5)
ShadowTheAge@11 409 local mixCount = 0
ShadowTheAge@11 410 for i=2,curRealmStat.realms do
ShadowTheAge@0 411 local data = curRealmStat[i]
ShadowTheAge@11 412 if data.count >= prevThreshold then
ShadowTheAge@11 413 mixCount = mixCount + 1
ShadowTheAge@11 414 prevThreshold = math.min(data.count/3,5)
ShadowTheAge@0 415 end
ShadowTheAge@0 416 end
ShadowTheAge@11 417 curRealmStat.threshold = prevThreshold
ShadowTheAge@11 418
ShadowTheAge@11 419 if (mixCount > 0) then
ShadowTheAge@11 420 lbRealm:SetText("Mixed "..bestRealm.realm.." +"..mixCount)
ShadowTheAge@11 421 else
ShadowTheAge@11 422 lbRealm:SetText(bestRealm.realm)
ShadowTheAge@0 423 end
ShadowTheAge@0 424 end
ShadowTheAge@0 425
ShadowTheAge@11 426 function addon:realmToolip()
ShadowTheAge@11 427 if not curRealmStat then return end
ShadowTheAge@11 428 local players = curRealmStat.players;
ShadowTheAge@11 429 local threshold = curRealmStat.threshold
ShadowTheAge@11 430 GameTooltip:AddLine("Players in zone: "..players)
ShadowTheAge@11 431 if not curRealmStat.complete then
ShadowTheAge@11 432 GameTooltip:AppendText("+")
ShadowTheAge@11 433 end
ShadowTheAge@11 434 for i=1,curRealmStat.realms do
ShadowTheAge@11 435 local data = curRealmStat[i]
ShadowTheAge@11 436 local percent = math.ceil(100 * data.count / players - 0.5) .. "%";
ShadowTheAge@11 437 if data.count >= threshold then
ShadowTheAge@11 438 GameTooltip:AddDoubleLine(data.realm, percent, 0,1,0,0,1,0)
ShadowTheAge@11 439 else
ShadowTheAge@11 440 GameTooltip:AddDoubleLine(data.realm, percent, 0.5,0.5,0.5,0.5,0.5,0.5)
ShadowTheAge@11 441 end
ShadowTheAge@11 442 end
ShadowTheAge@11 443 end
ShadowTheAge@11 444
ShadowTheAge@11 445 --[[function addon:addLabels(container, stats, epict, whitet, grayt, emptytext)
ShadowTheAge@0 446 if stats.realms == 0 then
ShadowTheAge@0 447 addon:AddLabel(container, emptytext)
ShadowTheAge@0 448 else
ShadowTheAge@0 449 local players = stats.players
ShadowTheAge@0 450 local pleft = players;
ShadowTheAge@0 451 for i=1,stats.realms do
ShadowTheAge@0 452 local data = stats[i]
ShadowTheAge@0 453 local count = data.count;
ShadowTheAge@0 454 local percent = math.ceil(100 * count / players - 0.5)
ShadowTheAge@0 455 local label = data.realm .. " (" .. percent .. "%)";
ShadowTheAge@0 456 local color
ShadowTheAge@0 457 if count < grayt then break
ShadowTheAge@0 458 elseif count < whitet then color = 1
ShadowTheAge@0 459 elseif count < epict then color = 2
ShadowTheAge@0 460 else color = 4 end
ShadowTheAge@0 461 pleft = pleft - count
ShadowTheAge@0 462 addon:AddLabel(container, label, color);
ShadowTheAge@0 463 end
ShadowTheAge@0 464 if pleft > 0 then
ShadowTheAge@0 465 addon:AddLabel(container, "Other" .. " (" .. math.ceil(100 * pleft / players - 0.5) .. "%)", 0);
ShadowTheAge@0 466 end
ShadowTheAge@0 467 end
ShadowTheAge@0 468 end
ShadowTheAge@0 469
ShadowTheAge@0 470 function addon:updateRecentRealms()
ShadowTheAge@0 471 if not recrealm then return end
ShadowTheAge@0 472 recrealm:ReleaseChildren()
ShadowTheAge@0 473 local nothing = true;
ShadowTheAge@0 474 for realm,color in pairs(recentRealms) do
ShadowTheAge@0 475 addon:AddLabel(recrealm, realm,color);
ShadowTheAge@0 476 nothing = false;
ShadowTheAge@0 477 end
ShadowTheAge@0 478 if nothing then
ShadowTheAge@0 479 addon:AddLabel(recrealm, "No recent realms");
ShadowTheAge@0 480 end
ShadowTheAge@0 481 end
ShadowTheAge@0 482
ShadowTheAge@0 483 function addon:ClearRecentRealms()
ShadowTheAge@0 484 recentRealms = {}
ShadowTheAge@0 485 addon:UpdateCurrentLfgInfo()
ShadowTheAge@0 486 addon:updateRecentRealms()
ShadowTheAge@0 487 end
ShadowTheAge@0 488
ShadowTheAge@0 489 function addon:updatePartyInfo()
ShadowTheAge@0 490 if not partyrealm then return end
ShadowTheAge@0 491 partyrealm:ReleaseChildren()
ShadowTheAge@0 492 local realms = {}
ShadowTheAge@0 493 local inGroup = IsInGroup()
ShadowTheAge@0 494 if IsInGroup() then
ShadowTheAge@0 495 if IsInRaid() then
ShadowTheAge@0 496 for i=1, MAX_RAID_MEMBERS do
ShadowTheAge@0 497 addon:AddUnitIdStat("raid"..i, realms)
ShadowTheAge@0 498 end
ShadowTheAge@0 499 else
ShadowTheAge@0 500 for i=1, MAX_PARTY_MEMBERS do
ShadowTheAge@0 501 addon:AddUnitIdStat("party"..i, realms)
ShadowTheAge@0 502 end
ShadowTheAge@0 503 end
ShadowTheAge@0 504 end
ShadowTheAge@0 505 if inGroup ~= wasInGroup then
ShadowTheAge@0 506 addon:SheduleScan()
ShadowTheAge@0 507 wasInGroup = inGroup
ShadowTheAge@0 508 addon:UpdateCurrentLfgInfo()
ShadowTheAge@0 509 leavebtn:SetDisabled(not inGroup)
ShadowTheAge@0 510 end
ShadowTheAge@0 511 local partyStat = addon:GetRealmStat(realms);
ShadowTheAge@0 512 addon:addLabels(partyrealm, partyStat, partyStat.players/2, 2, 0, "Not in party");
ShadowTheAge@11 513 end]]