233 lines
5.7 KiB
C#
233 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Oculus.Platform.Models;
|
|
using UnityEngine;
|
|
|
|
namespace Oculus.Platform.Samples.VrVoiceChat
|
|
{
|
|
public class RoomManager
|
|
{
|
|
public struct Invite
|
|
{
|
|
public readonly ulong RoomID;
|
|
|
|
public readonly string OwnerID;
|
|
|
|
public Invite(ulong roomID, string owner)
|
|
{
|
|
RoomID = roomID;
|
|
OwnerID = owner;
|
|
}
|
|
}
|
|
|
|
private ulong m_roomID;
|
|
|
|
private User m_remoteUser;
|
|
|
|
private static readonly float INVITE_POLL_FREQ_SECONDS = 5f;
|
|
|
|
private float m_nextPollTime;
|
|
|
|
private HashSet<ulong> m_pendingRoomRequests;
|
|
|
|
private List<Invite> m_invites;
|
|
|
|
public ulong RemoteUserID
|
|
{
|
|
get
|
|
{
|
|
return (m_remoteUser == null) ? 0 : m_remoteUser.ID;
|
|
}
|
|
}
|
|
|
|
public string RemoteOculusID
|
|
{
|
|
get
|
|
{
|
|
return (m_remoteUser == null) ? string.Empty : m_remoteUser.OculusID;
|
|
}
|
|
}
|
|
|
|
public bool ShouldPollInviteList
|
|
{
|
|
get
|
|
{
|
|
return m_pendingRoomRequests == null && Time.time >= m_nextPollTime;
|
|
}
|
|
}
|
|
|
|
public RoomManager()
|
|
{
|
|
Rooms.SetRoomInviteAcceptedNotificationCallback(LaunchedFromAcceptingInviteCallback);
|
|
Rooms.SetUpdateNotificationCallback(RoomUpdateCallback);
|
|
}
|
|
|
|
private void LaunchedFromAcceptingInviteCallback(Message<string> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
return;
|
|
}
|
|
Debug.Log("Launched Invite to join Room: " + msg.Data);
|
|
m_roomID = Convert.ToUInt64(msg.GetString());
|
|
}
|
|
|
|
public bool CheckForLaunchInvite()
|
|
{
|
|
if (m_roomID != 0)
|
|
{
|
|
JoinExistingRoom(m_roomID);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void CreateRoomAndLaunchInviteMenu()
|
|
{
|
|
Rooms.CreateAndJoinPrivate(RoomJoinPolicy.InvitedUsers, 2u, true).OnComplete(CreateAndJoinPrivateRoomCallback);
|
|
}
|
|
|
|
private void CreateAndJoinPrivateRoomCallback(Message<Oculus.Platform.Models.Room> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
return;
|
|
}
|
|
m_roomID = msg.Data.ID;
|
|
m_remoteUser = null;
|
|
PlatformManager.TransitionToState(PlatformManager.State.WAITING_FOR_ANSWER);
|
|
Rooms.LaunchInvitableUserFlow(m_roomID).OnComplete(OnLaunchInviteWorkflowComplete);
|
|
}
|
|
|
|
private void OnLaunchInviteWorkflowComplete(Message msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
}
|
|
}
|
|
|
|
public void UpdateActiveInvitesList()
|
|
{
|
|
m_nextPollTime = Time.time + INVITE_POLL_FREQ_SECONDS;
|
|
m_pendingRoomRequests = new HashSet<ulong>();
|
|
m_invites = new List<Invite>();
|
|
Notifications.GetRoomInviteNotifications().OnComplete(GetRoomInviteNotificationsCallback);
|
|
}
|
|
|
|
private void GetRoomInviteNotificationsCallback(Message msg_untyped)
|
|
{
|
|
Message<RoomInviteNotificationList> message = (Message<RoomInviteNotificationList>)msg_untyped;
|
|
if (message.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(message);
|
|
return;
|
|
}
|
|
foreach (RoomInviteNotification datum in message.Data)
|
|
{
|
|
m_pendingRoomRequests.Add(datum.RoomID);
|
|
Rooms.Get(datum.RoomID).OnComplete(GetRoomInfoCallback);
|
|
}
|
|
if (message.Data.Count == 0)
|
|
{
|
|
m_pendingRoomRequests = null;
|
|
PlatformManager.SetActiveInvites(m_invites);
|
|
}
|
|
}
|
|
|
|
private void GetRoomInfoCallback(Message<Oculus.Platform.Models.Room> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
return;
|
|
}
|
|
if (msg.Data.OwnerOptional != null)
|
|
{
|
|
Invite item = new Invite(msg.Data.ID, msg.Data.OwnerOptional.OculusID);
|
|
m_pendingRoomRequests.Remove(item.RoomID);
|
|
if (msg.Data.UsersOptional != null && msg.Data.UsersOptional.Count == 1)
|
|
{
|
|
m_invites.Add(item);
|
|
}
|
|
}
|
|
if (m_pendingRoomRequests.Count == 0)
|
|
{
|
|
m_pendingRoomRequests = null;
|
|
PlatformManager.SetActiveInvites(m_invites);
|
|
}
|
|
}
|
|
|
|
public void JoinExistingRoom(ulong roomID)
|
|
{
|
|
Rooms.Join(roomID, true).OnComplete(JoinRoomCallback);
|
|
}
|
|
|
|
private void JoinRoomCallback(Message<Oculus.Platform.Models.Room> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
return;
|
|
}
|
|
string text = ((msg.Data.OwnerOptional == null) ? string.Empty : msg.Data.OwnerOptional.OculusID);
|
|
int num = ((msg.Data.UsersOptional != null) ? msg.Data.UsersOptional.Count : 0);
|
|
Debug.LogFormat("Joined room: {0} owner: {1} count: ", msg.Data.ID, text, num);
|
|
m_roomID = msg.Data.ID;
|
|
if (msg.Data.UsersOptional == null || msg.Data.UsersOptional.Count != 2)
|
|
{
|
|
PlatformManager.TransitionToState(PlatformManager.State.HANGUP);
|
|
}
|
|
else
|
|
{
|
|
foreach (User item in msg.Data.UsersOptional)
|
|
{
|
|
if (item.ID != PlatformManager.MyID)
|
|
{
|
|
m_remoteUser = item;
|
|
}
|
|
}
|
|
PlatformManager.TransitionToState(PlatformManager.State.CONNECTED_IN_A_ROOM);
|
|
}
|
|
m_nextPollTime = Time.time;
|
|
}
|
|
|
|
private void RoomUpdateCallback(Message<Oculus.Platform.Models.Room> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
return;
|
|
}
|
|
string text = ((msg.Data.OwnerOptional == null) ? string.Empty : msg.Data.OwnerOptional.OculusID);
|
|
int num = ((msg.Data.UsersOptional != null) ? msg.Data.UsersOptional.Count : 0);
|
|
Debug.LogFormat("Room {0} Update: {1} owner: {2} count: ", msg.Data.ID, text, num);
|
|
if (msg.Data.UsersOptional == null || msg.Data.UsersOptional.Count != 2)
|
|
{
|
|
PlatformManager.TransitionToState(PlatformManager.State.HANGUP);
|
|
return;
|
|
}
|
|
foreach (User item in msg.Data.UsersOptional)
|
|
{
|
|
if (item.ID != PlatformManager.MyID)
|
|
{
|
|
m_remoteUser = item;
|
|
}
|
|
}
|
|
PlatformManager.TransitionToState(PlatformManager.State.CONNECTED_IN_A_ROOM);
|
|
}
|
|
|
|
public void LeaveCurrentRoom()
|
|
{
|
|
if (m_roomID != 0)
|
|
{
|
|
Rooms.Leave(m_roomID);
|
|
m_roomID = 0uL;
|
|
m_remoteUser = null;
|
|
}
|
|
PlatformManager.TransitionToState(PlatformManager.State.WAITING_TO_CALL_OR_ANSWER);
|
|
}
|
|
}
|
|
}
|