custom_achievements.inc 4.2 KB

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