//
// Copyright (C) 2014 Google Inc.
//
// 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;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
///
/// Represents the Google Play Games local user, providing access to
/// authentication and user-specific functionality. Implements Unity's
/// ILocalUser interface.
///
public class PlayGamesLocalUser : PlayGamesUserProfile, ILocalUser
{
///
/// A reference to the active Play Games platform instance.
///
internal PlayGamesPlatform mPlatform;
///
/// Cached player stats.
///
private PlayerStats mStats;
///
/// Initializes a new instance of the class.
///
/// The platform instance.
internal PlayGamesLocalUser(PlayGamesPlatform plaf)
: base("localUser", string.Empty, string.Empty)
{
mPlatform = plaf;
mStats = null;
}
///
/// Authenticates the local user. This is equivalent to calling
/// .
///
/// A callback to invoke with a boolean indicating success.
public void Authenticate(Action callback)
{
mPlatform.Authenticate(status => callback(status == SignInStatus.Success));
}
///
/// Authenticates the local user with an extended callback that includes the reason for failure.
/// This is equivalent to calling .
///
///
/// A callback to invoke with a boolean indicating success and a string containing the status.
///
public void Authenticate(Action callback)
{
mPlatform.Authenticate(status => callback(status == SignInStatus.Success, status.ToString()));
}
///
/// Loads the friends of the authenticated user.
///
/// A callback to invoke with a boolean indicating success.
public void LoadFriends(Action callback)
{
mPlatform.LoadFriends(this, callback);
}
///
/// Gets the local user's friends. This will be null until completes.
///
/// An array of the user's friends, or null if not yet loaded.
public IUserProfile[] friends
{
get { return mPlatform.GetFriends(); }
}
///
/// Gets a value indicating whether the local user is authenticated to Google Play Games.
///
/// true if authenticated; otherwise, false.
public bool authenticated
{
get { return mPlatform.IsAuthenticated(); }
}
///
/// Gets a value indicating whether the user is underage.
///
/// This is not implemented and returns true as a placeholder.
public bool underage
{
get { return true; }
}
///
/// Gets the display name of the local user.
///
/// The user's display name.
public new string userName
{
get
{
string retval = string.Empty;
if (authenticated)
{
retval = mPlatform.GetUserDisplayName();
if (!base.userName.Equals(retval))
{
ResetIdentity(retval, mPlatform.GetUserId(), mPlatform.GetUserImageUrl());
}
}
return retval;
}
}
///
/// Gets the user's Google ID (Player ID).
///
///
/// This ID is persistent and uniquely identifies the user across all games.
/// It is the preferred way to identify a player.
///
/// The user's Google ID.
public new string id
{
get
{
string retval = string.Empty;
if (authenticated)
{
retval = mPlatform.GetUserId();
if (!base.id.Equals(retval))
{
ResetIdentity(mPlatform.GetUserDisplayName(), retval, mPlatform.GetUserImageUrl());
}
}
return retval;
}
}
///
/// Gets a value indicating whether this user is a friend of the local user.
///
/// Always returns true.
public new bool isFriend
{
get { return true; }
}
///
/// Gets the user's state.
///
/// For the local user, this is always UserState.Online.
public new UserState state
{
get { return UserState.Online; }
}
///
/// Gets the URL of the user's avatar image.
///
/// The avatar image URL.
public new string AvatarURL
{
get
{
string retval = string.Empty;
if (authenticated)
{
retval = mPlatform.GetUserImageUrl();
if (!base.id.Equals(retval))
{
ResetIdentity(mPlatform.GetUserDisplayName(),
mPlatform.GetUserId(), retval);
}
}
return retval;
}
}
///
/// Gets the player's stats from the server.
///
/// A callback to be invoked with the status code and the player's stats.
/// The stats may be cached from a previous call.
///
public void GetStats(Action callback)
{
if (mStats == null || !mStats.Valid)
{
mPlatform.GetPlayerStats((rc, stats) =>
{
mStats = stats;
callback(rc, stats);
});
}
else
{
// Return cached stats with a success code.
callback(CommonStatusCodes.Success, mStats);
}
}
}
}
#endif