Initial commit
commit
42a0efff18
|
@ -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
|
Loading…
Reference in New Issue