Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5d32f5ae2f5f7495cb77f079a21c54b0, type: 3}
m_Name:
m_EditorClassIdentifier:
leaderboardName: test-leaderboard
leaderboardName: team-scores
--- !u!1 &497470367
GameObject:
m_ObjectHideFlags: 0
Expand Down
79 changes: 65 additions & 14 deletions Assets/Samples/LeaderboardsDemo/Scripts/LeaderboardUIController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using UnityEngine;
using TaloGameServices;
using UnityEngine.UIElements;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System;
using System.Collections.Generic;

public class LeaderboardUIController : MonoBehaviour
{
Expand All @@ -13,17 +13,21 @@ public class LeaderboardUIController : MonoBehaviour
private ListView entriesList;
private Label infoLabel;

private int filterIdx;
private string filter = "All";

private async void Start()
{
root = GetComponent<UIDocument>().rootVisualElement;
root.Q<Button>("post-btn").clicked += OnPostClick;
root.Q<Button>("filter-btn").clicked += OnFilterClick;

entriesList = root.Q<ListView>();
infoLabel = root.Q<Label>("info");

if (string.IsNullOrEmpty(leaderboardName))
{
throw new System.Exception("Please create a leaderboard and set the leaderboard name to its internal name");
throw new Exception("Please create a leaderboard and set the leaderboard name to its internal name");
}

await LoadEntries();
Expand All @@ -32,12 +36,17 @@ private async void Start()
private async void OnPostClick()
{
var username = root.Q<TextField>().text;
await Talo.Players.Identify("username", username);
var score = UnityEngine.Random.Range(0, 100);
var team = UnityEngine.Random.Range(0, 2) == 0 ? "Blue" : "Red";

var score = Random.Range(0, 100);
(LeaderboardEntry entry, bool updated) = await Talo.Leaderboards.AddEntry(leaderboardName, score);
await Talo.Players.Identify("username", username);
(LeaderboardEntry entry, bool updated) = await Talo.Leaderboards.AddEntry(
leaderboardName,
score,
("team", team)
);

infoLabel.text = $"You scored {score}.";
infoLabel.text = $"You scored {score} for the {team} team.";
if (updated) infoLabel.text += " Your highscore was updated!";

entriesList.Rebuild();
Expand All @@ -62,9 +71,26 @@ private async Task LoadEntries()

do
{
var res = await Talo.Leaderboards.GetEntries(leaderboardName, page);
page++;
done = res.isLastPage;
try
{
var res = await Talo.Leaderboards.GetEntries(leaderboardName, page);
page++;
done = res.isLastPage;
}
catch (RequestException e)
{
if (e.responseCode == 404)
{
infoLabel.text = $"Failed loading leaderboard {leaderboardName}. Does it exist?";
}
else
{
infoLabel.text = e.Message;
Debug.LogError(e);
}
return;
}

} while (!done);

HandleListVisibility();
Expand All @@ -78,13 +104,38 @@ private async Task LoadEntries()
return label;
};

var entries = Talo.Leaderboards.GetCachedEntries(leaderboardName);

entriesList.bindItem = (e, i) =>
{
e.Q<Label>().text = $"{i+1}. {entries[i].playerAlias.identifier} - {entries[i].score}";
LeaderboardEntry entry = entriesList.itemsSource[i] as LeaderboardEntry;
e.Q<Label>().text = $"{i+1}. {entry.playerAlias.identifier} - {entry.score} ({entry.GetProp("team", "No")} team)";
};

entriesList.itemsSource = entries;
entriesList.itemsSource = Talo.Leaderboards.GetCachedEntries(leaderboardName);
}

private string GetNextFilter(int idx)
{
return new [] { "All", "Blue", "Red" } [idx % 3];
}

private void OnFilterClick()
{
filterIdx++;
filter = GetNextFilter(filterIdx);

infoLabel.text = $"Filtering on {filter.ToLower()}";
root.Q<Button>("filter-btn").text = $"{GetNextFilter(filterIdx + 1)} team scores";

if (filter == "All")
{
entriesList.itemsSource = Talo.Leaderboards.GetCachedEntries(leaderboardName);
}
else
{
entriesList.itemsSource = new List<LeaderboardEntry>(Talo.Leaderboards.GetCachedEntries(leaderboardName)
.FindAll((e) => e.GetProp("team", "") == filter));
}

entriesList.Rebuild();
}
}
1 change: 1 addition & 0 deletions Assets/Samples/LeaderboardsDemo/UI/LeaderboardUI.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ui:VisualElement style="flex-direction: column; margin-top: 16px;">
<ui:TextField picking-mode="Ignore" text="Username" name="username" />
<ui:Button text="Add entry" display-tooltip-when-elided="true" name="post-btn" />
<ui:Button text="Blue team scores" display-tooltip-when-elided="true" name="filter-btn" />
</ui:VisualElement>
</ui:VisualElement>
</ui:VisualElement>
Expand Down
Loading