Clean up file management
XP data is now saved to one file per player
This commit is contained in:
parent
9ea36d3237
commit
3bda69567c
42
xp.lua
42
xp.lua
@ -1,35 +1,39 @@
|
||||
--File Manipulating
|
||||
specialties.writeXP = function(player, specialty, amount)
|
||||
local file = io.open(minetest.get_worldpath().."/"..player.."_"..specialty, "w")
|
||||
file:write(tostring(amount))
|
||||
specialties.writeXP = function(name)
|
||||
local file = io.open(minetest.get_worldpath().."/"..name.."_XP", "w")
|
||||
for skill,_ in pairs(specialties.players[name]) do
|
||||
file:write(skill.." "..tostring(specialties.players[name][skill]).."\n")
|
||||
end
|
||||
file:close()
|
||||
end
|
||||
specialties.readXP = function(player, specialty)
|
||||
local file = io.open(minetest.get_worldpath().."/"..player.."_"..specialty, "r")
|
||||
specialties.readXP = function(name, specialty)
|
||||
local file = io.open(minetest.get_worldpath().."/"..name.."_XP", "r")
|
||||
if file == nil then
|
||||
specialties.writeXP(player, specialty, 0)
|
||||
return 0
|
||||
specialties.writeXP(name)
|
||||
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
|
||||
local xp = file:read("*number")
|
||||
file:close()
|
||||
return xp
|
||||
end
|
||||
|
||||
--Table Modification
|
||||
specialties.changeXP = function(player, specialty, amount)
|
||||
local current = specialties.players[player][specialty]
|
||||
specialties.changeXP = function(name, specialty, amount)
|
||||
local current = specialties.players[name][specialty]
|
||||
if current+amount >= 0 then
|
||||
specialties.players[player][specialty] = current+amount
|
||||
print(specialties.players[player][specialty])
|
||||
specialties.players[name][specialty] = current+amount
|
||||
return true
|
||||
else
|
||||
return false
|
||||
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
|
Loading…
Reference in New Issue
Block a user