question

Cruiser dev team avatar image
Cruiser dev team asked

How to get Master Plyer ID withing a scheduled task?

I tried to run a Scheduled Azure Function for a segment of players. I need to revoke some characters from them. To achieve this I plan to use - GetAllUsersCharactersAsync - DeleteCharacterFromUserAsync

But the GetAllUsersCharactersAsync required PlayerId, which is absent in ScheduledTaskFunctionExecutionContext . I tried to cast context to PlayerPlayStreamFunctionExecutionContext that has PlayerID field but this is obviously incorrect.

from https://learn.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/cloudscript-af-context#use-the-context-model-when-executing-via-scheduled-task it looks like EntityPlayStreamFunctionExecutionContext could be the right class, but I do not understand how to properly use it. Why are TPayload and TArgs types are there?

scheduled tasks
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

·
Neils Shi avatar image
Neils Shi answered

For Scheduled Task, you can use "PlayerPlayStreamFunctionExecutionContext" to obtain the MasterPlayerAccountId via “context.PlayerProfile.PlayerId”. For more info, please refer to PlayFab CloudScript using Azure Functions Context Models - PlayFab | Microsoft Learn (https://learn.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/cloudscript-af-context#use-the-context-model-when-executing-in-the-context-of-a-player) .

4 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.

Cruiser dev team avatar image Cruiser dev team commented ·

I've seen the article but it lack the example and it is hard to debug in the cloud env. Is the following code correct, or looks wrong? I get null refs here in the cloud logs while executing from a scheduled task.

 [FunctionName("RevokeByType")]
         public static async Task<dynamic> RevokeByType(
             [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
             ILogger log)
         {
             var context = JsonConvert.DeserializeObject<PlayerPlayStreamFunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
                
             PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
             PlayFabSettings.staticSettings.DeveloperSecretKey = developerSecretKey;
    
             dynamic args = context.FunctionArgument;
             var charType = args["characterType"];
 var getCharsReq = new PlayFab.ServerModels.ListUsersCharactersRequest()
 {
     PlayFabId = context.PlayerProfile.PlayerId
 };
 var charsListResult = await PlayFab.PlayFabServerAPI.GetAllUsersCharactersAsync(getCharsReq);
0 Likes 0 ·
Neils Shi avatar image Neils Shi Cruiser dev team commented ·

The code you provided looks correct. Since if you don’t provide the arguments correctly when executing the scheduled task, trying to access args["characterType"] may result in a null reference. So, could you tell us how your scheduled task's Arguments are set? You can navigate to scheduled task edit page, set the Arguments of Task in the Arguments(JSON) field like:

 {
    "characterType": "value"
 }
0 Likes 0 ·
Cruiser dev team avatar image Cruiser dev team Neils Shi commented ·

Hi! Yes, the parameters were provided:7115-image.png

0 Likes 0 ·
image.png (78.0 KiB)
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.