142 lines
3.1 KiB
C#
142 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
using Oculus.Platform.Models;
|
|
using UnityEngine;
|
|
|
|
namespace Oculus.Platform.Samples.VrHoops
|
|
{
|
|
public class MatchmakingManager
|
|
{
|
|
public delegate void OnEnqueueResult(bool successful);
|
|
|
|
public delegate Player OnMatchPlayerAdded(int slot, User user);
|
|
|
|
private const string NORMAL_POOL = "NORMAL_QUICKMATCH";
|
|
|
|
private ulong m_matchRoom;
|
|
|
|
private readonly Dictionary<ulong, User> m_remotePlayers;
|
|
|
|
private OnEnqueueResult m_enqueueCallback;
|
|
|
|
private OnMatchPlayerAdded m_playerCallback;
|
|
|
|
public OnEnqueueResult EnqueueResultCallback
|
|
{
|
|
private get
|
|
{
|
|
return m_enqueueCallback;
|
|
}
|
|
set
|
|
{
|
|
m_enqueueCallback = value;
|
|
}
|
|
}
|
|
|
|
public OnMatchPlayerAdded MatchPlayerAddedCallback
|
|
{
|
|
private get
|
|
{
|
|
return m_playerCallback;
|
|
}
|
|
set
|
|
{
|
|
m_playerCallback = value;
|
|
}
|
|
}
|
|
|
|
public MatchmakingManager()
|
|
{
|
|
m_remotePlayers = new Dictionary<ulong, User>();
|
|
Matchmaking.SetMatchFoundNotificationCallback(MatchFoundCallback);
|
|
Rooms.SetUpdateNotificationCallback(MatchmakingRoomUpdateCallback);
|
|
}
|
|
|
|
public void QueueForMatch()
|
|
{
|
|
Matchmaking.Enqueue("NORMAL_QUICKMATCH").OnComplete(MatchmakingEnqueueCallback);
|
|
}
|
|
|
|
private void MatchmakingEnqueueCallback(Message msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
Debug.Log(msg.GetError().Message);
|
|
EnqueueResultCallback(false);
|
|
}
|
|
}
|
|
|
|
private void MatchFoundCallback(Message<Oculus.Platform.Models.Room> msg)
|
|
{
|
|
m_matchRoom = msg.Data.ID;
|
|
Matchmaking.JoinRoom(msg.Data.ID, true).OnComplete(MatchmakingJoinRoomCallback);
|
|
}
|
|
|
|
private void MatchmakingJoinRoomCallback(Message<Oculus.Platform.Models.Room> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
Debug.Log(msg.GetError().Message);
|
|
EnqueueResultCallback(false);
|
|
return;
|
|
}
|
|
Debug.Log("Match found and room joined " + m_matchRoom);
|
|
EnqueueResultCallback(true);
|
|
int num = 0;
|
|
if (msg.Data.UsersOptional == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (User item in msg.Data.UsersOptional)
|
|
{
|
|
Player player = MatchPlayerAddedCallback(num++, item);
|
|
if (PlatformManager.MyID != item.ID)
|
|
{
|
|
m_remotePlayers[item.ID] = item;
|
|
PlatformManager.P2P.AddRemotePlayer(player as RemotePlayer);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void MatchmakingRoomUpdateCallback(Message<Oculus.Platform.Models.Room> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
}
|
|
else
|
|
{
|
|
if (msg.Data.ID != m_matchRoom || msg.Data.UsersOptional == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (User item in msg.Data.UsersOptional)
|
|
{
|
|
if (PlatformManager.MyID != item.ID && !m_remotePlayers.ContainsKey(item.ID))
|
|
{
|
|
m_remotePlayers[item.ID] = item;
|
|
Player player = MatchPlayerAddedCallback(m_remotePlayers.Count, item);
|
|
PlatformManager.P2P.AddRemotePlayer(player as RemotePlayer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void EndMatch()
|
|
{
|
|
if (m_matchRoom != 0)
|
|
{
|
|
Rooms.Leave(m_matchRoom);
|
|
m_remotePlayers.Clear();
|
|
PlatformManager.P2P.DisconnectAll();
|
|
m_matchRoom = 0uL;
|
|
}
|
|
}
|
|
|
|
public void LeaveQueue()
|
|
{
|
|
Matchmaking.Cancel();
|
|
EndMatch();
|
|
}
|
|
}
|
|
}
|