PlayGamesLeaderboard.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // <copyright file="PlayGamesLeaderboard.cs" company="Google Inc.">
  2. // Copyright (C) 2015 Google Inc. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. #if (UNITY_ANDROID || (UNITY_IPHONE && !NO_GPGS))
  17. namespace GooglePlayGames
  18. {
  19. using System.Collections.Generic;
  20. using GooglePlayGames.BasicApi;
  21. using UnityEngine;
  22. using UnityEngine.SocialPlatforms;
  23. public class PlayGamesLeaderboard : ILeaderboard
  24. {
  25. private string mId;
  26. private UserScope mUserScope;
  27. private Range mRange;
  28. private TimeScope mTimeScope;
  29. private string[] mFilteredUserIds;
  30. private bool mLoading;
  31. private IScore mLocalUserScore;
  32. private uint mMaxRange;
  33. private List<PlayGamesScore> mScoreList = new List<PlayGamesScore>();
  34. private string mTitle;
  35. public PlayGamesLeaderboard(string id)
  36. {
  37. mId = id;
  38. }
  39. #region ILeaderboard implementation
  40. public void SetUserFilter(string[] userIDs)
  41. {
  42. mFilteredUserIds = userIDs;
  43. }
  44. public void LoadScores(System.Action<bool> callback)
  45. {
  46. PlayGamesPlatform.Instance.LoadScores(this, callback);
  47. }
  48. public bool loading
  49. {
  50. get
  51. {
  52. return mLoading;
  53. }
  54. internal set
  55. {
  56. mLoading = value;
  57. }
  58. }
  59. public string id
  60. {
  61. get
  62. {
  63. return mId;
  64. }
  65. set
  66. {
  67. mId = value;
  68. }
  69. }
  70. public UserScope userScope
  71. {
  72. get
  73. {
  74. return mUserScope;
  75. }
  76. set
  77. {
  78. mUserScope = value;
  79. }
  80. }
  81. public Range range
  82. {
  83. get
  84. {
  85. return mRange;
  86. }
  87. set
  88. {
  89. mRange = value;
  90. }
  91. }
  92. public TimeScope timeScope
  93. {
  94. get
  95. {
  96. return mTimeScope;
  97. }
  98. set
  99. {
  100. mTimeScope = value;
  101. }
  102. }
  103. public IScore localUserScore
  104. {
  105. get
  106. {
  107. return mLocalUserScore;
  108. }
  109. }
  110. public uint maxRange
  111. {
  112. get
  113. {
  114. return mMaxRange;
  115. }
  116. }
  117. public IScore[] scores
  118. {
  119. get
  120. {
  121. PlayGamesScore[] arr = new PlayGamesScore[mScoreList.Count];
  122. mScoreList.CopyTo(arr);
  123. return arr;
  124. }
  125. }
  126. public string title
  127. {
  128. get
  129. {
  130. return mTitle;
  131. }
  132. }
  133. #endregion
  134. internal bool SetFromData(LeaderboardScoreData data)
  135. {
  136. if (data.Valid)
  137. {
  138. Debug.Log("Setting leaderboard from: " + data);
  139. SetMaxRange(data.ApproximateCount);
  140. SetTitle(data.Title);
  141. SetLocalUserScore((PlayGamesScore)data.PlayerScore);
  142. foreach (IScore score in data.Scores)
  143. {
  144. AddScore((PlayGamesScore)score);
  145. }
  146. mLoading = data.Scores.Length == 0 || HasAllScores();
  147. }
  148. return data.Valid;
  149. }
  150. internal void SetMaxRange(ulong val)
  151. {
  152. mMaxRange = (uint)val;
  153. }
  154. internal void SetTitle(string value)
  155. {
  156. mTitle = value;
  157. }
  158. internal void SetLocalUserScore(PlayGamesScore score)
  159. {
  160. mLocalUserScore = score;
  161. }
  162. internal int AddScore(PlayGamesScore score)
  163. {
  164. if (mFilteredUserIds == null || mFilteredUserIds.Length == 0)
  165. {
  166. mScoreList.Add(score);
  167. }
  168. else
  169. {
  170. foreach (string fid in mFilteredUserIds)
  171. {
  172. if (fid.Equals(score.userID))
  173. {
  174. return mScoreList.Count;
  175. }
  176. }
  177. mScoreList.Add(score);
  178. }
  179. return mScoreList.Count;
  180. }
  181. public int ScoreCount
  182. {
  183. get
  184. {
  185. return mScoreList.Count;
  186. }
  187. }
  188. internal bool HasAllScores()
  189. {
  190. return mScoreList.Count >= mRange.count || mScoreList.Count >= maxRange;
  191. }
  192. }
  193. }
  194. #endif