View Single Post
12-04-12, 05:28 PM   #33
Billtopia
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 110
sure beats the following... this is what I was doing before, modified for the SendAddonMessage and to catch the thrown error if it happens

if it is passed just a GUID it will return true if the person is part of your faction or if you are a panda

if you pass a GUID, and a function to run if true / false and char name and unlimited args for the passed functions ... if a non panda it will automatically call the proper function (if it exists) and return true/false depending... if a panda it queues up the required info in a table, sends the addon message and waits up to 1 second for the error. if the error is thrown, it will call the ifFalse function and remove the panda from the queue... after the 1 second, the addon calls the ifTrue function if supplied and then removes the panda from the queue

it will call ifTrue/ifFalse with GUID, unpack(...)

Lua Code:
  1. local pandaQueue = {}
  2. local pandaE = 0
  3. local pandaDelay = 1
  4.  
  5. local pandaCheckFrame = CreateFrame("Frame","pandaCheckFrame", UIParent)
  6. pandaCheckFrame:SetScript("onUpdate",function( self, elapsed )
  7.     pandaE = pandaE + elapsed
  8.     if pandaE < 1/10 then
  9.         return
  10.     end
  11.     local e = pandaE
  12.     local count = #pandaQueue
  13.     pandaE = 0
  14.     if count == 0 then
  15.         return
  16.     end
  17.     for x = count, 1, -1 do
  18.         pandaQueue[x].time = pandaQueue[x].time + e
  19.         if pandaQueue[x].time > pandaDelay then
  20.             if type( pandaQueue[x].ifTrue ) == "function" then
  21.                 pandaQueue[x].ifTrue( pandaQueue[x].GUID, pandaQueue[x].event, unpack( pandaQueue[x] ) )
  22.             end
  23.             table.remove( pandaQueue, x )
  24.             return true
  25.         end
  26.     end
  27. end)
  28.  
  29. local pandaFilter = function( self, event, ... )
  30.     if #pandaQueue == 0 then
  31.         return false
  32.     end
  33.    
  34.     local arg = {...}
  35.     for x = #pandaQueue, 1, -1 do
  36.         if arg[1] == string.format(ERR_CHAT_PLAYER_NOT_FOUND_S, pandaQueue[x].name ) then
  37.             if type( pandaQueue[x].ifFalse ) == "function" then
  38.                 pandaQueue[x].ifFalse( pandaQueue[x].GUID, unpack( pandaQueue[x] ) )
  39.             end
  40.             table.remove( pandaQueue, x )
  41.             return true
  42.         end
  43.     end
  44.     return false
  45. end
  46.  
  47. ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", pandaFilter)
  48.  
  49. SameFaction = function( GUID, ifTrue, ifFalse, name, ... )
  50.     local isSame = false
  51.     local _, _, _, CompRace = GetPlayerInfoByGUID( GUID )
  52.     CompRace = string.lower(CompRace)
  53.    
  54.     if IsPlayerNeutral() then
  55.         isSame = true
  56.     elseif CompRace == "pandaren" then
  57.         if type( ifTrue ) == "function" or type( ifFalse ) == "function" then
  58.             local index = #pandaQueue + 1
  59.             pandaQueue[index] = { unpack(...) }
  60.             pandaQueue[index].GUID = GUID
  61.             pandaQueue[index].ifTrue = ifTrue
  62.             pandaQueue[index].ifFalse = ifFalse
  63.             pandaQueue[index].name = name
  64.             pandaQueue[index].time = 0
  65.             SendAddonMessage("FactionTest", "Pandaren Faction Test", "whisper", name)
  66.             return true
  67.         else
  68.             isSame = true
  69.         end
  70.     else
  71.         local PlayerIsAlliance = UnitFactionGroup("player") == "Alliance"
  72.         local Allies = { ["worgen"] = true, ["draenei"] = true, ["dwarf"] = true, ["gnome"] = true, ["human"] = true, ["nightelf"] = true }
  73.         local CompIsAlliance = Allies[CompRace] and true or false
  74.         if PlayerIsAlliance == CompIsAlliance then
  75.             isSame = true
  76.         end
  77.     end
  78.     if isSame then
  79.         if type( ifTrue ) == "function" then
  80.             ifTrue( GUID, unpack(...))
  81.         end
  82.         return true
  83.     end
  84.     if type( ifFalse ) == "function" then
  85.         ifFalse( GUID, unpack(...))
  86.     end
  87.     return false
  88. end

Last edited by Billtopia : 12-04-12 at 08:33 PM.
  Reply With Quote