DailyTaskService.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using Mir2Assistant.Common.Functions;
  2. using Mir2Assistant.Common.Models;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. namespace Mir2Assistant.TabForms.DailyTask
  12. {
  13. public delegate void CompleteEvent();
  14. public class DailyTaskService
  15. {
  16. public readonly MirGameInstanceModel gameInstance;
  17. /// <summary>
  18. /// 任务中要打的怪
  19. /// </summary>
  20. private ConcurrentDictionary<string, int> TaskMonster { get; set; } = new ConcurrentDictionary<string, int>();
  21. /// <summary>
  22. /// 找怪的路径
  23. /// </summary>
  24. public ConcurrentQueue<Point> FindPath { get; set; } = new ConcurrentQueue<Point>();
  25. public bool Pause = false;
  26. public event CompleteEvent? CompleteEvent;
  27. private bool _complete = true;
  28. public bool Complete
  29. {
  30. get
  31. {
  32. return _complete;
  33. }
  34. set
  35. {
  36. _complete = value;
  37. if (value)
  38. {
  39. lockedMonster = null;
  40. CompleteEvent?.Invoke();
  41. }
  42. }
  43. }
  44. public int pathIndex = 0;
  45. private MonsterModel? lockedMonster = null;
  46. public Point NextPoint { get; set; }
  47. public bool stopRun = false;
  48. string[] availableSkills = "裂神符,灭天火".Split(',');
  49. Random rand = new Random();
  50. public DailyTaskService(MirGameInstanceModel _gameInstance)
  51. {
  52. gameInstance = _gameInstance;
  53. gameInstance.NewSysMsg += (flag, str) =>
  54. {
  55. if (Complete)
  56. {
  57. return;
  58. }
  59. if (string.IsNullOrEmpty(str) || flag != 0x00ffffff)
  60. {
  61. return;
  62. }
  63. if (str.EndsWith("天罡正气经验!"))
  64. {
  65. Complete = true;
  66. }
  67. var regex = new Regex(@"你现在已经击败(.*?):(\d+)/(\d+)");
  68. Match match = regex.Match(str);
  69. if (match.Success)
  70. {
  71. if (match.Groups[2].Value == match.Groups[3].Value)
  72. {
  73. TaskMonster.TryRemove(match.Groups[1].Value, out int v);
  74. }
  75. }
  76. };
  77. }
  78. public void AddTaskMonster(string name, int qty)
  79. {
  80. if (TaskMonster.ContainsKey(name))
  81. {
  82. TaskMonster[name] = Math.Max(qty, TaskMonster[name]);
  83. }
  84. else
  85. {
  86. TaskMonster.TryAdd(name, qty);
  87. }
  88. }
  89. public async Task RunTask()
  90. {
  91. Complete = false;
  92. stopRun = false;
  93. lockedMonster = null;
  94. pathIndex = 0;
  95. bool oldPause = false;
  96. await Task.Run(() =>
  97. {
  98. while (!Complete)
  99. {
  100. try
  101. {
  102. oldPause = Pause;
  103. if (Pause)
  104. {
  105. if (!stopRun)
  106. {
  107. stopRun = true;
  108. GoRunFunction.FindPath(gameInstance, gameInstance.CharacterStatus!.X!.Value, gameInstance.CharacterStatus.Y!.Value);
  109. }
  110. continue;
  111. }
  112. else
  113. {
  114. if (oldPause)
  115. {
  116. lockedMonster = null;
  117. stopRun = false;
  118. }
  119. }
  120. if (lockedMonster == null)
  121. {
  122. lockedMonster = gameInstance.Monsters.Values?.FirstOrDefault(o => o.Flag != 0 && TaskMonster.Keys.Contains(o.Name!));
  123. }
  124. else
  125. {
  126. lockedMonster = gameInstance.Monsters.Values?.FirstOrDefault(o => o.Addr == lockedMonster.Addr);
  127. }
  128. if (lockedMonster != null)
  129. {
  130. if (!stopRun)
  131. {
  132. if (Math.Abs(lockedMonster.X!.Value - gameInstance!.CharacterStatus!.X!.Value) < 5 && Math.Abs(lockedMonster.Y!.Value - gameInstance.CharacterStatus.Y!.Value) < 5)
  133. {
  134. GoRunFunction.FindPath(gameInstance, gameInstance!.CharacterStatus!.X!.Value, gameInstance!.CharacterStatus!.Y!.Value);
  135. stopRun = true;
  136. }
  137. else
  138. {
  139. GoRunFunction.FindPath(gameInstance, lockedMonster.X!.Value + new int[] { 1, -1 }.OrderBy(o => rand.Next()).First(), lockedMonster.Y!.Value + new int[] { 1, -1 }.OrderBy(o => rand.Next()).First());
  140. }
  141. }
  142. if (lockedMonster.Flag == 0)
  143. {
  144. //TaskMonster[lockedMonster.Name!] -= 1;
  145. //if (TaskMonster[lockedMonster.Name!] <= 0)
  146. //{
  147. // TaskMonster.Remove(lockedMonster.Name!, out int v);
  148. //}
  149. lockedMonster = null;
  150. }
  151. else
  152. {
  153. var lmAddr = lockedMonster.Addr;
  154. MonsterFunction.SlayingMonster(gameInstance, lmAddr);
  155. Thread.Sleep(200);
  156. var skill = gameInstance.Skills.Where(o => availableSkills.Contains(o.Name)).OrderBy(o => rand.Next()).FirstOrDefault();
  157. if (skill != null)
  158. {
  159. MonsterFunction.LockMonster(gameInstance, lmAddr);
  160. SkillFunction.SkillCall(gameInstance, skill.Addr!.Value);
  161. }
  162. }
  163. }
  164. else
  165. {
  166. stopRun = false;
  167. if (FindPath.Count > 0)
  168. {
  169. if (pathIndex >= FindPath.Count)
  170. {
  171. pathIndex = 0;
  172. }
  173. var p = FindPath.Skip(pathIndex).First();
  174. GoRunFunction.FindPath(gameInstance, p.X, p.Y);
  175. if (Math.Abs(gameInstance.CharacterStatus!.X!.Value - p.X) < 3 && Math.Abs(gameInstance.CharacterStatus.Y!.Value - p.Y) < 3)
  176. {
  177. pathIndex++;
  178. }
  179. }
  180. }
  181. if (TaskMonster.Count == 0)
  182. {
  183. Complete = true;
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. Debug.WriteLine(ex.ToString());
  189. }
  190. Thread.Sleep(500);
  191. }
  192. });
  193. }
  194. }
  195. }