using System;
using System.Collections.Generic;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;
namespace LobbyRelaySample.lobby
{
///
/// Wrapper for all the interactions with the Lobby API.
///
public static class LobbyAPIInterface
{
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)
{
CreateLobbyOptions createOptions = new CreateLobbyOptions
{
IsPrivate = isPrivate,
Player = new Player(id: requesterUASId, data: localUserData)
};
var task = LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, createOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void DeleteLobbyAsync(string lobbyId, Action onComplete)
{
var task = LobbyService.Instance.DeleteLobbyAsync(lobbyId);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void JoinLobbyAsync_ByCode(string requesterUASId, string lobbyCode, Dictionary localUserData, Action onComplete)
{
JoinLobbyByCodeOptions joinOptions = new JoinLobbyByCodeOptions { Player = new Player(id: requesterUASId, data: localUserData) };
var task = LobbyService.Instance.JoinLobbyByCodeAsync(lobbyCode, joinOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void JoinLobbyAsync_ById(string requesterUASId, string lobbyId, Dictionary localUserData, Action onComplete)
{
JoinLobbyByIdOptions joinOptions = new JoinLobbyByIdOptions { Player = new Player(id: requesterUASId, data: localUserData) };
var task = LobbyService.Instance.JoinLobbyByIdAsync(lobbyId, joinOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void QuickJoinLobbyAsync(string requesterUASId, List filters, Dictionary localUserData, Action onComplete)
{
var joinRequest = new QuickJoinLobbyOptions
{
Filter = filters,
Player = new Player(id: requesterUASId, data: localUserData)
};
var task = LobbyService.Instance.QuickJoinLobbyAsync(joinRequest);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void LeaveLobbyAsync(string requesterUASId, string lobbyId, Action onComplete)
{
var task = LobbyService.Instance.RemovePlayerAsync(lobbyId, requesterUASId);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void QueryAllLobbiesAsync(List filters, Action onComplete)
{
QueryLobbiesOptions queryOptions = new QueryLobbiesOptions
{
Count = k_maxLobbiesToShow,
Filters = filters
};
var task = LobbyService.Instance.QueryLobbiesAsync(queryOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void GetLobbyAsync(string lobbyId, Action onComplete)
{
var task = LobbyService.Instance.GetLobbyAsync(lobbyId);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
///
/// Uupdates custom data to the lobby, for all to see.
///
public static void UpdateLobbyAsync(string lobbyId, Dictionary data, bool shouldLock, Action onComplete)
{
UpdateLobbyOptions updateOptions = new UpdateLobbyOptions { Data = data, IsLocked = shouldLock };
var task = LobbyService.Instance.UpdateLobbyAsync(lobbyId, updateOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void UpdatePlayerAsync(string lobbyId, string playerId, Dictionary data, Action onComplete, string allocationId, string connectionInfo)
{
UpdatePlayerOptions updateOptions = new UpdatePlayerOptions
{
Data = data,
AllocationId = allocationId,
ConnectionInfo = connectionInfo
};
var task = LobbyService.Instance.UpdatePlayerAsync(lobbyId, playerId, updateOptions);
AsyncRequestLobby.Instance.DoRequest(task, onComplete);
}
public static void SubscribeToLobbyUpdates(string lobbyId, LobbyEventCallbacks lobbyEvent, Action onLobbySubscribed)
{
var task = LobbyService.Instance.SubscribeToLobbyEventsAsync(lobbyId, lobbyEvent);
AsyncRequestLobby.Instance.DoRequest(task, onLobbySubscribed);
}
public static void HeartbeatPlayerAsync(string lobbyId)
{
var task = LobbyService.Instance.SendHeartbeatPingAsync(lobbyId);
AsyncRequestLobby.Instance.DoRequest(task, null);
}
}
}