Advertisement
Guest User

Untitled

a guest
May 6th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Windows.Media;
  6. using CommonBehaviors.Actions;
  7. using Styx;
  8. using Styx.Common;
  9. using Styx.CommonBot.Coroutines;
  10. using Styx.WoWInternals;
  11.  
  12. namespace NuokRealmHop
  13. {
  14.     public class LfgList
  15.     {
  16.         private static IEnumerable<int> GetAvailableCategorieIds
  17.         {
  18.             get
  19.             {
  20.                 using (new PerformanceLogger(NuokRealmHopSettings.Instance.DebugPerformace, "GetAvailableCategorieIds"))
  21.                 {
  22.                     using (StyxWoW.Memory.AcquireFrame())
  23.                     {
  24.                         const string getIdsLua = @"local categories = C_LFGList.GetAvailableCategories();
  25. local results = {}
  26. for i = 1, #categories do        
  27. local id = categories[i]
  28. if (id ~= nil) then
  29. table.insert(results, id)
  30. end
  31. end
  32. return unpack(results)";
  33.                         var l = new List<int>();
  34.                         var getResultIds = Lua.GetReturnValues(getIdsLua);
  35.                         l.AddRange(getResultIds.Select(Lua.ParseLuaValue<int>));
  36.                         return l;
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.  
  42.         public static List<CategoryInfo> GetAvailableCategories
  43.         {
  44.             get
  45.             {
  46.                 using (new PerformanceLogger(NuokRealmHopSettings.Instance.DebugPerformace, "GetAvailableCategories"))
  47.                 {
  48.                     using (StyxWoW.Memory.AcquireFrame())
  49.                     {
  50.                         var x = GetAvailableCategorieIds.Select(GetCategoryInfo).ToList();
  51.                         return x;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.  
  58.         /// <summary>
  59.         ///     Retrieve the search information for the last search only
  60.         /// </summary>
  61.         public static List<SearchResultInfo> GetSearchResults
  62.         {
  63.             get
  64.             {
  65.                 using (new PerformanceLogger(NuokRealmHopSettings.Instance.DebugPerformace, "GetSearchResults"))
  66.                 {
  67.                     const string getIdsLua = @"local numResults, resultIDTable = C_LFGList.GetSearchResults();
  68. local results = {}
  69. for i = 1, numResults do        
  70. local id = resultIDTable[i]
  71. if (id ~= nil) then
  72. table.insert(results, id)
  73. end
  74. end
  75. return unpack(results)";
  76.  
  77.                     var l = new List<int>();
  78.                     using (StyxWoW.Memory.AcquireFrame())
  79.                     {
  80.                         var getResultIds = Lua.GetReturnValues(getIdsLua);
  81.  
  82.                         l.AddRange(getResultIds.Select(Lua.ParseLuaValue<int>));
  83.                     }
  84.                     return (from i in l where i != 0 select GetSearchResultInfo(i)).ToList();
  85.                 }
  86.             }
  87.         }
  88.  
  89.         public static int NumActiveAppliications => Lua.GetReturnVal<int>("return C_LFGList.GetNumApplications()", 1);
  90.  
  91.         /// <summary>
  92.         ///     Gets all the currently applied for groups
  93.         /// </summary>
  94.         public static List<ApplicationInfo> GetApplicationInfos
  95.         {
  96.             get
  97.             {
  98.                 using (new PerformanceLogger(NuokRealmHopSettings.Instance.DebugPerformace, "GetApplicationInfos"))
  99.                 {
  100.                     var l = new List<ApplicationInfo>();
  101.  
  102.                     using (StyxWoW.Memory.AcquireFrame())
  103.                     {
  104.                         var apps = GetApplicationIds;
  105.                         l.AddRange(apps.Select(GetApplicationInfo));
  106.                     }
  107.                     return l;
  108.                 }
  109.             }
  110.         }
  111.  
  112.         /// <summary>
  113.         ///     Gets the players current realm name
  114.         /// </summary>
  115.         public static string GetRealmName
  116.         {
  117.             get
  118.             {
  119.                 using (new PerformanceLogger(NuokRealmHopSettings.Instance.DebugPerformace, "GetRealmName"))
  120.                 {
  121.                     var realm = "nil";
  122.                     using (StyxWoW.Memory.AcquireFrame())
  123.                     {
  124.                         string lua;
  125.                         var gi = StyxWoW.Me.GroupInfo;
  126.                         var meLeader = gi.GroupLeader == StyxWoW.Me;
  127.                         if (gi.IsInParty && !meLeader)
  128.                             lua =
  129.                                 @"local index = GetNumGroupMembers();
  130. for i = 1, index do
  131. if UnitIsGroupLeader('party' ..i) then
  132. local name, realm = UnitName('party' ..i);
  133. return realm;
  134. end
  135. end";
  136.  
  137.  
  138.                         else if (gi.IsInRaid && !meLeader)
  139.                             lua = @"local index = GetNumGroupMembers();
  140. for i = 1, index do
  141. if UnitIsGroupLeader('raid' ..i) then
  142. local name, realm = UnitName('raid' ..i);
  143. return realm;
  144. end
  145. end";
  146.                         else lua = "return GetRealmName();";
  147.                         realm = Lua.GetReturnVal<string>(lua, 0);
  148.                     }
  149.                     return realm;
  150.                 }
  151.             }
  152.         }
  153.  
  154.         private static IEnumerable<int> GetApplicationIds
  155.         {
  156.             get
  157.             {
  158.                 var l = new List<int>();
  159.                 const string lua = @"local apps = C_LFGList.GetApplications();
  160. local results = {}
  161. for i = 1, #apps do        
  162. local id = apps[i]
  163. if (id ~= nil) then
  164. table.insert(results, id)
  165. end
  166. end
  167. return unpack(results)";
  168.                 var getResultIds = Lua.GetReturnValues(lua);
  169.                 l.AddRange(getResultIds.Select(Lua.ParseLuaValue<int>));
  170.                 return l;
  171.             }
  172.         }
  173.  
  174.         /// <summary>
  175.         ///     Opens the LFG Frame in game
  176.         /// </summary>
  177.         public static void LFGLiftFrame_Show()
  178.         {
  179.             Lua.DoString("PVEFrame_ShowFrame('GroupFinderFrame', LFGListPVEStub)");
  180.             new SleepForLagDuration().ExecuteCoroutine();
  181.         }
  182.  
  183.         private static void Log(string format, params object[] args)
  184.         {
  185.             Logging.WriteDiagnostic(Colors.Goldenrod, "LFGList: " + format, args);
  186.         }
  187.  
  188.         /// <summary>
  189.         ///     Applies to join the group
  190.         /// </summary>
  191.         /// <param name="id">Group Id</param>
  192.         public static void ApplyToGroup(int id)
  193.         {
  194.             Lua.DoString($"C_LFGList.ApplyToGroup({id}, '', false, false, true);");
  195.         }
  196.  
  197.         /// <summary>
  198.         ///     Applies to join the group
  199.         /// </summary>
  200.         /// <param name="application"></param>
  201.         public static void ApplyToGroup(SearchResultInfo application)
  202.         {
  203.             Log($"Applying to Group {application} on {application.Realm}");
  204.             ApplyToGroup(application.Id);
  205.         }
  206.  
  207.         /// <summary>
  208.         ///     Accepts group invite pop ups
  209.         /// </summary>
  210.         public static void AcceptPopUps()
  211.         {
  212.             Lua.DoString("LFGListInviteDialog.AcceptButton:Click(); LFGListInviteDialog.AcknowledgeButton:Click();");
  213.         }
  214.  
  215.         private static CategoryInfo GetCategoryInfo(int categoryId)
  216.         {
  217.             var a = Lua.GetReturnValues($"return C_LFGList.GetCategoryInfo({categoryId});");
  218.  
  219.             var name = Lua.ParseLuaValue<string>(a[0]);
  220.             var seperate = Lua.ParseLuaValue<bool>(a[1]);
  221.             var auto = Lua.ParseLuaValue<bool>(a[2]);
  222.             var prefer = Lua.ParseLuaValue<bool>(a[3]);
  223.  
  224.             return new CategoryInfo
  225.             {
  226.                 CategoryId = categoryId,
  227.                 Name = name,
  228.                 SeparateRecommended = seperate,
  229.                 AutoChoose = auto,
  230.                 PreferCurrentArea = prefer
  231.             };
  232.         }
  233.  
  234.         /// <summary>
  235.         ///     Initializes a search for groups within the category
  236.         /// </summary>
  237.         /// <param name="categorieId">Category Id</param>
  238.         public static void Search(int categorieId)
  239.         {
  240.             Lua.DoString(
  241.                 $"local languages = C_LFGList.GetLanguageSearchFilter(); C_LFGList.Search({categorieId}, '', 0, 0, languages)");
  242.         }
  243.  
  244.         /// <summary>
  245.         ///     Initializes a search for groups within the category
  246.         /// </summary>
  247.         /// <param name="category">CategoryInfo</param>
  248.         public static void Search(CategoryInfo category)
  249.         {
  250.             Log($"Starting a search of {category}");
  251.             Search(category.CategoryId);
  252.         }
  253.  
  254.         /// <summary>
  255.         ///     Starts the search for groups
  256.         /// </summary>
  257.         /// <param name="category">The category you wish to search</param>
  258.         public static void Search(LFGCategory category)
  259.         {
  260.             Log($"Starting a search of {category}");
  261.             Search((int) category);
  262.         }
  263.  
  264.         private static ApplicationInfo GetApplicationInfo(int applicationId)
  265.         {
  266.             var info = Lua.GetReturnValues($"return C_LFGList.GetApplicationInfo({applicationId});");
  267.  
  268.             return new ApplicationInfo
  269.             {
  270.                 Id = Lua.ParseLuaValue<int>(info[0]),
  271.                 Status = GetApplicationState(Lua.ParseLuaValue<string>(info[1])),
  272.                 PendingStatus = Lua.ParseLuaValue<bool>(info[2]),
  273.                 ApplicationDuration = TimeSpan.FromMinutes(Lua.ParseLuaValue<double>(info[3])),
  274.                 AccpetedRole = Lua.ParseLuaValue<string>(info[4])
  275.             };
  276.         }
  277.  
  278.         private static SearchResultInfo GetSearchResultInfo(int resultId)
  279.         {
  280.             var r = Lua.GetReturnValues($"return C_LFGList.GetSearchResultInfo({resultId});");
  281.             //retrieve the realm & player name to see if they check out
  282.             var s = Lua.ParseLuaValue<string>(r[11]);
  283.  
  284.             if (s == null)
  285.                 return new SearchResultInfo
  286.                 {
  287.                     Id = resultId,
  288.                     ActivityId = Lua.ParseLuaValue<int>(r[1]),
  289.                     Name = Lua.ParseLuaValue<string>(r[2]),
  290.                     Comment = Lua.ParseLuaValue<string>(r[3]),
  291.                     VoiceChat = Lua.ParseLuaValue<bool>(r[4]),
  292.                     iLevel = Lua.ParseLuaValue<int>(r[5]),
  293.                     Age = TimeSpan.FromSeconds(Lua.ParseLuaValue<int>(r[6])),
  294.                     NumBNetFriends = Lua.ParseLuaValue<int>(r[7]),
  295.                     NumCharFriends = Lua.ParseLuaValue<int>(r[8]),
  296.                     NumGuildMates = Lua.ParseLuaValue<int>(r[9]),
  297.                     IsDelisted = Lua.ParseLuaValue<bool>(r[10]),
  298.                     PlayerCount = Lua.ParseLuaValue<int>(r[12]),
  299.                     AutoInvite = Lua.ParseLuaValue<bool>(r[13]),
  300.                     Realm = "null",
  301.                     LeaderName = "null"
  302.                 };
  303.  
  304.             char[] sep = {'-'};
  305.             var wowSplit = s.Split(sep, 2);
  306.  
  307.             var l = wowSplit.Length < 1 ? "null" : wowSplit[0];
  308.             var ra = wowSplit.Length < 2 ? "null" : wowSplit[1];
  309.  
  310.             return new SearchResultInfo
  311.             {
  312.                 Id = resultId,
  313.                 ActivityId = Lua.ParseLuaValue<int>(r[1]),
  314.                 Name = Lua.ParseLuaValue<string>(r[2]),
  315.                 Comment = Lua.ParseLuaValue<string>(r[3]),
  316.                 VoiceChat = Lua.ParseLuaValue<bool>(r[4]),
  317.                 iLevel = Lua.ParseLuaValue<int>(r[5]),
  318.                 Age = TimeSpan.FromSeconds(Lua.ParseLuaValue<int>(r[6])),
  319.                 NumBNetFriends = Lua.ParseLuaValue<int>(r[7]),
  320.                 NumCharFriends = Lua.ParseLuaValue<int>(r[8]),
  321.                 NumGuildMates = Lua.ParseLuaValue<int>(r[9]),
  322.                 IsDelisted = Lua.ParseLuaValue<bool>(r[10]),
  323.                 PlayerCount = Lua.ParseLuaValue<int>(r[12]),
  324.                 AutoInvite = Lua.ParseLuaValue<bool>(r[13]),
  325.                 LeaderName = l,
  326.                 Realm = ra
  327.             };
  328.         }
  329.  
  330.         public static bool LeaveGroup()
  331.         {
  332.             Log("Leaving Current Group");
  333.             Lua.DoString("LeaveParty();");
  334.             var s = new SleepForLagDuration();
  335.             s.ExecuteCoroutine();
  336.             return StyxWoW.Me.GroupInfo.IsInParty || StyxWoW.Me.GroupInfo.IsInRaid;
  337.         }
  338.  
  339.         private static ApplicationState GetApplicationState(string application)
  340.         {
  341.             switch (application)
  342.             {
  343.                 case "invited":
  344.                     return ApplicationState.Invited;
  345.                 case "failed":
  346.                     return ApplicationState.Failed;
  347.                 case "cancelled":
  348.                     return ApplicationState.Cancelled;
  349.                 case "applied":
  350.                     return ApplicationState.Applied;
  351.             }
  352.  
  353.             Log($"{application} not listed as a state");
  354.             return ApplicationState.Failed;
  355.         }
  356.     }
  357.  
  358.     [SuppressMessage("ReSharper", "InconsistentNaming")]
  359.     public enum LFGCategory
  360.     {
  361.         Questing = 1,
  362.         Dungeons = 2,
  363.         Raids = 3,
  364.         Arenas = 4,
  365.         Scenarios = 5,
  366.         Custom = 6,
  367.         Skirmishes = 7,
  368.         Battlegrounds = 8,
  369.         RatedBGs = 9,
  370.         Ashran = 10
  371.     }
  372.  
  373.     [Flags]
  374.     public enum ApplicationState
  375.     {
  376.         Invited,
  377.         Failed,
  378.         Cancelled,
  379.         Applied
  380.     }
  381.  
  382.     public class CategoryInfo
  383.     {
  384.         public bool AutoChoose;
  385.         public int CategoryId;
  386.         public string Name;
  387.         public bool PreferCurrentArea;
  388.         public bool SeparateRecommended;
  389.         public LFGCategory Category => (LFGCategory) CategoryId;
  390.  
  391.         public override string ToString()
  392.         {
  393.             return $"{Name}";
  394.         }
  395.     }
  396.  
  397.     public class ApplicationInfo
  398.     {
  399.         public string AccpetedRole;
  400.         public TimeSpan ApplicationDuration;
  401.         public int Id;
  402.         public bool PendingStatus;
  403.         public ApplicationState Status;
  404.  
  405.         public override string ToString()
  406.         {
  407.             return $"ApplicationId {Id}";
  408.         }
  409.     }
  410.  
  411.     [SuppressMessage("ReSharper", "InconsistentNaming")]
  412.     public class SearchResultInfo
  413.     {
  414.         public int ActivityId;
  415.         public TimeSpan Age;
  416.         public bool AutoInvite;
  417.         public string Comment;
  418.         public int Id;
  419.         public int iLevel;
  420.         public bool IsDelisted;
  421.         public string LeaderName;
  422.         public string Name;
  423.         public int NumBNetFriends;
  424.         public int NumCharFriends;
  425.         public int NumGuildMates;
  426.         public int PlayerCount;
  427.         public string Realm;
  428.         public bool VoiceChat;
  429.  
  430.         public void ApplyToGroup()
  431.         {
  432.             LfgList.ApplyToGroup(this);
  433.         }
  434.  
  435.         public override string ToString()
  436.         {
  437.             return $"{Name} ({Id})";
  438.         }
  439.     }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement