MapDouyinLiveGiftComponentSystem.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json.Linq;
  4. namespace ET.Server
  5. {
  6. [FriendOf(typeof (MapDouyinLiveGiftComponent))]
  7. public static class MapDouyinLiveGiftComponentSystem
  8. {
  9. public class MapDouyinLiveGiftComponentAwakeSystem: AwakeSystem<MapDouyinLiveGiftComponent>
  10. {
  11. protected override void Awake(MapDouyinLiveGiftComponent self)
  12. {
  13. Log.Info($"创建抖音直播礼物任务组件...");
  14. }
  15. }
  16. public class MapDouyinLiveGiftComponentDestroySystem: DestroySystem<MapDouyinLiveGiftComponent>
  17. {
  18. protected override void Destroy(MapDouyinLiveGiftComponent self)
  19. {
  20. Log.Info($"销毁抖音直播礼物任务组件");
  21. self.StopTask();
  22. }
  23. }
  24. public class MapDouyinLiveGiftComponentUpdateSystem: UpdateSystem<MapDouyinLiveGiftComponent>
  25. {
  26. protected override void Update(MapDouyinLiveGiftComponent self)
  27. {
  28. WNPlayer player = self.GetParent<Map>().Player;
  29. bool tokenIsNull = player.GetComponent<PlayerDataComponent>().Data.TokenIsNull;
  30. if (tokenIsNull)
  31. {
  32. return;
  33. }
  34. // 运行中状态
  35. if (self.Status == 3)
  36. {
  37. return;
  38. }
  39. Log.Debug($"检查抖音直播礼物任务状态...Status={self.Status}");
  40. self.Status = self.CheckTaskStatus();
  41. switch (self.Status)
  42. {
  43. case -1:
  44. {
  45. // 接口请求出错
  46. Log.Debug($"检查抖音直播礼物任务状态 - 抖音接口请求出错");
  47. break;
  48. }
  49. case 1:
  50. {
  51. // 数据推送回调任务不存在
  52. Log.Debug($"检查抖音直播礼物任务状态 - 任务已经被删除,可能是因为主播取消挂载/已关播");
  53. break;
  54. }
  55. case 2:
  56. {
  57. // 数据推送回调任务未启动则启动
  58. Log.Debug($"检查抖音直播礼物任务状态 - 任务启动...");
  59. self.StartTask();
  60. break;
  61. }
  62. case 3:
  63. // 数据推送回调任务运行中,不处理
  64. break;
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 启动礼物推送任务
  70. /// </summary>
  71. /// <param name="self"></param>
  72. private static void StartTask(this MapDouyinLiveGiftComponent self)
  73. {
  74. // 请求头
  75. Dictionary<string, string> head = new Dictionary<string, string>();
  76. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  77. // 参数
  78. JObject param = new JObject();
  79. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  80. param.Add("appid", DouyinConst.Appid);
  81. param.Add("msg_type", "live_gift");
  82. string str = HttpHelper.PostRequestByDouyin(DouyinConst.StartTaskUrl, head, param);
  83. if (string.IsNullOrEmpty(str))
  84. {
  85. Log.Error($"启动礼物推送任务 - StartTaskUrl 请求失败...返回为null");
  86. return;
  87. }
  88. JObject jObject = JObject.Parse(str);
  89. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  90. if (errNo != 0)
  91. {
  92. if (errNo == 40004)
  93. {
  94. Log.Debug($"启动礼物推送任务 - StartTaskUrl 请求成功 - 40004 容错处理:AccessToken不合法,下一秒重新刷新");
  95. self.GetParent<Map>().DomainScene().GetComponent<GameDouyinComponent>().AccessTokenTime = 0;
  96. }
  97. else
  98. {
  99. Log.Error($"启动礼物推送任务 - StartTaskUrl 请求成功...返回错误:{errNo}");
  100. }
  101. return;
  102. }
  103. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  104. self.TaskId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("task_id"));
  105. }
  106. /// <summary>
  107. /// 停止礼物推送任务
  108. /// </summary>
  109. /// <param name="self"></param>
  110. private static void StopTask(this MapDouyinLiveGiftComponent self)
  111. {
  112. // 请求头
  113. Dictionary<string, string> head = new Dictionary<string, string>();
  114. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  115. // 参数
  116. JObject param = new JObject();
  117. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  118. param.Add("appid", DouyinConst.Appid);
  119. param.Add("msg_type", "live_gift");
  120. string str = HttpHelper.PostRequestByDouyin(DouyinConst.StopTaskUrl, head, param);
  121. if (string.IsNullOrEmpty(str))
  122. {
  123. Log.Error($"停止礼物推送任务 - StopTaskUrl 请求失败...返回为null");
  124. return;
  125. }
  126. JObject jObject = JObject.Parse(str);
  127. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  128. if (errNo != 0)
  129. {
  130. if (errNo == 40004)
  131. {
  132. Log.Debug($"停止礼物推送任务 - StopTaskUrl 请求成功 - 40004 容错处理:AccessToken不合法,下一秒重新刷新");
  133. self.GetParent<Map>().DomainScene().GetComponent<GameDouyinComponent>().AccessTokenTime = 0;
  134. }
  135. else
  136. {
  137. Log.Error($"停止礼物推送任务 - StopTaskUrl 请求成功...返回错误:{errNo}");
  138. }
  139. return;
  140. }
  141. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  142. }
  143. /// <summary>
  144. /// 检查礼物推送任务状态 (1-任务不存在, 2-任务未启动, 3-任务运行中)
  145. /// </summary>
  146. /// <param name="self"></param>
  147. /// <returns></returns>
  148. private static int CheckTaskStatus(this MapDouyinLiveGiftComponent self)
  149. {
  150. Dictionary<string, string> head = new Dictionary<string, string>();
  151. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  152. // 参数
  153. Dictionary<string, string> param = new Dictionary<string, string>();
  154. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  155. param.Add("appid", DouyinConst.Appid);
  156. param.Add("msg_type", "live_gift");
  157. string str = HttpHelper.GetRequestByDouyin(DouyinConst.CheckTaskUrl, head, param);
  158. if (string.IsNullOrEmpty(str))
  159. {
  160. Log.Error($"检查礼物推送任务状态 - CheckTaskUrl 请求失败...返回为null");
  161. return -1;
  162. }
  163. JObject jObject = JObject.Parse(str);
  164. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  165. if (errNo != 0)
  166. {
  167. if (errNo == 40004)
  168. {
  169. Log.Debug($"检查礼物推送任务状态 - CheckTaskUrl 请求成功 - 40004 容错处理:AccessToken不合法,下一秒重新刷新");
  170. self.GetParent<Map>().DomainScene().GetComponent<GameDouyinComponent>().AccessTokenTime = 0;
  171. }
  172. else
  173. {
  174. Log.Error($"检查礼物推送任务状态 - CheckTaskUrl 请求成功...返回错误:{errNo}");
  175. }
  176. return -1;
  177. }
  178. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  179. return Convert.ToInt32(jObject.SelectToken("data").SelectToken("status"));
  180. }
  181. }
  182. }