aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>2016-06-25 15:25:41 +0200
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>2016-06-25 15:25:41 +0200
commitda536ccc5d3d32f3f952830f25c9647143a26a67 (patch)
tree90b0336019da6f2ec2a094420ea540c16a7fd649
parentc31be8c268722d70d32b6843e038130183657d7a (diff)
downloadatm-da536ccc5d3d32f3f952830f25c9647143a26a67.tar.gz
atm-da536ccc5d3d32f3f952830f25c9647143a26a67.tar.bz2
atm-da536ccc5d3d32f3f952830f25c9647143a26a67.zip
Checks if account list is long enough before saving account data
-rw-r--r--init.lua16
1 files changed, 13 insertions, 3 deletions
diff --git a/init.lua b/init.lua
index 75cc1db..8addda7 100644
--- a/init.lua
+++ b/init.lua
@@ -1,6 +1,7 @@
atm = {}
atm.balance = {}
atm.pth = minetest.get_worldpath().."/atm_accounts"
+atm.linecount = 0
function atm.showform (player)
atm.readaccounts()
@@ -34,8 +35,10 @@ end
function atm.readaccounts ()
local file = io.open(atm.pth, "r")
+ local l = 0
if file then
repeat
+ l = l + 1
local balance = file:read("*n")
if x == nil then
break
@@ -44,6 +47,7 @@ function atm.readaccounts ()
atm.balance[name:sub(2)] = balance
until file:read(0) == nil
io.close(file)
+ atm.linecount = l
else
atm.balance = {}
end
@@ -54,12 +58,18 @@ function atm.saveaccounts()
return
end
local data = {}
- local output = io.open(atm.pth, "w")
+
+ local l
for k, v in pairs(atm.balance) do
table.insert(data, string.format("%d %s\n", v, k))
+ l = l+1
+ end
+ if not l < atm.linecount then
+ local output = io.open(atm.pth, "w")
+ output:write(table.concat(data))
+ io.close(output)
end
- output:write(table.concat(data))
- io.close(output)
+
end
minetest.register_on_joinplayer(function(player)