question

anudeep avatar image
anudeep asked

Unable to use namespaces for Azure Cloudscript functions.

using System;
using UnityEngine;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using PlayFab.ServerModels;
using PlayFab.Json;
using System.Collections.Generic;
using PlayFab.DataModels;
using System.Net.Http;
using System.Net.Http.Headers;
using PlayFab.CloudScriptModels;
using PlayFab;
using PlayFab.Samples;
using Naninovel;


namespace PlayFab.AzureFunctions
{
    public  class  FunctionHandlers
    {
        public int freeChoicesValue;
        public int premiumChoicesValue;
        [FunctionName("UpdateStats")]
        public static async Task<dynamic> UpdateStats([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, ILogger log)
        {
            var context= await FunctionContext<dynamic>.Create(req);
            var args = context.FunctionArgument;
            var variableManager = Engine.GetService<ICustomVariableManager>();
            var freeChoices = variableManager.GetVariableValue("g_freeChoices");
            var premiumChoices = variableManager.GetVariableValue("g_premiumChoices");
            freeChoicesValue = int.Parse(freeChoices);
            premiumChoicesValue = int.Parse(premiumChoices);

            var request = new UpdatePlayerStatisticsRequest{
                PlayFabId = context.CurrentPlayerId,
                Statistics = new List<StatisticUpdate>{
                    StatisticName = "Free Choices",
                    Value = freeChoicesValue
                }, new StatisticUpdate{
                    StatisticName = "Premium Choices",
                    Value = premiumChoicesValue
                }
            };
            var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);
            return await serverApi.UpdatePlayerStatisticsRequest(request);
        }

        [FunctionName("FreeChoiceRewards")]
        public static async Task<dynamic> FreeChoiceRewards([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, ILogger log){
            var context = await FunctionContext<dynamic>.Create(req);
            var args = context.FunctionArgument;
            var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);
            PlayFabClientAPI.GetPlayerStatistics(
                new GetPlayerStatisticsRequest {StatisticName = statNames},
                OnGetFreeChoiceStatistics,
                error => Debug.LogError(error.GenerateErrorReport())
            );
        }

        void OnGetFreeChoiceStatistics(GetPlayerStatisticsResult result){
            if(result.Statistics[0].Value%2 == 0){
                RewardCurrency(5);
            }
        }       

        void RewardCurrency(int amount){
            AddUserVirutalCurrencyRequest request = new AddUserVirutalCurrencyRequest();
            request.VirtualCurrency="BC";
            request.Amount = amount;
            PlayFabClientAPI.AddUserVirtualCurrencyAsync(request, OnRewardedCurrencyResult, OnPlayFabError);
        }

        void OnRewardedCurrencyResult(ModifyUserVirtualCurrencyResult result){
            Debug.Log("Rewarded Successfully! ");
        }

        void OnPlayFabError(PlayFabError error){
            Debug.Log(error.GenerateErrorReport);
        }
    }
    
    
    
}


So this is my function handlers script, where UpdateStats and FreeChoiceRewards are two azure functions I had created through VS Code and copied the function url into playfab.
I have saved this script in unity so, as you can see it can retrieve the game data, but however My IDE isn't able to find Microsoft.Azure.Webjobs and more as seen in the screenshot.
Had this issue initially with Visual Studio since i was using that for my unity project, but switched to VS Code because it wasn't showing any errors. But upon reloading it today, I am getting this. Even after installing the extensions/nu get packages.

Besides i also want to confirm, if this is the right way to do azure functions? and call these functions using ExecuteFunction()

Player DataCloudScriptPlayStream
azurew.png (113.6 KiB)
3 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.

Rick Chen avatar image Rick Chen ♦ commented ·

can you please describe how you set up your Azure function in detail? Have you followed this document to set up?

0 Likes 0 ·
anudeep avatar image anudeep Rick Chen ♦ commented ·

Yes I have. Currently I am not facing any of these errors.

0 Likes 0 ·
premium.png (123.3 KiB)
anudeep avatar image anudeep anudeep commented ·

getting the same issue with ServerApi as well

0 Likes 0 ·

1 Answer

·
Rick Chen avatar image
Rick Chen answered

As the error log indicates, FunctionExecutionContext does not contains any variable named "ApiSettings". It seems like you misused the FunctionExecutionContext at line 33, where you wrote "context.ApiSettings", the "context.AuthenticationContext" at the same line is also incorrect, it should be "context.TitleAuthenticationContext". Please check this document to understand the structure of the FunctionExecutionContext.

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.