using System; using System.Collections.Generic; using System.Threading.Tasks; using Unity.Services.Rooms; using Unity.Services.Rooms.Models; using Unity.Services.Rooms.Rooms; namespace LobbyRooms.Rooms { /// /// Does all the interactions with Rooms. /// public static class RoomsInterface { 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; 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_maxRoomsToShow = 64; public static void CreateRoomAsync(string requesterUASId, string roomName, int maxPlayers, bool isPrivate, Action> onComplete) { CreateRoomRequest createRequest = new CreateRoomRequest(new CreateRequest( name: roomName, player: new Unity.Services.Rooms.Models.Player(requesterUASId), maxPlayers: maxPlayers, isPrivate: isPrivate )); var task = RoomsService.RoomsApiClient.CreateRoomAsync(createRequest); new InProgressRequest>(task, onComplete); } public static void DeleteRoomAsync(string roomId, Action onComplete) { DeleteRoomRequest deleteRequest = new DeleteRoomRequest(roomId); var task = RoomsService.RoomsApiClient.DeleteRoomAsync(deleteRequest); new InProgressRequest(task, onComplete); } public static void JoinRoomAsync(string requesterUASId, string roomId, string roomCode, Action> onComplete) { JoinRoomRequest joinRequest = new JoinRoomRequest(new JoinRequest( player: new Unity.Services.Rooms.Models.Player(requesterUASId), // TODO: Oh, we can supply initial data here. id: roomId, roomCode: roomCode )); var task = RoomsService.RoomsApiClient.JoinRoomAsync(joinRequest); new InProgressRequest>(task, onComplete); } public static void LeaveRoomAsync(string requesterUASId, string roomId, Action onComplete) { RemovePlayerRequest leaveRequest = new RemovePlayerRequest(roomId, requesterUASId); var task = RoomsService.RoomsApiClient.RemovePlayerAsync(leaveRequest); new InProgressRequest(task, onComplete); } public static void QueryAllRoomsAsync(Action> onComplete) { QueryRoomsRequest queryRequest = new QueryRoomsRequest(new QueryRequest(count: k_maxRoomsToShow)); var task = RoomsService.RoomsApiClient.QueryRoomsAsync(queryRequest); new InProgressRequest>(task, onComplete); } public static void GetRoomAsync(string roomId, Action> onComplete) { GetRoomRequest getRequest = new GetRoomRequest(roomId); var task = RoomsService.RoomsApiClient.GetRoomAsync(getRequest); new InProgressRequest>(task, onComplete); } public static void UpdateRoomAsync(string roomId, Dictionary data, Action> onComplete) { UpdateRoomRequest updateRequest = new UpdateRoomRequest(roomId, new UpdateRequest( data: data )); var task = RoomsService.RoomsApiClient.UpdateRoomAsync(updateRequest); new InProgressRequest>(task, onComplete); } public static void UpdatePlayerAsync(string roomId, string playerId, Dictionary data, Action> onComplete) { UpdatePlayerRequest updateRequest = new UpdatePlayerRequest(roomId, playerId, new PlayerUpdateRequest( data: data )); var task = RoomsService.RoomsApiClient.UpdatePlayerAsync(updateRequest); new InProgressRequest>(task, onComplete); } } }