Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.8 KiB

  1. namespace Oculus.Platform.Samples.VrHoops
  2. {
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using Oculus.Platform;
  6. using Oculus.Platform.Models;
  7. // This class coordinates with the Oculus Platform Matchmaking Service to
  8. // establish a Quickmatch session with one or two other players.
  9. public class MatchmakingManager
  10. {
  11. // the name we setup on the Developer Dashboard for the quickmatch pool
  12. private const string NORMAL_POOL = "NORMAL_QUICKMATCH";
  13. // the ID of the Room the matchmaking service sent to join
  14. private ulong m_matchRoom;
  15. // the list of players that join the match room.
  16. // it may not be all the match players since some might disconnect
  17. // before joining the room, but then again they might disconnect
  18. // midway through a match as well.
  19. private readonly Dictionary<ulong, User> m_remotePlayers;
  20. public MatchmakingManager()
  21. {
  22. m_remotePlayers = new Dictionary<ulong, User>();
  23. Matchmaking.SetMatchFoundNotificationCallback(MatchFoundCallback);
  24. Rooms.SetUpdateNotificationCallback(MatchmakingRoomUpdateCallback);
  25. }
  26. public delegate void OnEnqueueResult(bool successful);
  27. public delegate Player OnMatchPlayerAdded(int slot, User user);
  28. private OnEnqueueResult m_enqueueCallback;
  29. private OnMatchPlayerAdded m_playerCallback;
  30. public OnEnqueueResult EnqueueResultCallback
  31. {
  32. private get { return m_enqueueCallback; }
  33. set { m_enqueueCallback = value; }
  34. }
  35. public OnMatchPlayerAdded MatchPlayerAddedCallback
  36. {
  37. private get { return m_playerCallback; }
  38. set { m_playerCallback = value; }
  39. }
  40. public void QueueForMatch()
  41. {
  42. Matchmaking.Enqueue (NORMAL_POOL).OnComplete(MatchmakingEnqueueCallback);
  43. }
  44. void MatchmakingEnqueueCallback(Message msg)
  45. {
  46. if (msg.IsError)
  47. {
  48. Debug.Log(msg.GetError().Message);
  49. EnqueueResultCallback(false);
  50. return;
  51. }
  52. }
  53. void MatchFoundCallback(Message<Room> msg)
  54. {
  55. m_matchRoom = msg.Data.ID;
  56. Matchmaking.JoinRoom(msg.Data.ID, true).OnComplete(MatchmakingJoinRoomCallback);
  57. }
  58. void MatchmakingJoinRoomCallback(Message<Room> msg)
  59. {
  60. if (msg.IsError)
  61. {
  62. Debug.Log (msg.GetError().Message);
  63. EnqueueResultCallback(false);
  64. return;
  65. }
  66. Debug.Log ("Match found and room joined " + m_matchRoom);
  67. EnqueueResultCallback(true);
  68. // this sample doesn't try to coordinate that all the players see consistent
  69. // positioning to assigned courts, but that would be a great next feature to add
  70. int slot = 0;
  71. if (msg.Data.UsersOptional != null)
  72. {
  73. foreach (var user in msg.Data.UsersOptional)
  74. {
  75. var player = MatchPlayerAddedCallback(slot++, user);
  76. if (PlatformManager.MyID != user.ID)
  77. {
  78. m_remotePlayers[user.ID] = user;
  79. PlatformManager.P2P.AddRemotePlayer (player as RemotePlayer);
  80. }
  81. }
  82. }
  83. }
  84. void MatchmakingRoomUpdateCallback(Message<Room> msg)
  85. {
  86. if (msg.IsError)
  87. {
  88. PlatformManager.TerminateWithError(msg);
  89. return;
  90. }
  91. // check to make sure the room is valid as there are a few odd timing issues (for
  92. // example when leaving a room) that can trigger an uninteresting update
  93. if (msg.Data.ID == m_matchRoom)
  94. {
  95. if (msg.Data.UsersOptional != null)
  96. {
  97. foreach (User user in msg.Data.UsersOptional)
  98. {
  99. if (PlatformManager.MyID != user.ID && !m_remotePlayers.ContainsKey(user.ID))
  100. {
  101. m_remotePlayers[user.ID] = user;
  102. var player = MatchPlayerAddedCallback(m_remotePlayers.Count, user);
  103. PlatformManager.P2P.AddRemotePlayer(player as RemotePlayer);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. public void EndMatch()
  110. {
  111. if (m_matchRoom != 0)
  112. {
  113. Rooms.Leave (m_matchRoom);
  114. m_remotePlayers.Clear ();
  115. PlatformManager.P2P.DisconnectAll ();
  116. m_matchRoom = 0;
  117. }
  118. }
  119. public void LeaveQueue()
  120. {
  121. Matchmaking.Cancel();
  122. EndMatch();
  123. }
  124. }
  125. }