You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Scripts/Dev/PlayFab/PlayFabLeaderboards.cs

100 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
public class PlayFabLeaderboards : MonoBehaviour
{
public static string DisplayName;
public void UpdateLevelsCompleted(int levelsCompleted)
{
var request = new UpdatePlayerStatisticsRequest
{
Statistics = new List<StatisticUpdate>
{
new StatisticUpdate
{
StatisticName = GameConstants.LevelCompletedStatsKey,
Value = levelsCompleted
}
}
};
PlayFabClientAPI.UpdatePlayerStatistics(request, OnStatisticsUpdateSuccess, OnStatisticsUpdateFailure);
}
public void UpdateLevelCompletionTime(string tier, int levelNumber, int completionTimeInSeconds)
{
string statisticName = $"{tier}_Level_{levelNumber}_Time";
var request = new UpdatePlayerStatisticsRequest
{
Statistics = new List<StatisticUpdate>
{
new StatisticUpdate
{
StatisticName = statisticName,
Value = completionTimeInSeconds
}
}
};
PlayFabClientAPI.UpdatePlayerStatistics(request, OnStatisticsUpdateSuccess, OnStatisticsUpdateFailure);
}
private void OnStatisticsUpdateSuccess(UpdatePlayerStatisticsResult result)
{
Debug.Log("Successfully updated player statistics!");
}
private void OnStatisticsUpdateFailure(PlayFabError error)
{
Debug.LogError("Failed to update player statistics: " + error.GenerateErrorReport());
}
public void GetLeaderboardByKey(string key,Action<List<PlayerLeaderboardEntry>> onSuccess, Action<PlayFabError> onFailure)
{
var request = new GetLeaderboardRequest
{
StatisticName = key,
StartPosition = 0,
MaxResultsCount = 1
};
PlayFabClientAPI.GetLeaderboard(request, result => onSuccess(result.Leaderboard), onFailure);
}
public void GetLeaderboard(Action<List<PlayerLeaderboardEntry>> onSuccess, Action<PlayFabError> onFailure)
{
var request = new GetLeaderboardRequest
{
StatisticName = GameConstants.LevelCompletedStatsKey,
StartPosition = 0,
MaxResultsCount = 10
};
PlayFabClientAPI.GetLeaderboard(request, result => onSuccess(result.Leaderboard), onFailure);
}
public void SetDisplayName(string displayName)
{
var request = new UpdateUserTitleDisplayNameRequest
{
DisplayName = displayName
};
PlayFabClientAPI.UpdateUserTitleDisplayName(request, OnDisplayNameUpdateSuccess, OnDisplayNameUpdateFailure);
DisplayName = displayName;
}
private void OnDisplayNameUpdateSuccess(UpdateUserTitleDisplayNameResult result)
{
Debug.Log("Display name set successfully: " + result.DisplayName);
}
private void OnDisplayNameUpdateFailure(PlayFabError error)
{
Debug.LogError("Failed to set display name: " + error.GenerateErrorReport());
}
}