123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- 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($"销毁抖音直播礼物任务组件");
- self.StopTask();
- }
- }
- public class MapDouyinLiveGiftComponentUpdateSystem: UpdateSystem<MapDouyinLiveGiftComponent>
- {
- protected override void Update(MapDouyinLiveGiftComponent self)
- {
- WNPlayer player = self.GetParent<Map>().Player;
- bool tokenIsNull = player.GetComponent<PlayerDataComponent>().Data.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)
- {
- if (errNo == 40004)
- {
- Log.Debug($"启动礼物推送任务 - StartTaskUrl 请求成功 - 40004 容错处理:AccessToken不合法,下一秒重新刷新");
- self.GetParent<Map>().DomainScene().GetComponent<GameDouyinComponent>().AccessTokenTime = 0;
- }
- else
- {
- 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)
- {
- if (errNo == 40004)
- {
- Log.Debug($"停止礼物推送任务 - StopTaskUrl 请求成功 - 40004 容错处理:AccessToken不合法,下一秒重新刷新");
- self.GetParent<Map>().DomainScene().GetComponent<GameDouyinComponent>().AccessTokenTime = 0;
- }
- else
- {
- 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)
- {
- if (errNo == 40004)
- {
- Log.Debug($"检查礼物推送任务状态 - CheckTaskUrl 请求成功 - 40004 容错处理:AccessToken不合法,下一秒重新刷新");
- self.GetParent<Map>().DomainScene().GetComponent<GameDouyinComponent>().AccessTokenTime = 0;
- }
- else
- {
- Log.Error($"检查礼物推送任务状态 - CheckTaskUrl 请求成功...返回错误:{errNo}");
- }
- return -1;
- }
- self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
- return Convert.ToInt32(jObject.SelectToken("data").SelectToken("status"));
- }
- }
- }
|