| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using JCEngineCore;
- using UnityEngine.SceneManagement;
- using DG.Tweening;
- public class UserPlayer : JCEntity
- {
- public static UserPlayer ins;
- public UserPlayer() {
- ins = this;
- }
- public static void ConnectServer() {
- if (ins != null) return;
- if (HomeMgr.ins) HomeMgr.ins.ShowAuthLoginMask(true);
- JCEngine.boot(CommonConfig.businessServerWsURI, new UserPlayer());
- }
- 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() {
- Debug.Log("UserPlayer onDestroy()");
- if (canReconnnect) {
- JCEngine.reboot(this);
- }
- }
- public override void onMiss() {
- Debug.Log("UserPlayer onMiss()");
- if (canReconnnect) {
- JCEngine.reboot(this);
- }
- }
- public void Close() {
- if (ins == this) ins = null;
- this.canReconnnect = false;
- this.channel.close();
- Debug.Log("user player close");
- }
- //向服务端发送的请求
- public void authToken() {
- string p0 = PlayerPrefs.GetString("IdAndToken", "");
- #if UNITY_EDITOR
- if (string.IsNullOrEmpty(p0)) {
- p0 = "12&f29f72fcba4b4a63aa4f965b5d603a4e";
- }
- #endif
- call("authToken", p0);
- }
- //被服务端调用的函数
- public void onAuthRes(bool res) {
- Debug.Log("onAuthRes," + res);
- if (res) {
- UserComp.ins.getUserInfo(delegate(UserInfo userInfo) {
- LoginMgr.myUserInfo = userInfo;
- if (HomeView.ins) {
- HomeView.ins.RenderMyAvatarSprite();
- HomeView.ins.RenderNameOrGender();
- HomeView.ins.RenderDeviceNames();
- }
- if (HomeMgr.ins) {
- HomeMgr.ins.ShowAuthLoginMask(false);
- }
- });
- } else {
- //clear token info
- PlayerPrefs.DeleteKey("IdAndToken");
- //tip
- PopupMgr.ins.ShowTip("登录Token失效!");
- //delay back login or quit
- Sequence seq = DOTween.Sequence();
- seq.AppendInterval(1.5f);
- seq.AppendCallback(() => {
- if (SceneManager.GetActiveScene().name == "Home") {
- SceneManager.LoadScene("Login", LoadSceneMode.Single);
- } else {
- Application.Quit();
- }
- });
- //close socket
- Close();
- }
- }
- public void onInvitePK(int avatarID, string nickname, int roomID, int roomType) {
- string[] pkTypeNames = {"静止靶", "野兔闯关", "野鸡闯关", "野狼闯关"};
- string pkTypeName = pkTypeNames[roomType];
- string tip = $"好友邀请你参加{pkTypeName}PK";
- System.Action cb = delegate() {
- if (SceneManager.GetActiveScene().name == "Home") {
- PKMatchingView view = PKMatchingView.Create();
- view.isFriendPKInvitee = true;
- view.targetJoinRoomID = roomID;
- GlobalData.pkMatchType = PKMatchType.OnlinePK;
- GlobalData.matchRoomType = roomType;
- }
- };
- PopupMgr.ins.ShowPKInviteNotice(avatarID, nickname, tip, cb);
- }
- }
|