Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using PlayFab.Plugins.CloudScript;
using PlayFab.ServerModels;
namespace YourNameSpace
{
public static class CountPlayersInSegmentAndUpdateTitleData
{
[FunctionName("GetCountPlayersInSegmentAndUpdateTitleData")]
public static async Task<dynamic> GetCountPlayersInSegmentAndUpdateTitleData(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, ILogger log)
{
log.LogDebug("Started before context");
var context = await FunctionContext<dynamic>.Create(req);
string NUMBER_OF_PLAYER_TODAY_TITLE_DATA_KEY = "NumberOfPlayersToday";
var getPlayersInSegmentRequest = new GetPlayersInSegmentRequest()
{
// This already includes all Api data needed like TitleId or DevSecret
AuthenticationContext = context.AuthenticationContext,
MaxBatchSize = 1,
SegmentId = "XXXXXX"
};
var getPlayersInSegmentResult = await PlayFab.PlayFabServerAPI.GetPlayersInSegmentAsync(getPlayersInSegmentRequest);
log.LogDebug("getPlayersInSegmentResult - ", getPlayersInSegmentResult);
SetTitleDataRequest setTitleDataRequest = new SetTitleDataRequest()
{
Key = NUMBER_OF_PLAYER_TODAY_TITLE_DATA_KEY,
Value = getPlayersInSegmentResult.Result.ProfilesInSegment.ToString()
};
var result = await PlayFab.PlayFabServerAPI.SetTitleDataAsync(setTitleDataRequest);
return result.Error != null ? "Update Done" : "Failed";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment