aboutsummaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorgpcf <gpcf@gpcf.eu>2024-09-22 15:31:37 +0200
committergpcf <gpcf@gpcf.eu>2024-09-22 15:31:37 +0200
commit925b6f62e4ce62aa4c06ee2a3a444eebf5b5c307 (patch)
tree220607d0a4c435eb6fa4494071004ff972746b09 /init.lua
parent5b1a159c17df70b5275cdd92f0e515d46b073ee1 (diff)
downloadatm-925b6f62e4ce62aa4c06ee2a3a444eebf5b5c307.tar.gz
atm-925b6f62e4ce62aa4c06ee2a3a444eebf5b5c307.tar.bz2
atm-925b6f62e4ce62aa4c06ee2a3a444eebf5b5c307.zip
Add welfare caps, move backend to json
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua32
1 files changed, 20 insertions, 12 deletions
diff --git a/init.lua b/init.lua
index a8e39c2..36cfa0b 100644
--- a/init.lua
+++ b/init.lua
@@ -1,18 +1,20 @@
--- Copyright (c) 2016, 2017, 2018 Gabriel PĂ©rez-Cerezo, licensed under WTFPL.
+-- Copyright (c) 2016-18, 2024 gpcf, licensed under WTFPL.
-- Wire Transfers (c) 2018 Hans von Smacker
-- Large ATMs (C) 2017 Hans von Smacker
atm = {}
atm.balance = {}
+atm.welfare = {}
+atm.default_welfare = 1000
atm.pending_transfers = {}
atm.completed_transactions = {}
atm.pth = minetest.get_worldpath().."/atm_accounts"
+atm.json_pth = minetest.get_worldpath().."/atm_accounts.json"
atm.pth_wt = minetest.get_worldpath().."/atm_wt_transactions"
local modpath = minetest.get_modpath("atm")
atm.startbalance = 30
function atm.ensure_init(name)
-- Ensure the atm account for the placer specified by name exists
- atm.readaccounts()
if not atm.balance[name] then
atm.balance[name] = atm.startbalance
end
@@ -239,6 +241,17 @@ end
function atm.readaccounts ()
local b = atm.balance
+ local file = io.open(atm.json_pth, "r")
+ if file then
+ local res = minetest.parse_json(file:read("*a"))
+ file.close()
+ for k,v in pairs(res) do
+ atm.balance[k] = v[1]
+ atm.welfare[k] = v[2]
+ end
+ return
+ end
+
local file = io.open(atm.pth, "r")
if file then
repeat
@@ -261,12 +274,9 @@ function atm.saveaccounts()
end
local data = {}
for k, v in pairs(atm.balance) do
- table.insert(data, string.format("%d %s\n", v, k))
+ data[k] = {v, atm.welfare[k]}
end
-
- local output = io.open(atm.pth, "w")
- output:write(table.concat(data))
- io.close(output)
+ minetest.safe_file_write(atm.json_pth, minetest.write_json(data, true))
end
@@ -292,11 +302,6 @@ end
-minetest.register_on_joinplayer(function(player)
- atm.readaccounts()
-end)
-
-
-- ATM node definitions
minetest.register_node("atm:atm", {
@@ -596,3 +601,6 @@ minetest.register_craft({
})
dofile(modpath .. "/interest.lua")
+
+atm.readaccounts()
+minetest.register_on_shutdown(atm.saveaccounts)