using System.Collections.Generic; using UnityEngine; /* Socket组件-PK */ public class PKComp : JCUnityLib.Singleton { public void randomMatch() { MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo(); UserPlayer.ins.call("pkComp.randomMatch", playerInfo, GlobalData.matchGameType); } public void cancelRandomMatch() { UserPlayer.ins.call("pkComp.cancelRandomMatch", LoginMgr.myUserInfo.id); } public void inviteFriendGamePK(int targetID, string inviterViewUUID) { MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo(); UserPlayer.ins.call("pkComp.inviteFriendGamePK", playerInfo, GlobalData.matchGameType, targetID, inviterViewUUID); } public void acceptFriendGamePK(string roomKey) { MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo(); UserPlayer.ins.call("pkComp.acceptFriendGamePK", playerInfo, roomKey); } public void inviteOtherTryAgainGamePK(string inviterViewUUID) { //get other info int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2; MatchPlayerInfo ohterInfo = GlobalData.matchPlayerInfos[otherIndex]; MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo(); UserPlayer.ins.call("pkComp.inviteOtherTryAgainGamePK", playerInfo, GlobalData.matchGameType, ohterInfo.playerID, inviterViewUUID); } public void rejectPKInvite(string roomKey) { UserPlayer.ins.call("pkComp.rejectPKInvite", roomKey); } public void acceptOtherTryAgainGamePK(string roomKey) { MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo(); UserPlayer.ins.call("pkComp.acceptOtherTryAgainGamePK", playerInfo, roomKey); } private MatchPlayerInfo CreateMyMatchPlayerInfo() { UserInfo u = LoginMgr.myUserInfo; MatchPlayerInfo playerInfo = new MatchPlayerInfo(u.id, u.avatarID, u.avatarUrl, u.nickname); return playerInfo; } //被服务端调用的接口 public void onGamePKMatchSuccess(List playerInfoList, int playerIndexInRoom, string roomKey, string pkServerURL) { GlobalData.roomKey = roomKey; GlobalData.matchPlayerInfos = playerInfoList; GlobalData.playerIndexInRoom = playerIndexInRoom; CommonConfig.gamePKServerWsURL = pkServerURL; if (PKMatchingView.ins) { PKMatchingView.ins.EnterGameSceneOnMatchSuccess(); } } public void onInviteFriendGamePK(MatchPlayerInfo matchPlayerInfo, int gameType, string roomKey) { string pkTypeName = GetPKTypeName(gameType); if (pkTypeName == null) return; string tip = TextAutoLanguage2.GetTextByKey("tip_pk_friend-invite-join") + pkTypeName; System.Action cb = delegate() { if (CanShowFriendInviteOrMatch()) { PKMatchingView view = PKMatchingView.Create(); view.isFriendPKInvitee = true; view.targetJoinRoomKey = roomKey; GlobalData.pkMatchType = PKMatchType.OnlinePK; GlobalData.matchGameType = gameType; } else if (PKMatchingView.ins) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("当前正处于匹配状态,无法接受好友PK邀请!")); } }; System.Action cbCancel = delegate() { rejectPKInvite(roomKey); }; if (CanShowFriendInviteOrMatch()) { PopupMgr.ins.ShowPKInviteNotice(matchPlayerInfo.avatarID, matchPlayerInfo.avatarUrl, matchPlayerInfo.nickname, tip, cb, cbCancel, cbCancel); } } public void onInviteOtherTryAgainGamePK(MatchPlayerInfo matchPlayerInfo, int gameType, string roomKey) { string pkTypeName = GetPKTypeName(gameType); if (pkTypeName == null) return; string tip = TextAutoLanguage2.GetTextByKey("tip_pk_invite-try-again") + pkTypeName; System.Action cb = delegate() { if (CanShowTryAgainInviteOrMatch(matchPlayerInfo)) { PKMatchingView view = PKMatchingView.Create(); view.isTryAgainInvitee = true; view.targetJoinTryAgainRoomKey = roomKey; GlobalData.pkMatchType = PKMatchType.OnlinePK; GlobalData.matchGameType = gameType; } else { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("目前状态下,无法接受该邀请!")); } }; System.Action cbCancel = delegate() { rejectPKInvite(roomKey); }; if (CanShowTryAgainInviteOrMatch(matchPlayerInfo)) { PopupMgr.ins.ShowPKInviteNotice(matchPlayerInfo.avatarID, matchPlayerInfo.avatarUrl, matchPlayerInfo.nickname, tip, cb, cbCancel, cbCancel); } } public void onRejectPKInvite(string inviterViewUUID) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("对方拒绝了你的邀请")); PKMatchingView view = PKMatchingView.ins; if (!view) return; if (view.viewUUID != inviterViewUUID) return; view.eventOnRejectPKInvite?.Invoke(); } //内部方法 private string GetPKTypeName(int gameType) { string pkTypeName = null; switch (gameType) { case 9: pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_1"); break; case 10: pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_2"); break; case 11: pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_3"); break; case 12: pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_4"); break; } return pkTypeName; } private bool CanShowFriendInviteOrMatch() { return !PKMatchingView.ins && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home"; } private bool CanShowTryAgainInviteOrMatch(MatchPlayerInfo inviterInfo) { bool condition1 = !PKMatchingView.ins && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home"; bool condition2 = false; if (GlobalData.pkMatchType == PKMatchType.OnlinePK) { if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) { foreach (var item in GlobalData.matchPlayerInfos) { if (inviterInfo.playerID == item.playerID) { condition2 = true; break; } } } } return condition1 || condition2; } }