//
// Copyright (C) 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#if UNITY_ANDROID
namespace GooglePlayGames
{
using System.Collections.Generic;
using GooglePlayGames.BasicApi;
using UnityEngine;
using UnityEngine.SocialPlatforms;
///
/// Represents a Google Play Games leaderboard. The class provides a way to configure and store
/// data for a specific leaderboard.
/// Implements Unity's generic ILeaderboard interface.
///
public class PlayGamesLeaderboard : ILeaderboard
{
///
/// The ID of the leaderboard.
///
private string mId;
///
/// The user scope for the leaderboard scores. For example, determines if scores are fetched
/// from all players (Global) or just the user's friends (FriendsOnly).
///
private UserScope mUserScope;
///
/// Specifies the start rank and the number of scores to retrieve.
///
private Range mRange;
///
/// Filters scores by time period. For example, AllTime, Weekly, Daily.
///
private TimeScope mTimeScope;
///
/// An array of user IDs to filter the scores.
///
private string[] mFilteredUserIds;
///
/// A boolean flag that is true while the scores are being loaded; otherwise, false.
///
private bool mLoading;
///
/// The score of the local user.
///
private IScore mLocalUserScore;
///
/// The approximate total number of scores in the leaderboard.
///
private uint mMaxRange;
///
/// The list of loaded scores.
///
private List mScoreList = new List();
///
/// The title of the leaderboard.
///
private string mTitle;
///
/// Initializes a new instance of the class.
///
/// The leaderboard ID.
public PlayGamesLeaderboard(string id)
{
mId = id;
}
#region ILeaderboard implementation
///
/// Sets a filter to load scores only for a specific set of users.
///
/// The array of user IDs to filter by.
public void SetUserFilter(string[] userIDs)
{
mFilteredUserIds = userIDs;
}
///
/// Initiates the loading of scores from the Google Play Games platform.
///
/// A callback that will be invoked with a boolean indicating the success of the operation.
public void LoadScores(System.Action callback)
{
PlayGamesPlatform.Instance.LoadScores(this, callback);
}
///
/// Gets a value indicating whether the leaderboard scores are currently loading.
///
/// true if loading; otherwise, false.
public bool loading
{
get { return mLoading; }
internal set { mLoading = value; }
}
///
/// Gets or sets the leaderboard ID.
///
/// The leaderboard ID.
public string id
{
get { return mId; }
set { mId = value; }
}
///
/// Gets or sets the user scope for the scores to be loaded.
///
/// The user scope.
public UserScope userScope
{
get { return mUserScope; }
set { mUserScope = value; }
}
///
/// Gets or sets the rank range for the scores to be loaded.
///
/// The rank range.
public Range range
{
get { return mRange; }
set { mRange = value; }
}
///
/// Gets or sets the time scope for the scores to be loaded.
///
/// The time scope.
public TimeScope timeScope
{
get { return mTimeScope; }
set { mTimeScope = value; }
}
///
/// Gets the local user's score on this leaderboard.
///
/// The local user's score.
public IScore localUserScore
{
get { return mLocalUserScore; }
}
///
/// Gets the approximate number of total scores in the leaderboard.
///
/// The maximum range of scores.
public uint maxRange
{
get { return mMaxRange; }
}
///
/// Gets the array of loaded scores.
///
/// The scores.
public IScore[] scores
{
get
{
PlayGamesScore[] arr = new PlayGamesScore[mScoreList.Count];
mScoreList.CopyTo(arr);
return arr;
}
}
///
/// Gets the title of the leaderboard.
///
/// The title.
public string title
{
get { return mTitle; }
}
#endregion
///
/// Populates the leaderboard's properties from a object.
///
/// The data object containing leaderboard information.
/// true if the data was valid and applied; otherwise, false.
internal bool SetFromData(LeaderboardScoreData data)
{
if (data.Valid)
{
OurUtils.Logger.d("Setting leaderboard from: " + data);
SetMaxRange(data.ApproximateCount);
SetTitle(data.Title);
SetLocalUserScore((PlayGamesScore) data.PlayerScore);
foreach (IScore score in data.Scores)
{
AddScore((PlayGamesScore) score);
}
mLoading = data.Scores.Length == 0 || HasAllScores();
}
return data.Valid;
}
///
/// Sets the maximum range (approximate total number of scores).
///
/// The value for the maximum range.
internal void SetMaxRange(ulong val)
{
mMaxRange = (uint) val;
}
///
/// Sets the title of the leaderboard.
///
/// The title string.
internal void SetTitle(string value)
{
mTitle = value;
}
///
/// Sets the local user's score.
///
/// The local user's score.
internal void SetLocalUserScore(PlayGamesScore score)
{
mLocalUserScore = score;
}
///
/// Adds a score to the internal list of scores. If a user filter is active,
/// the score will only be added if the user ID matches the filter.
///
/// The score to add.
/// The new count of scores in the list.
internal int AddScore(PlayGamesScore score)
{
if (mFilteredUserIds == null || mFilteredUserIds.Length == 0)
{
mScoreList.Add(score);
}
else
{
foreach (string fid in mFilteredUserIds)
{
if (fid.Equals(score.userID))
{
mScoreList.Add(score);
break;
}
}
}
return mScoreList.Count;
}
///
/// Gets the number of scores currently loaded.
///
/// The score count.
public int ScoreCount
{
get { return mScoreList.Count; }
}
///
/// Checks if all requested scores have been loaded.
///
/// true if the number of loaded scores matches the requested range or the total number of scores; otherwise, false.
internal bool HasAllScores()
{
return mScoreList.Count >= mRange.count || mScoreList.Count >= maxRange;
}
}
}
#endif