-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleLeaderboardHandler.cs
156 lines (142 loc) · 5.75 KB
/
ExampleLeaderboardHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using LootLocker;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace SametHope.RapidLeaderboard
{
/// <summary>
/// Handles the UI and interaction for displaying and updating a guest leaderboard.
/// </summary>
public class ExampleLeaderboardHandler : MonoBehaviour
{
[Header("General")]
[SerializeField] private GameObject _boardContent;
[SerializeField] private Button _submitButton;
[SerializeField] private TMP_InputField _nameField;
[SerializeField] private GuestLeaderboard _leaderboard;
[Tooltip("Should leaderboard get refreshed automatically when this object gets enabled.")]
[SerializeField] private bool _refreshOnEnable;
[Header("Session Entry Texts")]
[SerializeField] private TextMeshProUGUI _sessionRankText;
[SerializeField] private TextMeshProUGUI _sessionNameText;
[SerializeField] private TextMeshProUGUI _sessionScoreText;
[Header("Other Entry Texts")]
[SerializeField] private TextMeshProUGUI[] _rankTexts;
[SerializeField] private TextMeshProUGUI[] _nameTexts;
[SerializeField] private TextMeshProUGUI[] _scoreTexts;
[Header("Information Texts")]
[SerializeField] private TextMeshProUGUI _infoTMP;
[SerializeField] private string _defaultLoadingText = "Loading...";
[SerializeField] private string _defaultFailureText = "Something went wrong \n<size=16>{0}</size>";
[SerializeField] private string _defaultSuccessText = "";
/// <summary>
/// This function is used to get the score. Set it from another class when the game ends, etc.
/// </summary>
public static Func<int> GetSessionScore = () => { return -1; };
private void OnEnable()
{
if (_refreshOnEnable)
{
SetForLoading();
RefreshAllEntriesInOrder(() => SetForSuccess());
}
}
/// <summary>
/// Submits the player's score and refreshes the leaderboard UI.
/// </summary>
public void SubmitAndRefresh()
{
SetForLoading();
LeaderboardManager.SetName(_nameField.text, (nameResponse) =>
{
_leaderboard.SubmitScore(GetSessionScore(), (scoreResponse) =>
{
RefreshAllEntriesInOrder(() => SetForSuccess());
}, SetForError);
}, SetForError);
}
/// <summary>
/// Refreshes all entries in the leaderboard in order.
/// </summary>
/// <param name="onSuccess">Callback to invoke when the refreshing is successful.</param>
public void RefreshAllEntriesInOrder(Action onSuccess = null)
{
// Refresh other entries first, then refresh session entry, and finally call success
RefreshOtherEntries(() => RefreshSessionEntry(onSuccess));
}
/// <summary>
/// Called when name input field is modified to update the submit button.
/// </summary>
public void UpdateButtonState()
{
_submitButton.interactable = _nameField.text != "" && !string.IsNullOrWhiteSpace(_nameField.text);
}
private void RefreshOtherEntries(Action onSuccess = null)
{
if (TryGetFetchCount(out int count))
{
_leaderboard.GetScoreList(count, (listResponse) =>
{
for (int i = 0; i < count; i++)
{
_rankTexts[i].text = $"{listResponse.items[i].rank}.";
_nameTexts[i].text = $"{listResponse.items[i].player.name}";
_scoreTexts[i].text = $"{listResponse.items[i].score}";
}
onSuccess?.Invoke();
}, SetForError);
}
}
private void RefreshSessionEntry(Action onSuccess = null)
{
_leaderboard.GetMemberRank((rankResponse) =>
{
// Means player is not on the leaderboard yet, it is not an big problem so we just do this
if (rankResponse.player == null)
{
_sessionRankText.text = string.Empty;
_sessionNameText.text = string.Empty;
_sessionScoreText.text = string.Empty;
}
else
{
_sessionRankText.text = $"{rankResponse.rank}.";
_sessionNameText.text = $"{rankResponse.player.name}";
_sessionScoreText.text = $"{rankResponse.score}";
}
onSuccess?.Invoke();
}, SetForError);
}
private bool TryGetFetchCount(out int count)
{
if (_rankTexts.Length != _nameTexts.Length || _nameTexts.Length != _scoreTexts.Length)
{
Debug.LogError($"Leaderboard entry TMP arrays are not the same length, this should never happen.", this);
count = 0;
return false;
}
else
{
count = _rankTexts.Length;
return true;
}
}
private void SetForLoading()
{
_boardContent.SetActive(false);
_infoTMP.text = _defaultLoadingText;
}
private void SetForError(LootLockerResponse response)
{
Debug.LogError($"Something went wrong: {response.Error}", this);
_boardContent.SetActive(false);
_infoTMP.text = string.Format(_defaultFailureText, response.Error);
}
private void SetForSuccess()
{
_boardContent.SetActive(true);
_infoTMP.text = _defaultSuccessText;
}
}
}