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