custom_achievements.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #if defined __custom_achievements_included
  2. #endinput
  3. #endif
  4. #define __custom_achievements_included
  5. /**
  6. * Controls type of achievement. Intended to be used when displaying the achievements in some
  7. * fashion (e.g., on a webpage).
  8. *
  9. * This has been deprecated; any applications wishing to integrate will have to understand the
  10. * meaning of the metadata themselves.
  11. */
  12. enum AchievementStyle {
  13. AchievementStyle_Undefined = 0, // Style is undefined (may need to be custom-made).
  14. AchievementStyle_Single, // Either it's achieved or it isn't; no renderable progress.
  15. AchievementStyle_Progress, // Requires a progress bar.
  16. };
  17. /**
  18. * Called when achievement metadata is fetched for an account. If the account has no metadata
  19. * for the specified achievement, an empty string is returned.
  20. *
  21. * There is no defined metadata format other than it being a string; the plugin is free to store
  22. * any relevant persistent information pertaining to that achievement for an account, if any.
  23. */
  24. typedef AchievementMetadataCallback = function void(CustomAchievement achievement,
  25. const char[] metadata, any data);
  26. methodmap CustomAchievement {
  27. /**
  28. * Instantiates a custom achievement with the specified internal name, returning an
  29. * identifier.
  30. */
  31. public native CustomAchievement(const char[] internalName, AchievementStyle style);
  32. /**
  33. * Flags the achievement as completed for this client. If the achievement hasn't been
  34. * awarded to the client before, `notify` is true, and the player is in the server,
  35. * `OnCustomAchievementAwarded` is fired.
  36. */
  37. public native void AwardToAccountID(int steamid3, bool notify = true);
  38. /**
  39. * Flags the achievement as completed for this client.
  40. */
  41. public void Award(int client, bool notify = true) {
  42. if (!IsFakeClient(client) && IsClientAuthorized(client)) {
  43. this.AwardToAccountID(GetSteamAccountID(client), notify);
  44. }
  45. }
  46. /**
  47. * Performs a threaded query to get the metadata for this achievement for the specified
  48. * Steam account.
  49. */
  50. public native void FetchMetadataByAccountID(int steamid3,
  51. AchievementMetadataCallback callback, any data);
  52. /**
  53. * Performs a threaded query to get the metadata for this achievement for the specified
  54. * user.
  55. *
  56. * (You'll probably want to pass the client in under the `data` argument in some way.)
  57. */
  58. public void FetchMetadata(int client, AchievementMetadataCallback callback,
  59. any data) {
  60. if (!IsFakeClient(client) && IsClientAuthorized(client)) {
  61. this.FetchMetadataByAccountID(GetSteamAccountID(client), callback, data);
  62. }
  63. }
  64. /**
  65. * Store the account-specific metadata for this achievement.
  66. */
  67. public native bool StoreMetadataByAccountID(int steamid3, const char[] metadata);
  68. /**
  69. * Performs a fast query to store the metadata for this achievement for the specified
  70. * client.
  71. */
  72. public bool StoreMetadata(int client, const char[] metadata) {
  73. if (!IsFakeClient(client) && IsClientAuthorized(client)) {
  74. return this.StoreMetadataByAccountID(GetSteamAccountID(client), metadata);
  75. }
  76. return false;
  77. }
  78. /**
  79. * Resets this achievement, removing stored metadata and removing the "achieved" state for
  80. * the specified account.
  81. *
  82. * There is no `CustomAchievement.Reset(int client)` by design as achievements are not meant
  83. * to be reset so often; this is mainly intended as a built-in testing function.
  84. */
  85. public native void ResetByAccountID(int steamid3);
  86. }
  87. /**
  88. * Called when an achievement is awarded to an in-game player.
  89. *
  90. * TODO implement a function to test if achievement(s) were already awarded, to check if a
  91. * milestone is reached, or something.
  92. */
  93. forward void OnCustomAchievementAwarded(int client, CustomAchievement achievement);
  94. public SharedPlugin __pl_custom_achievements = {
  95. name = "custom-achievements",
  96. file = "custom_achievements.smx",
  97. #if defined REQUIRE_PLUGIN
  98. required = 1,
  99. #else
  100. required = 0,
  101. #endif
  102. };
  103. #if !defined REQUIRE_PLUGIN
  104. public __pl_custom_achievements_SetNTVOptional() {
  105. MarkNativeAsOptional("CustomAchievement.CustomAchievement");
  106. MarkNativeAsOptional("CustomAchievement.AwardToAccountID");
  107. MarkNativeAsOptional("CustomAchievement.Award");
  108. MarkNativeAsOptional("CustomAchievement.FetchMetadataByAccountID");
  109. MarkNativeAsOptional("CustomAchievement.FetchMetadata");
  110. MarkNativeAsOptional("CustomAchievement.StoreMetadataByAccountID");
  111. MarkNativeAsOptional("CustomAchievement.StoreMetadata");
  112. MarkNativeAsOptional("CustomAchievement.ResetByAccountID");
  113. }
  114. #endif