浏览代码

All Functionality redirected from LobbyAPIInterface (Except Wire Integration)

/main/staging/2021_Upgrade/Async_Refactor
当前提交
aeb54c1c
共有 3 个文件被更改,包括 54 次插入56 次删除
  1. 9
      Assets/Scripts/GameLobby/Lobby/LobbyAPIInterface.cs
  2. 32
      Assets/Scripts/GameLobby/Lobby/LobbyAsyncRequests.cs
  3. 69
      Assets/Scripts/GameLobby/Tests/PlayMode/LobbyRoundtripTests.cs

9
Assets/Scripts/GameLobby/Lobby/LobbyAPIInterface.cs


using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.Services.Lobbies.Models;
namespace LobbyRelaySample.lobby
{

public static class LobbyAPIInterface
{
/* TODO Delete LobbyAPIInterface
public static void CreateLobbyAsync(string requesterUASId, string lobbyName, int maxPlayers, bool isPrivate, Dictionary<string, PlayerDataObject> localUserData, Action<Lobby> onComplete)
{
CreateLobbyOptions createOptions = new CreateLobbyOptions

var task = LobbyService.Instance.RemovePlayerAsync(lobbyId, requesterUASId);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
/// <summary>
/// Uupdates custom data to the lobby, for all to see.
/// </summary>

};
var task = LobbyService.Instance.UpdatePlayerAsync(lobbyId, playerId, updateOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
}*/
public static void SubscribeToLobbyUpdates(string lobbyId, LobbyEventCallbacks lobbyEvent, Action<ILobbyEvents> onLobbySubscribed)
{

32
Assets/Scripts/GameLobby/Lobby/LobbyAsyncRequests.cs


return null;
}
string uasId = AuthenticationService.Instance.PlayerId;
try
{
string uasId = AuthenticationService.Instance.PlayerId;
CreateLobbyOptions createOptions = new CreateLobbyOptions
CreateLobbyOptions createOptions = new CreateLobbyOptions
{
IsPrivate = isPrivate,
Player = new Player(id: uasId, data: CreateInitialPlayerData(localUser))
};
var lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, createOptions);
JoinLobby(lobby);
return lobby;
}
catch (Exception ex)
IsPrivate = isPrivate,
Player = new Player(id: uasId, data: CreateInitialPlayerData(localUser))
};
var lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, createOptions);
Debug.LogError($"Lobby Create failed:\n{ex}");
return null;
}
}
JoinLobby(lobby);
return lobby;
public async Task<Lobby> GetLobbyAsync(string lobbyId)
{
await m_rateLimitQuery.WaitUntilCooldown();
return await LobbyService.Instance.GetLobbyAsync(lobbyId);
}
/// <summary>

69
Assets/Scripts/GameLobby/Tests/PlayMode/LobbyRoundtripTests.cs


using System.Collections.Generic;
using System.Linq;
using Test.Tools;
using System.Threading.Tasks;
using Unity.Services.Lobbies;
using LobbyAPIInterface = LobbyRelaySample.lobby.LobbyAPIInterface;
namespace Test
{

private LobbyRelaySample.Auth.SubIdentity_Authentication m_auth;
private bool m_didSigninComplete = false;
private Dictionary<string, PlayerDataObject> m_mockUserData; // This is handled in the LobbyAsyncRequest calls normally, but we need to supply this for the direct Lobby API calls.
LobbyUser m_LocalUser;
[OneTimeSetUp]
public void Setup()
{

m_LocalUser = new LobbyUser(true);
}
[UnityTearDown]

{ LobbyAPIInterface.DeleteLobbyAsync(m_workingLobbyId, null);
{ yield return AsyncTestHelper.Await(async ()=> await LobbyAsyncRequests.Instance.LeaveLobbyAsync(m_workingLobbyId));
m_workingLobbyId = null;
}
yield return new WaitForSeconds(0.5f); // We need a yield anyway, so wait long enough to probably delete the lobby. There currently (6/22/2021) aren't other tests that would have issues if this took longer.

yield return
new WaitForSeconds(
1); // To prevent a possible 429 with the upcoming Query request, in case a previous test had one; Query requests can only occur at a rate of 1 per second.
float timeout = 5;
Debug.Log("Getting Lobby List 1");
QueryResponse queryResponse = null;
yield return AsyncTestHelper.Await(async () => queryResponse = await LobbyAsyncRequests.Instance.RetrieveLobbyListAsync());

Assert.Greater(timeout, 0, "Timeout check (query #0)");
timeout = 5;
LobbyAPIInterface.CreateLobbyAsync(m_auth.GetContent("id"), lobbyName, 100, false, m_mockUserData,
(r) => { createResponse = r; });
while (createResponse == null && timeout > 0)
{
yield return new WaitForSeconds(0.25f);
timeout -= 0.25f;
}
yield return AsyncTestHelper.Await(async () =>
createResponse = await LobbyAsyncRequests.Instance.CreateLobbyAsync(
lobbyName,
100,
false,
m_LocalUser));
Assert.Greater(timeout, 0, "Timeout check (create)");
Assert.IsNotNull(createResponse, "CreateLobbyAsync should return a non-null result.");
m_workingLobbyId = createResponse.Id;
Assert.AreEqual(lobbyName, createResponse.Name, "Created lobby should match the provided name.");

yield return AsyncTestHelper.Await(async () => queryResponse = await LobbyAsyncRequests.Instance.RetrieveLobbyListAsync());
Debug.Log("Got Lobby List 2");
Assert.Greater(timeout, 0, "Timeout check (query #1)");
Assert.IsNotNull(queryResponse, "QueryAllLobbiesAsync should return a non-null result. (#1)");
Assert.AreEqual(1 + numLobbiesIni, queryResponse.Results.Count, "Queried lobbies list should contain the test lobby.");
Assert.IsTrue(queryResponse.Results.Where(r => r.Name == lobbyName).Count() == 1, "Checking queried lobby for name.");

timeout = 5;
yield return AsyncTestHelper.Await(async ()=> lobby = await LobbyService.Instance.GetLobbyAsync(createResponse.Id));
yield return AsyncTestHelper.Await(async ()=> lobby = await LobbyAsyncRequests.Instance.GetLobbyAsync(createResponse.Id));
Assert.Greater(timeout, 0, "Timeout check (get)");
bool didDeleteFinish = false;
timeout = 5;
LobbyAPIInterface.DeleteLobbyAsync(m_workingLobbyId, () => { didDeleteFinish = true; });
while (timeout > 0 && !didDeleteFinish)
{ yield return new WaitForSeconds(0.25f);
timeout -= 0.25f;
}
Assert.Greater(timeout, 0, "Timeout check (delete)");
yield return AsyncTestHelper.Await(async ()=> await LobbyAsyncRequests.Instance.LeaveLobbyAsync(m_workingLobbyId));
timeout = 5;
Debug.Log("Getting Lobby List 3");
yield return AsyncTestHelper.Await(async () => queryResponse = await LobbyAsyncRequests.Instance.RetrieveLobbyListAsync());

Assert.Greater(timeout, 0, "Timeout check (query #2)");
Assert.IsNotNull(queryResponse, "QueryAllLobbiesAsync should return a non-null result. (#2)");
Assert.AreEqual(numLobbiesIni, queryResponse.Results.Count, "Queried lobbies list should be empty.");

/// <summary>
/// If the Lobby create call fails, we want to ensure we call onComplete so we can act on the failure.
/// If the Lobby create call fails, we return null
public IEnumerator OnCompletesOnFailure()
public IEnumerator CreateFailsWithNull()
{
if (!m_didSigninComplete)
yield return new WaitForSeconds(3);

bool? didComplete = null;
LobbyAPIInterface.CreateLobbyAsync("ThisStringIsInvalidHere", "lobby name", 123, false, m_mockUserData, (r) => { didComplete = (r == null); });
float timeout = 5;
while (didComplete == null && timeout > 0)
{ yield return new WaitForSeconds(0.25f);
timeout -= 0.25f;
}
Lobby createLobby = null;
yield return AsyncTestHelper.Await(async () =>
createLobby = await LobbyAsyncRequests.Instance.CreateLobbyAsync(
"lobby name",
123,
false,
m_LocalUser));
Assert.Greater(timeout, 0, "Timeout check");
Assert.NotNull(didComplete, "Should have called onComplete, even if the async request failed.");
Assert.True(didComplete, "The returned object will be null, so expect to need to handle it.");
Assert.Null(createLobby, "The returned object will be null, so expect to need to handle it.");
}
}
}
正在加载...
取消
保存