UserPlayer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using JCEngineCore;
  5. using UnityEngine.SceneManagement;
  6. using DG.Tweening;
  7. public class UserPlayer : JCEntity
  8. {
  9. public static UserPlayer ins;
  10. public UserPlayer() {
  11. ins = this;
  12. }
  13. public static void ConnectServer() {
  14. if (ins != null) return;
  15. if (HomeMgr.ins) HomeMgr.ins.ShowAuthLoginMask(true);
  16. JCEngine.boot(CommonConfig.businessServerWsURI, new UserPlayer());
  17. }
  18. //之所以做成协程延迟触发,是因为用编辑器调试时,停止运行后会触发断线重连,就会造成游戏停止调试了,但socket还连接的现象。
  19. IEnumerator ReconnenctServer() {
  20. yield return new WaitForSecondsRealtime(0.1f);
  21. JCEngine.reboot(this);
  22. }
  23. bool canReconnnect = true;
  24. public override void onLoad() {
  25. Debug.Log("UserPlayer onLoad()");
  26. authToken();
  27. }
  28. public override void onReload() {
  29. Debug.Log("UserPlayer onReload()");
  30. authToken();
  31. }
  32. public override void onDestroy() {
  33. Debug.Log("UserPlayer onDestroy()");
  34. if (canReconnnect) {
  35. JC.Unity.CoroutineStarter.Start(ReconnenctServer());
  36. }
  37. }
  38. public override void onMiss() {
  39. Debug.Log("UserPlayer onMiss()");
  40. if (canReconnnect) {
  41. JC.Unity.CoroutineStarter.Start(ReconnenctServer());
  42. }
  43. }
  44. public void Close() {
  45. if (ins == this) ins = null;
  46. this.canReconnnect = false;
  47. this.channel.close();
  48. Debug.Log("user player close");
  49. }
  50. //向服务端发送的请求
  51. public void authToken() {
  52. string p0 = PlayerPrefs.GetString("IdAndToken", "");
  53. #if UNITY_EDITOR
  54. if (string.IsNullOrEmpty(p0)) {
  55. p0 = "12&f29f72fcba4b4a63aa4f965b5d603a4e";
  56. }
  57. #endif
  58. call("authToken", p0);
  59. }
  60. //被服务端调用的函数
  61. public void onAuthRes(bool res) {
  62. Debug.Log("onAuthRes," + res);
  63. if (res) {
  64. UserComp.ins.getUserInfo(delegate(UserInfo userInfo) {
  65. LoginMgr.myUserInfo = userInfo;
  66. if (HomeView.ins) {
  67. HomeView.ins.RenderMyAvatarSprite();
  68. HomeView.ins.RenderNameOrGender();
  69. HomeView.ins.RenderDeviceNames();
  70. }
  71. if (HomeMgr.ins) {
  72. HomeMgr.ins.ShowAuthLoginMask(false);
  73. }
  74. });
  75. } else {
  76. //clear token info
  77. PlayerPrefs.DeleteKey("IdAndToken");
  78. //tip
  79. PopupMgr.ins.ShowTip("登录Token失效!");
  80. //delay back login or quit
  81. Sequence seq = DOTween.Sequence();
  82. seq.AppendInterval(1.5f);
  83. seq.AppendCallback(() => {
  84. if (SceneManager.GetActiveScene().name == "Home") {
  85. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  86. } else {
  87. Application.Quit();
  88. }
  89. });
  90. //close socket
  91. Close();
  92. }
  93. }
  94. public void onInvitePK(int avatarID, string nickname, int roomID, int roomType) {
  95. string[] pkTypeNames = {"静止靶", "野兔闯关", "野鸡闯关", "野狼闯关"};
  96. string pkTypeName = pkTypeNames[roomType];
  97. string tip = $"好友邀请你参加{pkTypeName}PK";
  98. System.Action cb = delegate() {
  99. if (!PKMatchingView.ins && SceneManager.GetActiveScene().name == "Home") {
  100. PKMatchingView view = PKMatchingView.Create();
  101. view.isFriendPKInvitee = true;
  102. view.targetJoinRoomID = roomID;
  103. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  104. GlobalData.matchRoomType = roomType;
  105. } else if (PKMatchingView.ins) {
  106. PopupMgr.ins.ShowTip("当前正处于匹配状态,无法接受好友PK邀请!");
  107. }
  108. };
  109. if (!PKMatchingView.ins && SceneManager.GetActiveScene().name == "Home") {
  110. PopupMgr.ins.ShowPKInviteNotice(avatarID, nickname, tip, cb);
  111. }
  112. }
  113. }