| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using JCEngineCore;
- using UnityEngine.SceneManagement;
- using DG.Tweening;
- using JCUnityLib;
- /* Socket组件-玩家业务 */
- public class UserPlayer : JCEntity
- {
- public static UserPlayer ins;
- public UserPlayer() {
- ins = this;
- components.Add("PKComp", PKComp.Instance);
- components.Add("UserComp", UserComp.Instance);
- GetUserInfo();
- }
- public static void ConnectServer() {
- if (ins != null) return;
- if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(true);
- if (CommonConfig.businessServerWsURL != null) {
- JCEngine.boot(CommonConfig.businessServerWsURL, new UserPlayer());
- } else {
- LoginByToken();
- }
- }
- private static int _RetryLoginByTokenCount = -1;
- public static void LoginByToken()
- {
- if (++_RetryLoginByTokenCount > 0)
- AuthLoginMask.ins?.SetRetryCount(_RetryLoginByTokenCount);
- CoroutineStarter.Start(LoginController.Instance.LoginByToken((res) => {
- if (res.code == 0) {
- _RetryLoginByTokenCount = -1;
- string loginToken = (string)res.data;
- CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
- PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
- JCEngine.boot(CommonConfig.businessServerWsURL, new UserPlayer());
- } else if (res.code == -9999) {
- if (_RetryLoginByTokenCount < 3) {
- LoginByToken();
- } else {
- _RetryLoginByTokenCount = -1;
- AuthLoginMask.ins?.SetAutoRetryFail();
- }
- } else {
- _RetryLoginByTokenCount = -1;
- handleAuthExpire();
- }
- }));
- }
- //之所以做成协程延迟触发,是因为用编辑器调试时,停止运行后会触发断线重连,就会造成游戏停止调试了,但socket还连接的现象。
- IEnumerator ReconnenctServer() {
- yield return new WaitForSecondsRealtime(0.1f);
- JCEngine.reboot(this);
- }
- bool canReconnnect = true;
- public override void onLoad() {
- Debug.Log("UserPlayer onLoad()");
- authToken();
- }
- public override void onReload() {
- Debug.Log("UserPlayer onReload()");
- authToken();
- }
- public override void onDestroy() {
- loginAuthed = false;
- Debug.Log("UserPlayer onDestroy()");
- if (canReconnnect) {
- JCUnityLib.CoroutineStarter.Start(ReconnenctServer());
- }
- }
- public override void onMiss() {
- Debug.Log("UserPlayer onMiss()");
- if (canReconnnect) {
- JCUnityLib.CoroutineStarter.Start(ReconnenctServer());
- }
- }
- public void Close() {
- LoginMgr.myUserInfo.id = 0;
- if (ins == this) ins = null;
- this.canReconnnect = false;
- this.channel.close();
- Debug.Log("user player close");
- }
- //向服务端发送的请求
- public void authToken() {
- string p0 = PlayerPrefs.GetString(LoginMgr.LoginTokenKey, "");
- string p1 = SceneManager.GetActiveScene().name;
- call("authToken2", p0, p1);
- }
-
- //被服务端调用的函数
- public bool loginAuthed;
- public void onAuthRes(bool res) {
- Debug.Log("onAuthRes," + res);
- loginAuthed = res;
- if (!loginAuthed) handleAuthExpire();
- }
- //获取用户信息,失败了就重试
- public void GetUserInfo()
- {
- if (CommonConfig.StandaloneMode)
- {
- UserInfo userInfo = UserInfo.LoadLocal(0);
- if (userInfo == null) userInfo = new();
- DoAfterGetUserInfo(userInfo);
- return;
- }
- UserPlayer userPlayer = this;
- CoroutineStarter.Start(LoginController.Instance.GetUserInfo((res) =>
- {
- if (ins != userPlayer) return;
- if (res.code == 0)
- {
- UserInfo userInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(res.data as string);
- DoAfterGetUserInfo(userInfo);
- }
- else AuthLoginMask.ins?.SetAutoRetryFail();
- }));
- }
- IEnumerator showTip()
- {
- yield return new WaitForSeconds(0.3f);
- GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip);
- settingsViewObj.GetComponent<HomeView_Tip>().setHomeTip(0);
- }
- public bool hasGetUserInfo;
- private void DoAfterGetUserInfo(UserInfo userInfo)
- {
- if (hasGetUserInfo) return;
- hasGetUserInfo = true;
- LoginMgr.myUserInfo = userInfo;
- if (HomeView.ins) {
- HomeView.ins.RenderMyAvatarSprite();
- HomeView.ins.RenderNameOrGender();
- HomeView.ins.RenderDeviceNames();
- }
- if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(false);
- //if (SceneManager.GetActiveScene().name.Equals("Home") && !LoginMgr.myUserInfo.IsGuideFinish(0)) {
- // //NewUserGuiderManager.ins?.ReviewNewUserGuide();
- // //安装后首次进入APP和首次登录账号进入APP后
- // //GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip);
- // //settingsViewObj.GetComponent<HomeView_Tip>().setHomeTip(0);
- // CoroutineStarter.Start(showTip());
- // LoginMgr.myUserInfo.SaveGuideFinish(0);
- //}
- if (SceneManager.GetActiveScene().name.Equals("Home")) {
- if (LoginMgr.myUserInfo.nickname == "test10086") InitLogReporter();
- }
- if (SceneManager.GetActiveScene().name.Equals("Home") && !CommonConfig.StandaloneMode) {
- System.Action eOnAgree = () => {
- GPSTool.GetAddress((address) => {
- if (address != null) {
- if (LoginMgr.myUserInfo.country == address[0]
- && LoginMgr.myUserInfo.state == address[1]
- && LoginMgr.myUserInfo.city == address[2]
- ) return;
- LoginMgr.myUserInfo.country = address[0];
- LoginMgr.myUserInfo.state = address[1];
- LoginMgr.myUserInfo.city = address[2];
- LoginMgr.myUserInfo.Save();
- }
- });
- if (!LoginMgr.myUserInfo.IsGuideFinish(0))
- {
- CoroutineStarter.Start(showTip());
- LoginMgr.myUserInfo.SaveGuideFinish(0);
- }
- };
- System.Action eOnReject = () => {
- if (!LoginMgr.myUserInfo.IsGuideFinish(0))
- {
- CoroutineStarter.Start(showTip());
- LoginMgr.myUserInfo.SaveGuideFinish(0);
- }
- };
- if (!HomeView.ShowProminentBeforeConnectBLE_Event(eOnAgree,eOnReject)) {
- eOnAgree.Invoke();
- }
- //else
- //{
- // if (!LoginMgr.myUserInfo.IsGuideFinish(0))
- // {
- // CoroutineStarter.Start(showTip());
- // LoginMgr.myUserInfo.SaveGuideFinish(0);
- // }
- //}
- }
- }
- //控制台
- private static bool s_LogReporterInited = false;
- public static void InitLogReporter()
- {
- if (s_LogReporterInited) return;
- s_LogReporterInited = true;
- Object.Instantiate(Resources.Load("Prefabs/LogReporter"));
- Debug.LogWarning("在屏幕上画个圈即可打开控制台");
- Debug.Log("app-version: " + Resources.Load<TextAsset>("app-version").text);
- }
- public static Object InitReturnLogReporter()
- {
- if (s_LogReporterInited) return null;
- s_LogReporterInited = true;
- Object o = Object.Instantiate(Resources.Load("Prefabs/LogReporter"));
- Debug.LogWarning("在屏幕上画个圈即可打开控制台");
- Debug.Log("app-version: " + Resources.Load<TextAsset>("app-version").text);
- return o;
- }
- private static void handleAuthExpire()
- {
- //tip
- if (AuthLoginMask.ins && AuthLoginMask.ins.IsVisiable()) {
- AuthLoginMask.ins.SetText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
- } else {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
- }
- //delay back login
- Sequence seq = DOTween.Sequence();
- seq.AppendInterval(1.5f);
- seq.AppendCallback(() => {
- if (SceneManager.GetActiveScene().name.Equals("Home")) {
- SceneManager.LoadScene("Login", LoadSceneMode.Single);
- } else {
- Application.Quit(); //不是Home场景,就关闭游戏
- }
- });
- //close userplayer
- PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
- ins?.Close();
- }
- public void onAnyMessage(string msg)
- {
- SideTipView.ShowTip(msg, msg.EndsWith("失败") ? Color.yellow : Color.white);
- }
- public void onRequestAddFriend() {
- GameObject tipTopObject = PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_friend-receive-request"));
- tipTopObject.AddComponent<UnityEngine.UI.Button>().onClick.AddListener(() => {
- if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != "Home") return;
- //ViewMgr.Instance.ShowView<FriendView>("setFirstBtnTabsIndex", 1);
- ViewManager2.ShowView(ViewManager2.Path_SocialView);
- GameObject.Destroy(tipTopObject);
- });
- tempData.hasFriendRequest = true;
- }
- public void onHasFriendTip() {
- tempData.hasFriendRequest = true;
- }
- public TempData tempData = new TempData();
- public class TempData {
- public System.Action onUpdate;
- private bool _hasFriendRequest;
- public bool hasFriendRequest {
- get {
- return _hasFriendRequest;
- }
- set {
- _hasFriendRequest = value;
- onUpdate?.Invoke();
- }
- }
- }
- /**
- * 获取一个起始和结束的当月时间
- */
- public string[] getDateArray() {
- System.DateTime currentTime = System.DateTime.Now;
- System.DateTime firstDayOfMonth = new System.DateTime(currentTime.Year, currentTime.Month, 1);
- System.DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
- //Debug.Log("getDateArray First day of current month: " + firstDayOfMonth.ToString("yyyy-MM-dd"));
- //Debug.Log("getDateArray Last day of current month: " + lastDayOfMonth.ToString("yyyy-MM-dd"));
- return new string[] { firstDayOfMonth.ToString("yyyy-MM-dd"), lastDayOfMonth.ToString("yyyy-MM-dd") };
- }
- }
|