# Rooms and the lobby

> Create and find rooms, show a room list, and share state through room and player properties.

Source: https://syncverse.aqaddoura.com/docs/rooms

## Create a room

Room names are unique within a project. Creating a name that already exists does not fail silently: the response has status `DuplicatedNameError` and carries the existing room, so you can join it instead.

```csharp
var options = new RoomOptions {
    MaxPlayers = 8,       // capped by the server (and by your plan in the cloud)
    IsVisible = true,     // listed in the lobby and found by JoinRandomRoom
    IsOpen = true,        // accepts new players
    PublicProps = new Dictionary<string, string> { ["map"] = "dust" },
};
master.CreateRoom ("arena", options, res => {
    if (res.Room == null) { Debug.LogError (res.Status); return; }
    room = master.ConnectToRoom (res.Room); // closes the lobby socket by default
    room.OnConnected += () => room.Join ("Ava");
});
```

| Status | Meaning |
| --- | --- |
| `Success` | The room exists and is announced to the lobby. |
| `DuplicatedNameError` | A room with this name exists; res.Room is that room. |
| `InvalidName` | Empty, whitespace or longer than 64 characters. |
| `RoomLimit` | The project has its plan's maximum of rooms open. |
| `NotAllowed` | The player's access token is restricted to another room. |
| `ServerFull` | A self-hosted server reached MAX\_ROOMS. |

## Find a room

```csharp
master.GetRoomByName ("arena", res => {
    if (res.Success) room = master.ConnectToRoom (res.Room);
});

master.JoinRandomRoom (res => {
    if (res.Success) room = master.ConnectToRoom (res.Room);
    else master.CreateRoom ("room-" + Random.Range (0, 9999), null, OnCreated);
});
```

`JoinRandomRoom` picks a room that is visible, open and not full. Hidden rooms are still found by name with `GetRoomByName` or by id with `GetRoomById`.

## Join a room

`ConnectToRoom` opens the room socket and, by default, closes the lobby socket. Call `Join` once `OnConnected` fires. Before `OnJoined` the room already sends you the players who are there and everything buffered, so objects spawned earlier appear right away.

| Join failure | Meaning |
| --- | --- |
| `RoomFull` | maxPlayers reached. |
| `RoomClosed` | The master client set the room to closed. |
| `AlreadyJoined` | This connection already joined. |

Leave with `room.Leave()`. A room with no players is removed after 30 seconds.

## Show a room list

```csharp
master.OnRoomListUpdated += rooms => {
    foreach (var r in rooms)
        Debug.Log ($"{r.Name} {r.PlayerCount}/{r.MaxPlayers} {(r.IsOpen ? "" : "closed")}");
};
master.JoinLobby ();   // fetches the list, then keeps it current
// master.Rooms always holds the latest list; OnRoomAdded / OnRoomRemoved / OnRoomUpdated report changes.
```

## Properties and the master client

The first player in a room is its **master client**. When they leave, the next player who joined takes over. The master client owns scene objects and is the only one who can change room properties and options.

```csharp
// Any player: your own properties (merge; an empty string deletes a key).
room.LocalPlayer.SetCustomProperty ("team", "red");

// Master client only: room properties, public properties (shown in the lobby), open / visible.
if (room.IsMasterClient) {
    room.SetCustomProperties (new StringDictionary { ["round"] = "2" });
    room.SetPublicProperties (new StringDictionary { ["map"] = "caves" });
    room.SetOptions (isOpen: false, isVisible: false);   // lock the room when the match starts
}

room.OnPlayerPropsChanged += (player, delta) => { /* ... */ };
room.OnMasterClientChanged += player => Debug.Log ($"{player.NickName} is the master client now");
```

> Properties are last-writer-wins string pairs. Keep them small; every change is broadcast to the room.

### Latency

The room pings the server every 2 seconds. `room.Ping` holds the smoothed round trip in milliseconds and `OnPingUpdated` reports changes.
