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 { 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 { 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> onSuccess, Action onFailure) { var request = new GetLeaderboardRequest { StatisticName = key, StartPosition = 0, MaxResultsCount = 1 }; PlayFabClientAPI.GetLeaderboard(request, result => onSuccess(result.Leaderboard), onFailure); } public void GetLeaderboard(Action> onSuccess, Action 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()); } }