DEBUG = true
function Debug(text)
if DEBUG == true then
print(text)
end
end
require("sqlite")
sql.Query("CREATE TABLE IF NOT EXISTS saves ( inc INTEGER UNIQUE AUTO INCREMENT, id TEXT NOT NULL, name TEXT NOT NULL, job TEXT NOT NULL, money INTEGER NOT NULL )")
function Save( ply )
local id = ply:UniqueID();
id = sql.SQLStr( id );
sql.Query( "UPDATE saves SET job = '" .. sql.SQLStr( ply:GetNWString( "job" ) ) .. "', money = '" .. sql.SQLStr( ply:GetNWInt( "money" ) ) .. "' WHERE id = '" .. id .. "' AND name = '" .. sql.SQLStr( ply:Nick() ) .. "'" );
Debug("Saved")
Notify(ply,0,5,"Your profile has been saved" )
end
AddChatCommand( "/save", Save, true )
hook.Add("PlayerDisconnect", "Save player on disconnect", Save )
function AutoSave()
for k,v in pairs(player.GetAll()) do
Save(v)
end
end
function CreateNewSave( ply )
local id = ply:UniqueID();
id = sql.SQLStr( id );
print(sql.Query( "INSERT INTO saves ( id, name, job, money ) VALUES ( '" .. id .. "', '" .. sql.SQLStr( ply:Nick() ) .. "', '" .. sql.SQLStr( "Citizen" ) .. "', '" .. "500" .. "' )"))
Debug("Created new save")
NotifyAll(0,8,"Profile for " .. ply:Nick() .. " has been made" )
end
function Load( ply )
local id = ply:UniqueID();
id = sql.SQLStr( id );
if (sql.QueryRow( "SELECT * FROM saves WHERE id = '" .. id .. "' AND name = '" .. sql.SQLStr( ply:Nick() ) .. "' LIMIT 1;")==nil) then
Debug("New player profile for " .. ply:Nick() )
CreateNewSave( ply )
end
local data = sql.QueryRow( "SELECT * FROM saves WHERE id = '" .. id .. "' AND name = '" .. sql.SQLStr( ply:Nick() ) .. "'" );
ply:SetNWString( "job", tostring(data["job"]) )
ply:SetNWInt( "money", tonumber(data["money"]) )
Debug("Loaded profile for " .. ply:Nick() )
NotifyAll(0,5,"Loaded profile for " .. ply:Nick() )
end
hook.Add("PlayerInitialSpawn", "Load player profile", Load )
Debug("data.lua loaded")