question

oglazirin avatar image
oglazirin asked

Implementing skill-based matchmaking with PlayFab + Photon

Hi everyone!

I'm trying to implement skill-based matchmaking for my game by using Photon Networking + PlayFab. It's almost working but I'm stuck at the very end. Here is what I did so far:

1) My PlayFab Title is integrated with Photon plugin.

2) Newly registered players get default PlayerStatistic "Rank" via CloudScript. (Thanks to this thread)

handlers.setDefaultRankForNewPlayer = function(args, context) 
{
    var updateStatistics = server.UpdatePlayerStatistics({
        PlayFabId: currentPlayerId,
        Statistics: [{
            "StatisticName": "Rank",
            "Value": 1500
        }]
    });
}

3) When a player logs into my game I'm fetching his current Rank and PlayFabID from PlayFab.

4) When new Photon rooms are created, I'm assigning CustomRoomProperty "RoomRank" to those rooms with the value of the current rank of the Player that created this room.

5) When players are searching for existing rooms, I'm comparing their current rank to the RoomRank and limit the room search filter accordingly. (Thanks to this video)

6) When the room is full (in my case only 2 people: player and enemy) a match begins.

7) When the match ends I need to update each player's rank in PlayFab. I've written a CloudScript function, that takes PlayFabID's of both players and the result of the match (win/loose/draw). It then calculates the new rank for each player and updates each player's statistic accordingly.

The problem: from where shall I call the CloudScript function from step 7 ?

One of the clients could issue ExecuteCloudScript request, but I dont want the clients to be responsible for this call, because a) it's not their responsibility and b) if the client's internet connection fails then the other player will never get his rank updated.

I could use a webhook that updates both player's ranking when a room is closed. But this weebhook needs to know the result of the game and both player's IDs and I dont know how to get those prameters to my webhook.

I could actually store both player's IDs and the game result in Photon's CustomRoomProperties but I'm not sure how to access it from a webhook.

I really need help with this!

Thank you!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Since you're using Photon Realtime, you'll have to account for the fact that you're dependent upon the client to some degree. But let's address the basic need: First, write the data for the match into a Shared Group Data whose ID is that of the room (the GameId you get from the room - see our Photon guide here: https://api.playfab.com/docs/using-photon-with-playfab/). That way, on a RoomClosed event, you can just read the data you need from there, and update the players.

Now, the real issue is the data you're reading to evaluate the game. Since you're not using Photon Enterprise or custom game servers of any kind, you're dependent upon the data the clients are sending. Depending on the nature of your game, there are a number of approaches you could take, but this would be the part of the process that'll require some game-specific design to work through. If you'd like to discuss your gameplay in more detail to help determine how best to approach this, but would rather not have those details here, feel free to open a ticket with us.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

oglazirin avatar image oglazirin commented ·

Thank you! Shared groups are exactly what I was looking for. I think I will follow your advice and open a ticket to discuss some other possible design that will allow to not depend on player data so much.

0 Likes 0 ·
oglazirin avatar image
oglazirin answered

Hello @Brendan!

I've been trying to implement the solution you suggested but faced another issue. How do you debug webhook code? I couldn't find any sensible way to get an error message from a webhook, when something goes wrong.

For example the following code doesn't work for some reason:

handlers.RoomCreated = function (args)
{
    var playerId = args.UserId;
    var photonRoomName = args.GameId;


    var playerStats = server.GetUserStatistics({
        PlayFabId: playerId
    }).UserStatistics;


    server.CreateSharedGroup
    ({
        SharedGroupId: photonRoomName
    });


    server.UpdateSharedGroupData
    ({
        SharedGroupId: photonRoomName,
        Data: [{
            "PlayFabId": playerId,
            "Elo": playerStats.Elo
        }]
    });
}

How should I approach debugging this error? The only solution that I have in mind is to wrap everything in one big try catch block and then write the error message to title data, to be able to see it from the GameManager. But of course there must be a better way.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Since any response goes back to the caller, debugging webhook calls requires a bit more work - and you're right that you shouldn't be writing log into to Title Data, as it's not really designed for that. What I would recommend is using events (ex: https://api.playfab.com/Documentation/Server/method/WritePlayerEvent) to get logging information for your Cloud Script handlers, so that you can review the behavior in the PlayStream debugger.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

oglazirin avatar image oglazirin commented ·

Nice solution! Thank you!

For anyone who has the same problem here is the code sample:

handlers.RoomCreated = function (args)
{
    server.WritePlayerEvent
    ({
        PlayFabId: args.UserId,
        EventName: "MY_DEBUG_EVENT",
        Body:
        {
            "DebugMessage1": "your debug message 1",
            "DebugMessage2": "your debug message 2"
        }
    });
}

When the webhook is triggered, you can open PlayStream Debugger and see MY_DEBUG_EVENT. Click on info icon to see the event data.

1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.