Clean up file management

XP data is now saved to one file per player
This commit is contained in:
Austin Shenk 2013-06-29 23:45:28 -04:00
parent 9ea36d3237
commit 3bda69567c

42
xp.lua
View File

@ -1,35 +1,39 @@
--File Manipulating --File Manipulating
specialties.writeXP = function(player, specialty, amount) specialties.writeXP = function(name)
local file = io.open(minetest.get_worldpath().."/"..player.."_"..specialty, "w") local file = io.open(minetest.get_worldpath().."/"..name.."_XP", "w")
file:write(tostring(amount)) for skill,_ in pairs(specialties.players[name]) do
file:write(skill.." "..tostring(specialties.players[name][skill]).."\n")
end
file:close() file:close()
end end
specialties.readXP = function(player, specialty) specialties.readXP = function(name, specialty)
local file = io.open(minetest.get_worldpath().."/"..player.."_"..specialty, "r") local file = io.open(minetest.get_worldpath().."/"..name.."_XP", "r")
if file == nil then if file == nil then
specialties.writeXP(player, specialty, 0) specialties.writeXP(name)
return 0 local empty = {}
for skill,_ in pairs(specialties.skills) do
empty[skill] = 0
end
return empty
end
local xp = {}
local line = file:read("*l")
while line ~= nil do
local params = line:split(" ")
xp[params[1]] = tonumber(params[2])
line = file:read("*l")
end end
local xp = file:read("*number")
file:close() file:close()
return xp return xp
end end
--Table Modification --Table Modification
specialties.changeXP = function(player, specialty, amount) specialties.changeXP = function(name, specialty, amount)
local current = specialties.players[player][specialty] local current = specialties.players[name][specialty]
if current+amount >= 0 then if current+amount >= 0 then
specialties.players[player][specialty] = current+amount specialties.players[name][specialty] = current+amount
print(specialties.players[player][specialty])
return true return true
else else
return false return false
end end
end end
--XP Updates
specialties.updateXP = function(player)--Called every 10 seconds
for skill,_ in pairs(specialties.skills) do
specialties.writeXP(player, skill, specialties.players[player][skill])
end
end