question

buihuuloc6 avatar image
buihuuloc6 asked

Azure Function can't from Reward Ads action

Hello,

I have an azure function call: WatchAdsProcesstor

I can this function normally from Unity Client both locally or ater deployed to Azure Function.

However, When I set the function in Automation=>Reward Ad.

I got this error from Log on Azure

`2020-11-08T09:46:54.629 [Error] Executed 'WatchAdsProcessor' (Failed, Id=c5608617-7178-4bd2-8dc7-f7d0122574f4, Duration=60ms)Object reference not set to an instance of an object.`

Here is the details of the code.

public static async Task<dynamic> WatchAdsProcessor(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
    HttpRequestMessage req, ILogger log)
{
    /* Create the function execution's context through the request */
    var context = await FunctionContext<dynamic>.Create(req);
    var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);
    var getUserDataResult = await serverApi.GetUserDataAsync(new GetUserDataRequest
    {
        Keys = new List<string> {PlayerDataKey.BasicPlayerDataKey},
        PlayFabId = context.MasterPlayerId
    });

    // var content = JSON.parse(playerData.Data["BasicPlayerData"].Value);
    var playerData =
        Newtonsoft.Json.JsonConvert.DeserializeObject<PlayerData>(getUserDataResult.Result
            .Data[PlayerDataKey.BasicPlayerDataKey].Value);

    var serverTimeResult =  await serverApi.GetTimeAsync(new GetTimeRequest());
    playerData.AdInTheDay += 1;
    playerData.LastAdPlayedTime = serverTimeResult.Result.Time;
    var adsDataResult = await serverApi.GetTitleDataAsync(new GetTitleDataRequest
    {
        Keys = new List<string> {"VideosData"}
    });

    VideosData adsData =
        Newtonsoft.Json.JsonConvert.DeserializeObject<VideosData>(adsDataResult.Result.Data["VideosData"]);


    int extraDiamond = 0;

    if (playerData.AdInTheDay >= adsData.MaxAdPerDay)
    {
        return new {messageValue = "Over Watched"};
    }

    var updateUserDataResult = await serverApi.UpdateUserDataAsync(new UpdateUserDataRequest
    {
        Data = new Dictionary<string, string>
            {
                {PlayerDataKey.BasicPlayerDataKey, Newtonsoft.Json.JsonConvert.SerializeObject(playerData)}},
        PlayFabId = context.MasterPlayerId
    });


    if (playerData.AdInTheDay < adsData.VideoAds.Count)
    {
        extraDiamond = adsData.VideoAds[playerData.AdInTheDay].DiamondsToReward;
    }

    if (extraDiamond > 0)
    {
        var updateCurrencyResult = await serverApi.AddUserVirtualCurrencyAsync(new AddUserVirtualCurrencyRequest
        {
            PlayFabId = context.MasterPlayerId,
            Amount = extraDiamond,
            VirtualCurrency = "DI"
        });
    }

    return new {messageValue = ""};
}

Could somebody help here?

Also, Do you know how to show debug log with Azure Function. I have added some log however , It didn't show up in the console

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 context model used by PlayFab when executing a Azure Function triggered by the Reward Ad’s action is “PlayerPlayStreamFunctionExecutionContext”, details are listed in this section: https://docs.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, which is different from the one used for the ExecuteFunction API. That should be the reason why your script were returning errors when executing via Reward Ad’s action, most likely line 6

“var context = await FunctionContext<dynamic>.Create(req);”

is problematic. To troubleshoot this issue, you can add some logs in the script and enable logs to help you debug, please check out this doc for more info: https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#stream-logs

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.