using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Lobby;
using Unity.Services.Lobbies.Models;
namespace LobbyRelaySample.lobby
{
///
/// Wrapper for all the interactions with the Lobby API.
///
public static class LobbyAPIInterface
{
///
/// API calls are asynchronous, but for debugging and other reasons we want to reify them as objects so that they can be monitored.
///
private class InProgressRequest
{
public InProgressRequest(Task task, Action onComplete)
{
DoRequest(task, onComplete);
}
private async void DoRequest(Task task, Action onComplete)
{
T result = default;
string currentTrace = System.Environment.StackTrace; // If we don't get the calling context here, it's lost once the async operation begins.
try {
result = await task;
} catch (Exception e) {
Exception eFull = new Exception($"Call stack before async call:\n{currentTrace}\n", e);
throw eFull;
} finally {
onComplete?.Invoke(result);
}
}
}
private const int k_maxLobbiesToShow = 16; // If more are necessary, consider retrieving paginated results or using filters.
public static void CreateLobbyAsync(string requesterUASId, string lobbyName, int maxPlayers, bool isPrivate, Dictionary localUserData, Action> onComplete)
{
CreateLobbyRequest createRequest = new CreateLobbyRequest(new CreateRequest(
name: lobbyName,
player: new Player(id: requesterUASId, data: localUserData),
maxPlayers: maxPlayers,
isPrivate: isPrivate
));
var task = LobbyService.LobbyApiClient.CreateLobbyAsync(createRequest);
new InProgressRequest>(task, onComplete);
}
public static void DeleteLobbyAsync(string lobbyId, Action onComplete)
{
DeleteLobbyRequest deleteRequest = new DeleteLobbyRequest(lobbyId);
var task = LobbyService.LobbyApiClient.DeleteLobbyAsync(deleteRequest);
new InProgressRequest(task, onComplete);
}
public static void JoinLobbyAsync_ByCode(string requesterUASId, string lobbyCode, Dictionary localUserData, Action> onComplete)
{
JoinLobbyByCodeRequest joinRequest = new JoinLobbyByCodeRequest(new JoinByCodeRequest(lobbyCode, new Player(id: requesterUASId, data: localUserData)));
var task = LobbyService.LobbyApiClient.JoinLobbyByCodeAsync(joinRequest);
new InProgressRequest>(task, onComplete);
}
public static void JoinLobbyAsync_ById(string requesterUASId, string lobbyId, Dictionary localUserData, Action> onComplete)
{
JoinLobbyByIdRequest joinRequest = new JoinLobbyByIdRequest(lobbyId, new Player(id: requesterUASId, data: localUserData));
var task = LobbyService.LobbyApiClient.JoinLobbyByIdAsync(joinRequest);
new InProgressRequest>(task, onComplete);
}
public static void LeaveLobbyAsync(string requesterUASId, string lobbyId, Action onComplete)
{
RemovePlayerRequest leaveRequest = new RemovePlayerRequest(lobbyId, requesterUASId);
var task = LobbyService.LobbyApiClient.RemovePlayerAsync(leaveRequest);
new InProgressRequest(task, onComplete);
}
public static void QueryAllLobbiesAsync(List filters, Action> onComplete)
{
QueryLobbiesRequest queryRequest = new QueryLobbiesRequest(new QueryRequest(count: k_maxLobbiesToShow, filter: filters));
var task = LobbyService.LobbyApiClient.QueryLobbiesAsync(queryRequest);
new InProgressRequest>(task, onComplete);
}
public static void GetLobbyAsync(string lobbyId, Action> onComplete)
{
GetLobbyRequest getRequest = new GetLobbyRequest(lobbyId);
var task = LobbyService.LobbyApiClient.GetLobbyAsync(getRequest);
new InProgressRequest>(task, onComplete);
}
public static void UpdateLobbyAsync(string lobbyId, Dictionary data, Action> onComplete)
{
UpdateLobbyRequest updateRequest = new UpdateLobbyRequest(lobbyId, new UpdateRequest(
data: data
));
var task = LobbyService.LobbyApiClient.UpdateLobbyAsync(updateRequest);
new InProgressRequest>(task, onComplete);
}
public static void UpdatePlayerAsync(string lobbyId, string playerId, Dictionary data, Action> onComplete, string allocationId, string connectionInfo)
{
UpdatePlayerRequest updateRequest = new UpdatePlayerRequest(lobbyId, playerId, new PlayerUpdateRequest(
data: data,
allocationId: allocationId,
connectionInfo: connectionInfo
));
var task = LobbyService.LobbyApiClient.UpdatePlayerAsync(updateRequest);
new InProgressRequest>(task, onComplete);
}
public static void HeartbeatPlayerAsync(string lobbyId)
{
HeartbeatRequest request = new HeartbeatRequest(lobbyId);
var task = LobbyService.LobbyApiClient.HeartbeatAsync(request);
new InProgressRequest(task, null);
}
}
}