UserPlayer.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. //获取用户信息,超时了就重试
  84. private bool _hasExecuteRequestUserInfoAvoidFail = false;
  85. IEnumerator RequestUserInfoAvoidFail()
  86. {
  87. if (_hasExecuteRequestUserInfoAvoidFail) yield break;
  88. _hasExecuteRequestUserInfoAvoidFail = true;
  89. while (!_hasGetUserInfo && ins == this) {
  90. UserComp.Instance.getUserInfo(DoAfterGetUserInfo);
  91. yield return new WaitForSeconds(3.3f);
  92. }
  93. }
  94. private bool _hasGetUserInfo = false;
  95. private void DoAfterGetUserInfo(UserInfo userInfo)
  96. {
  97. if (_hasGetUserInfo) return;
  98. _hasGetUserInfo = true;
  99. LoginMgr.myUserInfo = userInfo;
  100. if (HomeView.ins) {
  101. HomeView.ins.RenderMyAvatarSprite();
  102. HomeView.ins.RenderNameOrGender();
  103. HomeView.ins.RenderDeviceNames();
  104. }
  105. if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(false);
  106. if (SceneManager.GetActiveScene().name.Equals("Home") && !LoginMgr.myUserInfo.IsGuideFinish(0)) {
  107. NewUserGuiderManager.ins?.ReviewNewUserGuide();
  108. }
  109. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  110. if (LoginMgr.myUserInfo.nickname == "test10086") InitLogReporter();
  111. }
  112. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  113. System.Action eOnAgree = () => {
  114. GPSTool.GetAddress((address) => {
  115. if (address != null) {
  116. Debug.Log("登陆时获取地理位置成功:" + string.Join(" ", address));
  117. if (LoginMgr.myUserInfo.country == address[0]
  118. && LoginMgr.myUserInfo.state == address[1]
  119. && LoginMgr.myUserInfo.city == address[2]
  120. ) return;
  121. LoginMgr.myUserInfo.country = address[0];
  122. LoginMgr.myUserInfo.state = address[1];
  123. LoginMgr.myUserInfo.city = address[2];
  124. LoginMgr.myUserInfo.Save();
  125. }
  126. });
  127. };
  128. if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
  129. }
  130. }
  131. //控制台
  132. private static bool s_LogReporterInited = false;
  133. public static void InitLogReporter()
  134. {
  135. if (s_LogReporterInited) return;
  136. s_LogReporterInited = true;
  137. Object.Instantiate(Resources.Load("Prefabs/LogReporter"));
  138. Debug.LogWarning("在屏幕上画个圈即可打开控制台");
  139. }
  140. private static void handleAuthExpire()
  141. {
  142. //tip
  143. if (AuthLoginMask.ins && AuthLoginMask.ins.IsVisiable()) {
  144. AuthLoginMask.ins.SetText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  145. } else {
  146. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  147. }
  148. //delay back login
  149. Sequence seq = DOTween.Sequence();
  150. seq.AppendInterval(1.5f);
  151. seq.AppendCallback(() => {
  152. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  153. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  154. } else {
  155. Application.Quit(); //不是Home场景,就关闭游戏
  156. }
  157. });
  158. //close userplayer
  159. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  160. ins?.Close();
  161. }
  162. public void onAnyMessage(string msg)
  163. {
  164. SideTipView.ShowTip(msg, msg.EndsWith("失败") ? Color.yellow : Color.white);
  165. }
  166. public void onRequestAddFriend() {
  167. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_friend-receive-request"));
  168. tempData.hasFriendRequest = true;
  169. }
  170. public void onHasFriendTip() {
  171. tempData.hasFriendRequest = true;
  172. }
  173. public TempData tempData = new TempData();
  174. public class TempData {
  175. public System.Action onUpdate;
  176. private bool _hasFriendRequest;
  177. public bool hasFriendRequest {
  178. get {
  179. return _hasFriendRequest;
  180. }
  181. set {
  182. _hasFriendRequest = value;
  183. onUpdate?.Invoke();
  184. }
  185. }
  186. }
  187. }