question

luizcarlos-bs avatar image
luizcarlos-bs asked

How can I make a Cloudscript to transfer PlayerData between two diffent titles?

I'm trying to make a Cloudscript as Azure Function on vscode, on my Mac. The problem is that I can't find anything close to what I want to do. I have one Player with a specific PlayerData in TitleA and I want to transfer that data to TitleB, but I don't know how I can do this, so I'm coding and trying, but getting a bunch of errors, please help me with code. My code for now is like this:

 using System;
 using System.IO;
 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Azure.WebJobs;
 using Microsoft.Azure.WebJobs.Extensions.Http;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Logging;
 using Newtonsoft.Json;
 using PlayFab;
 using PlayFab.AllModels;
    
 public static class TransferPlayerData
 {
     [FunctionName("TransferPlayerData")]
     public static async Task<IActionResult> Run(
         [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
         ILogger log)
     {
         log.LogInformation("C# HTTP trigger function processed a request.");
    
         string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
         TransferPlayerDataRequest data = JsonConvert.DeserializeObject<TransferPlayerDataRequest>(requestBody);
    
         var sourceTitleData = await PlayFabAllSDK.AdminAPI.GetUserDataAsync(new GetUserDataRequest
         {
             PlayFabId = data.SourcePlayerId,
             Keys = null
         });
    
         var destTitleData = await PlayFabAllSDK.AdminAPI.GetUserDataAsync(new GetUserDataRequest
         {
             PlayFabId = data.DestinationPlayerId,
             Keys = null
         });
    
         var titleDataToTransfer = sourceTitleData.Data;
    
         foreach (var key in titleDataToTransfer.Keys)
         {
             await PlayFabAllSDK.AdminAPI.UpdateUserDataAsync(new UpdateUserDataRequest
             {
                 PlayFabId = data.DestinationPlayerId,
                 Data = titleDataToTransfer,
                 Permission = UserDataPermission.Public
             });
         }
    
         return new OkObjectResult("Player data transferred successfully.");
     }
    
     private class TransferPlayerDataRequest
     {
         public string SourcePlayerId;
         public string DestinationPlayerId;
     }
 }
apisPlayer DataCloudScript
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.

Neils Shi avatar image Neils Shi commented ·

I will do some research.

0 Likes 0 ·

1 Answer

·
Neils Shi avatar image
Neils Shi answered

You can follow the Quickstart: Writing a PlayFab CloudScript using Azure Functions to learn how to create an Azure Fuctiuon, and here is the code for your reference.

 [FunctionName("TransferPlayerData")]
         public static async Task&lt;dynamic&gt; TransferPlayerData(
             [HttpTrigger(AuthorizationLevel.Function,"post", Route = null)] HttpRequest req, ILogger log)
         {
             FunctionExecutionContext&lt;dynamic&gt; context = JsonConvert.DeserializeObject&lt;FunctionExecutionContext&lt;dynamic&gt;&gt;(await req.ReadAsStringAsync());
             dynamic args = context.FunctionArgument;
             dynamic SourcePlayerId = null;
             if (args != null &amp;&amp; args["SourcePlayerId"] != null)
             {
                 SourcePlayerId = args["SourcePlayerId"];
             }
    
             dynamic DestinationPlayerId = null;
             if (args != null &amp;&amp; args["DestinationPlayerId"] != null)
             {
                 DestinationPlayerId = args["DestinationPlayerId"];
             }
    
             var getUserDataRequest = new GetUserDataRequest
             {
                 PlayFabId = SourcePlayerId,
             };
             var settings = new PlayFabApiSettings
             {
                 TitleId = "",
                 DeveloperSecretKey = ""
    
             };
             var authContext = new PlayFabAuthenticationContext
             {
                 EntityToken = context.TitleAuthenticationContext.EntityToken
             };
             var serverApi = new PlayFabServerInstanceAPI(settings,authContext);
             var getUserDataResponese = await serverApi.GetUserDataAsync(getUserDataRequest);
    
    
             foreach (var item in getUserDataResponese.Result.Data.Keys)
             {
              var updateUserDataRequest = new UpdateUserDataRequest
             {
                 PlayFabId = DestinationPlayerId,
                 Data = new Dictionary&lt;string, string&gt;()
                 {
                      { item, getUserDataResponese.Result.Data[item].Value }
                 }
             };
             var settings2 = new PlayFabApiSettings
             {
                 TitleId = "",
                 DeveloperSecretKey =""
             };
             var authContext2 = new PlayFabAuthenticationContext
             {
                 EntityToken = context.TitleAuthenticationContext.EntityToken
             };
             var serverApi2 = new PlayFabServerInstanceAPI(settings2,authContext2);
             var updateUserDataResponese = serverApi2.UpdateUserDataAsync(updateUserDataRequest);
             }
    
             return new{
                 getUserDataResponese.Result.Data
             };
    
         }
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.