158 lines
4.0 KiB
C#
158 lines
4.0 KiB
C#
using System.Collections.Generic;
|
|
using Oculus.Platform.Models;
|
|
using UnityEngine;
|
|
|
|
namespace Oculus.Platform.Samples.VrHoops
|
|
{
|
|
public class LeaderboardManager
|
|
{
|
|
public delegate void OnMostWinsLeaderboardUpdated(SortedDictionary<int, LeaderboardEntry> entries);
|
|
|
|
public delegate void OnHighScoreLeaderboardUpdated(SortedDictionary<int, LeaderboardEntry> entries);
|
|
|
|
private const string MOST_MATCHES_WON = "MOST_MATCHES_WON";
|
|
|
|
private const string HIGHEST_MATCH_SCORE = "HIGHEST_MATCH_SCORE";
|
|
|
|
private const int TOP_N_COUNT = 5;
|
|
|
|
private const float LEADERBOARD_POLL_FREQ = 30f;
|
|
|
|
private float m_nextCheckTime;
|
|
|
|
private volatile SortedDictionary<int, LeaderboardEntry> m_mostWins;
|
|
|
|
private bool m_foundLocalUserMostWinsEntry;
|
|
|
|
private long m_numWins;
|
|
|
|
private OnMostWinsLeaderboardUpdated m_mostWinsCallback;
|
|
|
|
private volatile SortedDictionary<int, LeaderboardEntry> m_highScores;
|
|
|
|
private bool m_foundLocalUserHighScore;
|
|
|
|
private OnHighScoreLeaderboardUpdated m_highScoreCallback;
|
|
|
|
public OnMostWinsLeaderboardUpdated MostWinsLeaderboardUpdatedCallback
|
|
{
|
|
set
|
|
{
|
|
m_mostWinsCallback = value;
|
|
}
|
|
}
|
|
|
|
public OnHighScoreLeaderboardUpdated HighScoreLeaderboardUpdatedCallback
|
|
{
|
|
set
|
|
{
|
|
m_highScoreCallback = value;
|
|
}
|
|
}
|
|
|
|
public void CheckForUpdates()
|
|
{
|
|
if (Time.time >= m_nextCheckTime && PlatformManager.CurrentState == PlatformManager.State.WAITING_TO_PRACTICE_OR_MATCHMAKE)
|
|
{
|
|
m_nextCheckTime = Time.time + 30f;
|
|
QueryMostWinsLeaderboard();
|
|
QueryHighScoreLeaderboard();
|
|
}
|
|
}
|
|
|
|
private void QueryMostWinsLeaderboard()
|
|
{
|
|
if (m_mostWins == null)
|
|
{
|
|
m_mostWins = new SortedDictionary<int, LeaderboardEntry>();
|
|
m_foundLocalUserMostWinsEntry = false;
|
|
Leaderboards.GetEntries("MOST_MATCHES_WON", 5, LeaderboardFilterType.None, LeaderboardStartAt.Top).OnComplete(MostWinsGetEntriesCallback);
|
|
}
|
|
}
|
|
|
|
private void MostWinsGetEntriesCallback(Message<LeaderboardEntryList> msg)
|
|
{
|
|
if (!msg.IsError)
|
|
{
|
|
foreach (LeaderboardEntry datum in msg.Data)
|
|
{
|
|
m_mostWins[datum.Rank] = datum;
|
|
if (datum.User.ID == PlatformManager.MyID)
|
|
{
|
|
m_foundLocalUserMostWinsEntry = true;
|
|
m_numWins = datum.Score;
|
|
}
|
|
}
|
|
if (msg.Data.HasNextPage)
|
|
{
|
|
Leaderboards.GetNextEntries(msg.Data).OnComplete(MostWinsGetEntriesCallback);
|
|
return;
|
|
}
|
|
if (!m_foundLocalUserMostWinsEntry)
|
|
{
|
|
Leaderboards.GetEntries("MOST_MATCHES_WON", 1, LeaderboardFilterType.None, LeaderboardStartAt.CenteredOnViewer).OnComplete(MostWinsGetEntriesCallback);
|
|
return;
|
|
}
|
|
}
|
|
if (m_mostWinsCallback != null)
|
|
{
|
|
m_mostWinsCallback(m_mostWins);
|
|
}
|
|
m_mostWins = null;
|
|
}
|
|
|
|
private void QueryHighScoreLeaderboard()
|
|
{
|
|
if (m_highScores == null)
|
|
{
|
|
m_highScores = new SortedDictionary<int, LeaderboardEntry>();
|
|
m_foundLocalUserHighScore = false;
|
|
Leaderboards.GetEntries("HIGHEST_MATCH_SCORE", 5, LeaderboardFilterType.None, LeaderboardStartAt.Top).OnComplete(HighestScoreGetEntriesCallback);
|
|
}
|
|
}
|
|
|
|
private void HighestScoreGetEntriesCallback(Message<LeaderboardEntryList> msg)
|
|
{
|
|
if (!msg.IsError)
|
|
{
|
|
foreach (LeaderboardEntry datum in msg.Data)
|
|
{
|
|
m_highScores[datum.Rank] = datum;
|
|
if (datum.User.ID == PlatformManager.MyID)
|
|
{
|
|
m_foundLocalUserHighScore = true;
|
|
}
|
|
}
|
|
if (msg.Data.HasNextPage)
|
|
{
|
|
Leaderboards.GetNextEntries(msg.Data).OnComplete(HighestScoreGetEntriesCallback);
|
|
return;
|
|
}
|
|
if (!m_foundLocalUserHighScore)
|
|
{
|
|
Leaderboards.GetEntries("HIGHEST_MATCH_SCORE", 1, LeaderboardFilterType.None, LeaderboardStartAt.CenteredOnViewer).OnComplete(HighestScoreGetEntriesCallback);
|
|
return;
|
|
}
|
|
}
|
|
if (m_highScoreCallback != null)
|
|
{
|
|
m_highScoreCallback(m_highScores);
|
|
}
|
|
m_highScores = null;
|
|
}
|
|
|
|
public void SubmitMatchScores(bool wonMatch, uint score)
|
|
{
|
|
if (wonMatch)
|
|
{
|
|
m_numWins++;
|
|
Leaderboards.WriteEntry("MOST_MATCHES_WON", m_numWins);
|
|
}
|
|
if (score != 0)
|
|
{
|
|
Leaderboards.WriteEntry("HIGHEST_MATCH_SCORE", score);
|
|
}
|
|
}
|
|
}
|
|
}
|