question

Leyla Ahmedzadeh avatar image
Leyla Ahmedzadeh asked

Time interval

I want to create a time interval for a specific function. For example, players can only press the play button and enter the game from 12 AM to 2 PM. Other than that players can't enter.

limits
10 |1200

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

Made Wang avatar image
Made Wang answered

Can you describe the usage scenario?

If only prevent or allow players to call the API provided by PlayFab. You can do this by modifying the API policy. You can implement it using Azure Function Cloud Script and call this Azure Function Cloud Script at fixed time through Scheduled Task.

1 comment
10 |1200

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

Leyla Ahmedzadeh avatar image Leyla Ahmedzadeh commented ·

Do you have the example C# code in Azure Function Cloud Script ? That would be very helpful

0 Likes 0 ·
Made Wang avatar image
Made Wang answered

You can refer to the code below. I wrote 3 cases in one function, but you don't need to call them all at the same time.

If you're modifying the policy for the first time, I suggest you use Postman and create a new Title to do some testing to understand what they do.

        [FunctionName("UpdatePolicy")]
        public static async Task<IActionResult> UpdatePolicy(
             [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
             ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            var settings = new PlayFabApiSettings
            {
                TitleId = context.TitleAuthenticationContext.Id,
                DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process),
            };
            var adminApi=new PlayFabAdminInstanceAPI(settings);


            //Get
            var getPolicy=new GetPolicyRequest()
            {
                PolicyName="ApiPolicy"
            };
            var getPolicyResult=await adminApi.GetPolicyAsync(getPolicy);

            //Add
            var addPolicy=new UpdatePolicyRequest()
            {
                PolicyName="ApiPolicy",
                OverwritePolicy=false,
                PolicyVersion=getPolicyResult.Result.PolicyVersion,
                Statements=new List<PermissionStatement>()
                {
                    new PermissionStatement()
                    {
                        Resource="pfrn:api--/Client/LoginWithCustomID",
                        Action="*",
                        Effect=PlayFab.AdminModels.EffectType.Allow,
                        Principal="*",
                        Comment="Allow client to login with custom id."
                    }
                }
            };
            await adminApi.UpdatePolicyAsync(addPolicy);

            //Update
            var  newStatements=getPolicyResult.Result.Statements;
            foreach(var item in newStatements)
            {
                if(item.Resource=="pfrn:api--/Client/LoginWithCustomID")
                {
                    item.Effect=PlayFab.AdminModels.EffectType.Deny;
                    item.Comment="Deny client to login with custom id.";
                }
            }
            var updatePolicy=new UpdatePolicyRequest()
            {
                PolicyName="ApiPolicy",
                OverwritePolicy=true,
                PolicyVersion=getPolicyResult.Result.PolicyVersion,
                Statements=newStatements
            };
            await adminApi.UpdatePolicyAsync(updatePolicy);


            return null;
        } 


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.