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.",
};
}
});
}
});