using System ;
using System.Collections.Generic ;
using System.Threading.Tasks ;
using Unity.Services.Lobbies.Lobby ;
using Unity.Services.Lobbies.Models ;
namespace LobbyRelaySample.lobby
/// </summary>
public static class LobbyAPIInterface
{
/// <summary>
/// API calls are asynchronous, but for debugging and other reasons we want to reify them as objects so that they can be monitored.
/// </summary>
private class InProgressRequest < T >
{
public InProgressRequest ( Task < T > task , Action < T > onComplete )
{
DoRequest ( task , onComplete ) ;
}
private async void DoRequest ( Task < T > task , Action < T > 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 ) ;
}
}
}
public static void CreateLobbyAsync ( string requesterUASId , string lobbyName , int maxPlayers , bool isPrivate , Dictionary < string , PlayerDataObject > localUserData , Action < Response < Lobby > > onComplete )
public static void CreateLobbyAsync ( string requesterUASId , string lobbyName , int maxPlayers , bool isPrivate , Dictionary < string , PlayerDataObject > localUserData , Action < Lobby > 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 < Response < Lobby > > ( task , onComplete ) ;
CreateLobbyOptions createOptions = new CreateLobbyOptions
{
IsPrivate = isPrivate ,
Player = new Player ( id : requesterUASId , data : localUserData )
} ;
var task = Lobbies . Instance . CreateLobbyAsync ( lobbyName , maxPlayers , createOptions ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void DeleteLobbyAsync ( string lobbyId , Action < Response > onComplete )
public static void DeleteLobbyAsync ( string lobbyId , Action onComplete )
DeleteLobbyRequest deleteRequest = new DeleteLobbyRequest ( lobbyId ) ;
var task = LobbyService . LobbyApiClient . DeleteLobbyAsync ( deleteRequest ) ;
new InProgressRequest < Response > ( task , onComplete ) ;
var task = Lobbies . Instance . DeleteLobbyAsync ( lobbyId ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void JoinLobbyAsync_ByCode ( string requesterUASId , string lobbyCode , Dictionary < string , PlayerDataObject > localUserData , Action < Response < Lobby > > onComplete )
public static void JoinLobbyAsync_ByCode ( string requesterUASId , string lobbyCode , Dictionary < string , PlayerDataObject > localUserData , Action < Lobby > onComplete )
JoinLobbyByCodeRequest joinRequest = new JoinLobbyByCodeRequest ( new JoinByCodeRequest ( lobbyCode , new Player ( id : requesterUASId , data : localUserData ) ) ) ;
var task = LobbyService . LobbyApiClient . JoinLobbyByCodeAsync ( joinRequest ) ;
new InProgressRequest < Response < Lobby > > ( task , onComplete ) ;
JoinLobbyByCodeOptions joinOptions = new JoinLobbyByCodeOptions { Player = new Player ( id : requesterUASId , data : localUserData ) } ;
var task = Lobbies . Instance . JoinLobbyByCodeAsync ( lobbyCode , joinOptions ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void JoinLobbyAsync_ById ( string requesterUASId , string lobbyId , Dictionary < string , PlayerDataObject > localUserData , Action < Response < Lobby > > onComplete )
public static void JoinLobbyAsync_ById ( string requesterUASId , string lobbyId , Dictionary < string , PlayerDataObject > localUserData , Action < Lobby > onComplete )
JoinLobbyByIdRequest joinRequest = new JoinLobbyByIdRequest ( lobbyId , new Player ( id : requesterUASId , data : localUserData ) ) ;
var task = LobbyService . LobbyApiClient . JoinLobbyByIdAsync ( joinRequest ) ;
new InProgressRequest < Response < Lobby > > ( task , onComplete ) ;
JoinLobbyByIdOptions joinOptions = new JoinLobbyByIdOptions { Player = new Player ( id : requesterUASId , data : localUserData ) } ;
var task = Lobbies . Instance . JoinLobbyByIdAsync ( lobbyId , joinOptions ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void LeaveLobbyAsync ( string requesterUASId , string lobbyId , Action < Response > 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 < Response > ( task , onComplete ) ;
var task = Lobbies . Instance . RemovePlayerAsync ( lobbyId , requesterUASId ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void QueryAllLobbiesAsync ( List < QueryFilter > filters , Action < Response < QueryResponse > > onComplete )
public static void QueryAllLobbiesAsync ( List < QueryFilter > filters , Action < QueryResponse > onComplete )
QueryLobbiesRequest queryRequest = new QueryLobbiesRequest ( new QueryRequest ( count : k_maxLobbiesToShow , filter : filters ) ) ;
var task = LobbyService . LobbyApiClient . QueryLobbiesAsync ( queryRequest ) ;
new InProgressRequest < Response < QueryResponse > > ( task , onComplete ) ;
QueryLobbiesOptions queryOptions = new QueryLobbiesOptions
{
Count = k_maxLobbiesToShow ,
Filters = filters
} ;
var task = Lobbies . Instance . QueryLobbiesAsync ( queryOptions ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void GetLobbyAsync ( string lobbyId , Action < Response < Lobby > > onComplete )
public static void GetLobbyAsync ( string lobbyId , Action < Lobby > onComplete )
GetLobbyRequest getRequest = new GetLobbyRequest ( lobbyId ) ;
var task = LobbyService . LobbyApiClient . GetLobbyAsync ( getRequest ) ;
new InProgressRequest < Response < Lobby > > ( task , onComplete ) ;
var task = Lobbies . Instance . GetLobbyAsync ( lobbyId ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void UpdateLobbyAsync ( string lobbyId , Dictionary < string , DataObject > data , Action < Response < Lobby > > onComplete )
public static void UpdateLobbyAsync ( string lobbyId , Dictionary < string , DataObject > data , Action < Lobby > onComplete )
UpdateLobbyRequest updateRequest = new UpdateLobbyRequest ( lobbyId , new UpdateRequest (
data : data
) ) ;
var task = LobbyService . LobbyApiClient . UpdateLobbyAsync ( updateRequest ) ;
new InProgressRequest < Response < Lobby > > ( task , onComplete ) ;
UpdateLobbyOptions updateOptions = new UpdateLobbyOptions { Data = data } ;
var task = Lobbies . Instance . UpdateLobbyAsync ( lobbyId , updateOptions ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
public static void UpdatePlayerAsync ( string lobbyId , string playerId , Dictionary < string , PlayerDataObject > data , Action < Response < Lobby > > onComplete , string allocationId , string connectionInfo )
public static void UpdatePlayerAsync ( string lobbyId , string playerId , Dictionary < string , PlayerDataObject > data , Action < Lobby > 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 < Response < Lobby > > ( task , onComplete ) ;
UpdatePlayerOptions updateOptions = new UpdatePlayerOptions
{
Data = data ,
AllocationId = allocationId ,
ConnectionInfo = connectionInfo
} ;
var task = Lobbies . Instance . UpdatePlayerAsync ( lobbyId , playerId , updateOptions ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
HeartbeatRequest request = new HeartbeatRequest ( lobbyId ) ;
var task = LobbyService . LobbyApiClient . HeartbeatAsync ( request ) ;
new InProgressRequest < Response > ( task , null ) ;
var task = Lobbies . Instance . SendHeartbeatPingAsync ( lobbyId ) ;
AsyncRequest . DoRequest ( task , null ) ;
}
}
}