PlayGamesLeaderboard.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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
  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 { return mLoading; }
  51. internal set { mLoading = value; }
  52. }
  53. public string id
  54. {
  55. get { return mId; }
  56. set { mId = value; }
  57. }
  58. public UserScope userScope
  59. {
  60. get { return mUserScope; }
  61. set { mUserScope = value; }
  62. }
  63. public Range range
  64. {
  65. get { return mRange; }
  66. set { mRange = value; }
  67. }
  68. public TimeScope timeScope
  69. {
  70. get { return mTimeScope; }
  71. set { mTimeScope = value; }
  72. }
  73. public IScore localUserScore
  74. {
  75. get { return mLocalUserScore; }
  76. }
  77. public uint maxRange
  78. {
  79. get { return mMaxRange; }
  80. }
  81. public IScore[] scores
  82. {
  83. get
  84. {
  85. PlayGamesScore[] arr = new PlayGamesScore[mScoreList.Count];
  86. mScoreList.CopyTo(arr);
  87. return arr;
  88. }
  89. }
  90. public string title
  91. {
  92. get { return mTitle; }
  93. }
  94. #endregion
  95. internal bool SetFromData(LeaderboardScoreData data)
  96. {
  97. if (data.Valid)
  98. {
  99. OurUtils.Logger.d("Setting leaderboard from: " + data);
  100. SetMaxRange(data.ApproximateCount);
  101. SetTitle(data.Title);
  102. SetLocalUserScore((PlayGamesScore) data.PlayerScore);
  103. foreach (IScore score in data.Scores)
  104. {
  105. AddScore((PlayGamesScore) score);
  106. }
  107. mLoading = data.Scores.Length == 0 || HasAllScores();
  108. }
  109. return data.Valid;
  110. }
  111. internal void SetMaxRange(ulong val)
  112. {
  113. mMaxRange = (uint) val;
  114. }
  115. internal void SetTitle(string value)
  116. {
  117. mTitle = value;
  118. }
  119. internal void SetLocalUserScore(PlayGamesScore score)
  120. {
  121. mLocalUserScore = score;
  122. }
  123. internal int AddScore(PlayGamesScore score)
  124. {
  125. if (mFilteredUserIds == null || mFilteredUserIds.Length == 0)
  126. {
  127. mScoreList.Add(score);
  128. }
  129. else
  130. {
  131. foreach (string fid in mFilteredUserIds)
  132. {
  133. if (fid.Equals(score.userID))
  134. {
  135. mScoreList.Add(score);
  136. break;
  137. }
  138. }
  139. }
  140. return mScoreList.Count;
  141. }
  142. public int ScoreCount
  143. {
  144. get { return mScoreList.Count; }
  145. }
  146. internal bool HasAllScores()
  147. {
  148. return mScoreList.Count >= mRange.count || mScoreList.Count >= maxRange;
  149. }
  150. }
  151. }
  152. #endif