view LFGFilter.lua @ 43:324bcc47f37a

remove debug
author rowaasr13
date Fri, 27 Jan 2017 10:34:43 +0300
parents bc6794e51c97
children 82db5d724aa5
line wrap: on
line source
-- [AUTOLOCAL START]
local CLASS_SORT_ORDER = CLASS_SORT_ORDER
local C_LFGList = C_LFGList
local GetLFGDungeonEncounterInfo = GetLFGDungeonEncounterInfo
local GetLFGDungeonNumEncounters = GetLFGDungeonNumEncounters
local MAX_CLASSES = MAX_CLASSES
local dump = DevTools_Dump
local match = string.match
local pairs = pairs
local setfenv = setfenv
local wipe = wipe
-- [AUTOLOCAL END]

LFGListFrame.SearchPanel.SearchBox:SetMaxLetters(2048)

local filter_expression_functions = setmetatable({}, {
   __mode = "k",
   __index = function(t, key)
      local func, error = loadstring("return " .. key)
      if error then print("Error in LFG filter expression:\n", error) end
      t[key] = func
      return func
   end
})

local aliases = {
   voice_chat       = "voice",
   hm               = "highmaul",
   brf              = "blackrock_foundry",
   blackrockfoundry = "blackrock_foundry",
   hellfirecitadel  = "hellfire_citadel",
   hellfire         = "hellfire_citadel",
   hfc              = "hellfire_citadel",
   en               = "emerald_nightmare",
   tov              = "trial_of_valor",
   healers          = "healer",
   members          = "member",
   tanks            = "tank",
   damagers         = "damager",
   damage           = "damager",
   plates           = "plate",
   mails            = "mail",
   leathers         = "leather",
   cloths           = "cloth",
   clothies         = "cloth",
}
for idx = 1, MAX_CLASSES do
   local class_lc = CLASS_SORT_ORDER[idx]:lower()
   aliases[class_lc .. "s"] = class_lc
end

local token_to_encounter_id = {
   highmaul = {
      lfg_dungeon_id = 849,
      { "kargath", "bladefist", "kargath_bladefist" },
      { "butcher", "the_butcher" },
      { "tectus" },
      { "brackenspore", "bracken" },
      { "twin_orgon", "twins" },
      { "koragh", "breaker" },
      { "imperator", "margok" },
   },
   blackrock_foundry = {
      lfg_dungeon_id = 847,
      { "oregorger", "oreg" },
      { "gruul" },
      { "hansgar", "franzok", "hans", "franz", "hans_and_franz", "twins" },
      { "beastlord", "darmac" },
      { "flamebender", "kagraz" },
      { "operator", "thogar" },
      { "blast_furnace", "bf", "furnace", "blast" },
      { "kromog" },
      { "iron_maidens", "im", "maidens" },
      { "blackhand" },
   }
}

-- Don't forget to update it when according to Blizzard code
function LFGListSearchPanel_DoSearch(self)
   local searchText = self.SearchBox:GetText();
   local real_search, filter_expression = searchText:match("^([^=]-)=(.+)$")
   if filter_expression then
      filter_expression = filter_expression:lower()
      self.filter_func = filter_expression_functions[filter_expression]
   end
   self.filter_expression = filter_expression

   local languages = C_LFGList.GetLanguageSearchFilter();
   C_LFGList.Search(self.categoryID, real_search or searchText, self.filters, self.preferredFilters, languages);
   self.searching = true;
   self.searchFailed = false;
   self.selectedResult = nil;
   LFGListSearchPanel_UpdateResultList(self);
   LFGListSearchPanel_UpdateResults(self);
end

local localized_encounter_name_to_idx = {}

local dead = true

local function InsertEncounterStateAliases(result_env, raid_token, completed_encounters)
   local encounter_aliases = token_to_encounter_id[raid_token]
   if not encounter_aliases then return end

   local lfg_dungeon_id = encounter_aliases.lfg_dungeon_id
   local encounter_names = localized_encounter_name_to_idx[lfg_dungeon_id]
   if not encounter_names then
      encounter_names = {}
      for idx = 1, GetLFGDungeonNumEncounters(lfg_dungeon_id) do
         local bossName, texture, isKilled = GetLFGDungeonEncounterInfo(lfg_dungeon_id, idx)
         encounter_names[bossName] = idx
      end
      localized_encounter_name_to_idx[lfg_dungeon_id] = encounter_names
   end

   for idx = 1, #completed_encounters do
      local encounter_id = encounter_names[completed_encounters[idx]]
      if encounter_id then
         local aliases = encounter_aliases[encounter_id]
         result_env["boss" .. encounter_id] = dead
         if aliases then
            for alias_idx = 1, #aliases do
               result_env[aliases[alias_idx]] = dead
            end
         end
      end
   end
end

local result_env = {}
-- =highmaul and ((normal and (name:match("imp") or defeated == 6)) or (heroic and defeated == 2))
function LFGListUtil_SortSearchResults(results)
   local self = LFGListFrame.SearchPanel
   if self.filter_expression then
      local check = self.filter_func
      local shift_down = 0
      local original_size = #results
      for idx = 1, original_size do
         local id = results[idx]
         local _, activityID, name, comment, voiceChat, iLvl, honorLevel, age, numBNetFriends, numCharFriends, numGuildMates, isDelisted, leaderName, numMembers = C_LFGList.GetSearchResultInfo(id)
         local completedEncounters = C_LFGList.GetSearchResultEncounterInfo(id)
         local memberCounts = C_LFGList.GetSearchResultMemberCounts(id)

         wipe(result_env)
         result_env.name = name:lower()
         result_env.comment = comment:lower()
         result_env.ilvl = iLvl
         -- TODO: should be calculated in meta
         result_env.defeated = completedEncounters and #completedEncounters or 0
         result_env.member = numMembers
         result_env.tank = memberCounts.TANK
         result_env.healer = memberCounts.HEALER
         result_env.damager = memberCounts.DAMAGER + memberCounts.NOROLE
         result_env.my_server = leaderName and not leaderName:find('-')
         if voiceChat and voiceChat ~= "" then
            result_env.voice = voiceChat
         end

         for idx = 1, numMembers do
            local role, class, classLocalized = C_LFGList.GetSearchResultMemberInfo(id, idx)
            local class_lc = class:lower()
            local prev_count = result_env[class_lc]
            result_env[class_lc] = prev_count and (prev_count + 1) or 0
         end
         for idx = 1, MAX_CLASSES do
            local class_lc = CLASS_SORT_ORDER[idx]:lower()
            local count = result_env[class_lc]
            if not count then result_env[class_lc] = 0 end
         end

         local raid_token
         if activityID == 37 then
            raid_token = "highmaul"
            result_env.normal = true
         elseif activityID == 38 then
            raid_token = "highmaul"
            result_env.heroic = true
         elseif activityID == 39 then
            raid_token = "blackrock_foundry"
            result_env.normal = true
         elseif activityID == 40 then
            raid_token = "blackrock_foundry"
            result_env.heroic = true
         elseif activityID == 409 then
            raid_token = "hellfire_citadel"
            result_env.normal = true
         elseif activityID == 410 then
            raid_token = "hellfire_citadel"
            result_env.heroic = true
         elseif activityID == 413 then
            raid_token = "emerald_nightmare"
            result_env.normal = true
         elseif activityID == 414 then
            raid_token = "emerald_nightmare"
            result_env.heroic = true
         elseif activityID == 456 then
            raid_token = "trial_of_valor"
            result_env.normal = true
         elseif activityID == 457 then
            raid_token = "trial_of_valor"
            result_env.heroic = true
         end

         if raid_token then
            result_env[raid_token] = true
            if completedEncounters then InsertEncounterStateAliases(result_env, raid_token, completedEncounters) end
         end

         result_env.dead = dead

         -- dump(result_env)

         for alias, original in pairs(aliases) do result_env[alias] = result_env[original] end

         -- dump(result_env)

         local pass
         if check then
            setfenv(check, result_env)
            pass = check()
         end

         if pass then
            if shift_down > 0 then
               results[idx - shift_down] = id
            end
         else
            shift_down = shift_down + 1
         end

      end
      for idx = original_size - shift_down + 1, original_size do
         results[idx] = nil
      end
   end
   table.sort(results, LFGListUtil_SortSearchResultsCB);
end

function LFGListUtil_SortSearchResultsCB(id1, id2)
   local id1, activityID1, name1, comment1, voiceChat1, iLvl1, age1, numBNetFriends1, numCharFriends1, numGuildMates1, isDelisted1 = C_LFGList.GetSearchResultInfo(id1);
   local id2, activityID2, name2, comment2, voiceChat2, iLvl2, age2, numBNetFriends2, numCharFriends2, numGuildMates2, isDelisted2 = C_LFGList.GetSearchResultInfo(id2);

   --If one has more friends, do that one first
   if ( numBNetFriends1 ~= numBNetFriends2 ) then
      return numBNetFriends1 > numBNetFriends2;
   end

   if ( numCharFriends1 ~= numCharFriends2 ) then
      return numCharFriends1 > numCharFriends2;
   end

   if ( numGuildMates1 ~= numGuildMates2 ) then
      return numGuildMates1 > numGuildMates2;
   end

   if ( activityID1 ~= activityID2 ) then
      return activityID1 > activityID2;
   end

   --If we aren't sorting by anything else, just go by ID
   return id1 < id2;
end

function LFGPrintRawResults()
   local totalResults, results = C_LFGList.GetSearchResults()
   for idx = 1, totalResults do
      local id1, activityID1, name1, comment1, voiceChat1, iLvl1, age1, numBNetFriends1, numCharFriends1, numGuildMates1, isDelisted1 = C_LFGList.GetSearchResultInfo(results[idx])
      print(id1, activityID1, C_LFGList.GetActivityInfo(activityID1), '*', name1)
   end
end

function LFGPrintFindDungeon(upper_limit, pattern)
   local lower_limit = 1
   if not pattern then
      lower_limit = upper_limit
      pattern = GetLFGDungeonInfo(upper_limit)
   end
   for idx = lower_limit, upper_limit do
      local name = GetLFGDungeonInfo(idx)
      if name and name:find(pattern) then
         print(idx, name)
         for enc_idx = 1, GetLFGDungeonNumEncounters(idx) do
            local bossName, texture, isKilled = GetLFGDungeonEncounterInfo(idx, enc_idx)
            print("*", enc_idx, bossName)
         end
      end
   end
end