UserPlayer.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 (HomeMgr.ins) HomeMgr.ins.ShowAuthLoginMask(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. UserComp.Instance.getUserInfo(delegate(UserInfo userInfo) {
  79. LoginMgr.myUserInfo = userInfo;
  80. if (HomeView.ins) {
  81. HomeView.ins.RenderMyAvatarSprite();
  82. HomeView.ins.RenderNameOrGender();
  83. HomeView.ins.RenderDeviceNames();
  84. }
  85. if (HomeMgr.ins) {
  86. HomeMgr.ins.ShowAuthLoginMask(false);
  87. }
  88. if (SceneManager.GetActiveScene().name.Equals("Home") && !LoginMgr.myUserInfo.IsGuideFinish(0)) {
  89. NewUserGuiderManager.ins?.ReviewNewUserGuide();
  90. }
  91. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  92. if (userInfo.phone == "10086") {
  93. var reporter = GameObject.Find("Reporter")?.GetComponent<Reporter>();
  94. if (reporter && !reporter.enabled) {
  95. reporter.enabled = true;
  96. Debug.LogWarning("调试控制台启动-在屏幕画圆激活");
  97. }
  98. }
  99. }
  100. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  101. System.Action eOnAgree = () => {
  102. GPSTool.GetAddress((address) => {
  103. if (address != null) {
  104. Debug.Log("登陆时获取地理位置成功:" + string.Join(" ", address));
  105. if (LoginMgr.myUserInfo.country == address[0]
  106. && LoginMgr.myUserInfo.state == address[1]
  107. && LoginMgr.myUserInfo.city == address[2]
  108. ) return;
  109. LoginMgr.myUserInfo.country = address[0];
  110. LoginMgr.myUserInfo.state = address[1];
  111. LoginMgr.myUserInfo.city = address[2];
  112. LoginMgr.myUserInfo.Save();
  113. }
  114. });
  115. };
  116. if (HomeView.ins && !HomeView.ins.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
  117. }
  118. });
  119. } else {
  120. handleAuthExpire();
  121. }
  122. }
  123. private static void handleAuthExpire()
  124. {
  125. //tip
  126. if (HomeMgr.ins && HomeMgr.ins.IsAuthLoginMaskActive()) {
  127. HomeMgr.ins.SetAuthLoginText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  128. } else {
  129. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  130. }
  131. //delay back login
  132. Sequence seq = DOTween.Sequence();
  133. seq.AppendInterval(1.5f);
  134. seq.AppendCallback(() => {
  135. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  136. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  137. } else {
  138. Application.Quit(); //不是Home场景,就关闭游戏
  139. }
  140. });
  141. //close userplayer
  142. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  143. ins?.Close();
  144. }
  145. public void onRequestAddFriend() {
  146. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_friend-receive-request"));
  147. tempData.hasFriendRequest = true;
  148. }
  149. public void onHasFriendTip() {
  150. tempData.hasFriendRequest = true;
  151. }
  152. public TempData tempData = new TempData();
  153. public class TempData {
  154. public System.Action onUpdate;
  155. private bool _hasFriendRequest;
  156. public bool hasFriendRequest {
  157. get {
  158. return _hasFriendRequest;
  159. }
  160. set {
  161. _hasFriendRequest = value;
  162. onUpdate?.Invoke();
  163. }
  164. }
  165. }
  166. }