PlatformTapManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. ViewManager.Show<ModalStatusView>("登录中...");
  37. TDSUser tdsUser = await TDSUser.GetCurrent();
  38. if (null != tdsUser)
  39. {
  40. InitAntiAddiction(tdsUser.ObjectId);
  41. return true;
  42. }
  43. ViewManager.Hide<ModalStatusView>();
  44. return false;
  45. }
  46. public async ETTask<bool> Login()
  47. {
  48. Log.Debug($"tap login");
  49. if (!PlatformManager.IsTaptap)
  50. {
  51. return false;
  52. }
  53. ViewManager.Show<ModalStatusView>("登录中...");
  54. try
  55. {
  56. var tdsUser = await TDSUser.LoginWithTapTap();
  57. Log.Debug($"tap login Success:{tdsUser}");
  58. InitAntiAddiction(tdsUser.ObjectId);
  59. return true;
  60. }
  61. catch (Exception e)
  62. {
  63. if (e is TapException tapError)
  64. {
  65. if (tapError.code == (int)TapErrorCode.ERROR_CODE_BIND_CANCEL) // 取消登录
  66. {
  67. Log.Debug("登录取消");
  68. }
  69. else
  70. {
  71. Log.Error($"Login Error:{tapError.code} message:{tapError.message}");
  72. PromptController.Instance.ShowFloatTextPrompt(tapError.message);
  73. }
  74. }
  75. else
  76. {
  77. Log.Error($"Login Error:{e}");
  78. PromptController.Instance.ShowFloatTextPrompt("登录异常");
  79. }
  80. }
  81. ViewManager.Hide<ModalStatusView>();
  82. return false;
  83. }
  84. public void InitAntiAddiction(string objectId)
  85. {
  86. Log.Debug($"tap InitAntiAddiction objectId {objectId}");
  87. string gameIdentifier = ClientID;
  88. // 是否启用时长限制功能
  89. bool useTimeLimit = true;
  90. // 是否启用消费限制功能
  91. bool usePaymentLimit = false;
  92. // 是否显示切换账号按钮
  93. bool showSwitchAccount = true;
  94. AntiAddictionUIKit.Init(gameIdentifier, useTimeLimit, usePaymentLimit, showSwitchAccount,
  95. (antiAddictionCallbackData) =>
  96. {
  97. int code = antiAddictionCallbackData.code;
  98. MsgExtraParams extras = antiAddictionCallbackData.extras;
  99. Log.Debug($"tap antiAddictionCallbackData code {code} extras title \n{extras?.title} description \n{extras?.description}");
  100. // 根据 code 不同提示玩家不同信息,详见下面的说明
  101. if (code == 500)
  102. {
  103. ET.Log.Debug("玩家登录后判断当前玩家可以进行游戏");
  104. // 开始计时
  105. AntiAddictionUIKit.EnterGame();
  106. LoginController.LoginTest(objectId).Coroutine();
  107. }
  108. else if(code == 1030)
  109. {
  110. ET.Log.Debug("未成年玩家当前无法进行游戏");
  111. }
  112. else if (code == 1095)
  113. {
  114. ET.Log.Debug("未成年允许游戏弹窗");
  115. }
  116. else if (code == 1000)
  117. {
  118. ET.Log.Debug("退出账号");
  119. }
  120. else if (code == 9002)
  121. {
  122. ET.Log.Debug("实名过程中点击了关闭实名窗");
  123. AntiAddictionUIKit.Startup(true, objectId);
  124. }
  125. else if (code == 1001)
  126. {
  127. ET.Log.Debug("点击切换账号按钮(v1.0.2 新增)");
  128. GameController.QuitToLoginView(true);
  129. }
  130. },
  131. (exception) =>
  132. {
  133. // 处理异常
  134. Log.Debug($"tap exception {exception.ToString()}");
  135. }
  136. );
  137. AntiAddictionUIKit.Startup(true, objectId);
  138. }
  139. public void OnEnterGame()
  140. {
  141. Log.Debug($"tap OnEnterGame");
  142. if (!PlatformManager.IsTaptap)
  143. {
  144. return;
  145. }
  146. var serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  147. var accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  148. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  149. var currentRole = roleInfosComponent.GetCurrentRole();
  150. TapDB.SetUser(accountInfoComponent.Account);
  151. TapDB.SetName(currentRole.Name);
  152. TapDB.SetLevel(RoleDataManager.lvl);
  153. TapDB.SetServer(serverInfosComponent.GetCurrentServerInfo().ServerName);
  154. Log.Debug($"tap TapDB \nSetUser {accountInfoComponent.Account} \nSetName {currentRole.Name} \nSetLevel {RoleDataManager.lvl} \nSetServer {serverInfosComponent.GetCurrentServerInfo().ServerName}");
  155. }
  156. public void OnQuitGame()
  157. {
  158. Log.Debug($"tap OnQuitGame");
  159. if (!PlatformManager.IsTaptap)
  160. {
  161. return;
  162. }
  163. TapDB.ClearUser();
  164. AntiAddictionUIKit.LeaveGame();
  165. AntiAddictionUIKit.Logout();
  166. }
  167. }
  168. }