PlatformTapManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using ET;
  2. using TapTap.Bootstrap;
  3. using TapTap.Common;
  4. using TapTap.TapDB;
  5. using Plugins.AntiAddictionUIKit;
  6. using System;
  7. using UnityEngine;
  8. namespace GFGGame
  9. {
  10. public class PlatformTapManager : SingletonBase<PlatformTapManager>
  11. {
  12. private readonly string ClientID = "K7bDyPGYlVx2AAtk6q";
  13. public void InitSDK()
  14. {
  15. Log.Debug($"tap InitSDK");
  16. if (!PlatformManager.IsTaptap)
  17. {
  18. return;
  19. }
  20. var config = new TapConfig.Builder()
  21. .ClientID(ClientID)
  22. .ClientToken("fjwWBtibB4Dj3UjyRQxK2tZ3f8fGNgg14tcRW38D")
  23. .ServerURL("https://k7bdypgy.cloud.tds1.tapapis.cn")
  24. .RegionType(RegionType.CN)
  25. .TapDBConfig(true, "tap", null, true)
  26. .ConfigBuilder();
  27. TapBootstrap.Init(config);
  28. }
  29. public async ETTask<bool> LoginCache()
  30. {
  31. Log.Debug($"tap LoginCache");
  32. if (!PlatformManager.IsTaptap)
  33. {
  34. return false;
  35. }
  36. TDSUser tdsUser = await TDSUser.GetCurrent();
  37. if (null != tdsUser)
  38. {
  39. InitAntiAddiction(tdsUser.ObjectId);
  40. return true;
  41. }
  42. return false;
  43. }
  44. public async ETTask<bool> Login()
  45. {
  46. Log.Debug($"tap login");
  47. if (!PlatformManager.IsTaptap)
  48. {
  49. return false;
  50. }
  51. try
  52. {
  53. var tdsUser = await TDSUser.LoginWithTapTap();
  54. Log.Debug($"tap login Success:{tdsUser}");
  55. InitAntiAddiction(tdsUser.ObjectId);
  56. return true;
  57. }
  58. catch (Exception e)
  59. {
  60. if (e is TapException tapError)
  61. {
  62. if (tapError.code == (int)TapErrorCode.ERROR_CODE_BIND_CANCEL) // 取消登录
  63. {
  64. Log.Debug("登录取消");
  65. }
  66. else
  67. {
  68. Log.Error($"Login Error:{tapError.code} message:{tapError.message}");
  69. PromptController.Instance.ShowFloatTextPrompt(tapError.message);
  70. }
  71. }
  72. else
  73. {
  74. Log.Error($"Login Error:{e}");
  75. PromptController.Instance.ShowFloatTextPrompt("登录异常");
  76. }
  77. }
  78. return false;
  79. }
  80. public void InitAntiAddiction(string objectId)
  81. {
  82. Log.Debug($"tap InitAntiAddiction objectId {objectId}");
  83. string gameIdentifier = ClientID;
  84. // 是否启用时长限制功能
  85. bool useTimeLimit = true;
  86. // 是否启用消费限制功能
  87. bool usePaymentLimit = false;
  88. // 是否显示切换账号按钮
  89. bool showSwitchAccount = true;
  90. AntiAddictionUIKit.Init(gameIdentifier, useTimeLimit, usePaymentLimit, showSwitchAccount,
  91. (antiAddictionCallbackData) =>
  92. {
  93. int code = antiAddictionCallbackData.code;
  94. MsgExtraParams extras = antiAddictionCallbackData.extras;
  95. Log.Debug($"tap antiAddictionCallbackData code {code} extras title \n{extras?.title} description \n{extras?.description}");
  96. // 根据 code 不同提示玩家不同信息,详见下面的说明
  97. if (code == 500)
  98. {
  99. ET.Log.Debug("玩家登录后判断当前玩家可以进行游戏");
  100. // 开始计时
  101. AntiAddictionUIKit.EnterGame();
  102. LoginController.LoginTest(objectId).Coroutine();
  103. }
  104. else if(code == 1030)
  105. {
  106. ET.Log.Debug("未成年玩家当前无法进行游戏");
  107. }
  108. else if (code == 1095)
  109. {
  110. ET.Log.Debug("未成年允许游戏弹窗");
  111. }
  112. else if (code == 1000)
  113. {
  114. ET.Log.Debug("退出账号");
  115. }
  116. else if (code == 9002)
  117. {
  118. ET.Log.Debug("实名过程中点击了关闭实名窗");
  119. AntiAddictionUIKit.Startup(true, objectId);
  120. }
  121. else if (code == 1001)
  122. {
  123. ET.Log.Debug("点击切换账号按钮(v1.0.2 新增)");
  124. GameController.QuitToLoginView(true);
  125. }
  126. },
  127. (exception) =>
  128. {
  129. // 处理异常
  130. Log.Debug($"tap exception {exception.ToString()}");
  131. }
  132. );
  133. AntiAddictionUIKit.Startup(true, objectId);
  134. }
  135. public void OnEnterGame()
  136. {
  137. Log.Debug($"tap OnEnterGame");
  138. if (!PlatformManager.IsTaptap)
  139. {
  140. return;
  141. }
  142. var serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  143. var accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  144. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  145. var currentRole = roleInfosComponent.GetCurrentRole();
  146. TapDB.SetUser(accountInfoComponent.Account);
  147. TapDB.SetName(currentRole.Name);
  148. TapDB.SetLevel(RoleDataManager.lvl);
  149. TapDB.SetServer(serverInfosComponent.GetCurrentServerInfo().ServerName);
  150. Log.Debug($"tap TapDB \nSetUser {accountInfoComponent.Account} \nSetName {currentRole.Name} \nSetLevel {RoleDataManager.lvl} \nSetServer {serverInfosComponent.GetCurrentServerInfo().ServerName}");
  151. }
  152. public void OnQuitGame()
  153. {
  154. Log.Debug($"tap OnQuitGame");
  155. if (!PlatformManager.IsTaptap)
  156. {
  157. return;
  158. }
  159. TapDB.ClearUser();
  160. AntiAddictionUIKit.LeaveGame();
  161. AntiAddictionUIKit.Logout();
  162. }
  163. }
  164. }