UserPlayer.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. GPSTool.GetAddress((address) => {
  101. if (address != null) {
  102. Debug.Log("登陆时获取地理位置成功:" + string.Join(" ", address));
  103. if (LoginMgr.myUserInfo.country == address[0]
  104. && LoginMgr.myUserInfo.state == address[1]
  105. && LoginMgr.myUserInfo.city == address[2]
  106. ) return;
  107. LoginMgr.myUserInfo.country = address[0];
  108. LoginMgr.myUserInfo.state = address[1];
  109. LoginMgr.myUserInfo.city = address[2];
  110. LoginMgr.myUserInfo.Save();
  111. }
  112. });
  113. });
  114. } else {
  115. handleAuthExpire();
  116. }
  117. }
  118. private static void handleAuthExpire()
  119. {
  120. //tip
  121. if (HomeMgr.ins && HomeMgr.ins.IsAuthLoginMaskActive()) {
  122. HomeMgr.ins.SetAuthLoginText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  123. } else {
  124. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  125. }
  126. //delay back login
  127. Sequence seq = DOTween.Sequence();
  128. seq.AppendInterval(1.5f);
  129. seq.AppendCallback(() => {
  130. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  131. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  132. } else {
  133. Application.Quit(); //不是Home场景,就关闭游戏
  134. }
  135. });
  136. //close userplayer
  137. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  138. ins?.Close();
  139. }
  140. public void onRequestAddFriend() {
  141. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_friend-receive-request"));
  142. tempData.hasFriendRequest = true;
  143. }
  144. public void onHasFriendTip() {
  145. tempData.hasFriendRequest = true;
  146. }
  147. public TempData tempData = new TempData();
  148. public class TempData {
  149. public System.Action onUpdate;
  150. private bool _hasFriendRequest;
  151. public bool hasFriendRequest {
  152. get {
  153. return _hasFriendRequest;
  154. }
  155. set {
  156. _hasFriendRequest = value;
  157. onUpdate?.Invoke();
  158. }
  159. }
  160. }
  161. }