using System.Collections.Generic; using Unity.Services.Rooms.Models; namespace LobbyRelaySample.Lobby { /// /// Convert the lobby resulting from a request into a LocalLobby for use in the game logic. /// public static class ToLocalLobby { /// /// Create a new LocalLobby from the content of a retrieved lobby. Its data can be copied into an existing LocalLobby for use. /// public static void Convert(Room lobby, LocalLobby outputToHere, LobbyUser existingLocalUser = null) { LobbyInfo info = new LobbyInfo { LobbyID = lobby.Id, LobbyCode = lobby.RoomCode, Private = lobby.IsPrivate, LobbyName = lobby.Name, MaxPlayerCount = lobby.MaxPlayers, RelayCode = lobby.Data?.ContainsKey("RelayCode") == true ? lobby.Data["RelayCode"].Value : null, State = lobby.Data?.ContainsKey("State") == true ? (LobbyState) int.Parse(lobby.Data["State"].Value) : LobbyState.Lobby, AllPlayersReadyTime = lobby.Data?.ContainsKey("AllPlayersReady") == true ? long.Parse(lobby.Data["AllPlayersReady"].Value) : (long?)null }; Dictionary lobbyUsers = new Dictionary(); foreach (var player in lobby.Players) { if (existingLocalUser != null && player.Id.Equals(existingLocalUser.ID)) { existingLocalUser.IsHost = lobby.HostId.Equals(player.Id); existingLocalUser.DisplayName = player.Data?.ContainsKey("DisplayName") == true ? player.Data["DisplayName"].Value : existingLocalUser.DisplayName; existingLocalUser.Emote = player.Data?.ContainsKey("Emote") == true ? player.Data["Emote"].Value : existingLocalUser.Emote; lobbyUsers.Add(existingLocalUser.ID, existingLocalUser); } else { LobbyUser user = new LobbyUser( displayName: player.Data?.ContainsKey("DisplayName") == true ? player.Data["DisplayName"].Value : "NewPlayer", isHost: lobby.HostId.Equals(player.Id), id: player.Id, emote: player.Data?.ContainsKey("Emote") == true ? player.Data["Emote"].Value : null, userStatus: player.Data?.ContainsKey("UserStatus") == true ? player.Data["UserStatus"].Value : UserStatus.Lobby.ToString() ); lobbyUsers.Add(user.ID, user); } } outputToHere.CopyObserved(info, lobbyUsers); } /// /// Create a list of new LocalLobby from the content of a retrieved lobby. /// public static List Convert(QueryResponse response) { List retLst = new List(); foreach (var lobby in response.Results) retLst.Add(Convert(lobby)); return retLst; } private static LocalLobby Convert(Room lobby) { LocalLobby data = new LocalLobby(); Convert(lobby, data, null); return data; } public static Dictionary RetrieveLobbyData(LocalLobby lobby) { Dictionary data = new Dictionary(); data.Add("RelayCode", lobby.RelayCode); data.Add("State", ((int)lobby.State).ToString()); // We only want the ArePlayersReadyTime to be set when we actually are ready for it, and it's null otherwise. So, don't set that here. return data; } public static Dictionary RetrieveUserData(LobbyUser user) { Dictionary data = new Dictionary(); if (user == null || string.IsNullOrEmpty(user.ID)) return data; data.Add("DisplayName", user.DisplayName); data.Add("Emote", user.Emote); // Emote could be null, which is fine. data.Add("UserStatus", user.UserStatus.ToString()); return data; } } }