api.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. achievements_lib = {}
  2. ----------------------------------------------
  3. ---------------DICHIARAZIONI------------------
  4. ----------------------------------------------
  5. local function update_storage() end
  6. local storage = minetest.get_mod_storage()
  7. local achievements = {} -- KEY: mod; VALUE: {KEY: achievement ID; VALUE: {whatever properties}
  8. local p_achievements = {} -- KEY: p_name; VALUE: {KEY: mod, VALUE: {KEY: achievement ID VALUE: true/nil}}}
  9. -- inizializzo storage caricando tutti i giocatori
  10. for pl_name, mods in pairs(storage:to_table().fields) do
  11. p_achievements[pl_name] = minetest.deserialize(mods)
  12. end
  13. ----------------------------------------------
  14. -------------------CORPO----------------------
  15. ----------------------------------------------
  16. function achievements_lib.register_achievements(mod, mod_achievements)
  17. assert(achievements[mod] == nil, "[ACHIEVEMENTS_LIB] There was an attempt to register the mod " .. mod .. " more than once! Be sure you didn't call this function twice and you didn't install any suspicious mod")
  18. achievements[mod] = mod_achievements
  19. end
  20. function achievements_lib.unlock_achievement(p_name, mod, achvmt_ID)
  21. local achievement = achievements[mod][achvmt_ID]
  22. if achievement == nil then return end
  23. if p_achievements[p_name][mod][achvmt_ID] ~= nil then return end
  24. p_achievements[p_name][mod][achvmt_ID] = true
  25. update_storage(p_name)
  26. end
  27. ----------------------------------------------
  28. --------------------UTILS---------------------
  29. ----------------------------------------------
  30. function achievements_lib.has_player_achievement(p_name, mod, achvmt_ID)
  31. return p_achievements[p_name][mod][achvmt_ID] ~= nil
  32. end
  33. function achievements_lib.is_player_in_storage(p_name, mod)
  34. if p_achievements[p_name] then
  35. if mod and p_achievements[p_name][mod] then
  36. return true
  37. else
  38. return false
  39. end
  40. else
  41. return false
  42. end
  43. end
  44. function achievements_lib.add_player_to_storage(p_name)
  45. if not minetest.get_player_by_name(p_name) then
  46. minetest.log("Warning", "[ACHIEVEMENTS_LIB] Player " .. p_name .. " must be online in order to be added to the storage!")
  47. return end
  48. if not p_achievements[p_name] then
  49. p_achievements[p_name] = {}
  50. end
  51. local update_storage = false
  52. for mod, _ in pairs(achievements) do
  53. if not achievements_lib.is_player_in_storage(p_name, mod) then
  54. p_achievements[p_name][mod] = {}
  55. update_storage = true
  56. end
  57. end
  58. if update_storage then
  59. storage:set_string(p_name, minetest.serialize(p_achievements[p_name]))
  60. end
  61. end
  62. ----------------------------------------------
  63. -----------------GETTERS----------------------
  64. ----------------------------------------------
  65. function achievements_lib.get_achievement(mod, achvmt_ID)
  66. return achievements[mod][achvmt_ID]
  67. end
  68. function achievements_lib.get_player_achievements(p_name, mod)
  69. return p_achievements[p_name][mod]
  70. end
  71. ----------------------------------------------
  72. ---------------FUNZIONI LOCALI----------------
  73. ----------------------------------------------
  74. function update_storage(p_name)
  75. storage:set_string(p_name, minetest.serialize(p_achievements[p_name]))
  76. end
  77. ----------------------------------------------
  78. ------------------DEPRECATED------------------
  79. ----------------------------------------------
  80. function achievements_lib.add_achievement(p_name, mod, achvmt_ID)
  81. minetest.log("warning", "[ACHIEVEMENTS_LIB] (" .. mod .. ") achievements_lib.add_achievement is deprecated: use unlock_achievement instead")
  82. achievements_lib.unlock_achievement(p_name, mod, achvmt_ID)
  83. end