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.
114 lines
3.7 KiB
C#
114 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using PlayFab;
|
|
using PlayFab.ClientModels;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
public static class LeaderboardUtils
|
|
{
|
|
public static string GetStatisticName(this LeaderboardType type)
|
|
{
|
|
return type switch
|
|
{
|
|
LeaderboardType.PiPuzzle => "PiPuzzle_LevelsCompleted",
|
|
LeaderboardType.HeroesOfLabyrinth => "HeroesOfLabyrinth_LevelsCompleted",
|
|
LeaderboardType.TowerEscape => "TowerEscape_HighScore",
|
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
|
};
|
|
}
|
|
}
|
|
public enum LeaderboardType
|
|
{
|
|
PiPuzzle,
|
|
HeroesOfLabyrinth,
|
|
TowerEscape
|
|
}
|
|
|
|
|
|
public class LeaderboardUIScreen : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject lbItemPrefab;
|
|
[SerializeField] private GameObject lbItemSelfPrefab;
|
|
[SerializeField] private Transform contentPiPuzzle;
|
|
[SerializeField] private Transform contentHOL;
|
|
[SerializeField] private Transform contentTowerEscape;
|
|
[SerializeField] private List<LBPedestalItem> _lbPedestalItems;
|
|
|
|
public void Init()
|
|
{
|
|
LoadLeaderboard(LeaderboardType.PiPuzzle, contentPiPuzzle);
|
|
LoadLeaderboard(LeaderboardType.HeroesOfLabyrinth, contentHOL);
|
|
LoadLeaderboard(LeaderboardType.TowerEscape, contentTowerEscape);
|
|
}
|
|
|
|
private void LoadLeaderboard(LeaderboardType type, Transform content)
|
|
{
|
|
string statName = type.GetStatisticName();
|
|
PlayFabManager.Instance.playFabLeaderboards.GetLeaderboard(statName, 10,
|
|
leaderboard => PopulateLeaderboard(leaderboard, content),
|
|
OnLeaderboardFetchFailure
|
|
);
|
|
}
|
|
|
|
private void PopulateLeaderboard(List<PlayerLeaderboardEntry> leaderboard, Transform parent)
|
|
{
|
|
foreach (Transform child in parent)
|
|
Destroy(child.gameObject);
|
|
|
|
foreach (var lbEntry in leaderboard)
|
|
{
|
|
PopulateLbItem(lbEntry, parent);
|
|
|
|
if (lbEntry.Position <= 2)
|
|
PopulatePedestalItem(lbEntry);
|
|
}
|
|
}
|
|
|
|
private void PopulateLbItem(PlayerLeaderboardEntry lbEntry, Transform parent)
|
|
{
|
|
bool isSelf = lbEntry.Profile != null &&
|
|
PlayFabManager.Instance.playFabUserDataManager.myProfile != null &&
|
|
lbEntry.Profile.PlayerId == PlayFabManager.Instance.playFabUserDataManager.myProfile.PlayerId;
|
|
|
|
var prefab = isSelf ? lbItemSelfPrefab : lbItemPrefab;
|
|
var lbItem = Instantiate(prefab, parent).GetComponent<LBEntryItem>();
|
|
|
|
lbItem.nameText.text = lbEntry.DisplayName ?? lbEntry.PlayFabId;
|
|
lbItem.rankText.text = (lbEntry.Position + 1).ToString();
|
|
lbItem.scoreText.text = lbEntry.StatValue.ToString();
|
|
|
|
PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(lbEntry.PlayFabId, sprite =>
|
|
{
|
|
lbItem.profilePic.sprite = sprite;
|
|
}, error =>
|
|
{
|
|
Debug.Log("Couldn't get pic");
|
|
});
|
|
}
|
|
|
|
private void PopulatePedestalItem(PlayerLeaderboardEntry lbEntry)
|
|
{
|
|
if (lbEntry.Position >= _lbPedestalItems.Count) return;
|
|
|
|
var pedestalItem = _lbPedestalItems[lbEntry.Position];
|
|
|
|
pedestalItem.nameText.text = lbEntry.DisplayName ?? lbEntry.PlayFabId;
|
|
pedestalItem.scoreText.text = lbEntry.StatValue.ToString();
|
|
|
|
PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(lbEntry.PlayFabId, sprite =>
|
|
{
|
|
pedestalItem.profilePic.sprite = sprite;
|
|
}, error =>
|
|
{
|
|
Debug.Log("Couldn't get pic");
|
|
});
|
|
}
|
|
|
|
private void OnLeaderboardFetchFailure(PlayFabError obj)
|
|
{
|
|
Debug.Log("Couldn't Load Leaderboards: " + obj.GenerateErrorReport());
|
|
}
|
|
|
|
public void OnClose() { }
|
|
}
|