IPlayGamesClient.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // <copyright file="IPlayGamesClient.cs" company="Google Inc.">
  2. // Copyright (C) 2014 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.BasicApi
  18. {
  19. using System;
  20. using UnityEngine.SocialPlatforms;
  21. /// <summary>
  22. /// Defines an abstract interface for a Play Games Client.
  23. /// </summary>
  24. /// <remarks>Concrete implementations
  25. /// might be, for example, the client for Android or for iOS. One fundamental concept
  26. /// that implementors of this class must adhere to is stable authentication state.
  27. /// This means that once Authenticate() returns true through its callback, the user is
  28. /// considered to be forever after authenticated while the app is running. The implementation
  29. /// must make sure that this is the case -- for example, it must try to silently
  30. /// re-authenticate the user if authentication is lost or wait for the authentication
  31. /// process to get fixed if it is temporarily in a bad state (such as when the
  32. /// Activity in Android has just been brought to the foreground and the connection to
  33. /// the Games services hasn't yet been established). To the user of this
  34. /// interface, once the user is authenticated, they're forever authenticated.
  35. /// Unless, of course, there is an unusual permanent failure such as the underlying
  36. /// service dying, in which it's acceptable that API method calls will fail.
  37. ///
  38. /// <para>All methods can be called from the game thread. The user of this interface
  39. /// DOES NOT NEED to call them from the UI thread of the game. Transferring to the UI
  40. /// thread when necessary is a responsibility of the implementors of this interface.</para>
  41. ///
  42. /// <para>CALLBACKS: all callbacks must be invoked in Unity's main thread.
  43. /// Implementors of this interface must guarantee that (suggestion: use
  44. /// <see cref="PlayGamesHelperObject.RunOnGameThread(System.Action)"/>).</para>
  45. /// </remarks>
  46. public interface IPlayGamesClient
  47. {
  48. /// <summary>
  49. /// Starts the authentication process.
  50. /// </summary>
  51. /// <remarks>If silent == true, no UIs will be shown
  52. /// (if UIs are needed, it will fail rather than show them). If silent == false,
  53. /// this may show UIs, consent dialogs, etc.
  54. /// At the end of the process, callback will be invoked to notify of the result.
  55. /// Once the callback returns true, the user is considered to be authenticated
  56. /// forever after.
  57. /// </remarks>
  58. /// <param name="silent">If set to <c>true</c> silent.</param>
  59. /// <param name="callback">Callback</param>
  60. void Authenticate(bool silent, Action<SignInStatus> callback);
  61. /// <summary>
  62. /// Returns whether or not user is authenticated.
  63. /// </summary>
  64. /// <returns><c>true</c> if the user is authenticated; otherwise, <c>false</c>.</returns>
  65. bool IsAuthenticated();
  66. /// <summary>
  67. /// Signs the user out.
  68. /// </summary>
  69. void SignOut();
  70. /// <summary>
  71. /// Returns the authenticated user's ID. Note that this value may change if a user signs
  72. /// on and signs in with a different account.
  73. /// </summary>
  74. /// <returns>The user's ID, <code>null</code> if the user is not logged in.</returns>
  75. string GetUserId();
  76. /// <summary>
  77. /// Loads friends of the authenticated user. This loads the entire list of friends.
  78. /// </summary>
  79. /// <param name="callback">Callback invoked when complete. bool argument
  80. /// indicates success.</param>
  81. void LoadFriends(Action<bool> callback);
  82. /// <summary>
  83. /// Returns a human readable name for the user, if they are logged in.
  84. /// </summary>
  85. /// <returns>The user's human-readable name. <code>null</code> if they are not logged
  86. /// in</returns>
  87. string GetUserDisplayName();
  88. /// <summary>
  89. /// Returns an id token, which can be verified server side, if they are logged in.
  90. /// </summary>
  91. string GetIdToken();
  92. /// <summary>
  93. /// The server auth code for this client.
  94. /// </summary>
  95. /// <remarks>
  96. /// Note: This function is currently only implemented for Android.
  97. /// </remarks>
  98. string GetServerAuthCode();
  99. /// <summary>
  100. /// Gets another server auth code.
  101. /// </summary>
  102. /// <remarks>This method should be called after authenticating, and exchanging
  103. /// the initial server auth code for a token. This is implemented by signing in
  104. /// silently, which if successful returns almost immediately and with a new
  105. /// server auth code.
  106. /// </remarks>
  107. /// <param name="reAuthenticateIfNeeded">Calls Authenticate if needed when
  108. /// retrieving another auth code. </param>
  109. /// <param name="callback">Callback returning the auth code, or null if there
  110. /// was a problem. NOTE: This callback can be called immediately.</param>
  111. void GetAnotherServerAuthCode(bool reAuthenticateIfNeeded,
  112. Action<string> callback);
  113. /// <summary>
  114. /// Gets the user's email.
  115. /// </summary>
  116. /// <remarks>The email address returned is selected by the user from the accounts present
  117. /// on the device. There is no guarantee this uniquely identifies the player.
  118. /// For unique identification use the id property of the local player.
  119. /// The user can also choose to not select any email address, meaning it is not
  120. /// available.
  121. /// </remarks>
  122. /// <returns>The user email or null if not authenticated or the permission is
  123. /// not available.</returns>
  124. string GetUserEmail();
  125. /// <summary>
  126. /// Returns the user's avatar url, if they are logged in and have an avatar.
  127. /// </summary>
  128. /// <returns>The URL to load the avatar image. <code>null</code> if they are not logged
  129. /// in</returns>
  130. string GetUserImageUrl();
  131. /// <summary>Gets the player stats.</summary>
  132. /// <param name="callback">Callback for response.</param>
  133. void GetPlayerStats(Action<CommonStatusCodes, PlayerStats> callback);
  134. /// <summary>
  135. /// Loads the users specified. This is mainly used by the leaderboard
  136. /// APIs to get the information of a high scorer.
  137. /// </summary>
  138. /// <param name="userIds">User identifiers.</param>
  139. /// <param name="callback">Callback.</param>
  140. void LoadUsers(string[] userIds, Action<IUserProfile[]> callback);
  141. /// <summary>
  142. /// Loads the achievements for the current signed in user and invokes
  143. /// the callback.
  144. /// </summary>
  145. void LoadAchievements(Action<Achievement[]> callback);
  146. /// <summary>
  147. /// Unlocks the achievement with the passed identifier.
  148. /// </summary>
  149. /// <remarks>If the operation succeeds, the callback
  150. /// will be invoked on the game thread with <code>true</code>. If the operation fails, the
  151. /// callback will be invoked with <code>false</code>. This operation will immediately fail if
  152. /// the user is not authenticated (i.e. the callback will immediately be invoked with
  153. /// <code>false</code>). If the achievement is already unlocked, this call will
  154. /// succeed immediately.
  155. /// </remarks>
  156. /// <param name="achievementId">The ID of the achievement to unlock.</param>
  157. /// <param name="successOrFailureCalllback">Callback used to indicate whether the operation
  158. /// succeeded or failed.</param>
  159. void UnlockAchievement(string achievementId, Action<bool> successOrFailureCalllback);
  160. /// <summary>
  161. /// Reveals the achievement with the passed identifier.
  162. /// </summary>
  163. /// <remarks>If the operation succeeds, the callback
  164. /// will be invoked on the game thread with <code>true</code>. If the operation fails, the
  165. /// callback will be invoked with <code>false</code>. This operation will immediately fail if
  166. /// the user is not authenticated (i.e. the callback will immediately be invoked with
  167. /// <code>false</code>). If the achievement is already in a revealed state, this call will
  168. /// succeed immediately.
  169. /// </remarks>
  170. /// <param name="achievementId">The ID of the achievement to reveal.</param>
  171. /// <param name="successOrFailureCalllback">Callback used to indicate whether the operation
  172. /// succeeded or failed.</param>
  173. void RevealAchievement(string achievementId, Action<bool> successOrFailureCalllback);
  174. /// <summary>
  175. /// Increments the achievement with the passed identifier.
  176. /// </summary>
  177. /// <remarks>If the operation succeeds, the
  178. /// callback will be invoked on the game thread with <code>true</code>. If the operation fails,
  179. /// the callback will be invoked with <code>false</code>. This operation will immediately fail
  180. /// if the user is not authenticated (i.e. the callback will immediately be invoked with
  181. /// <code>false</code>).
  182. /// </remarks>
  183. /// <param name="achievementId">The ID of the achievement to increment.</param>
  184. /// <param name="steps">The number of steps to increment by.</param>
  185. /// <param name="successOrFailureCalllback">Callback used to indicate whether the operation
  186. /// succeeded or failed.</param>
  187. void IncrementAchievement(string achievementId, int steps,
  188. Action<bool> successOrFailureCalllback);
  189. /// <summary>
  190. /// Set an achievement to have at least the given number of steps completed.
  191. /// </summary>
  192. /// <remarks>
  193. /// Calling this method while the achievement already has more steps than
  194. /// the provided value is a no-op. Once the achievement reaches the
  195. /// maximum number of steps, the achievement is automatically unlocked,
  196. /// and any further mutation operations are ignored.
  197. /// </remarks>
  198. /// <param name="achId">Ach identifier.</param>
  199. /// <param name="steps">Steps.</param>
  200. /// <param name="callback">Callback.</param>
  201. void SetStepsAtLeast(string achId, int steps, Action<bool> callback);
  202. /// <summary>
  203. /// Shows the appropriate platform-specific achievements UI.
  204. /// <param name="callback">The callback to invoke when complete. If null,
  205. /// no callback is called. </param>
  206. /// </summary>
  207. void ShowAchievementsUI(Action<UIStatus> callback);
  208. /// <summary>
  209. /// Shows the appropriate platform-specific friends sharing UI.
  210. /// <param name="callback">The callback to invoke when complete. If null,
  211. /// no callback is called. </param>
  212. /// </summary>
  213. void AskForLoadFriendsResolution(Action<UIStatus> callback);
  214. /// <summary>
  215. /// Returns the latest LoadFriendsStatus obtained from loading friends.
  216. /// </summary>
  217. LoadFriendsStatus GetLastLoadFriendsStatus();
  218. /// <summary>
  219. /// Shows the Play Games Player Profile UI for a specific user identifier.
  220. /// </summary>
  221. /// <param name="otherUserId">User Identifier.</param>
  222. /// <param name="otherPlayerInGameName">
  223. /// The game's own display name of the player referred to by userId.
  224. /// </param>
  225. /// <param name="currentPlayerInGameName">
  226. /// The game's own display name of the current player.
  227. /// </param>
  228. /// <param name="callback">Callback invoked upon completion.</param>
  229. void ShowCompareProfileWithAlternativeNameHintsUI(
  230. string otherUserId, string otherPlayerInGameName, string currentPlayerInGameName,
  231. Action<UIStatus> callback);
  232. /// <summary>
  233. /// Returns if the user has allowed permission for the game to access the friends list.
  234. /// </summary>
  235. /// <param name="forceReload">If true, this call will clear any locally cached data and
  236. /// attempt to fetch the latest data from the server. Normally, this should be set to {@code
  237. /// false} to gain advantages of data caching.</param> <param name="callback">Callback
  238. /// invoked upon completion.</param>
  239. void GetFriendsListVisibility(bool forceReload,
  240. Action<FriendsListVisibilityStatus> callback);
  241. /// <summary>
  242. /// Loads the first page of the user's friends
  243. /// </summary>
  244. /// <param name="pageSize">
  245. /// The number of entries to request for this initial page. Note that if cached
  246. /// data already exists, the returned buffer may contain more than this size, but it is
  247. /// guaranteed to contain at least this many if the collection contains enough records.
  248. /// </param>
  249. /// <param name="forceReload">
  250. /// If true, this call will clear any locally cached data and attempt to
  251. /// fetch the latest data from the server. This would commonly be used for something like a
  252. /// user-initiated refresh. Normally, this should be set to {@code false} to gain advantages
  253. /// of data caching.</param>
  254. /// <param name="callback">Callback invoked upon completion.</param>
  255. void LoadFriends(int pageSize, bool forceReload, Action<LoadFriendsStatus> callback);
  256. /// <summary>
  257. /// Loads the friends list page
  258. /// </summary>
  259. /// <param name="pageSize">
  260. /// The number of entries to request for this page. Note that if cached data already
  261. /// exists, the returned buffer may contain more than this size, but it is guaranteed
  262. /// to contain at least this many if the collection contains enough records.
  263. /// </param>
  264. /// <param name="callback"></param>
  265. void LoadMoreFriends(int pageSize, Action<LoadFriendsStatus> callback);
  266. /// <summary>
  267. /// Shows the leaderboard UI for a specific leaderboard.
  268. /// </summary>
  269. /// <remarks>If the passed ID is <code>null</code>, all leaderboards are displayed.
  270. /// </remarks>
  271. /// <param name="leaderboardId">The leaderboard to display. <code>null</code> to display
  272. /// all.</param>
  273. /// <param name="span">Timespan to display for the leaderboard</param>
  274. /// <param name="callback">If non-null, the callback to invoke when the
  275. /// leaderboard is dismissed.
  276. /// </param>
  277. void ShowLeaderboardUI(string leaderboardId,
  278. LeaderboardTimeSpan span,
  279. Action<UIStatus> callback);
  280. /// <summary>
  281. /// Loads the score data for the given leaderboard.
  282. /// </summary>
  283. /// <param name="leaderboardId">Leaderboard identifier.</param>
  284. /// <param name="start">Start indicating the top scores or player centric</param>
  285. /// <param name="rowCount">max number of scores to return. non-positive indicates
  286. /// no rows should be returned. This causes only the summary info to
  287. /// be loaded. This can be limited
  288. /// by the SDK.</param>
  289. /// <param name="collection">leaderboard collection: public or social</param>
  290. /// <param name="timeSpan">leaderboard timespan</param>
  291. /// <param name="callback">callback with the scores, and a page token.
  292. /// The token can be used to load next/prev pages.</param>
  293. void LoadScores(string leaderboardId, LeaderboardStart start,
  294. int rowCount, LeaderboardCollection collection,
  295. LeaderboardTimeSpan timeSpan,
  296. Action<LeaderboardScoreData> callback);
  297. /// <summary>
  298. /// Loads the more scores for the leaderboard.
  299. /// </summary>
  300. /// <remarks>The token is accessed
  301. /// by calling LoadScores() with a positive row count.
  302. /// </remarks>
  303. /// <param name="token">Token for tracking the score loading.</param>
  304. /// <param name="rowCount">max number of scores to return.
  305. /// This can be limited by the SDK.</param>
  306. /// <param name="callback">Callback.</param>
  307. void LoadMoreScores(ScorePageToken token, int rowCount,
  308. Action<LeaderboardScoreData> callback);
  309. /// <summary>
  310. /// Returns the max number of scores returned per call.
  311. /// </summary>
  312. /// <returns>The max results.</returns>
  313. int LeaderboardMaxResults();
  314. /// <summary>
  315. /// Submits the passed score to the passed leaderboard.
  316. /// </summary>
  317. /// <remarks>This operation will immediately fail
  318. /// if the user is not authenticated (i.e. the callback will immediately be invoked with
  319. /// <code>false</code>).
  320. /// </remarks>
  321. /// <param name="leaderboardId">Leaderboard identifier.</param>
  322. /// <param name="score">Score.</param>
  323. /// <param name="successOrFailureCalllback">Callback used to indicate whether the operation
  324. /// succeeded or failed.</param>
  325. void SubmitScore(string leaderboardId, long score,
  326. Action<bool> successOrFailureCalllback);
  327. /// <summary>
  328. /// Submits the score for the currently signed-in player.
  329. /// </summary>
  330. /// <param name="score">Score.</param>
  331. /// <param name="leaderboardId">leaderboard id.</param>
  332. /// <param name="metadata">metadata about the score.</param>
  333. /// <param name="successOrFailureCalllback">Callback upon completion.</param>
  334. void SubmitScore(string leaderboardId, long score, string metadata,
  335. Action<bool> successOrFailureCalllback);
  336. /// <summary>
  337. /// Asks user to give permissions for the given scopes.
  338. /// </summary>
  339. /// <param name="scopes">list of scopes to ask permission for</param>
  340. /// <param name="callback">Callback used to indicate the outcome of the operation.</param>
  341. void RequestPermissions(string[] scopes, Action<SignInStatus> callback);
  342. /// <summary>
  343. /// Returns whether or not user has given permissions for given scopes
  344. /// </summary>
  345. /// <seealso cref="GooglePlayGames.BasicApi.IPlayGamesClient.HasPermissions"/>
  346. /// <param name="scopes">list of scopes</param>
  347. /// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
  348. bool HasPermissions(string[] scopes);
  349. /// <summary>
  350. /// Gets the saved game client.
  351. /// </summary>
  352. /// <returns>The saved game client.</returns>
  353. SavedGame.ISavedGameClient GetSavedGameClient();
  354. /// <summary>
  355. /// Gets the events client.
  356. /// </summary>
  357. /// <returns>The events client.</returns>
  358. Events.IEventsClient GetEventsClient();
  359. /// <summary>
  360. /// Gets the video client.
  361. /// </summary>
  362. /// <returns>The video client.</returns>
  363. Video.IVideoClient GetVideoClient();
  364. IUserProfile[] GetFriends();
  365. /// <summary>
  366. /// Sets the gravity for popups (Android only).
  367. /// </summary>
  368. /// <remarks>This can only be called after authentication. It affects
  369. /// popups for achievements and other game services elements.</remarks>
  370. /// <param name="gravity">Gravity for the popup.</param>
  371. void SetGravityForPopups(Gravity gravity);
  372. }
  373. }
  374. #endif