|
@@ -0,0 +1,193 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
+
|
|
|
+namespace ET.Server
|
|
|
+{
|
|
|
+ [FriendOf(typeof (MapDouyinLiveGiftComponent))]
|
|
|
+ public static class MapDouyinLiveGiftComponentSystem
|
|
|
+ {
|
|
|
+ public class MapDouyinLiveGiftComponentAwakeSystem: AwakeSystem<MapDouyinLiveGiftComponent>
|
|
|
+ {
|
|
|
+ protected override void Awake(MapDouyinLiveGiftComponent self)
|
|
|
+ {
|
|
|
+ Log.Info($"创建抖音直播礼物任务组件...");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class MapDouyinLiveGiftComponentDestroySystem: DestroySystem<MapDouyinLiveGiftComponent>
|
|
|
+ {
|
|
|
+ protected override void Destroy(MapDouyinLiveGiftComponent self)
|
|
|
+ {
|
|
|
+ Log.Info($"销毁抖音直播礼物任务组件");
|
|
|
+
|
|
|
+ bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
|
|
|
+
|
|
|
+ if (tokenIsNull)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.StopTask();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class MapDouyinLiveGiftComponentUpdateSystem: UpdateSystem<MapDouyinLiveGiftComponent>
|
|
|
+ {
|
|
|
+ protected override void Update(MapDouyinLiveGiftComponent self)
|
|
|
+ {
|
|
|
+ bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
|
|
|
+
|
|
|
+ if (tokenIsNull)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 运行中状态
|
|
|
+ if (self.Status == 3)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.Debug($"检查抖音直播礼物任务状态...Status={self.Status}");
|
|
|
+
|
|
|
+ self.Status = self.CheckTaskStatus();
|
|
|
+
|
|
|
+ switch (self.Status)
|
|
|
+ {
|
|
|
+ case -1:
|
|
|
+ {
|
|
|
+ // 接口请求出错
|
|
|
+ Log.Debug($"检查抖音直播礼物任务状态 - 抖音接口请求出错");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 1:
|
|
|
+ {
|
|
|
+ // 数据推送回调任务不存在
|
|
|
+ Log.Debug($"检查抖音直播礼物任务状态 - 任务已经被删除,可能是因为主播取消挂载/已关播");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 2:
|
|
|
+ {
|
|
|
+ // 数据推送回调任务未启动则启动
|
|
|
+ Log.Debug($"检查抖音直播礼物任务状态 - 任务启动...");
|
|
|
+ self.StartTask();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 3:
|
|
|
+ // 数据推送回调任务运行中,不处理
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 启动礼物推送任务
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="self"></param>
|
|
|
+ private static void StartTask(this MapDouyinLiveGiftComponent self)
|
|
|
+ {
|
|
|
+ // 请求头
|
|
|
+ Dictionary<string, string> head = new Dictionary<string, string>();
|
|
|
+ head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
|
|
|
+ // 参数
|
|
|
+ JObject param = new JObject();
|
|
|
+ param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
|
|
|
+ param.Add("appid", DouyinConst.Appid);
|
|
|
+ param.Add("msg_type", "live_gift");
|
|
|
+
|
|
|
+ string str = HttpHelper.PostRequestByDouyin(DouyinConst.StartTaskUrl, head, param);
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(str))
|
|
|
+ {
|
|
|
+ Log.Error($"启动礼物推送任务 - StartTaskUrl 请求失败...返回为null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ JObject jObject = JObject.Parse(str);
|
|
|
+
|
|
|
+ int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
|
|
|
+
|
|
|
+ if (errNo != 0)
|
|
|
+ {
|
|
|
+ Log.Error($"启动礼物推送任务 - StartTaskUrl 请求成功...返回错误:{errNo}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
|
|
|
+ self.TaskId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("task_id"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 停止礼物推送任务
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="self"></param>
|
|
|
+ private static void StopTask(this MapDouyinLiveGiftComponent self)
|
|
|
+ {
|
|
|
+ // 请求头
|
|
|
+ Dictionary<string, string> head = new Dictionary<string, string>();
|
|
|
+ head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
|
|
|
+ // 参数
|
|
|
+ JObject param = new JObject();
|
|
|
+ param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
|
|
|
+ param.Add("appid", DouyinConst.Appid);
|
|
|
+ param.Add("msg_type", "live_gift");
|
|
|
+
|
|
|
+ string str = HttpHelper.PostRequestByDouyin(DouyinConst.StopTaskUrl, head, param);
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(str))
|
|
|
+ {
|
|
|
+ Log.Error($"停止礼物推送任务 - StopTaskUrl 请求失败...返回为null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ JObject jObject = JObject.Parse(str);
|
|
|
+
|
|
|
+ int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
|
|
|
+
|
|
|
+ if (errNo != 0)
|
|
|
+ {
|
|
|
+ Log.Error($"停止礼物推送任务 - StopTaskUrl 请求成功...返回错误:{errNo}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检查礼物推送任务状态 (1-任务不存在, 2-任务未启动, 3-任务运行中)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="self"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private static int CheckTaskStatus(this MapDouyinLiveGiftComponent self)
|
|
|
+ {
|
|
|
+ Dictionary<string, string> head = new Dictionary<string, string>();
|
|
|
+ head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
|
|
|
+ // 参数
|
|
|
+ Dictionary<string, string> param = new Dictionary<string, string>();
|
|
|
+ param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
|
|
|
+ param.Add("appid", DouyinConst.Appid);
|
|
|
+ param.Add("msg_type", "live_gift");
|
|
|
+
|
|
|
+ string str = HttpHelper.GetRequestByDouyin(DouyinConst.CheckTaskUrl, head, param);
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(str))
|
|
|
+ {
|
|
|
+ Log.Error($"检查礼物推送任务状态 - CheckTaskUrl 请求失败...返回为null");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ JObject jObject = JObject.Parse(str);
|
|
|
+
|
|
|
+ int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
|
|
|
+
|
|
|
+ if (errNo != 0)
|
|
|
+ {
|
|
|
+ Log.Error($"检查礼物推送任务状态 - CheckTaskUrl 请求成功...返回错误:{errNo}");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
|
|
|
+ return Convert.ToInt32(jObject.SelectToken("data").SelectToken("status"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|