UserPlayer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. bool canReconnnect = true;
  19. public override void onLoad() {
  20. Debug.Log("UserPlayer onLoad()");
  21. authToken();
  22. }
  23. public override void onReload() {
  24. Debug.Log("UserPlayer onReload()");
  25. authToken();
  26. }
  27. public override void onDestroy() {
  28. Debug.Log("UserPlayer onDestroy()");
  29. if (canReconnnect) {
  30. JCEngine.reboot(this);
  31. }
  32. }
  33. public override void onMiss() {
  34. Debug.Log("UserPlayer onMiss()");
  35. if (canReconnnect) {
  36. JCEngine.reboot(this);
  37. }
  38. }
  39. public void Close() {
  40. if (ins == this) ins = null;
  41. this.canReconnnect = false;
  42. this.channel.close();
  43. Debug.Log("user player close");
  44. }
  45. //向服务端发送的请求
  46. public void authToken() {
  47. string p0 = PlayerPrefs.GetString("IdAndToken", "");
  48. #if UNITY_EDITOR
  49. if (string.IsNullOrEmpty(p0)) {
  50. p0 = "12&f29f72fcba4b4a63aa4f965b5d603a4e";
  51. }
  52. #endif
  53. call("authToken", p0);
  54. }
  55. //被服务端调用的函数
  56. public void onAuthRes(bool res) {
  57. Debug.Log("onAuthRes," + res);
  58. if (res) {
  59. UserComp.ins.getUserInfo(delegate(UserInfo userInfo) {
  60. LoginMgr.myUserInfo = userInfo;
  61. if (HomeView.ins) {
  62. HomeView.ins.RenderMyAvatarSprite();
  63. HomeView.ins.RenderNameOrGender();
  64. HomeView.ins.RenderDeviceNames();
  65. }
  66. if (HomeMgr.ins) {
  67. HomeMgr.ins.ShowAuthLoginMask(false);
  68. }
  69. });
  70. } else {
  71. //clear token info
  72. PlayerPrefs.DeleteKey("IdAndToken");
  73. //tip
  74. PopupMgr.ins.ShowTip("登录Token失效!");
  75. //delay back login or quit
  76. Sequence seq = DOTween.Sequence();
  77. seq.AppendInterval(1.5f);
  78. seq.AppendCallback(() => {
  79. if (SceneManager.GetActiveScene().name == "Home") {
  80. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  81. } else {
  82. Application.Quit();
  83. }
  84. });
  85. //close socket
  86. Close();
  87. }
  88. }
  89. public void onInvitePK(int avatarID, string nickname, int roomID, int roomType) {
  90. string[] pkTypeNames = {"静止靶", "野兔闯关", "野鸡闯关", "野狼闯关"};
  91. string pkTypeName = pkTypeNames[roomType];
  92. string tip = $"好友邀请你参加{pkTypeName}PK";
  93. System.Action cb = delegate() {
  94. if (!PKMatchingView.ins && SceneManager.GetActiveScene().name == "Home") {
  95. PKMatchingView view = PKMatchingView.Create();
  96. view.isFriendPKInvitee = true;
  97. view.targetJoinRoomID = roomID;
  98. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  99. GlobalData.matchRoomType = roomType;
  100. } else if (PKMatchingView.ins) {
  101. PopupMgr.ins.ShowTip("当前正处于匹配状态,无法接受好友PK邀请!");
  102. }
  103. };
  104. if (!PKMatchingView.ins && SceneManager.GetActiveScene().name == "Home") {
  105. PopupMgr.ins.ShowPKInviteNotice(avatarID, nickname, tip, cb);
  106. }
  107. }
  108. }