Advertisement
Guest User

Untitled

a guest
Nov 28th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using PlayFab;
  5. using PlayFab.MultiplayerModels;
  6. using TMPro;
  7.  
  8. public class Matchmaking : MonoBehaviour
  9. {
  10. [SerializeField] private GameObject playButton;
  11. [SerializeField] private GameObject leaveQueueButton;
  12. [SerializeField] private TMP_Text queueStatusText;
  13. [SerializeField] private GameObject landingPage;
  14.  
  15. private const string queueName = "DebugQueue";
  16. private string matchTicketId;
  17.  
  18. private Coroutine pollTicketCoroutine;
  19.  
  20. public void StartMatchmaking() {
  21. this.playButton.SetActive(false);
  22. this.queueStatusText.text = "Submitting ticker";
  23. this.queueStatusText.gameObject.SetActive(true);
  24. NetworkManagerLobby.Instance.connType = "remoteClient";
  25.  
  26. PlayFabMultiplayerAPI.CreateMatchmakingTicket(
  27. new CreateMatchmakingTicketRequest {
  28. Creator = new MatchmakingPlayer {
  29. Entity = new EntityKey
  30. {
  31. Id = UserAuthorization.EntityId,
  32. Type = "title_player_account"
  33. },
  34. Attributes = new MatchmakingPlayerAttributes
  35. {
  36. DataObject = new
  37. {
  38. Latencies = new object[] {
  39. new {
  40. region = "NorthEurope",
  41. latency = 400
  42. }
  43. }
  44. }
  45. },
  46. },
  47. GiveUpAfterSeconds = 120,
  48. QueueName = queueName
  49. },
  50. OnMatchTicketCreated,
  51. OnMatchmakingError
  52. );
  53. }
  54.  
  55. private void OnMatchTicketCreated(CreateMatchmakingTicketResult result) {
  56. matchTicketId = result.TicketId;
  57. pollTicketCoroutine = StartCoroutine(PollTicket(result.TicketId));
  58. this.leaveQueueButton.SetActive(true);
  59. this.queueStatusText.text = "Created ticket";
  60. }
  61.  
  62. private IEnumerator PollTicket(string ticketId) {
  63. while(true) {
  64. PlayFabMultiplayerAPI.GetMatchmakingTicket(
  65. new GetMatchmakingTicketRequest
  66. {
  67. TicketId = ticketId,
  68. QueueName = queueName
  69. },
  70. OnGetMatchTicket,
  71. OnMatchmakingError
  72. );
  73. yield return new WaitForSeconds(6f);
  74. }
  75. }
  76.  
  77. private void OnGetMatchTicket(GetMatchmakingTicketResult result) {
  78. if(result.Status != "Canceled") {
  79. this.queueStatusText.text = $"Status: {result.Status}";
  80. }
  81.  
  82.  
  83. switch(result.Status) {
  84. case "Matched":
  85. StopCoroutine(pollTicketCoroutine);
  86. StartMatch(result.MatchId);
  87. break;
  88. case "Canceled":
  89. break;
  90. }
  91. }
  92.  
  93. private void StartMatch(string matchId) {
  94. this.queueStatusText.text = $"Starting match";
  95. PlayFabMultiplayerAPI.GetMatch(
  96. new GetMatchRequest
  97. {
  98. MatchId = matchId,
  99. QueueName = queueName
  100. },
  101. OnGetMatch,
  102. OnMatchmakingError
  103. );
  104. }
  105.  
  106. private void OnGetMatch(GetMatchResult result) {
  107. NetworkManagerLobby.Instance.networkAddress = result.ServerDetails.IPV4Address;
  108. NetworkManagerLobby.Instance.GetComponent<kcp2k.KcpTransport>().Port = (ushort) result.ServerDetails.Ports[0].Num;
  109. this.landingPage.SetActive(false);
  110. NetworkManagerLobby.Instance.StartClient();
  111. }
  112.  
  113. public void CancelMatchmaking() {
  114. PlayFabMultiplayerAPI.CancelMatchmakingTicket(
  115. new CancelMatchmakingTicketRequest
  116. {
  117. QueueName = queueName,
  118. TicketId = matchTicketId
  119. },
  120. OnCancelMatchTicket,
  121. OnMatchmakingError
  122. );
  123. }
  124.  
  125. private void OnCancelMatchTicket(CancelMatchmakingTicketResult result) {
  126. this.playButton.SetActive(true);
  127. this.leaveQueueButton.SetActive(false);
  128. this.queueStatusText.text = "";
  129. }
  130.  
  131. private void OnMatchmakingError(PlayFabError error) {
  132. this.playButton.SetActive(true);
  133. this.leaveQueueButton.SetActive(false);
  134. this.queueStatusText.text = "";
  135. Debug.Log(error.GenerateErrorReport());
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement