using UnityEngine;
using Oculus.Platform;
using Oculus.Platform.Models;
// Coordinates updating leaderboard scores and polling for leaderboard updates.
public class LeaderboardManager : MonoBehaviour
{
// API NAME for the leaderboard where we store how many matches the user has won
private const string MOST_MATCHES_WON = "MOST_MATCHES_WON";
// the top number of entries to query
private const int TOP_N_COUNT = 5;
// how often to poll the service for leaderboard updates
private const float LEADERBOARD_POLL_FREQ = 30.0f;
// the next time to check for leaderboard updates
private float m_nextCheckTime;
// whether we've found the local user's entry yet
private bool m_foundLocalUserMostWinsEntry;
// number of times the local user has won
private long m_numWins;
public void Update()
{
if (Time.time >= m_nextCheckTime)
{
m_nextCheckTime = Time.time + LEADERBOARD_POLL_FREQ;
QueryMostWinsLeaderboard();
}
}
void QueryMostWinsLeaderboard()
{
Leaderboards.GetEntries(MOST_MATCHES_WON, TOP_N_COUNT, LeaderboardFilterType.None,
LeaderboardStartAt.Top).OnComplete(MostWinsGetEntriesCallback);
}
void MostWinsGetEntriesCallback(Message<LeaderboardEntryList> msg)
{
if (!msg.IsError)
{
foreach (LeaderboardEntry entry in msg.Data)
{
string currentUserId;
Users.GetLoggedInUser().OnComplete(
(Message<User> msg) =>
{
currentUserId = msg.Data.OculusID;
if (entry.User.OculusID == currentUserId)
{
m_foundLocalUserMostWinsEntry = true;
m_numWins = entry.Score;
}
}
);
}
// results might be paged for large requests
if (msg.Data.HasNextPage)
{
Leaderboards.GetNextEntries(msg.Data).OnComplete(MostWinsGetEntriesCallback);
return;
}
// if local user not in the top, get their position specifically
if (!m_foundLocalUserMostWinsEntry)
{
Leaderboards.GetEntries(MOST_MATCHES_WON, 1, LeaderboardFilterType.None,
LeaderboardStartAt.CenteredOnViewer).OnComplete(MostWinsGetEntriesCallback);
return;
}
}
else
{
Debug.LogError(msg.GetError());
}
}
// submit the local player's match score to the leaderboard service
public void SubmitMatchScores(bool wonMatch)
{
if (wonMatch)
{
m_numWins += 1;
Leaderboards.WriteEntry(MOST_MATCHES_WON, m_numWins);
}
}
}
服务器到服务器 API
您可能需要从您信任的服务器操作排行榜。有关排行榜服务器到服务器 API 的详细信息,请参阅排行榜服务器到服务器 API。