From 3bda69567c229827b1c21d7f97ce18d6602da783 Mon Sep 17 00:00:00 2001 From: Austin Shenk Date: Sat, 29 Jun 2013 23:45:28 -0400 Subject: [PATCH] Clean up file management XP data is now saved to one file per player --- xp.lua | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/xp.lua b/xp.lua index 99252e0..a149910 100644 --- a/xp.lua +++ b/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 \ No newline at end of file