BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Platform / Samples / VrHoops / Scripts / LeaderboardManager.cs

LeaderboardManager.cs

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