question

Kim Strasser avatar image
Kim Strasser asked

Creating a rule for leaderboard version change

I want that my CloudScript function is triggered when the leaderboard named "Level 1-1" changes its version. It's a weekly leaderboard. In addition, I will check the new version of the leaderboard in my CloudScript function. If the new leaderboard version is "7", then I want to call server API SetTitleData and set a key/value pair to "Level1": "7". If the new leaderboard version is not "7", then delete the "Level1" key/value pair if it exists.

Players are only allowed to add scores to the leaderboard if the version is "7", therefore I need to change TitleData only if the leaderboard version changed from "6" to "7".

What event type should I use in Rules so that my CloudScript function is only triggered when the leaderboard named "Level 1-1" changes its version?

CloudScript
10 |1200

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

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

The title_statistic_version_changed event is the one for your case, however, currently it’s not available in Rules. As a workaround, you can use Webhooks to handle this, it can forward the event to your custom web endpoint by making a POST request, for instance, to your Azure Function’s URL, where you can perform some custom logics.

3 comments
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

I have two questions about webhooks.

1) Is it possible to find out which leaderboard triggered the webhook if I have more than one leaderboard in Game Manager? Because I cannot find a possibility to specify my leaderboards name "Level 1-1" in the webhook.

2) I get an error message when I want to get a key/value pair in my Azure function:

[FunctionName("LeaderboardVersionChanged")]
public static async Task<dynamic> LeaderboardNewApiCall(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log)
{
  var context = await FunctionContext<dynamic>.Create(req);
  var args = context.FunctionArgument;
  var leaderboardname = args["Leaderboardname"];
  log.LogInformation($"Leaderboardname = {leaderboardname}");
  var leaderboardversion = args["Leaderboardversion"];
  log.LogInformation($"Leaderboardversion = {leaderboardversion}");
[Error] Executed 'LeaderboardVersionChanged' (Failed, Id=410cadd8-dd28-4981-a44e-e219f70fe5e6, Duration=313ms)Object reference not set to an instance of an object.

How can I get the keys and the values of my webhook in my Azure function?

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

(1) The event data is passed as the JSON body of the request to Azure Function, you can find all the info in it, the body looks like the following:

[
    {
        "EventName": "title_statistic_version_changed",
        "StatisticName": "foo",
        "StatisticVersion": 1,
        "ScheduledResetInterval": "Never",
        "EventNamespace": "com.playfab",
        "EntityType": "title",
        "Source": "PlayFab",
        "EventId": "d908643061df43b5ac0f566bf3d044f6",
        "EntityId": "xxx",
        "SourceType": "BackEnd",
        "Timestamp": "2021-01-19T07:11:12.8497927Z",
        "PlayFabEnvironment": {
            "Vertical": "master",
            "Cloud": "main",
            "Application": "gamemanager",
            "Commit": "f407c01"
        }
    }
]

I don't think the request header you set is necessary for this case.

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

(2) FunctionContext is not the right context, to access key/value pairs, simply do the following:

        [FunctionName("HandleLeaderboardVersionChange")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            string statisticName = data[0].StatisticName;
            log.LogInformation("Statistic name: " + statisticName);
            string statisticVersion = data[0].StatisticVersion;
            log.LogInformation("Statistic version: " + statisticVersion);
            return new OkObjectResult(requestBody);
        }
0 Likes 0 ·

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.