Matchmaking
Summary
Extends the lobby from Tutorial 9 into a matchmaker for games with a fixed number of players per room. Before the lobby sends a player to a game room, it asks that room to hold a seat for them with a room-to-room RPC, and the room answers with a ticket the player connects with. The lobby fills the fullest game room first, starts game rooms only when players need them, and game rooms shut themselves down after they have been empty for a while.
Complete Tutorial 9 first. This page changes its 'ServerLobbyRoom', 'ServerGameRoom', 'ClientLobbyRoom', and 'Connect' scripts. It uses room-to-room RPCs, which Tutorial 10 introduces.
Requirements
Completion of Tutorial 9.
How It Works
In Tutorial 9, each game room writes its player count to its public data, and the lobby reads it from the ksRoomInfo list returned by Cluster.GetRoomInfo("Game"). That count is a snapshot from the lobby's last refresh, and a player the lobby sends to a room isn't counted until they finish connecting. If several players ask to join at once, or several lobbies share the game rooms, they can all be sent to the last free seat.
Only the game room knows how many players it really has, so the matchmaker lets it decide:
The lobby picks a room using public data, which is fine for choosing where to ask first.
The lobby asks the room to hold a seat with a
RESERVE_SEATroom-to-room RPC.The game room checks its real count: connected players plus seats it is already holding. It replies to the lobby with
SEAT_RESULT, which carries a ticket if it has a seat or no ticket if it is full, and holds the seat for a few seconds.The lobby sends the player the room and the ticket. The player connects to the game room with the ticket as a connect argument.
The game room's authentication handler lets in players with a valid ticket and turns everyone else away.
Room-to-room RPCs aren't confirmed, and an RPC to a room that is still starting or has stopped is dropped. The lobby handles that by giving up on a request that isn't answered in time and asking another room.
Scripting
Add Shared Constants
Add the cluster RPC ids, the room size, and the "no seat" authentication code to the constants script, so the lobby, game room, and client agree on them. Cluster RPCs have their own ids, separate from client RPC ids.
Consts.cs
Modify the Server Game Room Script
The game room holds seats for players the lobby is sending, lets in players with a ticket, and shuts down after it has been empty for EmptyTimeout seconds. A new room starts with no players, so make the timeout long enough for the first player the lobby sends to arrive.
A seat counts against the room's size from when it is reserved until its player joins, or until it expires if the player never arrives.
Its public data reports connected players plus held seats, so the lobby's next refresh sees them.
Authentication handlers must be registered in
Initialize, and they can run off the main thread, so the seats are only used under a lock.Virtual players also go through authentication, and
ConnectedPlayerCountdoesn't include them, so the handler lets them through.
ServerGameRoom.cs
Room.ShutDown doesn't stop the room immediately: the room keeps running for a few seconds after its last sync, and Room.OnShutDown fires as it stops. The room refuses new seats once it starts shutting down.
The tickets in this example stop players from taking a seat the lobby didn't give them, but System.Random numbers can be guessed. If players could gain from skipping the lobby, generate tickets with a cryptographic random number generator, or have the lobby pass something the game room can verify, as in Authentication.
Modify the Server Lobby Room Script
The lobby no longer keeps a target number of rooms. It queues players who ask to join, and each frame asks the fullest room that looks like it has space to hold a seat for the next queued player. When no room looks like it has space and no room is already starting, it starts one.
Cluster request callbacks run off the main thread, so they only update the room list under a lock. Cluster RPC handlers run on the main thread by default, as do matching and RPCs.
A room that refuses a seat, or doesn't answer in time, is skipped until the next refresh of the room list. A request can go unanswered because the room is still starting or has stopped, since room-to-room RPCs to those rooms are dropped.
If a seat arrives after the lobby has stopped waiting for it, or after its player has left the lobby, the lobby tells the room to release it.
A room started with
Cluster.StartRoomis inGetRoomInforesults right away, with the tags and public data passed toStartRoom. The start request completes once the new room has connected to the cluster, and from then on it receives room-to-room RPCs.
ServerLobbyRoom.cs
Modify the Client Lobby Room Script
The lobby now sends a ticket along with the room. Remove the code that sets the number of game rooms, since the lobby no longer uses it, and pass the ticket to the connect script.
ClientLobbyRoom.cs
Modify the Connect Script
ConnectToServer takes connect arguments to pass to Connect, which the game room's authentication handler receives as args. Change its signature and the room.Connect() call at its end:
Connect.cs
When the game room turns a player away, the client gets the connect status AUTH_ERR_USER_DEFINED with the room's code in result.Code. This happens if the seat expired before the player arrived. A failed connection doesn't fire OnDisconnect, so handle it in the connect handler by going back to the lobby:
Connect.cs
Testing
Test with the local cluster as in Tutorial 9. To see rooms fill, set MAX_PLAYERS to 2 and run several clients (for example, a build and the editor).
Build your scene config (CTRL + F2) and launch the local cluster.
Connect a client to the lobby and press J. The lobby starts a game room, the room holds a seat, and you join it. The game room's logs show the seat request.
Connect a second client and press J. It joins the same room, because that room is the fullest with space.
Connect a third client and press J. The room is full, so the lobby starts a second room.
Disconnect every client from a room and wait
EmptyTimeoutseconds. The room shuts itself down.
Last updated

