This sample demonstrates how to implement the Match Lobbies feature. A Match Lobby is an entity that allows a set of players to be matched and to play together.
This implementation uses the Shared Group Data feature, a data entity shared between a small group of players, where the game state and players' data will be stored during the match.
The Match Lobby data is also stored in a Cosmos DB instance, allowing players to list and join to any available lobby.
As PlayFab's documentation states, Shared Group Data should not be used by groups larger than a dozen or so players, at most.
Before configuring this project, first ensure the following prerequisites have been completed:
- Read and complete the PlayFab configuration.
- Read and complete the Azure Function configuration.
- Read and complete the Cosmos DB configuration.
- Read the Search Match Lobby implementation guide.
This is the current Architecture we're using for implementing the Match Lobby feature:
The implementation of the Match Lobby creation feature has the following steps:
The Unity Game is the first layer involved. Here is where the Match Lobby creation process starts with the player interaction, which triggers the execution of the CreateSharedGroup
Azure Function.
The requests to the CreateSharedGroup
are sent from the SharedGroupDataHandler
which uses the ExecuteFunction
method from the PlayFab SDK.
The same logic is used to call the CreateMatchLobby
function.
PlayFab allows the integration with Azure Functions working as an intermediary between the Unity game and the Azure Function.
In this sample, PlayFab receives the requests to execute the Azure Functions with the names CreateSharedGroup
and CreateMatchLobby
.
The CreateSharedGroup function creates the Shared Group Data (SGD) where the game data will be stored, with the TicTacToeSharedGroupData
model structure:
- The Game Status: It contains the status of the board at each movement, the current player, and the winner.
- The Match: It contains the Identifier of the P1, and P2.
- The Match Lobby: It contains the
MatchLobbyId
, that works also as its name, and the current availability.
This function is responsible for:
- Creates the Shared Group Data in PlayFab
- Adds the player who has triggered the function as a member of the group
- Updates the SGD's
SharedGroupId
andMatch
attributes. TheSharedGroupId
is updated with theID
return by PlayFab in the SGD creation, meanwhile theMatch
is updated in itsPlayerOnId
attribute with theID
of the player who has triggered the function.
The Azure Function CreateMatchLobby
is responsible for updating the Shared Group Data with the Match Lobby and storing it in Cosmos DB.
We perform this update with a request to the [UpdateSharedGroupData][ms-updatesharedgroupdata-doc] endpoint specifying the SharedGroupId
and the Match Lobby's data to update.
Finally, the TicTacToeSharedGroupData
is returned to the Game which starts the Start Match process.