UserPlayer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using JCEngineCore;
  5. using UnityEngine.SceneManagement;
  6. public class UserPlayer : JCEntity
  7. {
  8. public static UserPlayer ins;
  9. public UserPlayer() {
  10. ins = this;
  11. }
  12. public static void ConnectServer() {
  13. if (ins != null) return;
  14. if (HomeMgr.ins) HomeMgr.ins.ShowAuthLoginMask(true);
  15. JCEngine.boot(CommonConfig.businessServerWsURI, new UserPlayer());
  16. }
  17. bool canReconnnect = true;
  18. public override void onLoad() {
  19. Debug.Log("UserPlayer onLoad()");
  20. authToken();
  21. }
  22. public override void onReload() {
  23. Debug.Log("UserPlayer onReload()");
  24. authToken();
  25. }
  26. public override void onDestroy() {
  27. Debug.Log("UserPlayer onDestroy()");
  28. if (canReconnnect) {
  29. JCEngine.reboot(this);
  30. }
  31. }
  32. public override void onMiss() {
  33. Debug.Log("UserPlayer onMiss()");
  34. if (canReconnnect) {
  35. JCEngine.reboot(this);
  36. }
  37. }
  38. public void Close() {
  39. if (ins == this) ins = null;
  40. this.canReconnnect = false;
  41. this.channel.close();
  42. Debug.Log("user player close");
  43. }
  44. //向服务端发送的请求
  45. public void authToken() {
  46. string p0 = PlayerPrefs.GetString("IdAndToken", "");
  47. #if UNITY_EDITOR
  48. if (string.IsNullOrEmpty(p0)) {
  49. p0 = "12&f29f72fcba4b4a63aa4f965b5d603a4e";
  50. }
  51. #endif
  52. call("authToken", p0);
  53. }
  54. //被服务端调用的函数
  55. public void onAuthRes(bool res) {
  56. Debug.Log("onAuthRes," + res);
  57. if (res) {
  58. UserComp.ins.getUserInfo(delegate(UserInfo userInfo) {
  59. LoginMgr.myUserInfo = userInfo;
  60. if (HomeView.ins) {
  61. HomeView.ins.RenderMyAvatarSprite();
  62. HomeView.ins.RenderNameOrGender();
  63. HomeView.ins.RenderDeviceNames();
  64. }
  65. if (HomeMgr.ins) {
  66. HomeMgr.ins.ShowAuthLoginMask(false);
  67. }
  68. });
  69. } else {
  70. PlayerPrefs.DeleteKey("IdAndToken");
  71. SceneManager.LoadScene("Login", LoadSceneMode.Single);
  72. Close();
  73. }
  74. }
  75. public void onInvitePK(int avatarID, string nickname, int roomID, int roomType) {
  76. string tip = "";
  77. if (roomType == 0) {
  78. tip = "好友邀请你参加静止靶PK";
  79. }
  80. System.Action cb = delegate() {
  81. if (SceneManager.GetActiveScene().name == "Home") {
  82. PKMatchingView view = PKMatchingView.Create();
  83. view.isFriendPKInvitee = true;
  84. view.targetJoinRoomID = roomID;
  85. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  86. GlobalData.matchRoomType = roomType;
  87. }
  88. };
  89. PopupMgr.ins.ShowPKInviteNotice(avatarID, nickname, tip, cb);
  90. }
  91. }