Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
23 KiB

  1. //====== Copyright � 1996-2009, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: interface to stats, achievements, and leaderboards
  4. //
  5. //=============================================================================
  6. #ifndef ISTEAMUSERSTATS_H
  7. #define ISTEAMUSERSTATS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "isteamclient.h"
  12. #include "isteamremotestorage.h"
  13. // size limit on stat or achievement name (UTF-8 encoded)
  14. enum { k_cchStatNameMax = 128 };
  15. // maximum number of bytes for a leaderboard name (UTF-8 encoded)
  16. enum { k_cchLeaderboardNameMax = 128 };
  17. // maximum number of details int32's storable for a single leaderboard entry
  18. enum { k_cLeaderboardDetailsMax = 64 };
  19. // handle to a single leaderboard
  20. typedef uint64 SteamLeaderboard_t;
  21. // handle to a set of downloaded entries in a leaderboard
  22. typedef uint64 SteamLeaderboardEntries_t;
  23. // type of data request, when downloading leaderboard entries
  24. enum ELeaderboardDataRequest
  25. {
  26. k_ELeaderboardDataRequestGlobal = 0,
  27. k_ELeaderboardDataRequestGlobalAroundUser = 1,
  28. k_ELeaderboardDataRequestFriends = 2,
  29. k_ELeaderboardDataRequestUsers = 3
  30. };
  31. // the sort order of a leaderboard
  32. enum ELeaderboardSortMethod
  33. {
  34. k_ELeaderboardSortMethodNone = 0,
  35. k_ELeaderboardSortMethodAscending = 1, // top-score is lowest number
  36. k_ELeaderboardSortMethodDescending = 2, // top-score is highest number
  37. };
  38. // the display type (used by the Steam Community web site) for a leaderboard
  39. enum ELeaderboardDisplayType
  40. {
  41. k_ELeaderboardDisplayTypeNone = 0,
  42. k_ELeaderboardDisplayTypeNumeric = 1, // simple numerical score
  43. k_ELeaderboardDisplayTypeTimeSeconds = 2, // the score represents a time, in seconds
  44. k_ELeaderboardDisplayTypeTimeMilliSeconds = 3, // the score represents a time, in milliseconds
  45. };
  46. enum ELeaderboardUploadScoreMethod
  47. {
  48. k_ELeaderboardUploadScoreMethodNone = 0,
  49. k_ELeaderboardUploadScoreMethodKeepBest = 1, // Leaderboard will keep user's best score
  50. k_ELeaderboardUploadScoreMethodForceUpdate = 2, // Leaderboard will always replace score with specified
  51. };
  52. // a single entry in a leaderboard, as returned by GetDownloadedLeaderboardEntry()
  53. #pragma pack( push, 8 )
  54. struct LeaderboardEntry_t
  55. {
  56. CSteamID m_steamIDUser; // user with the entry - use SteamFriends()->GetFriendPersonaName() & SteamFriends()->GetFriendAvatar() to get more info
  57. int32 m_nGlobalRank; // [1..N], where N is the number of users with an entry in the leaderboard
  58. int32 m_nScore; // score as set in the leaderboard
  59. int32 m_cDetails; // number of int32 details available for this entry
  60. UGCHandle_t m_hUGC; // handle for UGC attached to the entry
  61. };
  62. #pragma pack( pop )
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Functions for accessing stats, achievements, and leaderboard information
  65. //-----------------------------------------------------------------------------
  66. class ISteamUserStats
  67. {
  68. public:
  69. // Ask the server to send down this user's data and achievements for this game
  70. virtual bool RequestCurrentStats() = 0;
  71. // Data accessors
  72. virtual bool GetStat( const char *pchName, int32 *pData ) = 0;
  73. virtual bool GetStat( const char *pchName, float *pData ) = 0;
  74. // Set / update data
  75. virtual bool SetStat( const char *pchName, int32 nData ) = 0;
  76. virtual bool SetStat( const char *pchName, float fData ) = 0;
  77. virtual bool UpdateAvgRateStat( const char *pchName, float flCountThisSession, double dSessionLength ) = 0;
  78. // Achievement flag accessors
  79. virtual bool GetAchievement( const char *pchName, bool *pbAchieved ) = 0;
  80. virtual bool SetAchievement( const char *pchName ) = 0;
  81. virtual bool ClearAchievement( const char *pchName ) = 0;
  82. // Get the achievement status, and the time it was unlocked if unlocked.
  83. // If the return value is true, but the unlock time is zero, that means it was unlocked before Steam
  84. // began tracking achievement unlock times (December 2009). Time is seconds since January 1, 1970.
  85. virtual bool GetAchievementAndUnlockTime( const char *pchName, bool *pbAchieved, uint32 *punUnlockTime ) = 0;
  86. // Store the current data on the server, will get a callback when set
  87. // And one callback for every new achievement
  88. //
  89. // If the callback has a result of k_EResultInvalidParam, one or more stats
  90. // uploaded has been rejected, either because they broke constraints
  91. // or were out of date. In this case the server sends back updated values.
  92. // The stats should be re-iterated to keep in sync.
  93. virtual bool StoreStats() = 0;
  94. // Achievement / GroupAchievement metadata
  95. // Gets the icon of the achievement, which is a handle to be used in ISteamUtils::GetImageRGBA(), or 0 if none set.
  96. // A return value of 0 may indicate we are still fetching data, and you can wait for the UserAchievementIconFetched_t callback
  97. // which will notify you when the bits are ready. If the callback still returns zero, then there is no image set for the
  98. // specified achievement.
  99. virtual int GetAchievementIcon( const char *pchName ) = 0;
  100. // Get general attributes for an achievement. Accepts the following keys:
  101. // - "name" and "desc" for retrieving the localized achievement name and description (returned in UTF8)
  102. // - "hidden" for retrieving if an achievement is hidden (returns "0" when not hidden, "1" when hidden)
  103. virtual const char *GetAchievementDisplayAttribute( const char *pchName, const char *pchKey ) = 0;
  104. // Achievement progress - triggers an AchievementProgress callback, that is all.
  105. // Calling this w/ N out of N progress will NOT set the achievement, the game must still do that.
  106. virtual bool IndicateAchievementProgress( const char *pchName, uint32 nCurProgress, uint32 nMaxProgress ) = 0;
  107. // Friends stats & achievements
  108. // downloads stats for the user
  109. // returns a UserStatsReceived_t received when completed
  110. // if the other user has no stats, UserStatsReceived_t.m_eResult will be set to k_EResultFail
  111. // these stats won't be auto-updated; you'll need to call RequestUserStats() again to refresh any data
  112. virtual SteamAPICall_t RequestUserStats( CSteamID steamIDUser ) = 0;
  113. // requests stat information for a user, usable after a successful call to RequestUserStats()
  114. virtual bool GetUserStat( CSteamID steamIDUser, const char *pchName, int32 *pData ) = 0;
  115. virtual bool GetUserStat( CSteamID steamIDUser, const char *pchName, float *pData ) = 0;
  116. virtual bool GetUserAchievement( CSteamID steamIDUser, const char *pchName, bool *pbAchieved ) = 0;
  117. // See notes for GetAchievementAndUnlockTime above
  118. virtual bool GetUserAchievementAndUnlockTime( CSteamID steamIDUser, const char *pchName, bool *pbAchieved, uint32 *punUnlockTime ) = 0;
  119. // Reset stats
  120. virtual bool ResetAllStats( bool bAchievementsToo ) = 0;
  121. // Leaderboard functions
  122. // asks the Steam back-end for a leaderboard by name, and will create it if it's not yet
  123. // This call is asynchronous, with the result returned in LeaderboardFindResult_t
  124. virtual SteamAPICall_t FindOrCreateLeaderboard( const char *pchLeaderboardName, ELeaderboardSortMethod eLeaderboardSortMethod, ELeaderboardDisplayType eLeaderboardDisplayType ) = 0;
  125. // as above, but won't create the leaderboard if it's not found
  126. // This call is asynchronous, with the result returned in LeaderboardFindResult_t
  127. virtual SteamAPICall_t FindLeaderboard( const char *pchLeaderboardName ) = 0;
  128. // returns the name of a leaderboard
  129. virtual const char *GetLeaderboardName( SteamLeaderboard_t hSteamLeaderboard ) = 0;
  130. // returns the total number of entries in a leaderboard, as of the last request
  131. virtual int GetLeaderboardEntryCount( SteamLeaderboard_t hSteamLeaderboard ) = 0;
  132. // returns the sort method of the leaderboard
  133. virtual ELeaderboardSortMethod GetLeaderboardSortMethod( SteamLeaderboard_t hSteamLeaderboard ) = 0;
  134. // returns the display type of the leaderboard
  135. virtual ELeaderboardDisplayType GetLeaderboardDisplayType( SteamLeaderboard_t hSteamLeaderboard ) = 0;
  136. // Asks the Steam back-end for a set of rows in the leaderboard.
  137. // This call is asynchronous, with the result returned in LeaderboardScoresDownloaded_t
  138. // LeaderboardScoresDownloaded_t will contain a handle to pull the results from GetDownloadedLeaderboardEntries() (below)
  139. // You can ask for more entries than exist, and it will return as many as do exist.
  140. // k_ELeaderboardDataRequestGlobal requests rows in the leaderboard from the full table, with nRangeStart & nRangeEnd in the range [1, TotalEntries]
  141. // k_ELeaderboardDataRequestGlobalAroundUser requests rows around the current user, nRangeStart being negate
  142. // e.g. DownloadLeaderboardEntries( hLeaderboard, k_ELeaderboardDataRequestGlobalAroundUser, -3, 3 ) will return 7 rows, 3 before the user, 3 after
  143. // k_ELeaderboardDataRequestFriends requests all the rows for friends of the current user
  144. virtual SteamAPICall_t DownloadLeaderboardEntries( SteamLeaderboard_t hSteamLeaderboard, ELeaderboardDataRequest eLeaderboardDataRequest, int nRangeStart, int nRangeEnd ) = 0;
  145. // as above, but downloads leaderboard entries for an arbitrary set of users - ELeaderboardDataRequest is k_ELeaderboardDataRequestUsers
  146. // if a user doesn't have a leaderboard entry, they won't be included in the result
  147. // a max of 100 users can be downloaded at a time, with only one outstanding call at a time
  148. virtual SteamAPICall_t DownloadLeaderboardEntriesForUsers( SteamLeaderboard_t hSteamLeaderboard, CSteamID *prgUsers, int cUsers ) = 0;
  149. // Returns data about a single leaderboard entry
  150. // use a for loop from 0 to LeaderboardScoresDownloaded_t::m_cEntryCount to get all the downloaded entries
  151. // e.g.
  152. // void OnLeaderboardScoresDownloaded( LeaderboardScoresDownloaded_t *pLeaderboardScoresDownloaded )
  153. // {
  154. // for ( int index = 0; index < pLeaderboardScoresDownloaded->m_cEntryCount; index++ )
  155. // {
  156. // LeaderboardEntry_t leaderboardEntry;
  157. // int32 details[3]; // we know this is how many we've stored previously
  158. // GetDownloadedLeaderboardEntry( pLeaderboardScoresDownloaded->m_hSteamLeaderboardEntries, index, &leaderboardEntry, details, 3 );
  159. // assert( leaderboardEntry.m_cDetails == 3 );
  160. // ...
  161. // }
  162. // once you've accessed all the entries, the data will be free'd, and the SteamLeaderboardEntries_t handle will become invalid
  163. virtual bool GetDownloadedLeaderboardEntry( SteamLeaderboardEntries_t hSteamLeaderboardEntries, int index, LeaderboardEntry_t *pLeaderboardEntry, int32 *pDetails, int cDetailsMax ) = 0;
  164. // Uploads a user score to the Steam back-end.
  165. // This call is asynchronous, with the result returned in LeaderboardScoreUploaded_t
  166. // Details are extra game-defined information regarding how the user got that score
  167. // pScoreDetails points to an array of int32's, cScoreDetailsCount is the number of int32's in the list
  168. virtual SteamAPICall_t UploadLeaderboardScore( SteamLeaderboard_t hSteamLeaderboard, ELeaderboardUploadScoreMethod eLeaderboardUploadScoreMethod, int32 nScore, const int32 *pScoreDetails, int cScoreDetailsCount ) = 0;
  169. // Attaches a piece of user generated content the user's entry on a leaderboard.
  170. // hContent is a handle to a piece of user generated content that was shared using ISteamUserRemoteStorage::FileShare().
  171. // This call is asynchronous, with the result returned in LeaderboardUGCSet_t.
  172. virtual SteamAPICall_t AttachLeaderboardUGC( SteamLeaderboard_t hSteamLeaderboard, UGCHandle_t hUGC ) = 0;
  173. // Retrieves the number of players currently playing your game (online + offline)
  174. // This call is asynchronous, with the result returned in NumberOfCurrentPlayers_t
  175. virtual SteamAPICall_t GetNumberOfCurrentPlayers() = 0;
  176. // Requests that Steam fetch data on the percentage of players who have received each achievement
  177. // for the game globally.
  178. // This call is asynchronous, with the result returned in GlobalAchievementPercentagesReady_t.
  179. virtual SteamAPICall_t RequestGlobalAchievementPercentages() = 0;
  180. // Get the info on the most achieved achievement for the game, returns an iterator index you can use to fetch
  181. // the next most achieved afterwards. Will return -1 if there is no data on achievement
  182. // percentages (ie, you haven't called RequestGlobalAchievementPercentages and waited on the callback).
  183. virtual int GetMostAchievedAchievementInfo( char *pchName, uint32 unNameBufLen, float *pflPercent, bool *pbAchieved ) = 0;
  184. // Get the info on the next most achieved achievement for the game. Call this after GetMostAchievedAchievementInfo or another
  185. // GetNextMostAchievedAchievementInfo call passing the iterator from the previous call. Returns -1 after the last
  186. // achievement has been iterated.
  187. virtual int GetNextMostAchievedAchievementInfo( int iIteratorPrevious, char *pchName, uint32 unNameBufLen, float *pflPercent, bool *pbAchieved ) = 0;
  188. // Returns the percentage of users who have achieved the specified achievement.
  189. virtual bool GetAchievementAchievedPercent( const char *pchName, float *pflPercent ) = 0;
  190. // Requests global stats data, which is available for stats marked as "aggregated".
  191. // This call is asynchronous, with the results returned in GlobalStatsReceived_t.
  192. // nHistoryDays specifies how many days of day-by-day history to retrieve in addition
  193. // to the overall totals. The limit is 60.
  194. virtual SteamAPICall_t RequestGlobalStats( int nHistoryDays ) = 0;
  195. // Gets the lifetime totals for an aggregated stat
  196. virtual bool GetGlobalStat( const char *pchStatName, int64 *pData ) = 0;
  197. virtual bool GetGlobalStat( const char *pchStatName, double *pData ) = 0;
  198. // Gets history for an aggregated stat. pData will be filled with daily values, starting with today.
  199. // So when called, pData[0] will be today, pData[1] will be yesterday, and pData[2] will be two days ago,
  200. // etc. cubData is the size in bytes of the pubData buffer. Returns the number of
  201. // elements actually set.
  202. virtual int32 GetGlobalStatHistory( const char *pchStatName, int64 *pData, uint32 cubData ) = 0;
  203. virtual int32 GetGlobalStatHistory( const char *pchStatName, double *pData, uint32 cubData ) = 0;
  204. #ifdef _PS3
  205. // Call to kick off installation of the PS3 trophies. This call is asynchronous, and the results will be returned in a PS3TrophiesInstalled_t
  206. // callback.
  207. virtual bool InstallPS3Trophies() = 0;
  208. // Returns the amount of space required at boot to install trophies. This value can be used when comparing the amount of space needed
  209. // by the game to the available space value passed to the game at boot. The value is set during InstallPS3Trophies().
  210. virtual uint64 GetTrophySpaceRequiredBeforeInstall() = 0;
  211. // On PS3, user stats & achievement progress through Steam must be stored with the user's saved game data.
  212. // At startup, before calling RequestCurrentStats(), you must pass the user's stats data to Steam via this method.
  213. // If you do not have any user data, call this function with pvData = NULL and cubData = 0
  214. virtual bool SetUserStatsData( const void *pvData, uint32 cubData ) = 0;
  215. // Call to get the user's current stats data. You should retrieve this data after receiving successful UserStatsReceived_t & UserStatsStored_t
  216. // callbacks, and store the data with the user's save game data. You can call this method with pvData = NULL and cubData = 0 to get the required
  217. // buffer size.
  218. virtual bool GetUserStatsData( void *pvData, uint32 cubData, uint32 *pcubWritten ) = 0;
  219. #endif
  220. };
  221. #define STEAMUSERSTATS_INTERFACE_VERSION "STEAMUSERSTATS_INTERFACE_VERSION010"
  222. // callbacks
  223. #pragma pack( push, 8 )
  224. //-----------------------------------------------------------------------------
  225. // Purpose: called when the latests stats and achievements have been received
  226. // from the server
  227. //-----------------------------------------------------------------------------
  228. struct UserStatsReceived_t
  229. {
  230. enum { k_iCallback = k_iSteamUserStatsCallbacks + 1 };
  231. uint64 m_nGameID; // Game these stats are for
  232. EResult m_eResult; // Success / error fetching the stats
  233. CSteamID m_steamIDUser; // The user for whom the stats are retrieved for
  234. };
  235. //-----------------------------------------------------------------------------
  236. // Purpose: result of a request to store the user stats for a game
  237. //-----------------------------------------------------------------------------
  238. struct UserStatsStored_t
  239. {
  240. enum { k_iCallback = k_iSteamUserStatsCallbacks + 2 };
  241. uint64 m_nGameID; // Game these stats are for
  242. EResult m_eResult; // success / error
  243. };
  244. //-----------------------------------------------------------------------------
  245. // Purpose: result of a request to store the achievements for a game, or an
  246. // "indicate progress" call. If both m_nCurProgress and m_nMaxProgress
  247. // are zero, that means the achievement has been fully unlocked.
  248. //-----------------------------------------------------------------------------
  249. struct UserAchievementStored_t
  250. {
  251. enum { k_iCallback = k_iSteamUserStatsCallbacks + 3 };
  252. uint64 m_nGameID; // Game this is for
  253. bool m_bGroupAchievement; // if this is a "group" achievement
  254. char m_rgchAchievementName[k_cchStatNameMax]; // name of the achievement
  255. uint32 m_nCurProgress; // current progress towards the achievement
  256. uint32 m_nMaxProgress; // "out of" this many
  257. };
  258. //-----------------------------------------------------------------------------
  259. // Purpose: call result for finding a leaderboard, returned as a result of FindOrCreateLeaderboard() or FindLeaderboard()
  260. // use CCallResult<> to map this async result to a member function
  261. //-----------------------------------------------------------------------------
  262. struct LeaderboardFindResult_t
  263. {
  264. enum { k_iCallback = k_iSteamUserStatsCallbacks + 4 };
  265. SteamLeaderboard_t m_hSteamLeaderboard; // handle to the leaderboard serarched for, 0 if no leaderboard found
  266. uint8 m_bLeaderboardFound; // 0 if no leaderboard found
  267. };
  268. //-----------------------------------------------------------------------------
  269. // Purpose: call result indicating scores for a leaderboard have been downloaded and are ready to be retrieved, returned as a result of DownloadLeaderboardEntries()
  270. // use CCallResult<> to map this async result to a member function
  271. //-----------------------------------------------------------------------------
  272. struct LeaderboardScoresDownloaded_t
  273. {
  274. enum { k_iCallback = k_iSteamUserStatsCallbacks + 5 };
  275. SteamLeaderboard_t m_hSteamLeaderboard;
  276. SteamLeaderboardEntries_t m_hSteamLeaderboardEntries; // the handle to pass into GetDownloadedLeaderboardEntries()
  277. int m_cEntryCount; // the number of entries downloaded
  278. };
  279. //-----------------------------------------------------------------------------
  280. // Purpose: call result indicating scores has been uploaded, returned as a result of UploadLeaderboardScore()
  281. // use CCallResult<> to map this async result to a member function
  282. //-----------------------------------------------------------------------------
  283. struct LeaderboardScoreUploaded_t
  284. {
  285. enum { k_iCallback = k_iSteamUserStatsCallbacks + 6 };
  286. uint8 m_bSuccess; // 1 if the call was successful
  287. SteamLeaderboard_t m_hSteamLeaderboard; // the leaderboard handle that was
  288. int32 m_nScore; // the score that was attempted to set
  289. uint8 m_bScoreChanged; // true if the score in the leaderboard change, false if the existing score was better
  290. int m_nGlobalRankNew; // the new global rank of the user in this leaderboard
  291. int m_nGlobalRankPrevious; // the previous global rank of the user in this leaderboard; 0 if the user had no existing entry in the leaderboard
  292. };
  293. struct NumberOfCurrentPlayers_t
  294. {
  295. enum { k_iCallback = k_iSteamUserStatsCallbacks + 7 };
  296. uint8 m_bSuccess; // 1 if the call was successful
  297. int32 m_cPlayers; // Number of players currently playing
  298. };
  299. //-----------------------------------------------------------------------------
  300. // Purpose: Callback indicating that a user's stats have been unloaded.
  301. // Call RequestUserStats again to access stats for this user
  302. //-----------------------------------------------------------------------------
  303. struct UserStatsUnloaded_t
  304. {
  305. enum { k_iCallback = k_iSteamUserStatsCallbacks + 8 };
  306. CSteamID m_steamIDUser; // User whose stats have been unloaded
  307. };
  308. //-----------------------------------------------------------------------------
  309. // Purpose: Callback indicating that an achievement icon has been fetched
  310. //-----------------------------------------------------------------------------
  311. struct UserAchievementIconFetched_t
  312. {
  313. enum { k_iCallback = k_iSteamUserStatsCallbacks + 9 };
  314. CGameID m_nGameID; // Game this is for
  315. char m_rgchAchievementName[k_cchStatNameMax]; // name of the achievement
  316. bool m_bAchieved; // Is the icon for the achieved or not achieved version?
  317. int m_nIconHandle; // Handle to the image, which can be used in SteamUtils()->GetImageRGBA(), 0 means no image is set for the achievement
  318. };
  319. //-----------------------------------------------------------------------------
  320. // Purpose: Callback indicating that global achievement percentages are fetched
  321. //-----------------------------------------------------------------------------
  322. struct GlobalAchievementPercentagesReady_t
  323. {
  324. enum { k_iCallback = k_iSteamUserStatsCallbacks + 10 };
  325. uint64 m_nGameID; // Game this is for
  326. EResult m_eResult; // Result of the operation
  327. };
  328. //-----------------------------------------------------------------------------
  329. // Purpose: call result indicating UGC has been uploaded, returned as a result of SetLeaderboardUGC()
  330. //-----------------------------------------------------------------------------
  331. struct LeaderboardUGCSet_t
  332. {
  333. enum { k_iCallback = k_iSteamUserStatsCallbacks + 11 };
  334. EResult m_eResult; // The result of the operation
  335. SteamLeaderboard_t m_hSteamLeaderboard; // the leaderboard handle that was
  336. };
  337. //-----------------------------------------------------------------------------
  338. // Purpose: callback indicating that PS3 trophies have been installed
  339. //-----------------------------------------------------------------------------
  340. struct PS3TrophiesInstalled_t
  341. {
  342. enum { k_iCallback = k_iSteamUserStatsCallbacks + 12 };
  343. uint64 m_nGameID; // Game these stats are for
  344. EResult m_eResult; // The result of the operation
  345. uint64 m_ulRequiredDiskSpace; // If m_eResult is k_EResultDiskFull, will contain the amount of space needed to install trophies
  346. };
  347. //-----------------------------------------------------------------------------
  348. // Purpose: callback indicating global stats have been received.
  349. // Returned as a result of RequestGlobalStats()
  350. //-----------------------------------------------------------------------------
  351. struct GlobalStatsReceived_t
  352. {
  353. enum { k_iCallback = k_iSteamUserStatsCallbacks + 12 };
  354. uint64 m_nGameID; // Game global stats were requested for
  355. EResult m_eResult; // The result of the request
  356. };
  357. #pragma pack( pop )
  358. #endif // ISTEAMUSER_H