Skip to content

Latest commit

 

History

History
202 lines (136 loc) · 9.55 KB

search-match-lobby.md

File metadata and controls

202 lines (136 loc) · 9.55 KB

Search Match Lobby

Index

Summary

This document explains how the Search Match Lobby feature was implemented.

The Search Match Lobby feature allows players to look for an specific Match Lobby, to which they'll be connecting for playing against another known player. We can filter these lobbies by their Ids or their Names.

Pre-requisites

Architecture

Before starting explaining how this feature works, lets see how the game was implemented.


alt-text


Regarding the Search Match Lobby feature, we can add the next information:

  • Tic-Tac-Toe game.

    • Here we have the Search option, where we can look for an specific Match Lobby using their Ids or their Names:


Implementation

The Search Match Lobby flow consists in six different steps:


alt-text


Unity Game: Starts the connection process

The Search Match Lobby process starts when a Player (let's call them Player 2) enters a Match Lobby filter (ID or name) for finding an specific Lobby for playing against another Player.



For performing the search, we are running the GetMatchLobbyList method from the MatchLobbyHandler class:

public IEnumerator GetMatchLobbyList(string filter = "")
{
    var request = new ExecuteFunctionRequest
    {
        FunctionName = "SearchMatchLobbies",
        FunctionParameter = new SearchMatchLobbiesRequest
        {
            SearchTerm = filter
        },
        AuthenticationContext = new PlayFabAuthenticationContext
        {
           EntityToken = Player.EntityToken
        }
    };

    PlayFabCloudScriptAPI.ExecuteFunction(request,
        (result) => {
            // Process result here.
        },
        (error) => {
            // Process error here.
        }
    );
}

This method uses the PlayFab's CloudScript API for doing a request to an specific Azure Function (which implementation we'll explain later). The method receives the filter the Player 02 has input. With this data, we create an instance of the SearchMatchLobbiesRequest class, which has the format that the SearchMatchLobbies Azure Function requires.

With all these classes we instantiate a new ExecuteFunctionRequest object, which has the required format for executing the specified Azure Function through the PlayFab service. For sending this request, we call the ExecuteFunction method from the PlayFabCloudScriptAPI class from the PlayFab SDK (see this document for further information on the Execute Function API method).

PlayFab Title: Integration with Azure Functions

With the changes we mentioned in the previous step we're using the PlayFab CloudScript feature for Azure Functions, which in short terms allows us to call ours own Azure Functions through the CloudScript service. You can find more information in this document, and in this tutorial.

For doing the related configuration, we recommend you to read and complete this guide.

Azure Function App: SearchMatchLobbies Function

NOTE: For this step, we also recommend to read and complete the Azure Function configuration.

The next step of the Search Match Lobby process starts with the Execution of the Search Match Lobbies Azure Function.

[FunctionName("SearchMatchLobbies")]
public static async Task<Wrapper<MatchLobbyInfo>> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
    [CosmosDB(ConnectionStringSetting = "PlayFabTicTacToeCosmosDB")] DocumentClient cosmosDBClient
)
{
    var context = await FunctionContext<SearchMatchLobbiesRequest>.Create(req);
    var lobbyListRequest = context.FunctionArgument;

    var result = // execute Cosmos DB Query here.

    return result;
}

This is an HTTP triggered function, which has a Cosmos DB input binding, from which we can retrieve a match lobby list.

From the input binding we retrieve a SearchMatchLobbiesRequest object (which has the search filters). Something important to highlight is that we parse the received request using the Create method from the FunctionContext class. This returns an object which contains the request data in the FunctionArgument property.

Then, we perform a request call to our Cosmos DB in this line for getting the required data. This data will be returned to the Unity Game (through the PlayFab's service) for listing it in the Match Lobby search page. This is performed in this line.

Finally, the Tic-Tac-Toe unity game receives the data from our Azure Function (check it here), process it, and shows it in the Match Lobbies Page:



Players then will be able to join to a match lobby clicking on the Join button.