UserPlayer.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using JCEngineCore;
  5. using UnityEngine.SceneManagement;
  6. using DG.Tweening;
  7. using JCUnityLib;
  8. /* Socket组件-玩家业务 */
  9. public class UserPlayer : JCEntity
  10. {
  11. public static UserPlayer ins;
  12. public UserPlayer() {
  13. ins = this;
  14. components.Add("PKComp", PKComp.Instance);
  15. components.Add("UserComp", UserComp.Instance);
  16. }
  17. public static void ConnectServer() {
  18. if (ins != null) return;
  19. if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(true);
  20. if (CommonConfig.businessServerWsURL != null) {
  21. JCEngine.boot(CommonConfig.businessServerWsURL, new UserPlayer());
  22. } else {
  23. CoroutineStarter.Start(LoginController.Instance.LoginByToken((res) => {
  24. if (res.code == 0) {
  25. string loginToken = (string)res.data;
  26. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  27. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  28. JCEngine.boot(CommonConfig.businessServerWsURL, new UserPlayer());
  29. } else {
  30. handleAuthExpire();
  31. }
  32. }));
  33. }
  34. }
  35. //之所以做成协程延迟触发,是因为用编辑器调试时,停止运行后会触发断线重连,就会造成游戏停止调试了,但socket还连接的现象。
  36. IEnumerator ReconnenctServer() {
  37. yield return new WaitForSecondsRealtime(0.1f);
  38. JCEngine.reboot(this);
  39. }
  40. bool canReconnnect = true;
  41. public override void onLoad() {
  42. Debug.Log("UserPlayer onLoad()");
  43. authToken();
  44. }
  45. public override void onReload() {
  46. Debug.Log("UserPlayer onReload()");
  47. authToken();
  48. }
  49. public override void onDestroy() {
  50. Debug.Log("UserPlayer onDestroy()");
  51. if (canReconnnect) {
  52. JCUnityLib.CoroutineStarter.Start(ReconnenctServer());
  53. }
  54. }
  55. public override void onMiss() {
  56. Debug.Log("UserPlayer onMiss()");
  57. if (canReconnnect) {
  58. JCUnityLib.CoroutineStarter.Start(ReconnenctServer());
  59. }
  60. }
  61. public void Close() {
  62. LoginMgr.myUserInfo.id = 0;
  63. if (ins == this) ins = null;
  64. this.canReconnnect = false;
  65. this.channel.close();
  66. Debug.Log("user player close");
  67. }
  68. //向服务端发送的请求
  69. public void authToken() {
  70. string p0 = PlayerPrefs.GetString(LoginMgr.LoginTokenKey, "");
  71. string p1 = SceneManager.GetActiveScene().name;
  72. call("authToken2", p0, p1);
  73. }
  74. //被服务端调用的函数
  75. public void onAuthRes(bool res) {
  76. Debug.Log("onAuthRes," + res);
  77. if (res) {
  78. JCUnityLib.CoroutineStarter.Start(RequestUserInfoAvoidFail());
  79. } else {
  80. handleAuthExpire();
  81. }
  82. }
  83. public void RetryRequestUserInfoAvoidFail()
  84. {
  85. _hasExecuteRequestUserInfoAvoidFail = false;
  86. _getUserInfoRetryCount = -1;
  87. JCUnityLib.CoroutineStarter.Start(RequestUserInfoAvoidFail());
  88. }
  89. //获取用户信息,超时了就重试
  90. private bool _hasExecuteRequestUserInfoAvoidFail = false;
  91. private int _getUserInfoRetryCount = -1;
  92. IEnumerator RequestUserInfoAvoidFail()
  93. {
  94. if (_hasExecuteRequestUserInfoAvoidFail) yield break;
  95. _hasExecuteRequestUserInfoAvoidFail = true;
  96. while (!_hasGetUserInfo && ins == this) {
  97. if (++_getUserInfoRetryCount <= 3) {
  98. UserComp.Instance.getUserInfo(DoAfterGetUserInfo);
  99. if (_getUserInfoRetryCount > 0)
  100. AuthLoginMask.ins?.SetRetryCount(_getUserInfoRetryCount);
  101. yield return new WaitForSeconds(3.3f);
  102. } else {
  103. AuthLoginMask.ins?.SetAutoRetryFail();
  104. yield break;
  105. }
  106. }
  107. Debug.Log("out");
  108. }
  109. private bool _hasGetUserInfo = false;
  110. private void DoAfterGetUserInfo(UserInfo userInfo)
  111. {
  112. if (_hasGetUserInfo) return;
  113. _hasGetUserInfo = true;
  114. LoginMgr.myUserInfo = userInfo;
  115. if (HomeView.ins) {
  116. HomeView.ins.RenderMyAvatarSprite();
  117. HomeView.ins.RenderNameOrGender();
  118. HomeView.ins.RenderDeviceNames();
  119. }
  120. if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(false);
  121. if (SceneManager.GetActiveScene().name.Equals("Home") && !LoginMgr.myUserInfo.IsGuideFinish(0)) {
  122. NewUserGuiderManager.ins?.ReviewNewUserGuide();
  123. }
  124. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  125. if (LoginMgr.myUserInfo.nickname == "test10086") InitLogReporter();
  126. }
  127. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  128. System.Action eOnAgree = () => {
  129. GPSTool.GetAddress((address) => {
  130. if (address != null) {
  131. Debug.Log("登陆时获取地理位置成功:" + string.Join(" ", address));
  132. if (LoginMgr.myUserInfo.country == address[0]
  133. && LoginMgr.myUserInfo.state == address[1]
  134. && LoginMgr.myUserInfo.city == address[2]
  135. ) return;
  136. LoginMgr.myUserInfo.country = address[0];
  137. LoginMgr.myUserInfo.state = address[1];
  138. LoginMgr.myUserInfo.city = address[2];
  139. LoginMgr.myUserInfo.Save();
  140. }
  141. });
  142. };
  143. if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
  144. }
  145. }
  146. //控制台
  147. private static bool s_LogReporterInited = false;
  148. public static void InitLogReporter()
  149. {
  150. if (s_LogReporterInited) return;
  151. s_LogReporterInited = true;
  152. Object.Instantiate(Resources.Load("Prefabs/LogReporter"));
  153. Debug.LogWarning("在屏幕上画个圈即可打开控制台");
  154. }
  155. private static void handleAuthExpire()
  156. {
  157. //tip
  158. if (AuthLoginMask.ins && AuthLoginMask.ins.IsVisiable()) {
  159. AuthLoginMask.ins.SetText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  160. } else {
  161. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  162. }
  163. //delay back login
  164. Sequence seq = DOTween.Sequence();
  165. seq.AppendInterval(1.5f);
  166. seq.AppendCallback(() => {
  167. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  168. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  169. } else {
  170. Application.Quit(); //不是Home场景,就关闭游戏
  171. }
  172. });
  173. //close userplayer
  174. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  175. ins?.Close();
  176. }
  177. public void onAnyMessage(string msg)
  178. {
  179. SideTipView.ShowTip(msg, msg.EndsWith("失败") ? Color.yellow : Color.white);
  180. }
  181. public void onRequestAddFriend() {
  182. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_friend-receive-request"));
  183. tempData.hasFriendRequest = true;
  184. }
  185. public void onHasFriendTip() {
  186. tempData.hasFriendRequest = true;
  187. }
  188. public TempData tempData = new TempData();
  189. public class TempData {
  190. public System.Action onUpdate;
  191. private bool _hasFriendRequest;
  192. public bool hasFriendRequest {
  193. get {
  194. return _hasFriendRequest;
  195. }
  196. set {
  197. _hasFriendRequest = value;
  198. onUpdate?.Invoke();
  199. }
  200. }
  201. }
  202. }