ModuleChat.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using CommonAI.ZoneClient;
  2. using CommonLang;
  3. using CommonLang.Concurrent;
  4. using CommonLang.Reflection;
  5. using CommonRPG.Protocol.Client;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. namespace CommonRPG.Client.BotTest.Runner.Modules
  12. {
  13. public class ModuleChat : RunnerModule
  14. {
  15. #region ChatData
  16. private static AtomicInteger s_index = new AtomicInteger(0);
  17. private static List<string> s_chat_list = new List<string>();
  18. static ModuleChat()
  19. {
  20. var all = File.ReadAllLines(Application.StartupPath + @"\ChatData.txt");
  21. foreach (var line in all)
  22. {
  23. if (line.Trim().Length > 0)
  24. {
  25. var kv = line.Split(new char[] { ':' }, 2);
  26. if (kv.Length == 2)
  27. {
  28. s_chat_list.Add(kv[1]);
  29. }
  30. else
  31. {
  32. s_chat_list.Add(line);
  33. }
  34. }
  35. }
  36. }
  37. #endregion
  38. private Random random = new Random();
  39. public ModuleChat(BotRunner r) : base(r)
  40. {
  41. base.Client.OnZoneActorEntered += Client_OnZoneActorEntered;
  42. }
  43. private void Client_OnZoneActorEntered(CommonAI.ZoneClient.ZoneActor obj)
  44. {
  45. obj.Parent.AddTimePeriodicMS(Config.ChatIntervalMS, (t) =>
  46. {
  47. if (base.IsEnable)
  48. {
  49. do_interval();
  50. }
  51. });
  52. }
  53. private void do_interval()
  54. {
  55. var text = s_chat_list[(int)(s_index.GetAndIncrement() % s_chat_list.Count)];
  56. try
  57. {
  58. var channel = CUtils.GetRandomInArray(Config.ChatChannels, this.random);
  59. base.Runner.Client.GameClient.Request<ClientChatResponse>(new ClientChatRequest()
  60. {
  61. channel_type = (short)channel,
  62. content = text,
  63. }, (err, rsp) =>
  64. {
  65. if (err != null) log.Error("SentChat : " + err.Message);
  66. else log.Info("SentChat : " + rsp);
  67. });
  68. // bot.chat_SendChat((err, rsp) =>
  69. // {
  70. // if (err != null) log.Error("SentChat : " + err.Message);
  71. // else log.Info("SentChat : " + rsp);
  72. // }, text, "", channel);
  73. }
  74. catch (Exception err)
  75. {
  76. log.Error(err.Message, err);
  77. }
  78. }
  79. //-------------------------------------------------------------------------------------------------------------------
  80. [Desc("聊天配置")]
  81. [Expandable]
  82. public class Config : RunnerModuleConfig
  83. {
  84. [Desc("聊天发送间隔")]
  85. public static int ChatIntervalMS = 5000;
  86. [Desc("聊天发送频道")]
  87. public static ChatChannel[] ChatChannels = new ChatChannel[]
  88. {
  89. ChatChannel.World,
  90. ChatChannel.Trade,
  91. };
  92. public override string ToString()
  93. {
  94. return "聊天配置";
  95. }
  96. }
  97. public enum ChatChannel
  98. {
  99. World = ClientChatRequest.CHANNEL_TYPE_WORLD,
  100. Trade = ClientChatRequest.CHANNEL_TYPE_TRADE,
  101. Guild = ClientChatRequest.CHANNEL_TYPE_GUILD,
  102. Team = ClientChatRequest.CHANNEL_TYPE_TEAM,
  103. Battle = ClientChatRequest.CHANNEL_TYPE_BATTLE,
  104. Area = ClientChatRequest.CHANNEL_TYPE_AREA,
  105. }
  106. }
  107. }