PKComp.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. /* Socket组件-PK */
  4. public class PKComp : JCUnityLib.Singleton<PKComp>
  5. {
  6. public void randomMatch() {
  7. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  8. UserPlayer.ins.call("pkComp.randomMatch", playerInfo, GlobalData.matchGameType);
  9. }
  10. public void cancelRandomMatch() {
  11. UserPlayer.ins.call("pkComp.cancelRandomMatch", LoginMgr.myUserInfo.id);
  12. }
  13. public void inviteFriendGamePK(int targetID, string inviterViewUUID) {
  14. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  15. UserPlayer.ins.call("pkComp.inviteFriendGamePK", playerInfo, GlobalData.matchGameType, targetID, inviterViewUUID);
  16. }
  17. public void acceptFriendGamePK(string roomKey) {
  18. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  19. UserPlayer.ins.call("pkComp.acceptFriendGamePK", playerInfo, roomKey);
  20. }
  21. public void inviteOtherTryAgainGamePK(string inviterViewUUID) {
  22. //get other info
  23. int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
  24. MatchPlayerInfo ohterInfo = GlobalData.matchPlayerInfos[otherIndex];
  25. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  26. UserPlayer.ins.call("pkComp.inviteOtherTryAgainGamePK", playerInfo, GlobalData.matchGameType, ohterInfo.playerID, inviterViewUUID);
  27. }
  28. public void rejectPKInvite(string roomKey) {
  29. UserPlayer.ins.call("pkComp.rejectPKInvite", roomKey);
  30. }
  31. public void acceptOtherTryAgainGamePK(string roomKey) {
  32. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  33. UserPlayer.ins.call("pkComp.acceptOtherTryAgainGamePK", playerInfo, roomKey);
  34. }
  35. private MatchPlayerInfo CreateMyMatchPlayerInfo() {
  36. UserInfo u = LoginMgr.myUserInfo;
  37. MatchPlayerInfo playerInfo = new MatchPlayerInfo(u.id, u.avatarID, u.avatarUrl, u.nickname);
  38. return playerInfo;
  39. }
  40. //被服务端调用的接口
  41. public void onGamePKMatchSuccess(List<MatchPlayerInfo> playerInfoList, int playerIndexInRoom, string roomKey, string pkServerURL) {
  42. GlobalData.roomKey = roomKey;
  43. GlobalData.matchPlayerInfos = playerInfoList;
  44. GlobalData.playerIndexInRoom = playerIndexInRoom;
  45. CommonConfig.gamePKServerWsURL = pkServerURL;
  46. if (PKMatchingView.ins) {
  47. PKMatchingView.ins.EnterGameSceneOnMatchSuccess();
  48. }
  49. }
  50. public void onInviteFriendGamePK(MatchPlayerInfo matchPlayerInfo, int gameType, string roomKey) {
  51. string pkTypeName = GetPKTypeName(gameType);
  52. if (pkTypeName == null) return;
  53. string tip = TextAutoLanguage2.GetTextByKey("tip_pk_friend-invite-join") + pkTypeName;
  54. System.Action cb = delegate() {
  55. if (CanShowFriendInviteOrMatch()) {
  56. PKMatchingView view = PKMatchingView.Create();
  57. view.isFriendPKInvitee = true;
  58. view.targetJoinRoomKey = roomKey;
  59. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  60. GlobalData.matchGameType = gameType;
  61. } else if (PKMatchingView.ins) {
  62. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("当前正处于匹配状态,无法接受好友PK邀请!"));
  63. }
  64. };
  65. System.Action cbCancel = delegate() {
  66. rejectPKInvite(roomKey);
  67. };
  68. if (CanShowFriendInviteOrMatch()) {
  69. PopupMgr.ins.ShowPKInviteNotice(matchPlayerInfo.avatarID, matchPlayerInfo.avatarUrl, matchPlayerInfo.nickname, tip, cb, cbCancel, cbCancel);
  70. }
  71. }
  72. public void onInviteOtherTryAgainGamePK(MatchPlayerInfo matchPlayerInfo, int gameType, string roomKey) {
  73. string pkTypeName = GetPKTypeName(gameType);
  74. if (pkTypeName == null) return;
  75. string tip = TextAutoLanguage2.GetTextByKey("tip_pk_invite-try-again") + pkTypeName;
  76. System.Action cb = delegate() {
  77. if (CanShowTryAgainInviteOrMatch(matchPlayerInfo)) {
  78. PKMatchingView view = PKMatchingView.Create();
  79. view.isTryAgainInvitee = true;
  80. view.targetJoinTryAgainRoomKey = roomKey;
  81. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  82. GlobalData.matchGameType = gameType;
  83. } else {
  84. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("目前状态下,无法接受该邀请!"));
  85. }
  86. };
  87. System.Action cbCancel = delegate() {
  88. rejectPKInvite(roomKey);
  89. };
  90. if (CanShowTryAgainInviteOrMatch(matchPlayerInfo)) {
  91. PopupMgr.ins.ShowPKInviteNotice(matchPlayerInfo.avatarID, matchPlayerInfo.avatarUrl, matchPlayerInfo.nickname, tip, cb, cbCancel, cbCancel);
  92. }
  93. }
  94. public void onRejectPKInvite(string inviterViewUUID) {
  95. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("对方拒绝了你的邀请"));
  96. PKMatchingView view = PKMatchingView.ins;
  97. if (!view) return;
  98. if (view.viewUUID != inviterViewUUID) return;
  99. view.eventOnRejectPKInvite?.Invoke();
  100. }
  101. //内部方法
  102. private string GetPKTypeName(int gameType) {
  103. string pkTypeName = null;
  104. switch (gameType) {
  105. case 9:
  106. pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_1");
  107. break;
  108. case 10:
  109. pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_2");
  110. break;
  111. case 11:
  112. pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_3");
  113. break;
  114. case 12:
  115. pkTypeName = TextAutoLanguage2.GetTextByKey("tip_pk_gametype_4");
  116. break;
  117. }
  118. return pkTypeName;
  119. }
  120. private bool CanShowFriendInviteOrMatch() {
  121. return !PKMatchingView.ins && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home";
  122. }
  123. private bool CanShowTryAgainInviteOrMatch(MatchPlayerInfo inviterInfo) {
  124. bool condition1 = !PKMatchingView.ins && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home";
  125. bool condition2 = false;
  126. if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
  127. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) {
  128. foreach (var item in GlobalData.matchPlayerInfos) {
  129. if (inviterInfo.playerID == item.playerID) {
  130. condition2 = true;
  131. break;
  132. }
  133. }
  134. }
  135. }
  136. return condition1 || condition2;
  137. }
  138. }