Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.5 KiB

  1. namespace Oculus.Platform.Samples.VrHoops
  2. {
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using Oculus.Platform;
  6. using Oculus.Platform.Models;
  7. // Coordinates updating leaderboard scores and polling for leaderboard updates.
  8. public class LeaderboardManager
  9. {
  10. // API NAME for the leaderboard where we store how many matches the user has won
  11. private const string MOST_MATCHES_WON = "MOST_MATCHES_WON";
  12. // API NAME for the leaderboard where we store the user's match score
  13. private const string HIGHEST_MATCH_SCORE = "HIGHEST_MATCH_SCORE";
  14. // the top number of entries to query
  15. private const int TOP_N_COUNT = 5;
  16. // how often to poll the service for leaderboard updates
  17. private const float LEADERBOARD_POLL_FREQ = 30.0f;
  18. // the next time to check for leaderboard updates
  19. private float m_nextCheckTime;
  20. // cache to hold most-wins leaderboard entries as they come in
  21. private volatile SortedDictionary<int, LeaderboardEntry> m_mostWins;
  22. // whether we've found the local user's entry yet
  23. private bool m_foundLocalUserMostWinsEntry;
  24. // number of times the local user has won
  25. private long m_numWins;
  26. // callback to deliver the most-wins leaderboard entries
  27. private OnMostWinsLeaderboardUpdated m_mostWinsCallback;
  28. // cache to hold high-score leaderboard entries as they come in
  29. private volatile SortedDictionary<int, LeaderboardEntry> m_highScores;
  30. // whether we've found the local user's entry yet
  31. private bool m_foundLocalUserHighScore;
  32. // callback to deliver the high-scores leaderboard entries
  33. private OnHighScoreLeaderboardUpdated m_highScoreCallback;
  34. public void CheckForUpdates()
  35. {
  36. if (Time.time >= m_nextCheckTime &&
  37. PlatformManager.CurrentState == PlatformManager.State.WAITING_TO_PRACTICE_OR_MATCHMAKE)
  38. {
  39. m_nextCheckTime = Time.time + LEADERBOARD_POLL_FREQ;
  40. QueryMostWinsLeaderboard();
  41. QueryHighScoreLeaderboard();
  42. }
  43. }
  44. #region Most Wins Leaderboard
  45. public delegate void OnMostWinsLeaderboardUpdated(SortedDictionary<int, LeaderboardEntry> entries);
  46. public OnMostWinsLeaderboardUpdated MostWinsLeaderboardUpdatedCallback
  47. {
  48. set { m_mostWinsCallback = value; }
  49. }
  50. void QueryMostWinsLeaderboard()
  51. {
  52. // if a query is already in progress, don't start a new one.
  53. if (m_mostWins != null)
  54. return;
  55. m_mostWins = new SortedDictionary<int, LeaderboardEntry>();
  56. m_foundLocalUserMostWinsEntry = false;
  57. Leaderboards.GetEntries(MOST_MATCHES_WON, TOP_N_COUNT, LeaderboardFilterType.None,
  58. LeaderboardStartAt.Top).OnComplete(MostWinsGetEntriesCallback);
  59. }
  60. void MostWinsGetEntriesCallback(Message<LeaderboardEntryList> msg)
  61. {
  62. if (!msg.IsError)
  63. {
  64. foreach (LeaderboardEntry entry in msg.Data)
  65. {
  66. m_mostWins[entry.Rank] = entry;
  67. if (entry.User.ID == PlatformManager.MyID)
  68. {
  69. m_foundLocalUserMostWinsEntry = true;
  70. m_numWins = entry.Score;
  71. }
  72. }
  73. // results might be paged for large requests
  74. if (msg.Data.HasNextPage)
  75. {
  76. Leaderboards.GetNextEntries(msg.Data).OnComplete(MostWinsGetEntriesCallback);
  77. return;
  78. }
  79. // if local user not in the top, get their position specifically
  80. if (!m_foundLocalUserMostWinsEntry)
  81. {
  82. Leaderboards.GetEntries(MOST_MATCHES_WON, 1, LeaderboardFilterType.None,
  83. LeaderboardStartAt.CenteredOnViewer).OnComplete(MostWinsGetEntriesCallback);
  84. return;
  85. }
  86. }
  87. // else an error is returned if the local player isn't ranked - we can ignore that
  88. if (m_mostWinsCallback != null)
  89. {
  90. m_mostWinsCallback(m_mostWins);
  91. }
  92. m_mostWins = null;
  93. }
  94. #endregion
  95. #region Highest Score Board
  96. public delegate void OnHighScoreLeaderboardUpdated(SortedDictionary<int, LeaderboardEntry> entries);
  97. public OnHighScoreLeaderboardUpdated HighScoreLeaderboardUpdatedCallback
  98. {
  99. set { m_highScoreCallback = value; }
  100. }
  101. void QueryHighScoreLeaderboard()
  102. {
  103. // if a query is already in progress, don't start a new one.
  104. if (m_highScores != null)
  105. return;
  106. m_highScores = new SortedDictionary<int, LeaderboardEntry>();
  107. m_foundLocalUserHighScore = false;
  108. Leaderboards.GetEntries(HIGHEST_MATCH_SCORE, TOP_N_COUNT, LeaderboardFilterType.None,
  109. LeaderboardStartAt.Top).OnComplete(HighestScoreGetEntriesCallback);
  110. }
  111. void HighestScoreGetEntriesCallback(Message<LeaderboardEntryList> msg)
  112. {
  113. if (!msg.IsError)
  114. {
  115. foreach (LeaderboardEntry entry in msg.Data)
  116. {
  117. m_highScores[entry.Rank] = entry;
  118. if (entry.User.ID == PlatformManager.MyID)
  119. {
  120. m_foundLocalUserHighScore = true;
  121. }
  122. }
  123. // results might be paged for large requests
  124. if (msg.Data.HasNextPage)
  125. {
  126. Leaderboards.GetNextEntries(msg.Data).OnComplete(HighestScoreGetEntriesCallback);;
  127. return;
  128. }
  129. // if local user not in the top, get their position specifically
  130. if (!m_foundLocalUserHighScore)
  131. {
  132. Leaderboards.GetEntries(HIGHEST_MATCH_SCORE, 1, LeaderboardFilterType.None,
  133. LeaderboardStartAt.CenteredOnViewer).OnComplete(HighestScoreGetEntriesCallback);
  134. return;
  135. }
  136. }
  137. // else an error is returned if the local player isn't ranked - we can ignore that
  138. if (m_highScoreCallback != null)
  139. {
  140. m_highScoreCallback(m_highScores);
  141. }
  142. m_highScores = null;
  143. }
  144. #endregion
  145. // submit the local player's match score to the leaderboard service
  146. public void SubmitMatchScores(bool wonMatch, uint score)
  147. {
  148. if (wonMatch)
  149. {
  150. m_numWins += 1;
  151. Leaderboards.WriteEntry(MOST_MATCHES_WON, m_numWins);
  152. }
  153. if (score > 0)
  154. {
  155. Leaderboards.WriteEntry(HIGHEST_MATCH_SCORE, score);
  156. }
  157. }
  158. }
  159. }