api.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. local function save_balances() end
  2. local function get_database() end
  3. local function validate_currency() end
  4. local function get_valid_value() end
  5. local storage = minetest.get_mod_storage()
  6. local registered_currencies = {}
  7. --[[
  8. def = {
  9. min_value : number =
  10. the smallest balance that a player can have.
  11. max_value : number =
  12. the biggest balance that a player can have.
  13. negative_balance : bool =
  14. if the balance can be less than 0.
  15. }
  16. ]]
  17. function currencies.register(currency, def)
  18. def = def or {}
  19. registered_currencies[currency] = def
  20. end
  21. function currencies.set(currency, pl_name, value)
  22. validate_currency(currency)
  23. local balances = get_database().balances
  24. balances[pl_name] = balances[pl_name] or {}
  25. balances[pl_name][currency] = get_valid_value(currency, value)
  26. save_balances(balances)
  27. end
  28. function currencies.add(currency, pl_name, amount)
  29. currencies.set(currency, pl_name, currencies.get(currency, pl_name) + amount)
  30. end
  31. function currencies.sub(currency, pl_name, amount)
  32. currencies.set(currency, pl_name, currencies.get(currency, pl_name) - amount)
  33. end
  34. function currencies.transfer(currency, from_pl_name, to_pl_name, amount)
  35. currencies.sub(currency, from_pl_name, amount)
  36. currencies.add(currency, to_pl_name, amount)
  37. end
  38. function currencies.exchange(currency, from_pl_name, from_amount, to_pl_name, to_amount)
  39. currencies.sub(currency, from_pl_name, from_amount)
  40. currencies.add(currency, to_pl_name, to_amount)
  41. end
  42. function currencies.get(currency, pl_name)
  43. validate_currency(currency)
  44. local balances = get_database().balances
  45. balances[pl_name] = balances[pl_name] or {}
  46. balances[pl_name][currency] = balances[pl_name][currency] or 0
  47. return balances[pl_name][currency]
  48. end
  49. function currencies.exists(currency)
  50. return registered_currencies[currency]
  51. end
  52. -- If currency is nil it'll reset every currency.
  53. function currencies.clear_balance(currency, pl_name)
  54. if currency then
  55. validate_currency(currency)
  56. currencies.set(currency, pl_name, 0)
  57. return
  58. end
  59. local balances = get_database().balances
  60. balances[pl_name] = {}
  61. save_balances(balances)
  62. end
  63. function currencies.remove_unregistered()
  64. local balances = get_database().balances
  65. for pl_name, pl_currencies in pairs(balances) do
  66. for currency, balance in pairs(pl_currencies) do
  67. if not currencies.exists(currency) then
  68. pl_currencies[currency] = nil
  69. end
  70. end
  71. end
  72. save_balances(balances)
  73. end
  74. function save_balances(balances)
  75. local db = get_database()
  76. db.balances = balances
  77. storage:set_string("database", minetest.serialize(db))
  78. end
  79. --[[
  80. database = {
  81. balances = {
  82. pl_name : string = {
  83. currency_1 : string = value : number,
  84. ...
  85. },
  86. ...
  87. }
  88. }
  89. ]]
  90. function get_database()
  91. local db = minetest.deserialize(storage:get_string("database"))
  92. -- Recreating the db selecting specific tables, to avoid copying a dirty datababe.
  93. local final_db = {}
  94. final_db.balances = db.balances or {}
  95. return final_db
  96. end
  97. function validate_currency(currency)
  98. assert(
  99. registered_currencies[currency],
  100. "A mod is trying to access to the " .. currency .. " currency, but it hasn't been registered."
  101. )
  102. end
  103. function get_valid_value(currency, value)
  104. local currency_def = registered_currencies[currency]
  105. if currency_def.negative_balance == false and value < 0 then value = 0 end
  106. if currency_def.min_value and value < currency_def.min_value then
  107. value = currency_def.min_value
  108. end
  109. if currency_def.max_value and value > currency_def.max_value then
  110. value = currency_def.max_value
  111. end
  112. return value
  113. end