Robot.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Server.MirObjects;
  2. using System.Text.RegularExpressions;
  3. namespace Server.MirEnvir
  4. {
  5. public class Robot
  6. {
  7. protected static Envir Envir
  8. {
  9. get { return Envir.Main; }
  10. }
  11. public int? Month;
  12. public int? Day;
  13. public int? Hour;
  14. public int? Minute;
  15. public DayOfWeek? DayOfWeek;
  16. private string Page;
  17. private static bool CheckHour;
  18. private static bool CheckMinute;
  19. private static DateTime NextCheck;
  20. private static readonly List<Robot> Robots = new List<Robot>();
  21. private static void SetNextCheck()
  22. {
  23. var next = Envir.Now;
  24. next = next.AddSeconds(-next.Second);
  25. next = next.AddMinutes(1);
  26. if (!CheckMinute)
  27. {
  28. next = next.AddMinutes(-next.Minute);
  29. next = next.AddHours(1);
  30. if (!CheckHour)
  31. {
  32. next = next.AddHours(-next.Hour);
  33. next = next.AddDays(1);
  34. }
  35. }
  36. NextCheck = next;
  37. }
  38. public static void Clear()
  39. {
  40. Robots.Clear();
  41. }
  42. private bool IsMatch(DateTime date)
  43. {
  44. if (Month != null && date.Month != Month) return false;
  45. if (Day != null && date.Day != Day) return false;
  46. if (Hour != null && date.Hour != Hour) return false;
  47. if (Minute != null && date.Minute != Minute) return false;
  48. if (DayOfWeek != null && date.DayOfWeek != DayOfWeek) return false;
  49. return true;
  50. }
  51. public static void Process(NPCScript script)
  52. {
  53. if (NextCheck > Envir.Now)
  54. {
  55. return;
  56. }
  57. var matches = Robots.Where(x => x.IsMatch(Envir.Now));
  58. foreach (var match in matches)
  59. {
  60. script.Call(match.Page);
  61. }
  62. SetNextCheck();
  63. }
  64. public static void AddRobot(string page)
  65. {
  66. Regex regex = new Regex(@"\(([0-9#]{1,2}),([0-9#]{1,2}),([0-9#]{1,2}),([0-9#]{1,2}),([^\s]+)\)");
  67. Match match = regex.Match(page);
  68. if (!match.Success) return;
  69. var robot = new Robot { Page = page };
  70. if (int.TryParse(match.Groups[1].Value, out int val))
  71. {
  72. robot.Month = val;
  73. }
  74. if (int.TryParse(match.Groups[2].Value, out val))
  75. {
  76. robot.Day = val;
  77. }
  78. if (int.TryParse(match.Groups[3].Value, out val))
  79. {
  80. CheckHour = true;
  81. robot.Hour = val;
  82. }
  83. if (int.TryParse(match.Groups[4].Value, out val))
  84. {
  85. CheckMinute = true;
  86. robot.Minute = val;
  87. }
  88. if (Enum.TryParse<DayOfWeek>(match.Groups[5].Value, true, out DayOfWeek enumVal))
  89. {
  90. robot.DayOfWeek = enumVal;
  91. }
  92. Robots.Add(robot);
  93. }
  94. }
  95. }