Browse Source

【优化】AccessToken为游戏服全局挂载刷新,TokenIsNull为玩家字段

johnclot69 1 year ago
parent
commit
24fe07c2bc

+ 2 - 0
DotNet/Hotfix/Helper/SceneFactory.cs

@@ -43,6 +43,8 @@ namespace ET.Server
                     // 战斗服的Ice会话组件
                     scene.AddComponent<BattleIceAgentComponent>();
                     scene.AddComponent<ObjectWait>();
+                    // 抖音AccessToken组件
+                    scene.AddComponent<GameDouyinComponent>();
                     break;
                 case SceneType.Location:
                     scene.AddComponent<LocationComponent>();

+ 5 - 15
DotNet/Hotfix/Scenes/Game/GameDouyinComponentSystem.cs

@@ -9,12 +9,12 @@ namespace ET.Server
     [FriendOf(typeof (GameDouyinComponent))]
     public static class GameDouyinComponentSystem
     {
-        public class GameDouyinComponentAwakeSystem: AwakeSystem<GameDouyinComponent, bool>
+        public class GameDouyinComponentAwakeSystem: AwakeSystem<GameDouyinComponent>
         {
-            protected override void Awake(GameDouyinComponent self, bool tokenIsNull)
+            protected override void Awake(GameDouyinComponent self)
             {
                 Log.Info($"创建抖音组件...");
-                self.TokenIsNull = tokenIsNull;
+                self.InitAccessToken();
             }
         }
 
@@ -29,19 +29,11 @@ namespace ET.Server
         {
             protected override void Update(GameDouyinComponent self)
             {
-                if (self.TokenIsNull)
-                {
-                    return;
-                }
-
                 if (TimeHelper.ClientNow() < self.AccessTokenTime)
                 {
                     return;
                 }
-
-                Log.Info($"AccessToken刷新...");
                 self.InitAccessToken();
-                Log.Info($"AccessToken刷新完成, AccessToken:{self.AccessToken}, AccessTokenTime:{self.AccessTokenTime}");
             }
         }
 
@@ -80,6 +72,8 @@ namespace ET.Server
             self.AccessToken = Convert.ToString(jObject.SelectToken("data").SelectToken("access_token"));
             long time = Convert.ToInt64(jObject.SelectToken("data").SelectToken("expires_in"));
             self.AccessTokenTime = TimeHelper.ClientNow() + time * 1000;
+
+            Log.Info($"AccessToken刷新完成, AccessToken:{self.AccessToken}, AccessTokenTime:{self.AccessTokenTime}");
         }
 
         /// <summary>
@@ -130,10 +124,6 @@ namespace ET.Server
         /// <param name="roomId"></param>
         public static void TopGifts(this GameDouyinComponent self, long roomId)
         {
-            if (self.TokenIsNull)
-            {
-                return;
-            }
             if (string.IsNullOrEmpty(self.AccessToken.Trim()))
             {
                 Log.Error($"TopGifts...AccessToken为null");

+ 3 - 15
DotNet/Hotfix/Scenes/Game/Handler/C2G_LoginGameHandler.cs

@@ -29,8 +29,6 @@ namespace ET.Server
             PlayerInfo playerInfo = new PlayerInfo();
             playerInfo.Id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone());
 
-            GameDouyinComponent douyinComponent = Root.Instance.Scene.GetComponent<GameDouyinComponent>();
-
             if (tokenIsNull)
             {
                 // Token为空初始化信息
@@ -38,22 +36,11 @@ namespace ET.Server
                 playerInfo.AnchorOpenId = playerInfo.Id.ToString();
                 playerInfo.AvatarUrl = "";
                 playerInfo.Name = "主播-" + playerInfo.Id;
-
-                if (douyinComponent == null)
-                {
-                    Root.Instance.Scene.AddComponent<GameDouyinComponent, bool>(true);
-                }
+                playerInfo.TokenIsNull = true;
             }
             else
             {
-                if (douyinComponent == null)
-                {
-                    douyinComponent = Root.Instance.Scene.AddComponent<GameDouyinComponent, bool>(false);
-
-                    douyinComponent.InitAccessToken();
-                }
-
-                JObject roomInfo = douyinComponent.GetRoomInfo(request.Token.Trim());
+                JObject roomInfo = scene.GetComponent<GameDouyinComponent>().GetRoomInfo(request.Token.Trim());
 
                 if (roomInfo == null)
                 {
@@ -68,6 +55,7 @@ namespace ET.Server
                 playerInfo.AnchorOpenId = Convert.ToString(info.SelectToken("anchor_open_id"));
                 playerInfo.AvatarUrl = Convert.ToString(info.SelectToken("avatar_url"));
                 playerInfo.Name = Convert.ToString(info.SelectToken("nick_name"));
+                playerInfo.TokenIsNull = false;
             }
 
             playerInfo.Sex = 0;

+ 3 - 9
DotNet/Hotfix/Scenes/Game/Map/MapDouyinLiveCommentComponentSystem.cs

@@ -20,14 +20,6 @@ namespace ET.Server
             protected override void Destroy(MapDouyinLiveCommentComponent self)
             {
                 Log.Info($"销毁抖音直播评论任务组件");
-
-                bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
-
-                if (tokenIsNull)
-                {
-                    return;
-                }
-
                 self.StopTask();
             }
         }
@@ -36,7 +28,9 @@ namespace ET.Server
         {
             protected override void Update(MapDouyinLiveCommentComponent self)
             {
-                bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
+                WNPlayer player = self.GetParent<Map>().Player;
+
+                bool tokenIsNull = player.GetComponent<PlayerDataComponent>().Data.TokenIsNull;
 
                 if (tokenIsNull)
                 {

+ 3 - 9
DotNet/Hotfix/Scenes/Game/Map/MapDouyinLiveGiftComponentSystem.cs

@@ -20,14 +20,6 @@ namespace ET.Server
             protected override void Destroy(MapDouyinLiveGiftComponent self)
             {
                 Log.Info($"销毁抖音直播礼物任务组件");
-
-                bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
-
-                if (tokenIsNull)
-                {
-                    return;
-                }
-
                 self.StopTask();
             }
         }
@@ -36,7 +28,9 @@ namespace ET.Server
         {
             protected override void Update(MapDouyinLiveGiftComponent self)
             {
-                bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
+                WNPlayer player = self.GetParent<Map>().Player;
+
+                bool tokenIsNull = player.GetComponent<PlayerDataComponent>().Data.TokenIsNull;
 
                 if (tokenIsNull)
                 {

+ 3 - 9
DotNet/Hotfix/Scenes/Game/Map/MapDouyinLiveLikeComponentSystem.cs

@@ -20,14 +20,6 @@ namespace ET.Server
             protected override void Destroy(MapDouyinLiveLikeComponent self)
             {
                 Log.Info($"销毁抖音直播点赞任务组件");
-
-                bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
-
-                if (tokenIsNull)
-                {
-                    return;
-                }
-
                 self.StopTask();
             }
         }
@@ -36,7 +28,9 @@ namespace ET.Server
         {
             protected override void Update(MapDouyinLiveLikeComponent self)
             {
-                bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
+                WNPlayer player = self.GetParent<Map>().Player;
+
+                bool tokenIsNull = player.GetComponent<PlayerDataComponent>().Data.TokenIsNull;
 
                 if (tokenIsNull)
                 {

+ 3 - 13
DotNet/Hotfix/Scenes/Game/Map/MapSystem.cs

@@ -33,12 +33,12 @@ namespace ET.Server
                 // 场景排行榜组件
                 self.AddComponent<MapRankComponent>();
                 // token为空过滤一下抖音相关组件
-                if (self.TokenIsNull())
+                if (player.GetComponent<PlayerDataComponent>().Data.TokenIsNull)
                 {
                     return;
                 }
                 // 抖音直播礼物置顶
-                Root.Instance.Scene.GetComponent<GameDouyinComponent>().TopGifts(self.RoomId);
+                self.DomainScene().GetComponent<GameDouyinComponent>().TopGifts(self.RoomId);
                 // 抖音直播评论任务组件
                 self.AddComponent<MapDouyinLiveCommentComponent>();
                 // 抖音直播礼物任务组件
@@ -468,17 +468,7 @@ namespace ET.Server
         /// <returns></returns>
         public static string GetDouyinAccessToken(this Map self)
         {
-            return Root.Instance.Scene.GetComponent<GameDouyinComponent>().AccessToken;
-        }
-
-        /// <summary>
-        /// Token 是否为空
-        /// </summary>
-        /// <param name="self"></param>
-        /// <returns></returns>
-        public static bool TokenIsNull(this Map self)
-        {
-            return Root.Instance.Scene.GetComponent<GameDouyinComponent>().TokenIsNull;
+            return self.DomainScene().GetComponent<GameDouyinComponent>().AccessToken;
         }
 
         /// <summary>

+ 1 - 3
DotNet/Model/Scenes/Game/GameDouyinComponent.cs

@@ -1,13 +1,11 @@
 namespace ET.Server
 {
     [ComponentOf(typeof (Scene))]
-    public class GameDouyinComponent: Entity, IAwake<bool>, IDestroy, IUpdate
+    public class GameDouyinComponent: Entity, IAwake, IDestroy, IUpdate
     {
         /** 抖音接口全局唯一调用凭据 **/
         public string AccessToken { get; set; }
         /** 有效时间 (时间戳) **/
         public long AccessTokenTime { get; set; }
-        /** Token标记 **/
-        public bool TokenIsNull { get; set; }
     }
 }