UserPlayer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. GetUserInfo();
  17. }
  18. public static void ConnectServer() {
  19. if (ins != null) return;
  20. if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(true);
  21. if (CommonConfig.businessServerWsURL != null) {
  22. JCEngine.boot(CommonConfig.businessServerWsURL, new UserPlayer());
  23. } else {
  24. LoginByToken();
  25. }
  26. }
  27. private static int _RetryLoginByTokenCount = -1;
  28. public static void LoginByToken()
  29. {
  30. if (++_RetryLoginByTokenCount > 0)
  31. AuthLoginMask.ins?.SetRetryCount(_RetryLoginByTokenCount);
  32. CoroutineStarter.Start(LoginController.Instance.LoginByToken((res) => {
  33. if (res.code == 0) {
  34. _RetryLoginByTokenCount = -1;
  35. string loginToken = (string)res.data;
  36. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  37. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  38. JCEngine.boot(CommonConfig.businessServerWsURL, new UserPlayer());
  39. } else if (res.code == -9999) {
  40. if (_RetryLoginByTokenCount < 3) {
  41. LoginByToken();
  42. } else {
  43. _RetryLoginByTokenCount = -1;
  44. AuthLoginMask.ins?.SetAutoRetryFail();
  45. }
  46. } else {
  47. _RetryLoginByTokenCount = -1;
  48. handleAuthExpire();
  49. }
  50. }));
  51. }
  52. //之所以做成协程延迟触发,是因为用编辑器调试时,停止运行后会触发断线重连,就会造成游戏停止调试了,但socket还连接的现象。
  53. IEnumerator ReconnenctServer() {
  54. yield return new WaitForSecondsRealtime(0.1f);
  55. JCEngine.reboot(this);
  56. }
  57. bool canReconnnect = true;
  58. public override void onLoad() {
  59. Debug.Log("UserPlayer onLoad()");
  60. authToken();
  61. }
  62. public override void onReload() {
  63. Debug.Log("UserPlayer onReload()");
  64. authToken();
  65. }
  66. public override void onDestroy() {
  67. loginAuthed = false;
  68. Debug.Log("UserPlayer onDestroy()");
  69. if (canReconnnect) {
  70. JCUnityLib.CoroutineStarter.Start(ReconnenctServer());
  71. }
  72. }
  73. public override void onMiss() {
  74. Debug.Log("UserPlayer onMiss()");
  75. if (canReconnnect) {
  76. JCUnityLib.CoroutineStarter.Start(ReconnenctServer());
  77. }
  78. }
  79. public void Close() {
  80. LoginMgr.myUserInfo.id = 0;
  81. if (ins == this) ins = null;
  82. this.canReconnnect = false;
  83. this.channel.close();
  84. Debug.Log("user player close");
  85. }
  86. //向服务端发送的请求
  87. public void authToken() {
  88. string p0 = PlayerPrefs.GetString(LoginMgr.LoginTokenKey, "");
  89. string p1 = SceneManager.GetActiveScene().name;
  90. call("authToken2", p0, p1);
  91. }
  92. //被服务端调用的函数
  93. public bool loginAuthed;
  94. public void onAuthRes(bool res) {
  95. Debug.Log("onAuthRes," + res);
  96. loginAuthed = res;
  97. if (!loginAuthed) handleAuthExpire();
  98. }
  99. //获取用户信息,失败了就重试
  100. public void GetUserInfo()
  101. {
  102. if (CommonConfig.StandaloneMode)
  103. {
  104. UserInfo userInfo = UserInfo.LoadLocal(0);
  105. if (userInfo == null) userInfo = new();
  106. DoAfterGetUserInfo(userInfo);
  107. return;
  108. }
  109. UserPlayer userPlayer = this;
  110. CoroutineStarter.Start(LoginController.Instance.GetUserInfo((res) =>
  111. {
  112. if (ins != userPlayer) return;
  113. if (res.code == 0)
  114. {
  115. UserInfo userInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(res.data as string);
  116. DoAfterGetUserInfo(userInfo);
  117. }
  118. else AuthLoginMask.ins?.SetAutoRetryFail();
  119. }));
  120. }
  121. IEnumerator showTip()
  122. {
  123. yield return new WaitForSeconds(0.3f);
  124. GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip);
  125. settingsViewObj.GetComponent<HomeView_Tip>().setHomeTip(0);
  126. }
  127. public bool hasGetUserInfo;
  128. private void DoAfterGetUserInfo(UserInfo userInfo)
  129. {
  130. if (hasGetUserInfo) return;
  131. hasGetUserInfo = true;
  132. LoginMgr.myUserInfo = userInfo;
  133. if (HomeView.ins) {
  134. HomeView.ins.RenderMyAvatarSprite();
  135. HomeView.ins.RenderNameOrGender();
  136. HomeView.ins.RenderDeviceNames();
  137. }
  138. if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(false);
  139. //if (SceneManager.GetActiveScene().name.Equals("Home") && !LoginMgr.myUserInfo.IsGuideFinish(0)) {
  140. // //NewUserGuiderManager.ins?.ReviewNewUserGuide();
  141. // //安装后首次进入APP和首次登录账号进入APP后
  142. // //GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip);
  143. // //settingsViewObj.GetComponent<HomeView_Tip>().setHomeTip(0);
  144. // CoroutineStarter.Start(showTip());
  145. // LoginMgr.myUserInfo.SaveGuideFinish(0);
  146. //}
  147. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  148. if (LoginMgr.myUserInfo.nickname == "test10086") InitLogReporter();
  149. }
  150. if (SceneManager.GetActiveScene().name.Equals("Home") && !CommonConfig.StandaloneMode) {
  151. System.Action eOnAgree = () => {
  152. GPSTool.GetAddress((address) => {
  153. if (address != null) {
  154. if (LoginMgr.myUserInfo.country == address[0]
  155. && LoginMgr.myUserInfo.state == address[1]
  156. && LoginMgr.myUserInfo.city == address[2]
  157. ) return;
  158. LoginMgr.myUserInfo.country = address[0];
  159. LoginMgr.myUserInfo.state = address[1];
  160. LoginMgr.myUserInfo.city = address[2];
  161. LoginMgr.myUserInfo.Save();
  162. }
  163. });
  164. if (!LoginMgr.myUserInfo.IsGuideFinish(0))
  165. {
  166. CoroutineStarter.Start(showTip());
  167. LoginMgr.myUserInfo.SaveGuideFinish(0);
  168. }
  169. };
  170. System.Action eOnReject = () => {
  171. if (!LoginMgr.myUserInfo.IsGuideFinish(0))
  172. {
  173. CoroutineStarter.Start(showTip());
  174. LoginMgr.myUserInfo.SaveGuideFinish(0);
  175. }
  176. };
  177. if (!HomeView.ShowProminentBeforeConnectBLE_Event(eOnAgree,eOnReject)) {
  178. eOnAgree.Invoke();
  179. }
  180. //else
  181. //{
  182. // if (!LoginMgr.myUserInfo.IsGuideFinish(0))
  183. // {
  184. // CoroutineStarter.Start(showTip());
  185. // LoginMgr.myUserInfo.SaveGuideFinish(0);
  186. // }
  187. //}
  188. }
  189. }
  190. //控制台
  191. private static bool s_LogReporterInited = false;
  192. public static void InitLogReporter()
  193. {
  194. if (s_LogReporterInited) return;
  195. s_LogReporterInited = true;
  196. Object.Instantiate(Resources.Load("Prefabs/LogReporter"));
  197. Debug.LogWarning("在屏幕上画个圈即可打开控制台");
  198. Debug.Log("app-version: " + Resources.Load<TextAsset>("app-version").text);
  199. }
  200. public static Object InitReturnLogReporter()
  201. {
  202. if (s_LogReporterInited) return null;
  203. s_LogReporterInited = true;
  204. Object o = Object.Instantiate(Resources.Load("Prefabs/LogReporter"));
  205. Debug.LogWarning("在屏幕上画个圈即可打开控制台");
  206. Debug.Log("app-version: " + Resources.Load<TextAsset>("app-version").text);
  207. return o;
  208. }
  209. private static void handleAuthExpire()
  210. {
  211. //tip
  212. if (AuthLoginMask.ins && AuthLoginMask.ins.IsVisiable()) {
  213. AuthLoginMask.ins.SetText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  214. } else {
  215. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  216. }
  217. //delay back login
  218. Sequence seq = DOTween.Sequence();
  219. seq.AppendInterval(1.5f);
  220. seq.AppendCallback(() => {
  221. if (SceneManager.GetActiveScene().name.Equals("Home")) {
  222. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  223. } else {
  224. Application.Quit(); //不是Home场景,就关闭游戏
  225. }
  226. });
  227. //close userplayer
  228. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  229. ins?.Close();
  230. }
  231. public void onAnyMessage(string msg)
  232. {
  233. SideTipView.ShowTip(msg, msg.EndsWith("失败") ? Color.yellow : Color.white);
  234. }
  235. public void onRequestAddFriend() {
  236. GameObject tipTopObject = PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_friend-receive-request"));
  237. tipTopObject.AddComponent<UnityEngine.UI.Button>().onClick.AddListener(() => {
  238. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != "Home") return;
  239. //ViewMgr.Instance.ShowView<FriendView>("setFirstBtnTabsIndex", 1);
  240. ViewManager2.ShowView(ViewManager2.Path_SocialView);
  241. GameObject.Destroy(tipTopObject);
  242. });
  243. tempData.hasFriendRequest = true;
  244. }
  245. public void onHasFriendTip() {
  246. tempData.hasFriendRequest = true;
  247. }
  248. public TempData tempData = new TempData();
  249. public class TempData {
  250. public System.Action onUpdate;
  251. private bool _hasFriendRequest;
  252. public bool hasFriendRequest {
  253. get {
  254. return _hasFriendRequest;
  255. }
  256. set {
  257. _hasFriendRequest = value;
  258. onUpdate?.Invoke();
  259. }
  260. }
  261. }
  262. /**
  263. * 获取一个起始和结束的当月时间
  264. */
  265. public string[] getDateArray() {
  266. System.DateTime currentTime = System.DateTime.Now;
  267. System.DateTime firstDayOfMonth = new System.DateTime(currentTime.Year, currentTime.Month, 1);
  268. System.DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
  269. //Debug.Log("getDateArray First day of current month: " + firstDayOfMonth.ToString("yyyy-MM-dd"));
  270. //Debug.Log("getDateArray Last day of current month: " + lastDayOfMonth.ToString("yyyy-MM-dd"));
  271. return new string[] { firstDayOfMonth.ToString("yyyy-MM-dd"), lastDayOfMonth.ToString("yyyy-MM-dd") };
  272. }
  273. }