Skip to content

MTT-3026 feat: Lobby and Authentication services to use async await #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using BossRoom.Scripts.Shared.Net.UnityServices.Auth;
using Unity.Multiplayer.Samples.BossRoom.Shared;
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
Expand Down Expand Up @@ -43,16 +44,24 @@ protected override void InitializeScope()
}

[Inject]
void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFacade, LocalLobbyUser localUser, LocalLobby localLobby)
async void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFacade, LocalLobbyUser localUser, LocalLobby localLobby)
{
var unityAuthenticationInitOptions = new InitializationOptions();
var profile = ProfileManager.Profile;
if (profile.Length > 0)
try
{
unityAuthenticationInitOptions.SetProfile(profile);
}
var unityAuthenticationInitOptions = new InitializationOptions();
var profile = ProfileManager.Profile;
if (profile.Length > 0)
{
unityAuthenticationInitOptions.SetProfile(profile);
}

authServiceFacade.DoSignInAsync(OnAuthSignIn, OnSignInFailed, unityAuthenticationInitOptions);
await authServiceFacade.SignInAsync(unityAuthenticationInitOptions);
OnAuthSignIn();
}
catch (Exception)
{
OnSignInFailed();
}

void OnAuthSignIn()
{
Expand Down
92 changes: 55 additions & 37 deletions Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Unity.Multiplayer.Samples.BossRoom.Client;
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Lobbies.Models;
using UnityEngine;

Expand Down Expand Up @@ -62,78 +62,96 @@ void OnDestroy()

//Lobby and Relay calls done from UI

public void CreateLobbyRequest(string lobbyName, bool isPrivate, int maxPlayers)
public async void CreateLobbyRequest(string lobbyName, bool isPrivate, int maxPlayers)
{
// before sending request to lobby service, populate an empty lobby name, if necessary
if (string.IsNullOrEmpty(lobbyName))
{
lobbyName = k_DefaultLobbyName;
}

m_LobbyServiceFacade.CreateLobbyAsync(lobbyName, maxPlayers, isPrivate, OnCreatedLobby, OnFailedLobbyCreateOrJoin);
BlockUIWhileLoadingIsInProgress();

var lobbyCreationAttempt = await m_LobbyServiceFacade.TryCreateLobbyAsync(lobbyName, maxPlayers, isPrivate);

if (lobbyCreationAttempt.Success)
{
m_LocalUser.IsHost = true;
m_LobbyServiceFacade.SetRemoteLobby(lobbyCreationAttempt.Lobby);

m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;

Debug.Log($"Created lobby with ID: {m_LocalLobby.LobbyID} and code {m_LocalLobby.LobbyCode}, Internal Relay Join Code{m_LocalLobby.RelayJoinCode}");
m_GameNetPortal.StartUnityRelayHost();
}
else
{
UnblockUIAfterLoadingIsComplete();
}
}

public void QueryLobbiesRequest(bool blockUI)
public async void QueryLobbiesRequest(bool blockUI)
{
if (!AuthenticationService.Instance.IsAuthorized)
if (UnityServices.State != ServicesInitializationState.Initialized)
{
return;
}

m_LobbyServiceFacade.RetrieveLobbyListAsync(
OnSuccess,
OnFailure
);

if (blockUI)
{
BlockUIWhileLoadingIsInProgress();
}

void OnSuccess(QueryResponse qr)
await m_LobbyServiceFacade.RetrieveAndPublishLobbyListAsync();
UnblockUIAfterLoadingIsComplete();
}

public async void JoinLobbyWithCodeRequest(string lobbyCode)
{
BlockUIWhileLoadingIsInProgress();

var result = await m_LobbyServiceFacade.TryJoinLobbyAsync(null, lobbyCode);

if (result.Success)
{
UnblockUIAfterLoadingIsComplete();
OnJoinedLobby(result.Lobby);
}

void OnFailure()
else
{
UnblockUIAfterLoadingIsComplete();
}
}

public void JoinLobbyWithCodeRequest(string lobbyCode)
public async void JoinLobbyRequest(LocalLobby lobby)
{
m_LobbyServiceFacade.JoinLobbyAsync(null, lobbyCode, OnJoinedLobby, OnFailedLobbyCreateOrJoin);
BlockUIWhileLoadingIsInProgress();
}

public void JoinLobbyRequest(LocalLobby lobby)
{
m_LobbyServiceFacade.JoinLobbyAsync(lobby.LobbyID, lobby.LobbyCode, OnJoinedLobby, OnFailedLobbyCreateOrJoin);
BlockUIWhileLoadingIsInProgress();
}

public void QuickJoinRequest()
{
m_LobbyServiceFacade.QuickJoinLobbyAsync(OnJoinedLobby, OnFailedLobbyCreateOrJoin);
BlockUIWhileLoadingIsInProgress();
}
var result = await m_LobbyServiceFacade.TryJoinLobbyAsync(lobby.LobbyID, lobby.LobbyCode);

void OnFailedLobbyCreateOrJoin()
{
UnblockUIAfterLoadingIsComplete();
if (result.Success)
{
OnJoinedLobby(result.Lobby);
}
else
{
UnblockUIAfterLoadingIsComplete();
}
}

void OnCreatedLobby(Lobby lobby)
public async void QuickJoinRequest()
{
m_LocalUser.IsHost = true;
m_LobbyServiceFacade.SetRemoteLobby(lobby);
BlockUIWhileLoadingIsInProgress();

m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;
var result = await m_LobbyServiceFacade.TryQuickJoinLobbyAsync();

Debug.Log($"Created lobby with ID: {m_LocalLobby.LobbyID} and code {m_LocalLobby.LobbyCode}, Internal Relay Join Code{m_LocalLobby.RelayJoinCode}");
m_GameNetPortal.StartUnityRelayHost();
if (result.Success)
{
OnJoinedLobby(result.Lobby);
}
else
{
UnblockUIAfterLoadingIsComplete();
}
}

void OnJoinedLobby(Lobby remoteLobby)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void ServiceErrorHandler(UnityServiceErrorMessage error)
void HandleLobbyError(UnityServiceErrorMessage error)
{
var errorMessage = error.Message;

switch (((LobbyServiceException)error.OriginalException).Reason)
{
case LobbyExceptionReason.LobbyConflict:
Expand All @@ -72,7 +73,7 @@ void HandleLobbyError(UnityServiceErrorMessage error)

void OnDestroy()
{
m_Subscriptions.Dispose();
m_Subscriptions?.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion Assets/BossRoom/Scripts/Shared/ApplicationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void Start()

private void OnDestroy()
{
m_LobbyServiceFacade.EndTracking();
m_LobbyServiceFacade?.EndTracking();
DIScope.RootScope.Dispose();
}

Expand Down
15 changes: 6 additions & 9 deletions Assets/BossRoom/Scripts/Shared/Infrastructure/UpdateRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class UpdateRunner : MonoBehaviour
class SubscriberData
{
public float Period;
public float PeriodCurrent;
public float NextCallTime;
}

readonly Queue<Action> m_PendingHandlers = new Queue<Action>();
Expand All @@ -31,7 +31,7 @@ public void OnDestroy()
/// Subscribe in order to have onUpdate called approximately every period seconds (or every frame, if period <= 0).
/// Don't assume that onUpdate will be called in any particular order compared to other subscribers.
/// </summary>
public void Subscribe(Action<float> onUpdate, float period)
public void Subscribe(Action<float> onUpdate, float updatePeriod)
{
if (onUpdate == null)
{
Expand All @@ -56,7 +56,7 @@ public void Subscribe(Action<float> onUpdate, float period)
{
if (m_Subscribers.Add(onUpdate))
{
m_SubscriberData.Add(onUpdate, new SubscriberData() {Period = period, PeriodCurrent = 0});
m_SubscriberData.Add(onUpdate, new SubscriberData() {Period = updatePeriod, NextCallTime = 0});
}
});
}
Expand Down Expand Up @@ -84,17 +84,14 @@ void Update()
m_PendingHandlers.Dequeue()?.Invoke();
}

float dt = Time.deltaTime;

foreach (var subscriber in m_Subscribers)
{
var subscriberData = m_SubscriberData[subscriber];
subscriberData.PeriodCurrent += dt;

if (subscriberData.PeriodCurrent > subscriberData.Period)
if (Time.time >= subscriberData.NextCallTime)
{
subscriber.Invoke(subscriberData.PeriodCurrent);
subscriberData.PeriodCurrent = 0;
subscriber.Invoke(Time.deltaTime);
subscriberData.NextCallTime = Time.time + subscriberData.Period;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,10 @@ public async void StartClientUnityRelayModeAsync(string joinCode, Action<string>

try
{
var clientRelayUtilityTask = UnityRelayUtilities.JoinRelayServerFromJoinCode(joinCode);
await clientRelayUtilityTask;
var (ipv4Address, port, allocationIdBytes, connectionData, hostConnectionData, key) = clientRelayUtilityTask.Result;
var (ipv4Address, port, allocationIdBytes, connectionData, hostConnectionData, key) =
await UnityRelayUtilities.JoinRelayServerFromJoinCode(joinCode);

m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode, null, null);
await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode);
utp.SetClientRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, hostConnectionData, isSecure: true);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,13 @@ public async void StartUnityRelayHost()

try
{
// we now need to get the joinCode?
var serverRelayUtilityTask = UnityRelayUtilities.AllocateRelayServerAndGetJoinCode(k_MaxUnityRelayConnections);
await serverRelayUtilityTask;
// we now have the info from the relay service
var (ipv4Address, port, allocationIdBytes, connectionData, key, joinCode) = serverRelayUtilityTask.Result;
var (ipv4Address, port, allocationIdBytes, connectionData, key, joinCode) =
await UnityRelayUtilities.AllocateRelayServerAndGetJoinCode(k_MaxUnityRelayConnections);

m_LocalLobby.RelayJoinCode = joinCode;
//next line enabled lobby and relay services integration
m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode, null, null);
await m_LobbyServiceFacade.UpdateLobbyDataAsync(m_LocalLobby.GetDataForUnityServices());
await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode);

// we now need to set the RelayCode somewhere :P
utp.SetHostRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, isSecure: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void OnClientDisconnect(ulong clientId)
m_Portal.NetManager.OnClientDisconnectCallback -= OnClientDisconnect;
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
{
m_LobbyServiceFacade.DeleteLobbyAsync(m_LobbyServiceFacade.CurrentUnityLobby.Id, null, null);
m_LobbyServiceFacade.DeleteLobbyAsync(m_LobbyServiceFacade.CurrentUnityLobby.Id);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,25 @@ void InjectDependencies(IPublisher<UnityServiceErrorMessage> unityServiceErrorMe
m_UnityServiceErrorMessagePublisher = unityServiceErrorMessagePublisher;
}

void OnServiceException(Exception e)
public async Task SignInAsync(InitializationOptions initializationOptions)
{
Debug.LogWarning(e.Message);
var reason = $"{e.Message} ({e.InnerException?.Message})";
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
}

public void DoSignInAsync(Action onSigninComplete, Action onFailed, InitializationOptions initializationOptions)
{
var task = TrySignIn(initializationOptions);
UnityServiceCallsTaskWrapper.RunTask<Exception>(task, onSigninComplete, onFailed, OnServiceException);
}

async Task TrySignIn(InitializationOptions initializationOptions)
{
await Unity.Services.Core.UnityServices.InitializeAsync(initializationOptions);
try
{
await Unity.Services.Core.UnityServices.InitializeAsync(initializationOptions);

if (!AuthenticationService.Instance.IsSignedIn)
if (!AuthenticationService.Instance.IsSignedIn)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
}
catch (Exception e)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
var reason = $"{e.Message} ({e.InnerException?.Message})";
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
throw;
}


}
}
}
Loading