commit 42a0efff1831bd135f05d5b635d24999dfe7bfb5 Author: Giov4 Date: Thu Mar 4 11:33:20 2021 +0100 Initial commit diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..718f019 --- /dev/null +++ b/init.lua @@ -0,0 +1,92 @@ +currencies = {} +local function save_database() end +local function get_database() end +local function validate_currency() end + +local storage = minetest.get_mod_storage() +local registered_currencies = {} + + +function currencies.register(currency) + registered_currencies[currency] = true +end + + + +function currencies.set(pl_name, currency, value) + validate_currency(currency) + + local db = get_database() + db[pl_name] = db[pl_name] or {} + db[pl_name][currency] = value + + save_database(db) +end + + + +function currencies.add(pl_name, currency, value) + validate_currency(currency) + + local db = get_database() + db[pl_name] = db[pl_name] or {} + db[pl_name][currency] = db[pl_name][currency] or 0 + db[pl_name][currency] = db[pl_name][currency] + value + + save_database(db) +end + + + +function currencies.sub(pl_name, currency, value) + validate_currency(currency) + + local db = get_database() + db[pl_name] = db[pl_name] or {} + db[pl_name][currency] = db[pl_name][currency] or 0 + db[pl_name][currency] = db[pl_name][currency] - value + + save_database(db) +end + + + +function currencies.get(pl_name, currency) + validate_currency(currency) + + local db = get_database() + db[pl_name] = db[pl_name] or {} + db[pl_name][currency] = db[pl_name][currency] or 0 + + return db[pl_name][currency] +end + + + +function save_database(value) + storage:set_string("database", minetest.serialize(value)) +end + + + +--[[ + database = { + pl_name : string = { + currency_1 : string = value : number, + ... + }, + ... + } +]] +function get_database() + return minetest.deserialize(storage:get_string("database")) or {} +end + + + +function validate_currency(currency) + assert( + registered_currencies[currency], + "A mod is trying to access to the " .. currency .. " currency, but it hasn't been registered." + ) +end \ No newline at end of file diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..df0d981 --- /dev/null +++ b/mod.conf @@ -0,0 +1,2 @@ +name = currencies +description = A simple API to register and manage currencies. \ No newline at end of file