question

Kim Strasser avatar image
Kim Strasser asked

Rule is not working

I want to create a rule that checks if the player has finished the current level the very first time. I f yes, then create a rating based on his score and add 100 gold coins to his account.

https://api.playfab.com/docs/tutorials/landing-automation/using-cloud-script-actions-with-playstream

I followed this tutorial but I'm not sure if my CloudScript is correct. What are the last two lines doing? Are they necessary?

var content = JSON.stringify({user: profile.PlayerId, event: psEvent.EventName});
var response = http.request('https://httpbin.org/status/200', 'post', content, 'application/json', null, true);

In addition, what is the Arguments (JSON) text box in my picture? When should I use this text box? Could you give me an example?

My problem is that the rule is not working. The function handlePlayStreamEventFirstTimeLevelFinished seems not to get executed, because I can not see a PlayStream event in the Game Manager and the player doesn't get his virtual currency. I only have a PlayStream event for player_finished_level_first_time in Game Manager but not for handlePlayStreamEventFirstTimeLevelFinished.

Raw event JSON
{
    "EventName": "player_finished_level_first_time",
    "EventNamespace": "title.BFD0A",
    "Source": "BFD0A",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "C30AD4505A3C3388",
    "EventId": "d52f1c2dd2174aba8a8e302ee7a3782c",
    "SourceType": "GameServer",
    "Timestamp": "2019-12-13T08:49:38.1730706Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "mainservercloudscriptint",
        "Commit": "f94b0ae"
    },
    "IsFirstTime": "true",
    "Comment": "The player finished this level the first time.",
    "Level": "1",
    "Playerscore": "80"
}

In addition, is it possible to get the rating somehow displayed in the client or is it not possible to return something from the function handlePlayStreamEventFirstTimeLevelFinished in my game?

handlers.UpdateLeaderboard = function (args, context)
{
    ...
    var isfirsttime = true;
    var resultevent = server.WritePlayerEvent({
                                      PlayFabId: currentPlayerId,
                                      EventName: "player_finished_level_first_time",
                                      Body: {
                                      "IsFirstTime": isfirsttime.toString(),
                                      "Comment": "The player finished this level the first time.",
                                      "Level": args.Level.toString(),
                                      "Playerscore": args.Playerscore.toString()
                                      }
                                      });
                                      
    if (resultevent.Error == null)
        log.info("Writing player_finished_level_first_time event successful.");
    else
        log.info("Writing player_finished_level_first_time event not successful.");
}
 
 
handlers.handlePlayStreamEventFirstTimeLevelFinished = function (args, context)
{
    // The event that triggered the action
    // (https://api.playfab.com/playstream/docs/PlayStreamEventModels)
    var psEvent = context.playStreamEvent;
	
    // The profile data of the player associated with the event
    // (https://api.playfab.com/playstream/docs/PlayStreamProfileModels)
    var profile = context.playerProfile;
    
    var level = context.Level;
    var playerscore = context.Playerscore;
    var rating = "";
    
    if (Number(playerscore) < 50)
    {
        rating = "Not bad for your first time.";
        server.AddUserVirtualCurrency({PlayFabID: currentPlayerId, VirtualCurrency: "GO", Amount: "100"});
    }
    if ((Number(playerscore) >= 50) && (Number(playerscore) < 150))
    {
        rating = "The first time and already a professional.";
        server.AddUserVirtualCurrency({PlayFabID: currentPlayerId, VirtualCurrency: "GO", Amount: "100"});
    }
    if (Number(playerscore) >= 150)
    {
        rating = "Is this the first time you played this level? This was amazing!";
        server.AddUserVirtualCurrency({PlayFabID: currentPlayerId, VirtualCurrency: "GO", Amount: "100"});
    }
    
    log.info("Level: " + level + "Your highscore: " + playerscore + " " + rating);
    log.info("Congratulations! You just got 100 gold coins for finishing " + level + " the first time.");
	
    // Post data about the event to an external API
    var content = JSON.stringify({user: profile.PlayerId, event: psEvent.EventName});
    var response = http.request('https://httpbin.org/status/200', 'post', content, 'application/json', null, true);
	
    return { externalAPIResponse: response };
}
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

·
Seth Du avatar image
Seth Du answered

The Rule is not triggered because the event doesn’t pass the “condition” you have configured in the Rule, which is caused by the variable type not matching.

In the Line 21 of your PlayerStream event JSON:

"IsFirstTime": "true",

And line 9 of Cloud Script code:

"IsFirstTime": isfirsttime.toString(),

We can see that IsFirstTime is a string.

Meanwhile, what you have configured in Rules is checking if it is a Boolean value. Hence, you can either change your code to set it as a Boolean value, or change the condition to compare its value as a string.

6 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 ·

Thanx. I understand. But I have another issue with the function handlePlayStreamEventFirstTimeLevelFinished. I found out in my log.info that level and playerscore are undefined and rating is "".

Why are level and playerscore undefined? I thought that they would have the same values as I assigned in var resultevent = server.WritePlayerEvent before:

var resultevent = server.WritePlayerEvent({
  PlayFabId: currentPlayerId,
  EventName: "player_finished_level_first_time",
              Body: {
             "IsFirstTime": isfirsttime,
             "Comment": "The player finished this level the first time.",
             "Level": args.Level.toString(),
             "Playerscore": args.Playerscore.toString()
                     }
           });

Is it necessary that I use Arguments (JSON) text box(in my picture above) in order to get the assigned values from server.WritePlayerEvent? When should I use the Arguments text box? Could you give me an example?

In addition, I don't know if I need the last three lines of code of handlePlayStreamEventFirstTimeLevelFinished. The code after: // Post data about the event to an external API.

For what purpose is this code used?

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Kim Strasser commented ·

It seems you are getting the arguments from context. In line 33:

var level = args.Level;

Context is a global argument maintained by PlayFab, where users can not directly interact. Any argument/parameters should be refered via args.xxxx

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Seth Du ♦ commented ·

I changed it but it's still not working. The function handlePlayStreamEventFirstTimeLevelFinished fails.

handlers.handlePlayStreamEventFirstTimeLevelFinished = function (args, context)
{
    var psEvent = context.playStreamEvent;
    var profile = context.playerProfile;
    
    var level = args.Level;
    var playerscore = args.Playerscore;

    ...
}
"Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "TypeError: Cannot read property 'Level' of null\n    at handlers.handlePlayStreamEventFirstTimeLevelFinished (BFD0A-main.js:819:22)\n    at Object.invokeFunction (Script:116:33)"
        }

What is wrong with var level = args.Level; ?

0 Likes 0 ·
Show more comments
Show more comments

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.