-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcommon.lua
68 lines (55 loc) · 1.3 KB
/
common.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
end
-- banking accounts storage
function atm.readaccounts ()
local b = atm.balance
local file = io.open(atm.pth, "r")
if file then
repeat
local balance = file:read("*n")
if balance == nil then
break
end
local name = file:read("*l")
b[name:sub(2)] = balance
until file:read(0) == nil
io.close(file)
end
end
function atm.saveaccounts()
if not atm.balance then
return
end
local data = {}
for k, v in pairs(atm.balance) do
table.insert(data, string.format("%d %s\n", v, k))
end
local output = io.open(atm.pth, "w")
output:write(table.concat(data))
io.close(output)
end
-- wire transfer data storage
function atm.read_transactions()
local file = io.open(atm.pth_wt, "r")
if file then
local data = file:read("*all")
atm.completed_transactions = minetest.deserialize(data)
end
end
function atm.write_transactions()
if not atm.completed_transactions then
return
end
local file = io.open(atm.pth_wt, "w")
local data = minetest.serialize(atm.completed_transactions)
file:write(data)
io.close(file)
end
minetest.register_on_joinplayer(function()
atm.readaccounts()
end)