question

taneesha21csu133-1 avatar image
taneesha21csu133-1 asked

Unable to get player statistics and update title data using cloudscript

I'm trying to access player statistics and update title data accordingly using azure functions but the api call doesn't seem to be going through. Does anyone know what I'm doing wrong here?

const { app } = require('@azure/functions'); const { PlayFab, PlayFabServer, PlayFabClient, PlayFabCloudScript } = require("playfab-sdk");

app.http('httpTrigger1', { methods: ['POST'], authLevel: 'anonymous', handler: async (request, context) => {

     // Configure PlayFab
     PlayFab.settings.titleId = titleId;
     PlayFab.settings.developerSecretKey = playFabSecretKey;
     const data = await request.json();

     const { PlayFabId } = data;
     const userAccountInfoRequest = {
         PlayFabId: PlayFabId
     };

     // const accountId = userAccountResult.Result.UserInfo.TitleInfo.TitlePlayerAccount.Id;
     let playerStatistics = 0;

     var statisticRequest = {
         PlayFabId: PlayFabId,
         StatisticNames: ["Levels Leaderboard"]
     };

     // get player statistics
     PlayFabServer.GetPlayerStatistics(statisticRequest, (result, error) => {
         if (error) {
             //context.log('Error getting player statistics:', error);
         } else {
             playerStatistics = result.Statistics;
             //context.log(result.status);
         }
     });

     context.log(playerStatistics, " is the xp statistic");

     //const { playerId, xp } = await req.json();

     // Calculate the player's level
     const level = Math.floor(playerStatistics / 1000);
     context.log(level, " this is the level");

     // Update the player's "Level" title data
     const updateDataRequest = {
         PlayFabId: PlayFabId,
         Data: {
             Level: level.toString()
         },
     };

     PlayFabServer.UpdateUserData(updateDataRequest, (error, result) => {
         if (error) {
             context.res = {
                 status: 500,
                 body: "Error updating player data in PlayFab.",
             };
         } else {
             context.res = {
                 status: 200,
                 body: "Player data updated successfully.",
             };
         }
     });

 }

});

CloudScriptLeaderboards and Statistics
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

·
Xiao Zha avatar image
Xiao Zha answered

May I know if you receive any error message when executing the function? Also, if you are using JavaSccript programing model V4, please make sure that your project meets the requirements in Migrate to v4 of the Node.js model for Azure Functions | Microsoft Learn. In addition, since in client, you can call "PlayFabClientAPI. GetPlayerStatistics "and “PlayFabClientAPI. UpdateUserData” directlly, there should be no need to do these with AzureFunction. Why you want to call GetPlayerStatistics and UpdateUserData with AzureFunction?

10 |1200

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

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.