//
// 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.
///
public class PlayGamesLocalUser : PlayGamesUserProfile, ILocalUser
{
internal PlayGamesPlatform mPlatform;
private string emailAddress;
private PlayerStats mStats;
internal PlayGamesLocalUser(PlayGamesPlatform plaf)
: base("localUser", string.Empty, string.Empty)
{
mPlatform = plaf;
emailAddress = null;
mStats = null;
}
///
/// Authenticates the local user. Equivalent to calling
/// .
///
public void Authenticate(Action callback)
{
mPlatform.Authenticate(callback);
}
///
/// Authenticates the local user. Equivalent to calling
/// .
///
public void Authenticate(Action callback)
{
mPlatform.Authenticate(callback);
}
///
/// Authenticates the local user. Equivalent to calling
/// .
///
public void Authenticate(Action callback, bool silent)
{
mPlatform.Authenticate(callback, silent);
}
///
/// Authenticates the local user. Equivalent to calling
/// .
///
public void Authenticate(Action callback, bool silent)
{
mPlatform.Authenticate(callback, silent);
}
///
/// Loads all friends of the authenticated user.
///
public void LoadFriends(Action callback)
{
mPlatform.LoadFriends(this, callback);
}
///
/// Synchronous version of friends, returns null until loaded.
///
public IUserProfile[] friends
{
get { return mPlatform.GetFriends(); }
}
///
/// Gets an id token for the user.
///
public string GetIdToken()
{
return mPlatform.GetIdToken();
}
///
/// Returns whether or not the local user is authenticated to Google Play Games.
///
///
/// true if authenticated; otherwise, false.
///
public bool authenticated
{
get { return mPlatform.IsAuthenticated(); }
}
///
/// Not implemented. As safety placeholder, returns true.
///
public bool underage
{
get { return true; }
}
///
/// Gets the display name of the user.
///
///
/// The display name of the user.
///
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.
///
/// This id is persistent and uniquely identifies the user
/// across all games that use Google Play Game Services. It is
/// the preferred method of uniquely identifying a player instead
/// of email address.
///
///
/// 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;
}
}
///
/// Returns true (since this is the local user).
///
public new bool isFriend
{
get { return true; }
}
///
/// Gets the local user's state. This is always UserState.Online for
/// the local user.
///
public new UserState state
{
get { return UserState.Online; }
}
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 email of the signed in player.
/// If your game requires a persistent, unique id for the
/// player, the use of PlayerId is recommendend since it does not
/// require extra permission consent from the user.
/// This is only available if the Requires Google Plus option
/// is added to the setup (which enables additional
/// permissions for the application).
/// NOTE: This property can only be accessed using the main Unity thread.
///
/// The email.
public string Email
{
get
{
// treat null as unitialized, empty as no email. This can
// happen when the web client is not initialized.
if (authenticated && string.IsNullOrEmpty(emailAddress))
{
emailAddress = mPlatform.GetUserEmail();
emailAddress = emailAddress ?? string.Empty;
}
return authenticated ? emailAddress : string.Empty;
}
}
///
/// Gets the player's stats.
///
/// Callback when they are available.
public void GetStats(Action callback)
{
if (mStats == null || !mStats.Valid)
{
mPlatform.GetPlayerStats((rc, stats) =>
{
mStats = stats;
callback(rc, stats);
});
}
else
{
// 0 = success
callback(CommonStatusCodes.Success, mStats);
}
}
}
}
#endif